QA finding 4: `ProcessHotKeys` never returns `false` for a matched `HotKeySwitch2AllPC` (`InputHook.cs:518-522`) — unlike every other hotkey branch it falls through, so a chord that does not itself hold Ctrl+Alt toggles ALL mode and still reaches the OS. `HotkeyMatch` grows `swallows`, true for every hotkey except that one when its configured chord requires neither Ctrl nor Alt; `handleKey` then queues the action and carries on encoding the key as an ordinary keystroke, which is what `ProcessHotKeys` returning `true` leaves to `KeyboardEvent` and `CallNextHookEx`. Documented in docs/protocol.md §4.5 and in the feature README, including the residual narrowing (Windows also requires the VK to land in the switch-machine range, `:611-620`). Unreachable today: the shortcut ships empty (`MouseWithoutBordersProperties.cs:30`) and an empty hotkey never matches (`Common.cs:127-129`). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
129 lines
5.9 KiB
Swift
129 lines
5.9 KiB
Swift
// Which key presses are commands to this app rather than keystrokes for the
|
|
// machine in control.
|
|
//
|
|
// A direct port of `InputHook.ProcessHotKeys` (`InputHook.cs:516-658`) and the
|
|
// predicate it leans on, `Common.HotkeyMatched` (`Common.cs:127-129`). The
|
|
// matcher decides and swallows; carrying out an action — sending the lock chord,
|
|
// re-opening sockets, flipping a setting — is the Engine's job, because none of
|
|
// it can happen inside the tap callback (CLAUDE.md invariant 4).
|
|
//
|
|
// The one exception is the escape set (contracts C2): those must take effect
|
|
// *before* the callback returns, so the tap applies them to the routing snapshot
|
|
// itself and the match merely says so.
|
|
|
|
import Foundation
|
|
import MWBCore
|
|
|
|
/// A matched hotkey and what it means for the key event that triggered it.
|
|
public struct HotkeyMatch: Sendable, Equatable {
|
|
/// What the Engine should do.
|
|
public var action: HotkeyAction
|
|
/// Whether the tap must route control back to this Mac before returning
|
|
/// (contracts C2). True for the escape set only.
|
|
public var routesLocal: Bool
|
|
/// Whether the key event is eaten. True for every hotkey but one — see
|
|
/// `switchToAll` in `match(vk:…)` (`InputHook.cs:518-522`).
|
|
public var swallows: Bool
|
|
|
|
/// A match.
|
|
public init(action: HotkeyAction, routesLocal: Bool = false, swallows: Bool = true) {
|
|
self.action = action
|
|
self.routesLocal = routesLocal
|
|
self.swallows = swallows
|
|
}
|
|
}
|
|
|
|
/// The hotkey rules, plus the one piece of state they need: when the lock hotkey
|
|
/// was last pressed.
|
|
///
|
|
/// A value type held by `EventTap` and touched only on the tap thread.
|
|
public struct HotkeyMatcher: Sendable {
|
|
/// Two presses of the lock hotkey inside this window lock every machine
|
|
/// instead of just the one in control (`InputHook.cs:544`).
|
|
public static let lockDoublePressWindow: TimeInterval = 0.5
|
|
|
|
/// The configured hotkeys. Replaced when settings change.
|
|
public var config: HotkeyConfig
|
|
|
|
private var lastLockPress: TimeInterval?
|
|
|
|
/// A matcher over these hotkeys.
|
|
public init(config: HotkeyConfig = HotkeyConfig()) {
|
|
self.config = config
|
|
}
|
|
|
|
/// Classifies one key-down. Returns `nil` when the key is an ordinary
|
|
/// keystroke; a non-`nil` result means the key is a command, and its
|
|
/// `swallows` says whether it is also eaten.
|
|
///
|
|
/// Windows checks in this order and so do we; only the order makes the
|
|
/// `else if` chain in `ProcessHotKeys` behave (`InputHook.cs:516-624`). The
|
|
/// mac-only escape is checked first because it must work even when the
|
|
/// Windows `Shift`/`Win` veto below would swallow the chord.
|
|
public mutating func match(
|
|
vk: Int32, modifiers: ModifierState, localSlot: Int?, destinationIsRemote: Bool,
|
|
now: TimeInterval
|
|
) -> HotkeyMatch? {
|
|
if HotkeyConfig.escape.matches(vk: vk, modifiers: modifiers) {
|
|
return HotkeyMatch(action: .escapedToLocal, routesLocal: true)
|
|
}
|
|
if config.switchToAll.matches(vk: vk, modifiers: modifiers) {
|
|
// The one hotkey `ProcessHotKeys` does not eat: its branch
|
|
// (`InputHook.cs:518-522`) falls through instead of returning
|
|
// `false`, so the key still reaches the OS unless the chord itself
|
|
// holds Ctrl+Alt and is caught by the `if (CtrlDown && altDown)`
|
|
// block below (`:585-621`). Unreachable until a hotkey editor
|
|
// exists — `Switch2AllPCShortcut` ships empty
|
|
// (`MouseWithoutBordersProperties.cs:30`) and an empty hotkey never
|
|
// matches (`Common.cs:127-129`).
|
|
return HotkeyMatch(
|
|
action: .toggleSwitchToAll,
|
|
swallows: config.switchToAll.control && config.switchToAll.option)
|
|
}
|
|
if config.toggleEasyMouse.matches(vk: vk, modifiers: modifiers) {
|
|
return HotkeyMatch(action: .toggleEasyMouse) // InputHook.cs:524-537
|
|
}
|
|
if config.lock.matches(vk: vk, modifiers: modifiers) {
|
|
return HotkeyMatch(action: lockAction(now: now)) // InputHook.cs:539-577
|
|
}
|
|
if config.reconnect.matches(vk: vk, modifiers: modifiers) {
|
|
return HotkeyMatch(action: .reconnect) // InputHook.cs:578-584
|
|
}
|
|
return switchMachineMatch(
|
|
vk: vk, modifiers: modifiers, localSlot: localSlot,
|
|
destinationIsRemote: destinationIsRemote)
|
|
}
|
|
|
|
/// Forgets the last lock press, so the next one cannot be read as the second
|
|
/// half of a double press. Called when control changes machines.
|
|
public mutating func resetLockTimer() {
|
|
lastLockPress = nil
|
|
}
|
|
|
|
/// One press sends the chord to the machine in control so *it* locks; a
|
|
/// second press inside 500 ms locks everything (`InputHook.cs:544-575`).
|
|
private mutating func lockAction(now: TimeInterval) -> HotkeyAction {
|
|
let isDouble =
|
|
lastLockPress.map { now - $0 < HotkeyMatcher.lockDoublePressWindow } ?? false
|
|
lastLockPress = now
|
|
return isDouble ? .lockAllMachines : .lockMachine
|
|
}
|
|
|
|
/// ⌃⌥F1..F4 → matrix slot 0..3 (`InputHook.cs:611-620`).
|
|
///
|
|
/// The two rules that come with it: Control and Option must both be down and
|
|
/// nothing else may be (`:586, 604-610` — issue #26597), and the slot naming
|
|
/// this Mac is an escape hotkey, so it routes control home from inside the
|
|
/// callback (contracts C2).
|
|
private func switchMachineMatch(
|
|
vk: Int32, modifiers: ModifierState, localSlot: Int?, destinationIsRemote: Bool
|
|
) -> HotkeyMatch? {
|
|
guard modifiers.control && modifiers.option else { return nil }
|
|
guard !modifiers.shift && !modifiers.command else { return nil }
|
|
let slot = Int(vk - config.switchMachineVK)
|
|
guard slot >= 0 && slot < HotkeyConfig.switchMachineSlots else { return nil }
|
|
let comingHome = destinationIsRemote && slot == localSlot
|
|
return HotkeyMatch(action: .switchToSlot(slot), routesLocal: comingHome)
|
|
}
|
|
}
|