Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

VPhoneKit: display-orientation model and device facts for foldables

Jacky9e1021fc18a9
diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
index f987f4d8..614a3c3b 100644
--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneDisplayOrientation.swift
@@ -94,6 +94,15 @@ public enum VPhoneDisplayOrientation: Int, CaseIterable, Sendable {
return CGPoint(x: turned.x + panel.width / 2, y: turned.y + panel.height / 2)
}
+ /// Full guest screenshots are upright, with a top-left origin. HID
+ /// touches use the fixed panel, also top-left. Keep this inverse shared
+ /// by windowed and headless automation; swapping dimensions is not enough.
+ public func panelPoint(fromScreenshot point: CGPoint, panel: CGSize) -> CGPoint {
+ let screen = displayedSize(panel: panel)
+ let fixed = panelPoint(fromScreen: CGPoint(x: point.x, y: screen.height - point.y), panel: panel)
+ return CGPoint(x: fixed.x, y: panel.height - fixed.y)
+ }
+
// MARK: - Turning
/// The largest size with the panel's aspect ratio whose box, turned by
@@ -148,3 +157,36 @@ public enum VPhoneDisplayOrientation: Int, CaseIterable, Sendable {
return rect
}
}
+
+/// An active top-left viewport inside a fixed portrait framebuffer. Input
+/// pixels refer to the upright cropped image; HID remains framebuffer-based.
+public struct VPhoneDisplayViewport: Equatable, Sendable {
+ public let panel: CGSize
+ public let active: CGSize
+ public let orientation: VPhoneDisplayOrientation
+
+ public init?(panel: CGSize, active: CGSize, orientation: VPhoneDisplayOrientation) {
+ guard panel.width.isFinite, panel.height.isFinite, active.width.isFinite, active.height.isFinite,
+ panel.width > 0, panel.height > 0, active.width > 0, active.height > 0,
+ active.width <= panel.width, active.height <= panel.height else { return nil }
+ self.panel = panel
+ self.active = active
+ self.orientation = orientation
+ }
+
+ public var screenshotSize: CGSize { orientation.displayedSize(panel: active) }
+
+ public func panelPoint(fromScreenshot point: CGPoint) -> CGPoint {
+ orientation.panelPoint(fromScreenshot: point, panel: active)
+ }
+
+ /// Crop in the upright full-frame image's top-left pixel coordinates.
+ public var screenshotCrop: CGRect {
+ switch orientation {
+ case .portrait: CGRect(origin: .zero, size: active)
+ case .landscapeLeft: CGRect(x: panel.height - active.height, y: 0, width: active.height, height: active.width)
+ case .upsideDown: CGRect(x: panel.width - active.width, y: panel.height - active.height, width: active.width, height: active.height)
+ case .landscapeRight: CGRect(x: 0, y: panel.width - active.width, width: active.height, height: active.width)
+ }
+ }
+}
diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift
index 1ded1477..7048b0d8 100644
--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift
+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift
@@ -140,6 +140,16 @@ public struct VPhoneGuestDevice: Sendable, Equatable {
/// The inner display and hinge are not represented by the VM's single panel.
public static let iPhone19_4 = iPhone("iPhone19,4", "v68ap", "iPhone Duo", width: 1398, height: 2034)
+ /// The Duo's inner display, the panel the fold opens onto. A machine is
+ /// created on the outer panel above; the open and book postures
+ /// relaunch on this one — the host refuses runtime display resizing
+ /// (measured 2026-10-25, see Research/Guest/iphone_duo_bringup.md), so
+ /// the panel is chosen at boot. Apple's iPhone Duo device profile declares
+ /// 2007×2853 at 3x (669×951 points); 2034 was a custom bring-up width.
+ public static let iPhone19_4InnerScreen = VPhoneVirtualMachineManifest.ScreenConfig(
+ width: 2007, height: 2853, pixelsPerInch: 460, scale: 3.0
+ )
+
/// iPhone XS Max: 1242x2688 at 458 ppi, 414x896 points. Its IPSW carries the
/// iOS 18 releases after 18.6, which the iPhone 16 never got; the research
/// board's own iOS 18 kernel boots it (the 18.x kernelcache recipes, `minimal`
diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
index 4a6a9dc1..e1dc0e4e 100644
--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/DisplayOrientationTests.swift
@@ -6,6 +6,25 @@ struct DisplayOrientationTests {
static let panel = CGSize(width: 393, height: 852)
static let screen = CGRect(x: 0, y: 0, width: 1512, height: 944)
+ @Test func `Duo screenshot pixels map into each fixed panel in every orientation`() {
+ for panel in [CGSize(width: 1398, height: 2034), CGSize(width: 2007, height: 2853)] {
+ for (orientation, expected) in [
+ (VPhoneDisplayOrientation.portrait, CGPoint(x: 0.2, y: 0.3)),
+ (.landscapeLeft, CGPoint(x: 0.3, y: 0.8)),
+ (.upsideDown, CGPoint(x: 0.8, y: 0.7)),
+ (.landscapeRight, CGPoint(x: 0.7, y: 0.2)),
+ ] {
+ let screen = orientation.displayedSize(panel: panel)
+ let point = orientation.panelPoint(fromScreenshot: CGPoint(x: screen.width * 0.2, y: screen.height * 0.3), panel: panel)
+ #expect(abs(point.x / panel.width - expected.x) < 0.000001)
+ #expect(abs(point.y / panel.height - expected.y) < 0.000001)
+ let restored = orientation.screenPoint(fromPanel: CGPoint(x: point.x, y: panel.height - point.y), panel: panel)
+ #expect(abs(restored.x - screen.width * 0.2) < 0.000001)
+ #expect(abs(screen.height - restored.y - screen.height * 0.3) < 0.000001)
+ }
+ }
+ }
+
// MARK: - Degrees
@Test func `vphoned degrees map to orientations`() {
@@ -168,3 +187,30 @@ struct DisplayOrientationTests {
#expect(rect == portrait)
}
}
+
+@Suite struct DisplayViewportTests {
+ @Test func `cover crop and input agree through every rotation`() {
+ let panel = CGSize(width: 2007, height: 2853)
+ let active = CGSize(width: 1398, height: 2034)
+ for orientation in VPhoneDisplayOrientation.allCases {
+ let viewport = VPhoneDisplayViewport(panel: panel, active: active, orientation: orientation)!
+ let cropped = CGPoint(x: viewport.screenshotSize.width * 0.2, y: viewport.screenshotSize.height * 0.3)
+ let full = CGPoint(x: viewport.screenshotCrop.minX + cropped.x, y: viewport.screenshotCrop.minY + cropped.y)
+ let fromCrop = viewport.panelPoint(fromScreenshot: cropped)
+ let fromFull = orientation.panelPoint(fromScreenshot: full, panel: panel)
+ #expect(abs(fromCrop.x - fromFull.x) < 0.000001)
+ #expect(abs(fromCrop.y - fromFull.y) < 0.000001)
+ #expect(viewport.screenshotCrop.size == viewport.screenshotSize)
+ #expect(CGRect(origin: .zero, size: orientation.displayedSize(panel: panel)).contains(viewport.screenshotCrop))
+ }
+ }
+
+ @Test func `invalid viewport cannot escape its backing`() {
+ let panel = CGSize(width: 2007, height: 2853)
+ for active in [CGSize.zero, CGSize(width: -1, height: 10), CGSize(width: 2008, height: 2853), CGSize(width: CGFloat.infinity, height: 10)] {
+ #expect(VPhoneDisplayViewport(panel: panel, active: active, orientation: .portrait) == nil)
+ }
+ let full = VPhoneDisplayViewport(panel: panel, active: panel, orientation: .landscapeLeft)!
+ #expect(full.screenshotCrop.origin == .zero)
+ }
+}