mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-02-18 00:17:40 +01:00
Windows: fix media key detection
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:bike_control/bluetooth/devices/hid/hid_device.dart';
|
||||
import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_ble_emulator.dart';
|
||||
import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_mdns_emulator.dart';
|
||||
import 'package:bike_control/bluetooth/devices/openbikecontrol/protocol_parser.dart';
|
||||
@@ -15,22 +14,20 @@ import 'package:bike_control/utils/actions/android.dart';
|
||||
import 'package:bike_control/utils/actions/base_actions.dart';
|
||||
import 'package:bike_control/utils/actions/remote.dart';
|
||||
import 'package:bike_control/utils/keymap/apps/my_whoosh.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:bike_control/utils/requirements/android.dart';
|
||||
import 'package:bike_control/utils/settings/settings.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:media_key_detector/media_key_detector.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
|
||||
import '../bluetooth/connection.dart';
|
||||
import '../bluetooth/devices/mywhoosh/link.dart';
|
||||
import 'keymap/apps/rouvy.dart';
|
||||
import 'media_key_handler.dart';
|
||||
import 'requirements/multi.dart';
|
||||
import 'requirements/platform.dart';
|
||||
import 'smtc_stub.dart' if (dart.library.io) 'package:smtc_windows/smtc_windows.dart';
|
||||
|
||||
final core = Core();
|
||||
|
||||
@@ -336,89 +333,3 @@ class CoreLogic {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MediaKeyHandler {
|
||||
final ValueNotifier<bool> isMediaKeyDetectionEnabled = ValueNotifier(false);
|
||||
|
||||
bool _smtcInitialized = false;
|
||||
SMTCWindows? _smtc;
|
||||
|
||||
void initialize() {
|
||||
isMediaKeyDetectionEnabled.addListener(() async {
|
||||
if (!isMediaKeyDetectionEnabled.value) {
|
||||
if (Platform.isWindows) {
|
||||
_smtc?.disableSmtc();
|
||||
} else {
|
||||
mediaKeyDetector.setIsPlaying(isPlaying: false);
|
||||
mediaKeyDetector.removeListener(_onMediaKeyDetectedListener);
|
||||
}
|
||||
} else {
|
||||
if (Platform.isWindows) {
|
||||
if (!_smtcInitialized) {
|
||||
_smtcInitialized = true;
|
||||
await SMTCWindows.initialize();
|
||||
}
|
||||
|
||||
_smtc = SMTCWindows(
|
||||
metadata: const MusicMetadata(
|
||||
title: 'BikeControl Media Key Handler',
|
||||
album: 'BikeControl',
|
||||
albumArtist: 'BikeControl',
|
||||
artist: 'BikeControl',
|
||||
),
|
||||
// Timeline info for the OS media player
|
||||
timeline: const PlaybackTimeline(
|
||||
startTimeMs: 0,
|
||||
endTimeMs: 1000,
|
||||
positionMs: 0,
|
||||
minSeekTimeMs: 0,
|
||||
maxSeekTimeMs: 1000,
|
||||
),
|
||||
config: const SMTCConfig(
|
||||
fastForwardEnabled: true,
|
||||
nextEnabled: true,
|
||||
pauseEnabled: true,
|
||||
playEnabled: true,
|
||||
rewindEnabled: true,
|
||||
prevEnabled: true,
|
||||
stopEnabled: true,
|
||||
),
|
||||
);
|
||||
_smtc!.buttonPressStream.listen(_onMediaKeyPressedListener);
|
||||
} else {
|
||||
mediaKeyDetector.addListener(_onMediaKeyDetectedListener);
|
||||
mediaKeyDetector.setIsPlaying(isPlaying: true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _onMediaKeyDetectedListener(MediaKey mediaKey) {
|
||||
_onMediaKeyPressedListener(switch (mediaKey) {
|
||||
MediaKey.playPause => PressedButton.play,
|
||||
MediaKey.rewind => PressedButton.rewind,
|
||||
MediaKey.fastForward => PressedButton.fastForward,
|
||||
MediaKey.volumeUp => PressedButton.channelUp,
|
||||
MediaKey.volumeDown => PressedButton.channelDown,
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onMediaKeyPressedListener(PressedButton mediaKey) async {
|
||||
final hidDevice = HidDevice('HID Device');
|
||||
final keyPressed = mediaKey.name;
|
||||
|
||||
final button = hidDevice.getOrAddButton(
|
||||
keyPressed,
|
||||
() => ControllerButton(keyPressed),
|
||||
);
|
||||
|
||||
var availableDevice = core.connection.controllerDevices.firstOrNullWhere(
|
||||
(e) => e.toString() == hidDevice.toString(),
|
||||
);
|
||||
if (availableDevice == null) {
|
||||
core.connection.addDevices([hidDevice]);
|
||||
availableDevice = hidDevice;
|
||||
}
|
||||
availableDevice.handleButtonsClickedWithoutLongPressSupport([button]);
|
||||
}
|
||||
}
|
||||
|
||||
96
lib/utils/media_key_handler.dart
Normal file
96
lib/utils/media_key_handler.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:bike_control/bluetooth/devices/hid/hid_device.dart';
|
||||
import 'package:bike_control/utils/core.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:media_key_detector/media_key_detector.dart';
|
||||
|
||||
import 'smtc_stub.dart' if (dart.library.io) 'package:smtc_windows/smtc_windows.dart';
|
||||
|
||||
class MediaKeyHandler {
|
||||
final ValueNotifier<bool> isMediaKeyDetectionEnabled = ValueNotifier(false);
|
||||
|
||||
bool _smtcInitialized = false;
|
||||
SMTCWindows? _smtc;
|
||||
|
||||
void initialize() {
|
||||
isMediaKeyDetectionEnabled.addListener(() async {
|
||||
if (!isMediaKeyDetectionEnabled.value) {
|
||||
if (Platform.isWindows) {
|
||||
_smtc?.disableSmtc();
|
||||
} else {
|
||||
mediaKeyDetector.setIsPlaying(isPlaying: false);
|
||||
mediaKeyDetector.removeListener(_onMediaKeyDetectedListener);
|
||||
}
|
||||
} else {
|
||||
if (Platform.isWindows) {
|
||||
if (!_smtcInitialized) {
|
||||
_smtcInitialized = true;
|
||||
await SMTCWindows.initialize();
|
||||
}
|
||||
|
||||
_smtc = SMTCWindows(
|
||||
metadata: const MusicMetadata(
|
||||
title: 'BikeControl Media Key Handler',
|
||||
album: 'BikeControl',
|
||||
albumArtist: 'BikeControl',
|
||||
artist: 'BikeControl',
|
||||
),
|
||||
// Timeline info for the OS media player
|
||||
timeline: const PlaybackTimeline(
|
||||
startTimeMs: 0,
|
||||
endTimeMs: 1000,
|
||||
positionMs: 0,
|
||||
minSeekTimeMs: 0,
|
||||
maxSeekTimeMs: 1000,
|
||||
),
|
||||
config: const SMTCConfig(
|
||||
fastForwardEnabled: true,
|
||||
nextEnabled: true,
|
||||
pauseEnabled: true,
|
||||
playEnabled: true,
|
||||
rewindEnabled: true,
|
||||
prevEnabled: true,
|
||||
stopEnabled: true,
|
||||
),
|
||||
);
|
||||
_smtc!.buttonPressStream.listen(_onMediaKeyPressedListener);
|
||||
} else {
|
||||
mediaKeyDetector.addListener(_onMediaKeyDetectedListener);
|
||||
mediaKeyDetector.setIsPlaying(isPlaying: true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _onMediaKeyDetectedListener(MediaKey mediaKey) {
|
||||
_onMediaKeyPressedListener(switch (mediaKey) {
|
||||
MediaKey.playPause => PressedButton.play,
|
||||
MediaKey.rewind => PressedButton.rewind,
|
||||
MediaKey.fastForward => PressedButton.fastForward,
|
||||
MediaKey.volumeUp => PressedButton.channelUp,
|
||||
MediaKey.volumeDown => PressedButton.channelDown,
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onMediaKeyPressedListener(PressedButton mediaKey) async {
|
||||
final hidDevice = HidDevice('HID Device');
|
||||
final keyPressed = mediaKey.name;
|
||||
|
||||
final button = hidDevice.getOrAddButton(
|
||||
keyPressed,
|
||||
() => ControllerButton(keyPressed),
|
||||
);
|
||||
|
||||
var availableDevice = core.connection.controllerDevices.firstOrNullWhere(
|
||||
(e) => e.toString() == hidDevice.toString(),
|
||||
);
|
||||
if (availableDevice == null) {
|
||||
core.connection.addDevices([hidDevice]);
|
||||
availableDevice = hidDevice;
|
||||
}
|
||||
availableDevice.handleButtonsClickedWithoutLongPressSupport([button]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user