Run a virtual iPhone on an Apple Silicon Mac.
Swift
Bring guest clipboard copies to the Mac when the VM window loses focus
Jackyfe46ee2f65cc
diff --git a/Skills/vphone-guest-control/references/guest-socket.md b/Skills/vphone-guest-control/references/guest-socket.mdindex f794c6c1..7f0e40a6 100644--- a/Skills/vphone-guest-control/references/guest-socket.md+++ b/Skills/vphone-guest-control/references/guest-socket.md@@ -61,7 +61,11 @@ text selector and skip coordinates entirely.## Typing text-`{"t":"type"}` only fills the clipboard. Either follow it with a paste:+`{"t":"type"}` only fills the clipboard. It stays in the guest: a guest+clipboard change made while the VM window is key is copied to the Mac+clipboard when the window loses focus, but not one made by `type`. A+`clipboard.set` through `rpc` is treated like a copy in the guest. Either+follow it with a paste:```shvphone-launchpad-cli guest send myphone '{"t":"type","text":"hello","screen":false}'diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneClipboardSync.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneClipboardSync.swiftindex ce3dee8e..3be10f69 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneClipboardSync.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneClipboardSync.swift@@ -7,6 +7,9 @@ import VPhoneCoreKit/// ⌘C, ⌘X and ⌘V in the VM window carry the clipboard through vphoned./// Paste sends a changed Mac clipboard to the guest before the guest pastes;/// copy and cut bring the guest clipboard back once the guest has written it.+/// A copy made in the guest without those keys (by touch, or by an app)+/// reaches the Mac when the VM window resigns key, which is when the user+/// goes to paste it somewhere else./// Only small text and images are synced (`VPhoneClipboardTransfer`).@MainActorfinal class VPhoneClipboardSync {@@ -15,6 +18,18 @@ final class VPhoneClipboardSync {/// unchanged Mac clipboard is not sent again, so a guest copy made by/// touch is not overwritten by an older Mac one.private var syncedMacChangeCount: Int?+ /// The guest clipboard as last sent to or written from the Mac, or as+ /// adopted when the window became key or the guest connected. Only a+ /// later guest change is brought to the Mac on resign key. The count+ /// starts again with each vphoned instance, so it is kept with the+ /// instance that gave it and never compared across one.+ private var syncedGuest: (instance: String?, changeCount: Int)?+ /// Moves on with every adopt and every sync. A read that finishes after+ /// a later one started is dropped, so a slow read (a guest request may+ /// wait out the 120 s socket timeout) neither holds up nor undoes a newer+ /// one.+ private var guestGeneration = 0+ private var isAdoptingForConnection = falseprivate var copyTask: Task<Void, Never>?/// How long copy waits for the guest to write its clipboard.@@ -38,6 +53,7 @@ final class VPhoneClipboardSync {let changeCount = pasteboard.changeCountlet payload = changeCount == syncedMacChangeCount ? nil : Self.macPayload(from: pasteboard)Task {+ var sent = falseif let payload {do {switch payload {@@ -45,11 +61,19 @@ final class VPhoneClipboardSync {case let .image(data): try await control.clipboardSet(imageData: data)}syncedMacChangeCount = changeCount+ sent = true} catch {print("[clipboard] Mac to guest: \(error)")}}Self.sendChord(usage: Self.usageV, to: control)+ // Read behind the paste key, which does not change the clipboard,+ // so the paste is not held up. The Mac clipboard the guest now+ // holds is not brought back on resign key, where it would replace+ // rich text with its plain text.+ if sent {+ await adopt(from: control)+ }}}@@ -73,9 +97,75 @@ final class VPhoneClipboardSync {}}+ // MARK: - Window Focus++ /// Takes the guest clipboard as it is now as synced. Called when the+ /// window becomes key: a change made while it was not, by automation or+ /// Guest Tools, was not copied by the user here.+ func adoptGuestClipboard() {+ guard let control, isAvailable else { return }+ Task { await adopt(from: control) }+ }++ /// Brings a guest copy made since the last sync to the Mac. Called when+ /// the window resigns key, so text copied by touch can be pasted in a Mac+ /// app. Without a count from this vphoned there is nothing known to be new.+ func bringGuestCopyToMac() {+ guard let control, isAvailable,+ let synced = syncedGuest, synced.instance == control.connectedInstance+ else { return }+ let generation = guestGeneration+ Task {+ guard let info = try? await control.clipboardInfoAfterQueuedInput(),+ generation == guestGeneration, synced.instance == control.connectedInstance,+ info.changeCount != synced.changeCount+ else { return }+ await writeToMac(info, control: control)+ }+ }++ /// Follows the connection the window polls. A vphoned that has connected+ /// gives the first count, so a copy made before the window ever resigned+ /// key still reaches the Mac; a disconnect forgets it.+ func connectionChanged(connected: Bool) {+ guard connected else {+ syncedGuest = nil+ return+ }+ guard let control, isAvailable, !isAdoptingForConnection,+ syncedGuest == nil || syncedGuest?.instance != control.connectedInstance+ else { return }+ isAdoptingForConnection = true+ Task {+ await adopt(from: control)+ isAdoptingForConnection = false+ }+ }++ /// Takes the count read now as synced, unless a later adopt or sync+ /// started before the read came back.+ private func adopt(from control: VPhoneGuestControl) async {+ guestGeneration += 1+ let generation = guestGeneration+ let instance = control.connectedInstance+ guard let info = try? await control.clipboardInfoAfterQueuedInput(),+ generation == guestGeneration, instance == control.connectedInstance+ else { return }+ markGuestSynced(info.changeCount, instance: instance)+ }++ private func markGuestSynced(_ changeCount: Int, instance: String?) {+ guestGeneration += 1+ syncedGuest = (instance, changeCount)+ }+// MARK: - Guest to Mac+ /// Writes the guest clipboard to the Mac. The count is taken as synced+ /// even when nothing is written, so content too large or of another kind+ /// is not tried again on every resign key.private func writeToMac(_ info: VPhoneGuestControl.ClipboardContent, control: VPhoneGuestControl) async {+ markGuestSynced(info.changeCount, instance: control.connectedInstance)let pasteboard = NSPasteboard.generalswitch VPhoneClipboardTransfer.guestKind(text: info.text, hasImage: info.hasImage) {case .text:diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swiftindex 0e2a77f3..55ea13cb 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift@@ -65,8 +65,9 @@ final class VPhoneGuestControl {@ObservationIgnored var unlocksAtStartup = false/// The `instance` of the last vphoned connected to. Kept across/// disconnects: a lost probe reconnects to the same vphoned, which is not- /// a start.- @ObservationIgnored private var connectedInstance: String?+ /// a start. Guest counters such as the clipboard's change count start+ /// again with each instance.+ @ObservationIgnored private(set) var connectedInstance: String?/// Called whenever the address the guest reports changes, nil on disconnect.@ObservationIgnored var onGuestIPAddressChange: ((String?) -> Void)?diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swiftindex e0a51d78..68deb8ca 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift@@ -117,6 +117,7 @@ class VPhoneVirtualMachineWindowController: NSObject, NSWindowDelegate {guard let self, let control = self.control else { return }self.updateHomeButton(connected: control.isConnected)self.updateSubtitle(control: control)+ self.captureView?.clipboardSync?.connectionChanged(connected: control.isConnected)}})@@ -227,6 +228,15 @@ class VPhoneVirtualMachineWindowController: NSObject, NSWindowDelegate {if window.isKeyWindow, captureView?.hardwareKeyboardEnabled == true {VPhoneHostHotKeys.shared.suspend()}++ // A guest copy made while this window is key reaches the Mac when it+ // resigns key; see `VPhoneClipboardSync`.+ keyStateObservers.append(center.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: .main) { [weak self] _ in+ MainActor.assumeIsolated { self?.captureView?.clipboardSync?.adoptGuestClipboard() }+ })+ keyStateObservers.append(center.addObserver(forName: NSWindow.didResignKeyNotification, object: window, queue: .main) { [weak self] _ in+ MainActor.assumeIsolated { self?.captureView?.clipboardSync?.bringGuestCopyToMac() }+ })}// MARK: - Titlediff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swiftindex f77d9b4c..fa8c81a4 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostAutomationServer.swift@@ -230,6 +230,9 @@ class VPhoneHostAutomationServer {return Self.reply(ok: false, error: "type requires text")}try await connectedControl().clipboardSet(text: text)+ // Not a copy the user made, so leaving the window does not+ // put it on the Mac clipboard.+ captureView?.clipboardSync?.adoptGuestClipboard()case "rpc":guard let method = json["method"] as? String, !method.isEmpty else {