mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 01:10:47 +01:00
265 lines
8.7 KiB
Dart
265 lines
8.7 KiB
Dart
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:accessibility/accessibility.dart';
|
|
import 'package:bike_control/bluetooth/devices/gyroscope/gyroscope_steering.dart';
|
|
import 'package:bike_control/bluetooth/messages/notification.dart';
|
|
import 'package:bike_control/gen/l10n.dart';
|
|
import 'package:bike_control/utils/actions/android.dart';
|
|
import 'package:bike_control/utils/actions/desktop.dart';
|
|
import 'package:bike_control/utils/core.dart';
|
|
import 'package:bike_control/utils/iap/iap_manager.dart';
|
|
import 'package:bike_control/utils/keymap/buttons.dart';
|
|
import 'package:bike_control/utils/keymap/keymap.dart';
|
|
import 'package:bike_control/widgets/keymap_explanation.dart';
|
|
import 'package:dartx/dartx.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:screen_retriever/screen_retriever.dart';
|
|
|
|
import '../keymap/apps/supported_app.dart';
|
|
|
|
enum SupportedMode { keyboard, touch, media }
|
|
|
|
sealed class ActionResult {
|
|
final String message;
|
|
const ActionResult(this.message);
|
|
}
|
|
|
|
class Success extends ActionResult {
|
|
const Success(super.message);
|
|
}
|
|
|
|
class NotHandled extends ActionResult {
|
|
const NotHandled(super.message);
|
|
}
|
|
|
|
class Ignored extends ActionResult {
|
|
const Ignored(super.message);
|
|
}
|
|
|
|
class Error extends ActionResult {
|
|
const Error(super.message);
|
|
}
|
|
|
|
abstract class BaseActions {
|
|
final List<SupportedMode> supportedModes;
|
|
|
|
SupportedApp? supportedApp;
|
|
|
|
BaseActions({required this.supportedModes});
|
|
|
|
void cleanup();
|
|
|
|
void init(SupportedApp? supportedApp) {
|
|
this.supportedApp = supportedApp;
|
|
debugPrint('Supported app: ${supportedApp?.name ?? "None"}');
|
|
|
|
if (supportedApp != null) {
|
|
final allButtons = core.connection.devices.map((e) => e.availableButtons).flatten().distinct().toList();
|
|
supportedApp.keymap.addNewButtons(allButtons);
|
|
}
|
|
}
|
|
|
|
Future<Offset> resolveTouchPosition({required KeyPair keyPair, required WindowEvent? windowInfo}) async {
|
|
if (keyPair.touchPosition != Offset.zero) {
|
|
// convert relative position to absolute position based on window info
|
|
|
|
// TODO support multiple screens
|
|
final Size displaySize;
|
|
final double devicePixelRatio;
|
|
if (Platform.isWindows) {
|
|
// TODO remove once https://github.com/flutter/flutter/pull/164460 is available in stable
|
|
final display = await screenRetriever.getPrimaryDisplay();
|
|
displaySize = display.size;
|
|
devicePixelRatio = 1.0;
|
|
} else {
|
|
final display = WidgetsBinding.instance.platformDispatcher.views.first.display;
|
|
displaySize = display.size;
|
|
devicePixelRatio = display.devicePixelRatio;
|
|
}
|
|
|
|
late final Size physicalSize;
|
|
if (this is AndroidActions) {
|
|
if (windowInfo != null && windowInfo.packageName != 'de.jonasbark.swiftcontrol') {
|
|
// a trainer app is in foreground, so use the always assume landscape
|
|
physicalSize = Size(max(displaySize.width, displaySize.height), min(displaySize.width, displaySize.height));
|
|
} else {
|
|
// display size is already in physical pixels
|
|
physicalSize = displaySize;
|
|
}
|
|
} else if (this is DesktopActions) {
|
|
// display size is in logical pixels, convert to physical pixels
|
|
// TODO on macOS the notch is included here, but it's not part of the usable screen area, so we should exclude it
|
|
physicalSize = displaySize / devicePixelRatio;
|
|
} else {
|
|
physicalSize = displaySize;
|
|
}
|
|
|
|
final x = (keyPair.touchPosition.dx / 100.0) * physicalSize.width;
|
|
final y = (keyPair.touchPosition.dy / 100.0) * physicalSize.height;
|
|
|
|
if (kDebugMode) {
|
|
print("Screen size: $physicalSize vs $displaySize => Touch at: $x, $y");
|
|
}
|
|
return Offset(x, y);
|
|
}
|
|
return Offset.zero;
|
|
}
|
|
|
|
Future<ActionResult> performAction(
|
|
ControllerButton button, {
|
|
required bool isKeyDown,
|
|
required bool isKeyUp,
|
|
ButtonTrigger trigger = ButtonTrigger.singleClick,
|
|
}) async {
|
|
if (supportedApp == null) {
|
|
return Error(
|
|
AppLocalizations.current.couldNotPerformButtonnamesplitbyuppercaseNoKeymapSet(button.name.splitByUpperCase()),
|
|
);
|
|
}
|
|
|
|
final keyPair = supportedApp!.keymap.getKeyPair(button, trigger: trigger);
|
|
if (keyPair == null || keyPair.hasNoAction) {
|
|
return Error(AppLocalizations.current.noActionAssignedForButton(button.name.splitByUpperCase()));
|
|
}
|
|
|
|
final guard = proGuard(button: button, trigger: trigger, keyPair: keyPair);
|
|
if (guard is! NotHandled) {
|
|
return guard;
|
|
}
|
|
|
|
// Handle Headwind actions
|
|
if (keyPair.inGameAction == InGameAction.headwindSpeed ||
|
|
keyPair.inGameAction == InGameAction.headwindHeartRateMode) {
|
|
final headwind = core.connection.accessories.where((h) => h.isConnected).firstOrNull;
|
|
if (headwind == null) {
|
|
return Error('No Headwind connected');
|
|
}
|
|
|
|
// Increment command count after successful execution
|
|
await IAPManager.instance.incrementCommandCount();
|
|
return await headwind.handleKeypair(keyPair, isKeyDown: isKeyDown);
|
|
}
|
|
|
|
if (core.logic.hasNoConnectionMethod) {
|
|
if (GyroscopeSteeringButtons.values.contains(button)) {
|
|
return Ignored('Too many messages from gyroscope steering');
|
|
} else {
|
|
return Error(AppLocalizations.current.pleaseSelectAConnectionMethodFirst);
|
|
}
|
|
} else if (!(await core.logic.isTrainerConnected())) {
|
|
return Error(AppLocalizations.current.noConnectionMethodIsConnectedOrActive);
|
|
}
|
|
|
|
final directConnectHandled = await _handleDirectConnect(keyPair, button, isKeyUp: isKeyUp, isKeyDown: isKeyDown);
|
|
if (directConnectHandled is NotHandled && directConnectHandled.message.isNotEmpty) {
|
|
core.connection.signalNotification(LogNotification(directConnectHandled.message));
|
|
} else if (directConnectHandled is! NotHandled) {
|
|
// Increment command count after successful execution
|
|
await IAPManager.instance.incrementCommandCount();
|
|
}
|
|
return directConnectHandled;
|
|
}
|
|
|
|
Future<ActionResult> _handleDirectConnect(
|
|
KeyPair keyPair,
|
|
ControllerButton button, {
|
|
required bool isKeyDown,
|
|
required bool isKeyUp,
|
|
}) async {
|
|
if (keyPair.inGameAction != null) {
|
|
final actions = <ActionResult>[];
|
|
for (final connectedTrainer in core.logic.connectedTrainerConnections) {
|
|
final result = await connectedTrainer.sendAction(
|
|
keyPair,
|
|
isKeyDown: isKeyDown,
|
|
isKeyUp: isKeyUp,
|
|
);
|
|
actions.add(result);
|
|
}
|
|
if (actions.isNotEmpty) {
|
|
return actions.first;
|
|
}
|
|
}
|
|
return NotHandled('');
|
|
}
|
|
|
|
ActionResult proGuard({
|
|
required ControllerButton button,
|
|
required ButtonTrigger trigger,
|
|
required KeyPair keyPair,
|
|
}) {
|
|
if (keyPair.isProAction && !IAPManager.instance.hasActiveSubscription) {
|
|
return Error('Pro subscription required for action: $keyPair');
|
|
}
|
|
|
|
if (!IAPManager.instance.hasActiveSubscription && supportedApp != null) {
|
|
final activeTriggers = ButtonTrigger.values.where((candidate) {
|
|
final candidatePair = supportedApp!.keymap.getKeyPair(button, trigger: candidate);
|
|
return candidatePair != null && !candidatePair.hasNoAction;
|
|
}).toList();
|
|
|
|
if (activeTriggers.length > 1 && trigger != activeTriggers.first) {
|
|
return Error('Pro subscription required for additional trigger types');
|
|
}
|
|
}
|
|
|
|
return NotHandled('');
|
|
}
|
|
}
|
|
|
|
class StubActions extends BaseActions {
|
|
StubActions({super.supportedModes = const []});
|
|
|
|
final List<PerformedAction> performedActions = [];
|
|
|
|
@override
|
|
Future<ActionResult> performAction(
|
|
ControllerButton button, {
|
|
bool isKeyDown = true,
|
|
bool isKeyUp = false,
|
|
ButtonTrigger trigger = ButtonTrigger.singleClick,
|
|
}) async {
|
|
performedActions.add(PerformedAction(button, isDown: isKeyDown, isUp: isKeyUp, trigger: trigger));
|
|
return Future.value(Ignored('${button.name.splitByUpperCase()} clicked'));
|
|
}
|
|
|
|
@override
|
|
void cleanup() {
|
|
performedActions.clear();
|
|
}
|
|
}
|
|
|
|
class PerformedAction {
|
|
final ControllerButton button;
|
|
final bool isDown;
|
|
final bool isUp;
|
|
final ButtonTrigger trigger;
|
|
|
|
PerformedAction(
|
|
this.button, {
|
|
required this.isDown,
|
|
required this.isUp,
|
|
this.trigger = ButtonTrigger.singleClick,
|
|
});
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
identical(this, other) ||
|
|
other is PerformedAction &&
|
|
runtimeType == other.runtimeType &&
|
|
button.copyWith(sourceDeviceId: null) == other.button.copyWith(sourceDeviceId: null) &&
|
|
isDown == other.isDown &&
|
|
isUp == other.isUp &&
|
|
trigger == other.trigger;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(button, isDown, isUp, trigger);
|
|
|
|
@override
|
|
String toString() {
|
|
return '{button: $button, isDown: $isDown, isUp: $isUp, trigger: $trigger}';
|
|
}
|
|
}
|