mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 18:19:21 +01:00
Volume keys are detected as system volume changes. At the volume limit a press changes nothing, so the mapped action was lost. Start detection with the volume at least 10 % away from either end, ignore float rounding when our own setVolume echoes back, and reset the baseline when detection is turned off. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
178 lines
6.5 KiB
Dart
178 lines
6.5 KiB
Dart
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_volume_controller/flutter_volume_controller.dart';
|
|
import 'package:media_key_detector/media_key_detector.dart';
|
|
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
|
|
|
import 'smtc_stub.dart' if (dart.library.io) 'package:smtc_windows/smtc_windows.dart';
|
|
|
|
/// Volume keys are only seen as a change in system volume. At 0 % or 100 %
|
|
/// a press in that direction changes nothing and the button is lost, so the
|
|
/// starting volume is kept at least this far from either end.
|
|
const double _volumeHeadroom = 0.1;
|
|
|
|
/// Volume reported back after [FlutterVolumeController.setVolume] may differ
|
|
/// by float rounding; anything closer than this is not a key press.
|
|
const double _volumeTolerance = 0.005;
|
|
|
|
@visibleForTesting
|
|
double volumeWithHeadroom(double volume) => volume.clamp(_volumeHeadroom, 1 - _volumeHeadroom);
|
|
|
|
@visibleForTesting
|
|
bool isVolumeKeyChange(double volume, double lastVolume) => (volume - lastVolume).abs() >= _volumeTolerance;
|
|
|
|
class MediaKeyHandler {
|
|
final ValueNotifier<bool> isMediaKeyDetectionEnabled = ValueNotifier(false);
|
|
|
|
bool _smtcInitialized = false;
|
|
double? _lastVolume;
|
|
SMTCWindows? _smtc;
|
|
|
|
void initialize() {
|
|
isMediaKeyDetectionEnabled.addListener(() async {
|
|
if (!isMediaKeyDetectionEnabled.value) {
|
|
FlutterVolumeController.removeListener();
|
|
_lastVolume = null;
|
|
if (Platform.isWindows) {
|
|
_smtc?.disableSmtc();
|
|
} else {
|
|
mediaKeyDetector.setIsPlaying(isPlaying: false);
|
|
mediaKeyDetector.removeListener(_onMediaKeyDetectedListener);
|
|
}
|
|
final hidDevice = core.connection.controllerDevices.firstOrNullWhere(
|
|
(e) => e is HidDevice && e.uniqueId == 'HID Device',
|
|
);
|
|
if (hidDevice != null) {
|
|
core.connection.disconnect(hidDevice, persistForget: false, forget: false);
|
|
}
|
|
} else {
|
|
_ensureHidDevice();
|
|
FlutterVolumeController.addListener(
|
|
(volume) {
|
|
if (_lastVolume == null) {
|
|
// Windows: a mapped volume key is reverted, so the volume never
|
|
// moves away from 0 % / 100 % on its own once it's there.
|
|
_lastVolume = Platform.isWindows ? volumeWithHeadroom(volume) : volume;
|
|
if (_lastVolume != volume) {
|
|
FlutterVolumeController.setVolume(_lastVolume!);
|
|
}
|
|
return;
|
|
}
|
|
if (isVolumeKeyChange(volume, _lastVolume!)) {
|
|
final bool hasAction;
|
|
if (volume > _lastVolume!) {
|
|
hasAction = _onMediaKeyDetectedListener(MediaKey.volumeUp);
|
|
} else {
|
|
hasAction = _onMediaKeyDetectedListener(MediaKey.volumeDown);
|
|
}
|
|
if (hasAction) {
|
|
// revert volume
|
|
FlutterVolumeController.setVolume(_lastVolume!);
|
|
} else {
|
|
_lastVolume = volume;
|
|
}
|
|
}
|
|
},
|
|
);
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
HidDevice _ensureHidDevice() {
|
|
// Display label is "Bluetooth Media Remote", but the uniqueId stays
|
|
// "HID Device" so keymaps saved before the rename keep matching.
|
|
final hidDevice = HidDevice('Bluetooth Media Remote', uniqueId: 'HID Device');
|
|
final existing = core.connection.controllerDevices.firstOrNullWhere(
|
|
(e) => e is HidDevice && e.uniqueId == hidDevice.uniqueId,
|
|
);
|
|
if (existing is HidDevice) {
|
|
return existing;
|
|
}
|
|
core.connection.addDevices([hidDevice]);
|
|
return hidDevice;
|
|
}
|
|
|
|
bool _onMediaKeyDetectedListener(MediaKey mediaKey) {
|
|
final availableDevice = _ensureHidDevice();
|
|
|
|
final keyPressed = mediaKey.name;
|
|
|
|
final button = availableDevice.getOrAddButton(
|
|
keyPressed,
|
|
() => ControllerButton(
|
|
keyPressed,
|
|
icon: switch (mediaKey) {
|
|
MediaKey.playPause => LucideIcons.play,
|
|
MediaKey.fastForward => LucideIcons.skipForward,
|
|
MediaKey.rewind => LucideIcons.skipBack,
|
|
MediaKey.volumeUp => LucideIcons.volume2,
|
|
MediaKey.volumeDown => LucideIcons.volume1,
|
|
},
|
|
),
|
|
);
|
|
|
|
// Send press followed by release. HidDevice has supportsLongPress: false,
|
|
// so the action is only executed via the release path
|
|
// (_handleButtonsReleased → _handleSingleButtonTap → performClick).
|
|
availableDevice.handleButtonsClicked([button]);
|
|
availableDevice.handleButtonsClicked([]);
|
|
|
|
return core.actionHandler.supportedApp?.keymap.hasAnyMappedAction(button) == true;
|
|
}
|
|
|
|
bool _onMediaKeyPressedListener(PressedButton mediaKey) {
|
|
return _onMediaKeyDetectedListener(switch (mediaKey) {
|
|
PressedButton.play => MediaKey.playPause,
|
|
PressedButton.pause => MediaKey.playPause,
|
|
PressedButton.next => MediaKey.fastForward,
|
|
PressedButton.previous => MediaKey.rewind,
|
|
PressedButton.stop => MediaKey.playPause,
|
|
PressedButton.fastForward => MediaKey.fastForward,
|
|
PressedButton.rewind => MediaKey.rewind,
|
|
PressedButton.record => throw UnimplementedError(),
|
|
PressedButton.channelUp => MediaKey.volumeUp,
|
|
PressedButton.channelDown => MediaKey.volumeDown,
|
|
});
|
|
}
|
|
}
|