Run a virtual iPhone on an Apple Silicon Mac.
Swift
Take touch2's normalized flag seriously, share the unclamped mapping
Lakr6d4ca34e76a9
diff --git a/VPhoneDaemon/Daemon/GuestAPI.swift b/VPhoneDaemon/Daemon/GuestAPI.swiftindex 398b8fb5..7a363edc 100644--- a/VPhoneDaemon/Daemon/GuestAPI.swift+++ b/VPhoneDaemon/Daemon/GuestAPI.swift@@ -261,6 +261,12 @@ enum GuestAPI {guard let phase = (params["phase"] as? String).flatMap(TouchPhase.init(rawValue:)) else {throw GuestAPIError.invalidRequest("phase must be down, move or up")}+ // `vp_hid_touch2` takes normalized coordinates only. `input.touch`+ // also accepts screen points, so refuse the flag here rather than+ // read points as fractions and put both fingers in the corner.+ guard params["normalized"] as? Bool ?? true else {+ throw GuestAPIError.invalidRequest("touch2 coordinates are normalized 0..1 only")+ }vp_hid_touch2(touchPhaseCode(phase),number(params, "x1"), number(params, "y1"),diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swiftindex badfaa7f..1d016f3a 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachineView.swift@@ -800,13 +800,7 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {/// reaches the edge the guest stops receiving new positions, so a long/// swipe could not keep scrolling.private func normalizeCoordinate(_ localPoint: NSPoint, allowOutside: Bool = false) -> CGPoint {- let geometry = displayGeometry- guard allowOutside else { return geometry.normalizedPoint(localPoint) }- let rect = geometry.displayRect- guard rect.width > 0, rect.height > 0 else { return .zero }- let x = (localPoint.x - rect.minX) / rect.width- let y = (localPoint.y - rect.minY) / rect.height- return CGPoint(x: x, y: geometry.isFlipped ? y : 1 - y)+ displayGeometry.normalizedPoint(localPoint, clamped: !allowOutside)}private func hitTestEdge(at point: CGPoint) -> Int {diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayGeometry.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayGeometry.swiftindex 0a260377..f1f3dd04 100644--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayGeometry.swift+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayGeometry.swift@@ -49,13 +49,20 @@ public struct VPhoneDisplayGeometry: Equatable, Sendable {/// A view point as a 0...1 display position with y down. Points on the/// letterbox bars clamp to the nearest display edge.- public func normalizedPoint(_ point: CGPoint) -> CGPoint {+ ///+ /// `clamped: false` keeps a point that falls outside the display outside it,+ /// so the result may leave 0...1. A trackpad scroll replayed as a touch+ /// needs that: the guest stops following a finger once it is pinned to the+ /// edge, so a long swipe has to carry coordinates past it.+ public func normalizedPoint(_ point: CGPoint, clamped: Bool = true) -> CGPoint {guard displayRect.width > 0, displayRect.height > 0 else { return .zero }let x = (point.x - displayRect.minX) / displayRect.widthlet y = (point.y - displayRect.minY) / displayRect.height+ let down = isFlipped ? y : 1 - y+ guard clamped else { return CGPoint(x: x, y: down) }return CGPoint(x: min(1, max(0, x)),- y: min(1, max(0, isFlipped ? y : 1 - y)),+ y: min(1, max(0, down)),)}diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayGeometryTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayGeometryTests.swiftindex 25b18c0f..19eca4f2 100644--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayGeometryTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayGeometryTests.swift@@ -65,6 +65,15 @@ struct DisplayGeometryTests {#expect(Self.close(geometry.normalizedPoint(CGPoint(x: 1500, y: 491)), CGPoint(x: 1, y: 0.5)))}+ @Test func `unclamped normalizing keeps a point off the display outside 0 and 1`() {+ let geometry = Self.geometry(Self.fullScreen)+ let rect = geometry.displayRect+ let left = geometry.normalizedPoint(CGPoint(x: rect.minX - rect.width, y: rect.midY), clamped: false)+ #expect(abs(left.x + 1) < 1e-6 && abs(left.y - 0.5) < 1e-6)+ let below = geometry.normalizedPoint(CGPoint(x: rect.midX, y: rect.minY - rect.height), clamped: false)+ #expect(abs(below.x - 0.5) < 1e-6 && abs(below.y - 2) < 1e-6)+ }+@Test func `a flipped view keeps y down`() {let geometry = Self.geometry(Self.fullScreen, flipped: true)let rect = geometry.displayRect