Run a virtual iPhone on an Apple Silicon Mac.
Swift
Start edge swipes through vphoned, with the digitizer edge flags
Maximilian Paßf22628872c7f
diff --git a/VPhoneDaemon/Daemon/GuestAPI.swift b/VPhoneDaemon/Daemon/GuestAPI.swiftindex 40b2b660..1d7acd5e 100644--- a/VPhoneDaemon/Daemon/GuestAPI.swift+++ b/VPhoneDaemon/Daemon/GuestAPI.swift@@ -98,6 +98,7 @@ enum GuestAPI {"capabilities": ["touch","touch2",+ "touch_edge","hid","apps","url",@@ -312,6 +313,15 @@ enum GuestAPI {guard let phase = (params["phase"] as? String).flatMap(TouchPhase.init(rawValue:)) else {throw GuestAPIError.invalidRequest("phase must be down, move or up")}+ // A touch that starts at a screen edge needs the digitizer edge+ // flags, which icli does not set; vphoned injects those itself.+ if let edge = params["edge"] as? Int, edge != 0 {+ guard params["normalized"] as? Bool ?? true else {+ throw GuestAPIError.invalidRequest("edge touch coordinates are normalized 0..1 only")+ }+ vp_hid_touch_edge(touchPhaseCode(phase), number(params, "x"), number(params, "y"), Int32(edge))+ return ["phase": phase.rawValue, "edge": edge]+ }return try touch(phase, x: number(params, "x"), y: number(params, "y"),normalized: params["normalized"] as? Bool ?? true,diff --git a/VPhoneDaemon/Native/Include/VphonedNative.h b/VPhoneDaemon/Native/Include/VphonedNative.hindex 937605a8..70808445 100644--- a/VPhoneDaemon/Native/Include/VphonedNative.h+++ b/VPhoneDaemon/Native/Include/VphonedNative.h@@ -27,6 +27,12 @@ bool vp_hid_load(void);/// `touchSequence` can carry two fingers at once, which is why this exists.void vp_hid_touch2(int phase, double x1, double y1, double x2, double y2);+/// Inject one finger that started at a screen edge, with the digitizer edge+/// flags UIKit's screen-edge gestures (home, Control Center) require. `edge` is+/// the host's `_VZTouch` swipeAim code: 1 top, 2 bottom, 4 right, 8 left.+/// Phase and coordinates as for vp_hid_touch2.+void vp_hid_touch_edge(int phase, double x, double y, int edge);+/// Turn the display on without pressing a button. False when/// SpringBoardServices has no SBSUndimScreen.bool vp_screen_undim(void);diff --git a/VPhoneDaemon/Native/vphoned_touch.m b/VPhoneDaemon/Native/vphoned_touch.mindex 4ea7d49c..ffc20ff7 100644--- a/VPhoneDaemon/Native/vphoned_touch.m+++ b/VPhoneDaemon/Native/vphoned_touch.m@@ -45,6 +45,15 @@ static bool gTouchUnavailableLogged;#define VP_DIG_TOUCH 0x00000002u#define VP_DIG_POSITION 0x00000004u#define VP_DIG_IDENTITY 0x00000020u+// Edge flags as touch firmware reports them for a touch that starts at a screen+// edge. UIKit (_UIEventHIDTouchEdgeTypeForHIDEvent) maps 0x800 to edge type 1,+// which UIScreenEdgePanGestureRecognizer requires to begin, and bits 24-27 to+// the edge aim (1 bottom, 2 top, 4 right, 8 left). Without them SpringBoard's+// bottom-edge home gesture (and Control Center / Notification Center from the+// top) never starts. The VZ USB touchscreen reports none of this: its HID+// report has only index, range, touch, x and y per finger.+#define VP_DIG_FROM_EDGE 0x00000800u+#define VP_DIG_AIM_SHIFT 24#define VP_TRANSDUCER_HAND 1// kIOHIDEventFieldDigitizerIsDisplayIntegrated: (kIOHIDEventTypeDigitizer << 16) | offset.#define VP_FIELD_IS_DISPLAY_INTEGRATED ((((uint32_t)11) << 16) | 25)@@ -141,3 +150,40 @@ void vp_hid_touch2(int phase, double x1, double y1, double x2, double y2) {break;}}++// Host edge code (`_VZTouch` swipeAim: 1 top, 2 bottom, 4 right, 8 left) to the+// HID edge flags, kept for the whole touch like real firmware does.+static uint32_t edge_flags(int edge) {+ uint32_t aim = 0;+ if (edge & 1) aim |= 2; // top+ if (edge & 2) aim |= 1; // bottom+ if (edge & 4) aim |= 4; // right+ if (edge & 8) aim |= 8; // left+ return aim ? (VP_DIG_FROM_EDGE | (aim << VP_DIG_AIM_SHIFT)) : 0;+}++void vp_hid_touch_edge(int phase, double x, double y, int edge) {+ if (!gTouchClient) {+ if (!vp_hid_load() && !gTouchUnavailableLogged) {+ gTouchUnavailableLogged = true;+ NSLog(@"vphoned: edge touch injection unavailable on this base");+ }+ if (!gTouchClient) return;+ }++ const uint32_t flags = edge_flags(edge);+ const double xs[1] = {x};+ const double ys[1] = {y};+ switch (phase) {+ case 0: // down+ dispatch_digitizer_points(xs, ys, 1, 1, 1, VP_DIG_TOUCH | VP_DIG_IDENTITY | flags);+ break;+ case 1: // move+ dispatch_digitizer_points(xs, ys, 1, 1, 1, VP_DIG_POSITION | flags);+ break;+ case 3: // up+ default:+ dispatch_digitizer_points(xs, ys, 1, 0, 0, VP_DIG_TOUCH | VP_DIG_IDENTITY | flags);+ break;+ }+}diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swiftindex 19cc98cc..8e447549 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift@@ -313,14 +313,30 @@ final class VPhoneGuestControl {enqueueInput("input.hid", params: params)}- func sendTouch(phase: Int, x: Double, y: Double) {+ /// `edge` is the screen edge the touch started at, in `_VZTouch` swipeAim+ /// encoding (1 top, 2 bottom, 4 right, 8 left, 0 none). A guest with+ /// `touch_edge` injects such a touch with the digitizer edge flags UIKit's+ /// screen-edge gestures (home, Control Center) require.+ func sendTouch(phase: Int, x: Double, y: Double, edge: Int = 0) {let name =switch phase {case 0: "down"case 1: "move"default: "up"}- enqueueInput("input.touch", params: ["phase": name, "x": x, "y": y, "normalized": true])+ var params: [String: Any] = ["phase": name, "x": x, "y": y, "normalized": true]+ if edge != 0, supportsEdgeTouch {+ params["edge"] = edge+ }+ enqueueInput("input.touch", params: params)+ }++ /// Whether the guest can inject a touch carrying screen-edge flags. The+ /// VZ USB touchscreen cannot: its HID report has no field for them, so+ /// `_VZTouch`'s swipeAim never reaches the guest and SpringBoard's+ /// bottom-edge home gesture does not start.+ var supportsEdgeTouch: Bool {+ isConnected && guestCapabilities.contains("touch_edge")}/// Whether the guest takes two fingers in one hand event. Guests that onlydiff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swiftindex 83746ba5..f23ad1a5 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift@@ -27,6 +27,10 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {}private var currentTouchSwipeAim: Int = 0+ /// The touch in flight started at a screen edge and goes through vphoned,+ /// which can mark it as an edge touch. Decided when the finger goes down and+ /// kept until it lifts, so one touch never switches injection path.+ private var edgeTouchViaGuest = falseprivate var isDragHighlightVisible = false/// Synthetic guest finger driven by an in-flight trackpad scroll gesture.@@ -587,7 +591,8 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {}let carry = scrollPendingscrollPending = .zero- beginScrollTouch(at: scrollStartPoint(for: pointer, travel: carry))+ let start = scrollStartPoint(for: pointer, travel: carry)+ beginScrollTouch(at: start.point, swipeAim: start.swipeAim)if scrollTouchPoint != nil {moveScrollTouch(by: carry)}@@ -639,21 +644,29 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {////// Up and the bottom strip are the window's, which is the interface's: on/// a sideways guest the Home indicator runs along a long side of the panel.- private func scrollStartPoint(for pointer: NSPoint, travel: CGPoint) -> NSPoint {+ ///+ /// The snapped touch is also marked as starting at the bottom edge+ /// (`swipeAim`): the guest's edge-pan recognizers ignore a touch that+ /// begins at the edge but does not carry the edge flags.+ private func scrollStartPoint(for pointer: NSPoint, travel: CGPoint) -> (point: NSPoint, swipeAim: Int) {let orientation = screenOrientationlet screenTravel = orientation.screenVector(fromPanel: travel)let screenPointer = orientation.screenPoint(fromPanel: pointer, panel: bounds.size)let screenHeight = orientation.displayedSize(panel: bounds.size).heightguard screenTravel.y > abs(screenTravel.x),screenPointer.y <= screenHeight * Self.homeEdgeSnapFraction- else { return pointer }- return orientation.panelPoint(fromScreen: CGPoint(x: screenPointer.x, y: 0), panel: bounds.size)+ else { return (pointer, 0) }+ let point = orientation.panelPoint(fromScreen: CGPoint(x: screenPointer.x, y: 0), panel: bounds.size)+ // The interface's bottom is a panel edge; on a sideways guest it is a+ // long side of the panel, which is what the digitizer reports.+ return (point, hitTestEdge(at: clampedTouchPoint(point)))}/// Press the synthetic finger down at `point`, so the drag starts where the/// user is pointing.- private func beginScrollTouch(at point: NSPoint) {+ private func beginScrollTouch(at point: NSPoint, swipeAim: Int = 0) {releaseSecondaryPress()+ currentTouchSwipeAim = swipeAimlet start = clampedTouchPoint(point)scrollTouchPoint = startscrollLastSend = 0@@ -735,6 +748,8 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {phase: 3, localPoint: clampedTouchPoint(current),timestamp: ProcessInfo.processInfo.systemUptime,)+ // The re-press lands inside the window: an ordinary touch.+ currentTouchSwipeAim = 0}private func hasScrollRebaseGapElapsed() -> Bool {@@ -767,6 +782,7 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {phase: 3, localPoint: point, allowOutside: true,timestamp: ProcessInfo.processInfo.systemUptime,)+ currentTouchSwipeAim = 0}private func beginPinch(at center: NSPoint) {@@ -1069,10 +1085,24 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {guard !localPoints.isEmpty else { return false }let normalized = localPoints.map { normalizeCoordinate($0, allowOutside: allowOutside) }+ // A single finger that starts at a screen edge goes through vphoned+ // when the guest can flag it as an edge touch. The native path cannot:+ // VZ forwards `swipeAim` to its USB touchscreen, whose HID report has+ // no field for it, so UIKit's edge-pan recognizers (SpringBoard's home+ // gesture) never begin. The choice is made on the down and kept until+ // the lift.+ if phase == 0 {+ edgeTouchViaGuest = localPoints.count == 1 && swipeAim != 0+ && control?.supportsEdgeTouch == true+ }+ defer {+ if phase == 3 { edgeTouchViaGuest = false }+ }+// iOS 18 bases: the VZ USB touchscreen dext emits no digitizer events on// the 26.x kernel, so route touches through vphoned's guest-side HID// injection. 26.x bases fall through to the native VZ multitouch path.- if let control, control.useGuestTouchInjection {+ if let control, control.useGuestTouchInjection || edgeTouchViaGuest {if normalized.count >= 2, control.supportsMultiTouch {control.sendTouch2(phase: phase,@@ -1080,8 +1110,22 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {x2: Double(normalized[1].x), y2: Double(normalized[1].y),)} else {+ var point = normalized[0]+ // UIKit only accepts an edge touch that begins within a few+ // points of the edge; the 32pt mouse zone rarely gets there.+ // Start it on the edge, like a finger sliding in.+ if phase == 0, control.supportsEdgeTouch {+ let inset: CGFloat = 0.001+ switch swipeAim {+ case VPhoneDisplayGeometry.Edge.top.rawValue: point.y = inset+ case VPhoneDisplayGeometry.Edge.bottom.rawValue: point.y = 1 - inset+ case VPhoneDisplayGeometry.Edge.right.rawValue: point.x = 1 - inset+ case VPhoneDisplayGeometry.Edge.left.rawValue: point.x = inset+ default: break+ }+ }control.sendTouch(- phase: phase, x: Double(normalized[0].x), y: Double(normalized[0].y),+ phase: phase, x: Double(point.x), y: Double(point.y), edge: swipeAim,)}return true