Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

vm: Hold back Mac shortcuts with PushSymbolicHotKeyMode

zqxwcedb19ff67aec3
diff --git a/Research/Guest/keyboard_event_pipeline.md b/Research/Guest/keyboard_event_pipeline.md
index 0149777f..e6234131 100644
--- a/Research/Guest/keyboard_event_pipeline.md
+++ b/Research/Guest/keyboard_event_pipeline.md
@@ -307,3 +307,13 @@ Host shortcuts still apply before the guest sees anything: with
Input Source") the Mac switches its own input source on the same fn press,
and a ⌃Space or ⌘Space bound to Spotlight or input-source switching on the
host is the host's unless the VM view's `capturesSystemKeys` takes it first.
+While a VM window is key, `VPhoneHostHotKeys` pushes
+`kHIHotKeyModeAllDisabledExceptUniversalAccess` so the guest gets them; that
+only takes effect when the responsible app is trusted for Accessibility, and
+lapses when vphone-vm is deactivated or exits. The mode does not cover the
+input-source hot keys: ⌃Space and ⌃⌥Space stay with the Mac, and neither the
+mode nor SkyLight's per-connection `SLSSetSymbolicHotKeyEnabledForConnection`
+lets Space reach vphone-vm (only Control's flags change arrives). Only switching
+the hot key off system-wide does, which can leave the Mac without it after a
+crash, so it is not done. Switch the guest's input source with 🌐 or Device ›
+Switch Guest Input Source.
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift
index 4609cc17..9c406a96 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift
@@ -1,165 +1,46 @@
-import Darwin
-import Foundation
+import ApplicationServices
+import Carbon.HIToolbox
// MARK: - Host Hot Keys
-/// Turns off the Mac's own keyboard shortcuts that a guest needs, while a VM
-/// window is key.
+/// Holds back the Mac's own keyboard shortcuts while a VM window is key.
///
/// macOS handles its symbolic hot keys — Spotlight, input-source switching,
/// the 🌐 shortcuts — in the window server, before the VM window's event
-/// monitor sees the key, and `capturesSystemKeys` does not reach them: with the
-/// default ⌃Space Spotlight binding, ⌃Space in an iPad guest opened Spotlight
-/// on the Mac instead of switching the guest's input source, and 🌐H showed the
-/// Mac's desktop. iPadOS uses the same keys for itself.
+/// monitor sees the key, and `capturesSystemKeys` does not reach them: ⌃Space
+/// in an iPad guest opened Spotlight on the Mac, and 🌐H showed its desktop.
///
-/// While a VM window is key, every enabled hot key on Space, and every one on
-/// fn with a letter, digit, punctuation or arrow key, is switched off through
-/// SkyLight's `CGSSetSymbolicHotKeyEnabled` (the call window switchers use for
-/// ⌘Tab), and switched back on when the window resigns key and when the
-/// process exits. F-key, media-key and ⌘Tab bindings are left alone.
-///
-/// The switch is the window server's, so a crash would leave the shortcuts
-/// off: the suspended set is written to `suspended-hotkeys.plist` with this
-/// process's pid, and the next VM process to start turns them back on when that
-/// pid is gone.
+/// `PushSymbolicHotKeyMode` asks the window server to ignore them while this
+/// process is frontmost. The Mac's settings are never changed: the mode lapses
+/// when the process is deactivated or exits, crash included. It only takes
+/// effect when the responsible app (Launchpad, or the terminal running
+/// `vphone-cli`) is trusted for Accessibility; otherwise the Mac keeps its
+/// shortcuts. The input-source hot keys (⌃Space) are outside the mode and stay
+/// with the Mac; the guest switches with 🌐 or Device › Switch Guest Input
+/// Source.
@MainActor
final class VPhoneHostHotKeys {
static let shared = VPhoneHostHotKeys()
- private typealias GetValue = @convention(c) (
- Int32, UnsafeMutablePointer<UInt16>, UnsafeMutablePointer<UInt16>, UnsafeMutablePointer<UInt32>,
- ) -> Int32
- private typealias IsEnabled = @convention(c) (Int32) -> Bool
- private typealias SetEnabled = @convention(c) (Int32, Bool) -> Int32
-
- private struct SkyLight {
- let get: GetValue
- let isEnabled: IsEnabled
- let setEnabled: SetEnabled
- }
-
- private nonisolated static let skyLight: SkyLight? = {
- guard let handle = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight", RTLD_NOW),
- let get = dlsym(handle, "CGSGetSymbolicHotKeyValue"),
- let isEnabled = dlsym(handle, "CGSIsSymbolicHotKeyEnabled"),
- let setEnabled = dlsym(handle, "CGSSetSymbolicHotKeyEnabled")
- else { return nil }
- return SkyLight(
- get: unsafeBitCast(get, to: GetValue.self),
- isEnabled: unsafeBitCast(isEnabled, to: IsEnabled.self),
- setEnabled: unsafeBitCast(setEnabled, to: SetEnabled.self),
- )
- }()
+ private var token: UnsafeMutableRawPointer?
+ private var reportedUntrusted = false
- /// The hot keys this process turned off. Read at exit, from `atexit`.
- private nonisolated(unsafe) static var suspended: [Int32] = []
-
- private nonisolated static let spaceKeyCode: UInt16 = 0x31
- private nonisolated static let functionModifier: UInt32 = 0x800000
- /// Symbolic hot key identifiers run well below this.
- private static let identifierLimit: Int32 = 512
-
- private init() {
- atexit {
- VPhoneHostHotKeys.resumeSuspended()
- }
- }
+ private init() {}
// MARK: - Suspend / Resume
- /// Turns the conflicting shortcuts off, once, until `resume()`.
func suspend() {
- guard Self.suspended.isEmpty, let skyLight = Self.skyLight else { return }
- var turnedOff: [Int32] = []
- for identifier in 0 ..< Self.identifierLimit where skyLight.isEnabled(identifier) {
- var character: UInt16 = 0
- var keyCode: UInt16 = 0
- var modifiers: UInt32 = 0
- guard skyLight.get(identifier, &character, &keyCode, &modifiers) == 0,
- Self.conflicts(keyCode: keyCode, modifiers: modifiers),
- skyLight.setEnabled(identifier, false) == 0
- else { continue }
- turnedOff.append(identifier)
+ guard token == nil else { return }
+ if !AXIsProcessTrusted(), !reportedUntrusted {
+ reportedUntrusted = true
+ print("[keys] not trusted for Accessibility; the Mac keeps its shortcuts while the VM window is key")
}
- Self.suspended = turnedOff
- Self.record(turnedOff)
+ token = PushSymbolicHotKeyMode(OptionBits(kHIHotKeyModeAllDisabledExceptUniversalAccess))
}
- /// Turns back on whatever `suspend()` turned off.
func resume() {
- Self.resumeSuspended()
- }
-
- private nonisolated static func resumeSuspended() {
- guard !suspended.isEmpty, let skyLight else { return }
- for identifier in suspended {
- _ = skyLight.setEnabled(identifier, true)
- }
- suspended = []
- record([])
- }
-
- /// Whether a Mac shortcut would take a key the guest needs: anything on
- /// Space (Spotlight, input sources), or fn with a key iPadOS also binds to
- /// 🌐 — letters, digits and punctuation (`kVK_*` 0x00–0x32).
- ///
- /// The arrows carry the fn flag whenever they are pressed, so a binding on
- /// fn and an arrow is often just an arrow with modifiers — ⌃← and ⌃→ for
- /// Spaces, ⌃↑ for Mission Control. Only the arrows on fn alone, which is
- /// 🌐 with an arrow, count.
- nonisolated static func conflicts(keyCode: UInt16, modifiers: UInt32) -> Bool {
- if keyCode == spaceKeyCode {
- return true
- }
- guard modifiers & functionModifier != 0 else { return false }
- if (0x7B ... 0x7E).contains(keyCode) {
- return modifiers & deviceIndependentModifiers == functionModifier
- }
- return keyCode <= 0x32
- }
-
- /// Shift, Control, Option, Command, fn (`NSEvent.ModifierFlags`' bits).
- private nonisolated static let deviceIndependentModifiers: UInt32 = 0xFF0000
-
- // MARK: - Crash Recovery
-
- private nonisolated static var recordURL: URL {
- FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
- .appendingPathComponent("vphone", isDirectory: true)
- .appendingPathComponent("suspended-hotkeys.plist")
- }
-
- private nonisolated static func record(_ identifiers: [Int32]) {
- let url = recordURL
- if identifiers.isEmpty {
- try? FileManager.default.removeItem(at: url)
- return
- }
- let plist: [String: Any] = ["pid": Int(getpid()), "hotkeys": identifiers.map(Int.init)]
- try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
- if let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) {
- try? data.write(to: url, options: .atomic)
- }
- }
-
- /// Turns back on the shortcuts a VM process that is no longer running left
- /// off.
- func recoverAfterCrash() {
- guard let skyLight = Self.skyLight,
- let data = try? Data(contentsOf: Self.recordURL),
- let plist = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? [String: Any],
- let pid = plist["pid"] as? Int,
- let identifiers = plist["hotkeys"] as? [Int]
- else { return }
- // Still running: its own resume will turn them back on.
- if pid != Int(getpid()), kill(pid_t(pid), 0) == 0 || errno == EPERM {
- return
- }
- for identifier in identifiers {
- _ = skyLight.setEnabled(Int32(identifier), true)
- }
- try? FileManager.default.removeItem(at: Self.recordURL)
- print("[keys] turned back on \(identifiers.count) Mac shortcuts a stopped VM had suspended")
+ guard let token else { return }
+ PopSymbolicHotKeyMode(token)
+ self.token = nil
}
}
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
index 5b97b2b0..b2aa12ed 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
@@ -50,10 +50,6 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
VPhoneDockName.label(name)
}
- if !command.noGraphics {
- VPhoneHostHotKeys.shared.recoverAfterCrash()
- }
-
signal(SIGINT, SIG_IGN)
let src = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
src.setEventHandler { [weak self] in