EventTap is InputHook on CGEventTap: one dedicated run-loop thread, the injected-event tag passed through, keys/modifiers/mouse/scroll encoded to WM_*/LLKHF_*, the virtual cursor clamped to primary+-1 as InputHook does, Easy Mouse gating on edge candidates, and tap-loss re-enabling the tap AND routing local (C2). HotkeyMatcher ports ProcessHotKeys including the 500 ms double-press lock and the Shift/Win veto; Permissions gains request() and the 5 s health check. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
114 lines
5.0 KiB
Swift
114 lines
5.0 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
|
|
|
|
/// A match.
|
|
public init(action: HotkeyAction, routesLocal: Bool = false) {
|
|
self.action = action
|
|
self.routesLocal = routesLocal
|
|
}
|
|
}
|
|
|
|
/// 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; any non-`nil` result means the key is swallowed locally.
|
|
///
|
|
/// 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) {
|
|
return HotkeyMatch(action: .toggleSwitchToAll) // InputHook.cs:518-522
|
|
}
|
|
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)
|
|
}
|
|
}
|