Run a virtual iPhone on an Apple Silicon Mac.
Swift
Keep the Mac's shortcuts off keys the guest uses; π combinations
Jacky3b6f38f502e9
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneApplication.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneApplication.swiftindex 36928c2f..0be6edb1 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneApplication.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneApplication.swift@@ -19,9 +19,9 @@ final class VPhoneApplication: NSApplication {/// the guest never sees a release without a press.private var menuKeyCodes = Set<UInt16>()- /// Keys whose key-down went to the guest through vphoned, so their key-up- /// goes the same way.- private var forwardedKeyCodes = Set<UInt16>()+ /// Keys whose key-down went to the guest through vphoned, with the usage+ /// sent, so their key-up goes the same way even after π is let go.+ private var forwardedKeys: [UInt16: (control: VPhoneGuestControl, usage: VPhoneGuestKeyMap.Usage)] = [:]/// The guest π is held through, while it is held. fn's release only arrives/// while the window is key, so leaving the window lets go of it here.@@ -41,10 +41,19 @@ final class VPhoneApplication: NSApplication {super.sendEvent(event)return}+ if !takeKeyEvent(event, view: view) {+ super.sendEvent(event)+ }+ }++ /// Whatever this class does with a key event for the VM view before the+ /// view's own handling. Returns true when the event stops here; false when+ /// it goes on to the virtual keyboard, which `VPhoneHostKeyEvents` then+ /// delivers to the view itself.+ func takeKeyEvent(_ event: NSEvent, view: VPhoneVirtualMachineView) -> Bool {if event.type == .flagsChanged {forwardGlobe(event, view: view)- super.sendEvent(event)- return+ return false}// Esc replays the guest's back gesture: iOS has no back key, and a// forwarded Escape only reads as cancel. The press and its release both@@ -55,22 +64,19 @@ final class VPhoneApplication: NSApplication {if event.type == .keyDown {view.performBackGesture()}- return+ return true}- if forwardDroppedKey(event, view: view) {- return+ if forwardThroughGuest(event, view: view) {+ return true}if event.type == .keyUp {- if menuKeyCodes.remove(event.keyCode) == nil {- super.sendEvent(event)- }- return+ return menuKeyCodes.remove(event.keyCode) != nil}if mainMenu?.performKeyEquivalent(with: event) == true {menuKeyCodes.insert(event.keyCode)- return+ return true}- super.sendEvent(event)+ return false}// MARK: - Keys the Virtual Keyboard Drops@@ -92,38 +98,51 @@ final class VPhoneApplication: NSApplication {object: event.window,queue: .main,) { [weak self] _ in- MainActor.assumeIsolated { self?.releaseGlobe() }+ MainActor.assumeIsolated { self?.releaseGlobe(leavingWindow: true) }}} else {- releaseGlobe()+ releaseGlobe(leavingWindow: false)}}- private func releaseGlobe() {+ /// Lets go of π in the guest. Leaving the window also lets go of every key+ /// still held through vphoned, whose release would never arrive.+ private func releaseGlobe(leavingWindow: Bool) {if let resignObserver {NotificationCenter.default.removeObserver(resignObserver)}resignObserver = nilguard let holder = globeHolder else { return }globeHolder = nil+ if leavingWindow {+ for (keyCode, key) in forwardedKeys {+ key.control.sendHIDUp(page: key.usage.page, usage: key.usage.usage)+ forwardedKeys[keyCode] = nil+ }+ }let globe = VPhoneGuestKeyMap.globeholder.sendHIDUp(page: globe.page, usage: globe.usage)}- /// Sends a key `_VZKeyboard` would drop β the JIS input-mode and character- /// keys, Context Menu, Insert, volume β through vphoned. Returns whether the- /// event was one of them; it then goes no further.- private func forwardDroppedKey(_ event: NSEvent, view: VPhoneVirtualMachineView) -> Bool {- guard let usage = VPhoneGuestKeyMap.usage(forDroppedKeyCode: event.keyCode),- let control = view.control- else { return false }- if event.type == .keyDown {- // The guest repeats a held key itself.- guard !event.isARepeat, forwardedKeyCodes.insert(event.keyCode).inserted else { return true }- control.sendHIDDown(page: usage.page, usage: usage.usage)- } else if forwardedKeyCodes.remove(event.keyCode) != nil {- control.sendHIDUp(page: usage.page, usage: usage.usage)+ /// Sends a key through vphoned instead of the virtual keyboard: a key+ /// `_VZKeyboard` would drop β the JIS input-mode and character keys,+ /// Context Menu, Insert, volume β and, while π is held, every key, so the+ /// guest sees π and the key from one keyboard and combines them (πH, πA,+ /// πβ). Returns whether the event went this way; it then goes no further.+ private func forwardThroughGuest(_ event: NSEvent, view: VPhoneVirtualMachineView) -> Bool {+ if event.type == .keyUp {+ guard let key = forwardedKeys.removeValue(forKey: event.keyCode) else { return false }+ key.control.sendHIDUp(page: key.usage.page, usage: key.usage.usage)+ return true}+ let usage = globeHolder != nil+ ? VPhoneGuestKeyMap.usage(whileGlobeHeld: event.keyCode)+ : VPhoneGuestKeyMap.usage(forDroppedKeyCode: event.keyCode)+ guard let usage, let control = view.control else { return false }+ // The guest repeats a held key itself.+ guard !event.isARepeat, forwardedKeys[event.keyCode] == nil else { return true }+ forwardedKeys[event.keyCode] = (control, usage)+ control.sendHIDDown(page: usage.page, usage: usage.usage)return true}}diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swiftnew file mode 100644index 00000000..6011e52a--- /dev/null+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift@@ -0,0 +1,165 @@+import Darwin+import Foundation++// MARK: - Host Hot Keys++/// Turns off the Mac's own keyboard shortcuts that a guest needs, 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.+///+/// 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.+@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+ }++ nonisolated(unsafe) private 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),+ )+ }()++ /// The hot keys this process turned off. Read at exit, from `atexit`.+ nonisolated(unsafe) private static var suspended: [Int32] = []++ nonisolated private static let spaceKeyCode: UInt16 = 0x31+ nonisolated private static let functionModifier: UInt32 = 0x80_0000+ /// Symbolic hot key identifiers run well below this.+ private static let identifierLimit: Int32 = 512++ private init() {+ atexit {+ VPhoneHostHotKeys.resumeSuspended()+ }+ }++ // 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)+ }+ Self.suspended = turnedOff+ Self.record(turnedOff)+ }++ /// Turns back on whatever `suspend()` turned off.+ func resume() {+ Self.resumeSuspended()+ }++ nonisolated private 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).+ nonisolated private static let deviceIndependentModifiers: UInt32 = 0xFF_0000++ // MARK: - Crash Recovery++ nonisolated private static var recordURL: URL {+ FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]+ .appendingPathComponent("vphone", isDirectory: true)+ .appendingPathComponent("suspended-hotkeys.plist")+ }++ nonisolated private 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")+ }+}diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swiftindex 88952750..3703dfac 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift@@ -69,6 +69,7 @@ class VPhoneVirtualMachineWindowController: NSObject {window.center()}window.setFrameAutosaveName(sceneName)+ observeKeyState(of: window)// A frame saved while the guest was sideways is turned back: the guest// boots in portrait, and the orientation poll turns it again if not.applyOrientation(.portrait, to: window, force: true)@@ -164,6 +165,25 @@ class VPhoneVirtualMachineWindowController: NSObject {container.turn(to: orientation, windowFrame: frame, animated: !force)}+ // MARK: - Mac Shortcuts++ /// The Mac's own shortcuts on keys the guest uses are off while this window+ /// is key; see `VPhoneHostHotKeys`.+ private func observeKeyState(of window: NSWindow) {+ let center = NotificationCenter.default+ center.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: .main) { _ in+ MainActor.assumeIsolated { VPhoneHostHotKeys.shared.suspend() }+ }+ for name in [NSWindow.didResignKeyNotification, NSWindow.willCloseNotification] {+ center.addObserver(forName: name, object: window, queue: .main) { _ in+ MainActor.assumeIsolated { VPhoneHostHotKeys.shared.resume() }+ }+ }+ if window.isKeyWindow {+ VPhoneHostHotKeys.shared.suspend()+ }+ }+// MARK: - Title/// The title bar uses one gap everywhere: before the close button, betweendiff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swiftindex ac215d4b..f3ea2b46 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift@@ -27,6 +27,9 @@ import ImageIO/// {"t":"swipe","x1":645,"y1":2600,"x2":645,"y2":1400,"ms":300} β swipe/// {"t":"key","name":"home"} β hardware key (home/power/volup/voldown)/// {"t":"key","name":"cmd+v"} β any other name goes to vphoned `input.key`+/// {"t":"hostkey","keys":"ctrl+space"} β the same key events a Mac keyboard gives the VM+/// window, through `VPhoneApplication` and the virtual+/// keyboard (windowed launches only)/// {"t":"type","text":"Hello"} β set guest clipboard/// {"t":"ping"} β vphoned request/response/// {"t":"rpc","method":"input.type","params":{"text":"ls\n"}}@@ -207,6 +210,15 @@ class VPhoneHostAutomationServer {}try await pressKey(name)+ case "hostkey":+ guard let keys = json["keys"] as? String else {+ return Self.reply(ok: false, error: "hostkey requires keys, such as ctrl+space, fn, esc or vk:0x66")+ }+ guard let view = windowedView else {+ return Self.reply(ok: false, error: "hostkey needs a VM window; this launch is headless")+ }+ try await VPhoneHostKeyEvents.press(keys, in: view)+case "type":guard let text = json["text"] as? String else {return Self.reply(ok: false, error: "type requires text")diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swiftnew file mode 100644index 00000000..b731c76a--- /dev/null+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swift@@ -0,0 +1,135 @@+import AppKit+import Foundation++// MARK: - Host Key Events++/// Key presses replayed as the Mac would deliver them to the VM window: the+/// modifiers as flags-changed events, the key as a key-down and key-up. Each+/// goes through `VPhoneApplication.takeKeyEvent(_:view:)` and, unless that+/// keeps it, to the VM view's own key handling and the virtual USB keyboard β+/// the path a real keyboard takes β so the automation socket can check what+/// reaches the guest from the Mac side.+///+/// The view is handed the event directly rather than through+/// `NSApp.sendEvent(_:)`: AppKit only routes key events to a key window, and+/// a test should not have to bring the VM window to the front. macOS's own+/// shortcuts are not involved; these events never pass the window server.+@MainActor+enum VPhoneHostKeyEvents {+ enum Error: Swift.Error, CustomStringConvertible {+ case unknownKey(String)+ case noWindow++ var description: String {+ switch self {+ case let .unknownKey(name): "unknown key \"\(name)\""+ case .noWindow: "the VM view has no window"+ }+ }+ }++ /// Modifier names, their `kVK_*` code and flag, in press order.+ private static let modifiers: [(names: [String], keyCode: UInt16, flag: NSEvent.ModifierFlags)] = [+ (["fn", "globe"], 0x3F, .function),+ (["ctrl", "control"], 0x3B, .control),+ (["opt", "option", "alt"], 0x3A, .option),+ (["shift"], 0x38, .shift),+ (["cmd", "command"], 0x37, .command),+ ]++ private static let keys: [String: UInt16] = {+ var keys: [String: UInt16] = [+ "a": 0x00, "s": 0x01, "d": 0x02, "f": 0x03, "h": 0x04, "g": 0x05, "z": 0x06, "x": 0x07,+ "c": 0x08, "v": 0x09, "b": 0x0B, "q": 0x0C, "w": 0x0D, "e": 0x0E, "r": 0x0F, "y": 0x10,+ "t": 0x11, "1": 0x12, "2": 0x13, "3": 0x14, "4": 0x15, "6": 0x16, "5": 0x17, "=": 0x18,+ "9": 0x19, "7": 0x1A, "-": 0x1B, "8": 0x1C, "0": 0x1D, "]": 0x1E, "o": 0x1F, "u": 0x20,+ "[": 0x21, "i": 0x22, "p": 0x23, "l": 0x25, "j": 0x26, "'": 0x27, "k": 0x28, ";": 0x29,+ "\\": 0x2A, ",": 0x2B, "/": 0x2C, "n": 0x2D, "m": 0x2E, ".": 0x2F, "`": 0x32,+ "return": 0x24, "enter": 0x24, "tab": 0x30, "space": 0x31, "delete": 0x33, "backspace": 0x33,+ "esc": 0x35, "escape": 0x35, "forwarddelete": 0x75, "home": 0x73, "end": 0x77,+ "pageup": 0x74, "pagedown": 0x79, "left": 0x7B, "right": 0x7C, "down": 0x7D, "up": 0x7E,+ "eisu": 0x66, "kana": 0x68, "yen": 0x5D, "underscore": 0x5E, "menu": 0x6E, "insert": 0x72,+ ]+ for (index, code) in [0x7A, 0x78, 0x63, 0x76, 0x60, 0x61, 0x62, 0x64, 0x65, 0x6D, 0x67, 0x6F].enumerated() {+ keys["f\(index + 1)"] = UInt16(code)+ }+ return keys+ }()++ /// Presses a combination such as `ctrl+space`, `cmd+shift+3`, `fn` or+ /// `vk:0x66`, and releases it.+ static func press(_ combination: String, in view: NSView) async throws {+ guard let window = view.window else { throw Error.noWindow }+ let parts = combination.lowercased().split(separator: "+").map { String($0).trimmingCharacters(in: .whitespaces) }++ var held: [(keyCode: UInt16, flag: NSEvent.ModifierFlags)] = []+ var key: UInt16?+ // Modifiers alone (`fn`, `ctrl+shift`) are pressed and released with no key.+ for part in parts {+ if let modifier = modifiers.first(where: { $0.names.contains(part) }) {+ held.append((modifier.keyCode, modifier.flag))+ } else if part.hasPrefix("vk:"), let code = UInt16(part.dropFirst(3).replacingOccurrences(of: "0x", with: ""), radix: 16) {+ key = code+ } else if let code = keys[part] {+ key = code+ } else {+ throw Error.unknownKey(part)+ }+ }++ var flags: NSEvent.ModifierFlags = []+ for modifier in held {+ flags.insert(modifier.flag)+ send(.flagsChanged, keyCode: modifier.keyCode, flags: flags, in: window)+ try await Task.sleep(for: .milliseconds(30))+ }+ if let key {+ send(.keyDown, keyCode: key, flags: flags, in: window)+ try await Task.sleep(for: .milliseconds(60))+ send(.keyUp, keyCode: key, flags: flags, in: window)+ try await Task.sleep(for: .milliseconds(30))+ } else {+ try await Task.sleep(for: .milliseconds(60))+ }+ for modifier in held.reversed() {+ flags.remove(modifier.flag)+ send(.flagsChanged, keyCode: modifier.keyCode, flags: flags, in: window)+ try await Task.sleep(for: .milliseconds(30))+ }+ }++ private static func send(_ type: NSEvent.EventType, keyCode: UInt16, flags: NSEvent.ModifierFlags, in window: NSWindow) {+ guard let view = window.firstResponder as? VPhoneVirtualMachineView ?? window.contentView?.firstVMView,+ let event = NSEvent.keyEvent(+ with: type,+ location: .zero,+ modifierFlags: flags,+ timestamp: ProcessInfo.processInfo.systemUptime,+ windowNumber: window.windowNumber,+ context: nil,+ characters: "",+ charactersIgnoringModifiers: "",+ isARepeat: false,+ keyCode: keyCode,+ ) else { return }+ if (NSApp as? VPhoneApplication)?.takeKeyEvent(event, view: view) == true {+ return+ }+ switch type {+ case .keyDown: view.keyDown(with: event)+ case .keyUp: view.keyUp(with: event)+ default: view.flagsChanged(with: event)+ }+ }+}++private extension NSView {+ /// The VM view inside this view, when the window's first responder is not it.+ var firstVMView: VPhoneVirtualMachineView? {+ if let view = self as? VPhoneVirtualMachineView { return view }+ for child in subviews {+ if let view = child.firstVMView { return view }+ }+ return nil+ }+}diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swiftindex 7810a4c9..2763d640 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift@@ -28,6 +28,10 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {NSApp.setActivationPolicy(command.noGraphics ? .prohibited : .regular)VPhoneDockName.set(VPhoneDockName.name(forConfig: command.config))+ if !command.noGraphics {+ VPhoneHostHotKeys.shared.recoverAfterCrash()+ }+signal(SIGINT, SIG_IGN)let src = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)src.setEventHandler {diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestKeyMap.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestKeyMap.swiftindex 6efcb312..d03afbbb 100644--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestKeyMap.swift+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestKeyMap.swift@@ -53,4 +53,51 @@ public enum VPhoneGuestKeyMap {public static func usage(forDroppedKeyCode keyCode: UInt16) -> Usage? {droppedKeys[keyCode]}++ // MARK: - Keys Pressed With π++ /// The usage for a key pressed while π is held.+ ///+ /// π reaches the guest through vphoned, from a different keyboard service+ /// than the virtual USB keyboard, and iPadOS only combines keys from one+ /// service: π from vphoned with H from the virtual keyboard is not πH. So+ /// while π is held, the other keys go through vphoned as well.+ ///+ /// macOS rewrites fn with an arrow into Home, End, Page Up and Page Down+ /// before AppKit sees it; held with π, those are turned back into the+ /// arrows the user pressed, which is what iPadOS's π shortcuts bind.+ public static func usage(whileGlobeHeld keyCode: UInt16) -> Usage? {+ if let arrow = globeArrows[keyCode] {+ return Usage(page: 0x07, usage: arrow)+ }+ return keyboardUsages[keyCode].map { Usage(page: 0x07, usage: $0) }+ ?? droppedKeys[keyCode]+ }++ /// fn+arrow as macOS delivers it β the arrow's usage.+ static let globeArrows: [UInt16: UInt32] = [+ 0x73: 0x50, // Home β Left+ 0x77: 0x4F, // End β Right+ 0x74: 0x52, // Page Up β Up+ 0x79: 0x51, // Page Down β Down+ ]++ /// `kVK_*` β keyboard page (7) usage, for the keys a US/ISO keyboard has.+ static let keyboardUsages: [UInt16: UInt32] = [+ 0x00: 0x04, 0x0B: 0x05, 0x08: 0x06, 0x02: 0x07, 0x0E: 0x08, 0x03: 0x09, // a b c d e f+ 0x05: 0x0A, 0x04: 0x0B, 0x22: 0x0C, 0x26: 0x0D, 0x28: 0x0E, 0x25: 0x0F, // g h i j k l+ 0x2E: 0x10, 0x2D: 0x11, 0x1F: 0x12, 0x23: 0x13, 0x0C: 0x14, 0x0F: 0x15, // m n o p q r+ 0x01: 0x16, 0x11: 0x17, 0x20: 0x18, 0x09: 0x19, 0x0D: 0x1A, 0x07: 0x1B, // s t u v w x+ 0x10: 0x1C, 0x06: 0x1D, // y z+ 0x12: 0x1E, 0x13: 0x1F, 0x14: 0x20, 0x15: 0x21, 0x17: 0x22, // 1 2 3 4 5+ 0x16: 0x23, 0x1A: 0x24, 0x1C: 0x25, 0x19: 0x26, 0x1D: 0x27, // 6 7 8 9 0+ 0x24: 0x28, 0x35: 0x29, 0x33: 0x2A, 0x30: 0x2B, 0x31: 0x2C, // Return Esc Delete Tab Space+ 0x1B: 0x2D, 0x18: 0x2E, 0x21: 0x2F, 0x1E: 0x30, 0x2A: 0x31, // - = [ ] \+ 0x29: 0x33, 0x27: 0x34, 0x32: 0x35, 0x2B: 0x36, 0x2F: 0x37, 0x2C: 0x38, // ; ' ` , . /+ 0x0A: 0x64, // ISO Β§+ 0x7A: 0x3A, 0x78: 0x3B, 0x63: 0x3C, 0x76: 0x3D, 0x60: 0x3E, 0x61: 0x3F, // F1-F6+ 0x62: 0x40, 0x64: 0x41, 0x65: 0x42, 0x6D: 0x43, 0x67: 0x44, 0x6F: 0x45, // F7-F12+ 0x75: 0x4C, // Forward Delete+ 0x7C: 0x4F, 0x7B: 0x50, 0x7D: 0x51, 0x7E: 0x52, // Right Left Down Up+ ]}diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestKeyMapTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestKeyMapTests.swiftindex 3f9e3e87..ba73e807 100644--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestKeyMapTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestKeyMapTests.swift@@ -30,4 +30,26 @@ struct GuestKeyMapTests {#expect([0x07, 0x0C].contains(usage.page))}}++ @Test+ func `keys held with π go as their keyboard usages, arrows restored`() {+ // πH, πA, πC, πN, πQ, πE.+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x04) == .init(page: 0x07, usage: 0x0B))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x00) == .init(page: 0x07, usage: 0x04))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x08) == .init(page: 0x07, usage: 0x06))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x2D) == .init(page: 0x07, usage: 0x11))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x0C) == .init(page: 0x07, usage: 0x14))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x0E) == .init(page: 0x07, usage: 0x08))+ // macOS sends fn+β as Home; the guest gets πβ.+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x73) == .init(page: 0x07, usage: 0x50))+ #expect(VPhoneGuestKeyMap.usage(whileGlobeHeld: 0x79) == .init(page: 0x07, usage: 0x51))+ }++ @Test+ func `the π table covers every letter and digit once`() {+ let usages = VPhoneGuestKeyMap.keyboardUsages.values+ for usage in UInt32(0x04) ... 0x27 {+ #expect(usages.filter { $0 == usage }.count == 1)+ }+ }}