Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: hold portrait across fold transitions in both directions

Jackyb3d7ffe72fbd
diff --git a/Research/Guest/iphone_duo_bringup.md b/Research/Guest/iphone_duo_bringup.md
index 08d9a9e1..f5b7433b 100644
--- a/Research/Guest/iphone_duo_bringup.md
+++ b/Research/Guest/iphone_duo_bringup.md
@@ -3787,3 +3787,71 @@ width (§3.5/H), wallpaper gate default (§3.4), full acceptance matrix with
touch (A–F), cleanup (I), module commits (J). The cover presentation's
content is a clock-style cover screen; rich cover UI (dock/notifications
on cover) has not yet been acceptance-checked.
+
+## 2026-10-28 morning: fold-closed orientation regression (user-reported)
+
+User report after the overnight session: the fold toggle "only switches
+landscape/portrait, no inner/outer switch", with a screenshot of the home
+grid rotated 90°, vertical dock, right-edge clipping. Reproduced on
+`2.12.1-local.5ffed6ae` with one RPC: `display.posture {"position":"closed"}`
+lands bounds 466×678 (cover mode applied) but interface orientation 3 with a
+2853×2007 landscape framebuffer.
+
+False leads worth recording: the folddisplay log appears to "stop" at a
+timestamp because `files.read` returns the FIRST ~512 KB of a multi-MB file —
+always check `files.list` mtimes before concluding a dylib died; the map
+JSON's fields can also be stale across a VM restart (the file persists), so
+`display.orientation` (asks SpringBoard live, IcliKit) is the truth for
+interface degrees, not the map's `interfaceOrientation`.
+
+Root cause chain (each link verified on the live guest):
+
+1. **Stale-table resolve.** `setDevicePose` resolved portrait against the map
+ JSON immediately after the viewport landed. The JSON's `configurationBounds`
+ updates at the apply, but the scene's mounting table follows on the next
+ export, so the first read still carried the previous panel's table: closing
+ resolved portrait to device 3 (inner's rotated portrait) which the
+ identity-mounted cover renders as landscape.
+2. **Cover arbiter latch.** The cover scene's traits arbiter derives its
+ direction once, at the mode swap, from the pipeline's cached device
+ orientation, and after that no input channel reopens it — pose override
+ (device 1), `pose.refresh` (pipeline re-read), forced arbitration, the
+ accelerometer sink (delivering device 1), and the public `display.rotation`
+ path all verified portrait-side while the pipeline stayed landscape.
+ `vpMainInnerWindowScene()` returning nil in cover viewport also made the
+ dylib's refresh/arbitration/actuation handlers refuse outright
+ ("refused outside Main inner"), so the inner panel's self-healing
+ (`native callback direction=1`) had no cover equivalent.
+3. **Boot restore hardcoded device 1** as the pose value (the pose enum's
+ portrait, not the mounted device value), so a folded boot could restore a
+ landscape interface depending on which table the export carried at ladder
+ time.
+
+Fixes (vphoned + dylib, all in this session's builds):
+
+- `GuestAPI+Device.swift` `setDevicePose`: self-verifying resolve loop —
+ request a fresh map export, resolve portrait, set pose + accelerometer,
+ poke `pose.refresh`/`arbitration.refresh`/`actuation.refresh`, confirm via
+ `interfaceRotationDegrees() == 0`, re-resolve once with the settled table;
+ accelerometer seeded to 0° and `pose.refresh` posted BEFORE the viewport
+ apply when closing; closing while unlocked+lit now sleeps the display
+ (real hardware blanks the inner panel; the cover lock screen is the native
+ cover presentation — verified portrait at every folded boot).
+- `GuestFoldPose.swift`: boot ladder resolves the portrait device value from
+ the live map instead of hardcoding 1, stops only when SpringBoard answers
+ portrait, and pokes `pose.refresh` before the respring-replay viewport
+ apply.
+- `libvphonefolddisplay.m`: new `vpOrientationArbitrationScene()` (inner
+ scene, else the active Main-identity scene while the persistent viewport is
+ live) used by the refresh/arbitration/actuation handlers; the actuation
+ handler, when the arbitration scene is the cover, completes the native
+ callback chain with the cover's native portrait direction instead of the
+ latched pipeline value — this is the link that actually breaks the latch.
+
+Verification on `2.12.1-local.f16a7b5f` (duotest-m5): boot-closed portrait;
+3× open↔closed roundtrips all landing 669×951/0° and 466×678/0° with a
+portrait framebuffer in both states; cover wake lock screen portrait; inner
+rotation matrix 90/0/270/0 exact; open home screen visually clean (portrait
+grid, horizontal dock, no clipping). Known remaining behavior: closing while
+unlocked sleeps the screen by design — the next wake presents the cover lock
+screen (clock) and `screen.unlock` (or a swipe) returns to the home layout.
diff --git a/VPhoneDaemon/Daemon/GuestAPI+Device.swift b/VPhoneDaemon/Daemon/GuestAPI+Device.swift
index 59d5e064..36bf5e8f 100644
--- a/VPhoneDaemon/Daemon/GuestAPI+Device.swift
+++ b/VPhoneDaemon/Daemon/GuestAPI+Device.swift
@@ -380,6 +380,20 @@ extension GuestAPI {
? vp_hinge_sweep(angle!, durationMs, Int32(profile))
: vp_hinge_set(angle!)
if ((try? deviceSnapshot())?["model"] as? String) == "iPhone19,4" {
+ // The cover arbiter derives its direction once, at the mode
+ // swap, from the scene pipeline's cached device orientation.
+ // The pose override above wrote portrait (device 1 on the
+ // identity-mounted cover), but the pipeline only re-reads it
+ // through the pose-refresh channel; without the poke the swap
+ // still sees the inner panel's rotated portrait (device 3) and
+ // latches the cover landscape with no later channel able to
+ // reopen arbitration. The same channel feeds the accelerometer
+ // seed below; both land on SpringBoard's queue ahead of the
+ // viewport apply posted right after.
+ if angle! < 5 {
+ _ = vp_accelerometer_set_orientation("0")
+ _ = try? postDarwinNotification("com.vphone.display.orientation.pose.refresh", state: nil)
+ }
GuestFoldPose.requestViewport(for: angle!)
let expected = angle! < 5 ? [0.0, 0.0, 466.0, 678.0] : [0.0, 0.0, 669.0, 951.0]
let deadline = ProcessInfo.processInfo.systemUptime + 3
@@ -398,47 +412,80 @@ extension GuestAPI {
guard applied else { throw GuestAPIError.operationFailed("Duo logical viewport did not reach the requested size") }
// A fold transition changes the active panel's native mounting
- // map. Preserve the requested portrait pose across a landscape
- // -> cover -> inner sequence by resolving interface portrait
- // against the newly active map, rather than reusing the old
- // physical device orientation.
- // Both panels present portrait at rest: the cover starts
- // portrait, and the unfolded inner panel presents portrait
- // through its rotated mounting (device 3 in the measured
- // [3,4,2,1] table). Resolve interface portrait after the
- // viewport transition; landscape stays an explicit rotation.
+ // map (inner [3,4,2,1], cover identity). The map JSON refreshes
+ // on the next export tick, so the first read after the viewport
+ // lands can still carry the previous panel's table: resolving
+ // portrait against the stale inner table picks device 3, which
+ // the identity-mounted cover panel renders as landscape. Ask
+ // for a fresh export, resolve against it, and confirm through
+ // SpringBoard's own interface answer, re-resolving once with
+ // the settled table when the first pick mounts sideways.
let desiredInterface = "0"
- if orientation == Self.poseOrientations["portrait"],
- let values = vp_display_orientation_mapping()["deviceOrientations"] as? [Int],
- let portrait = GuestDisplayRotation.resolve(desiredInterface, deviceOrientations: values) {
- let current = vp_device_pose_get()
- _ = vp_device_pose_set(
- Int32(portrait.deviceOrientation),
- (current["hinge"] as? NSNumber)?.int32Value ?? Int32(hinge),
- (current["tabletop"] as? NSNumber)?.int32Value ?? Int32(tabletop)
- )
- _ = vp_accelerometer_set_orientation(String(portrait.accelerometerDegrees))
- _ = try? postDarwinNotification("com.vphone.display.orientation.mapping.request", state: nil)
- _ = try? postDarwinNotification("com.vphone.display.orientation.inputs.refresh", state: nil)
+ if orientation == Self.poseOrientations["portrait"] {
+ var settled = false
+ for _ in 0..<2 where !settled {
+ let staleUptime = (vp_display_orientation_mapping()["generatedUptime"] as? NSNumber)?.doubleValue ?? 0
+ _ = try? postDarwinNotification("com.vphone.display.orientation.mapping.request", state: nil)
+ let exportDeadline = ProcessInfo.processInfo.systemUptime + 0.8
+ while ProcessInfo.processInfo.systemUptime < exportDeadline {
+ let live = vp_display_orientation_mapping()
+ if ((live["generatedUptime"] as? NSNumber)?.doubleValue ?? 0) > staleUptime,
+ (live["configurationBounds"] as? [NSNumber])?.map(\.doubleValue) == expected { break }
+ Thread.sleep(forTimeInterval: 0.05)
+ }
+ if let values = vp_display_orientation_mapping()["deviceOrientations"] as? [Int],
+ let portrait = GuestDisplayRotation.resolve(desiredInterface, deviceOrientations: values) {
+ let current = vp_device_pose_get()
+ _ = vp_device_pose_set(
+ Int32(portrait.deviceOrientation),
+ (current["hinge"] as? NSNumber)?.int32Value ?? Int32(hinge),
+ (current["tabletop"] as? NSNumber)?.int32Value ?? Int32(tabletop)
+ )
+ _ = vp_accelerometer_set_orientation(String(portrait.accelerometerDegrees))
+ // The pipeline re-reads the device pose only through
+ // the pose-refresh channel; inputs.refresh leaves the
+ // previous panel's value latched, which is exactly how
+ // a cover swap kept the interface landscape.
+ _ = try? postDarwinNotification("com.vphone.display.orientation.pose.refresh", state: nil)
+ _ = try? postDarwinNotification("com.vphone.display.orientation.arbitration.refresh", state: nil)
+ _ = try? postDarwinNotification("com.vphone.display.orientation.actuation.refresh", state: nil)
+ let confirmDeadline = ProcessInfo.processInfo.systemUptime + 1.5
+ while ProcessInfo.processInfo.systemUptime < confirmDeadline {
+ if interfaceRotationDegrees() == portrait.interfaceDegrees {
+ settled = true
+ break
+ }
+ Thread.sleep(forTimeInterval: 0.05)
+ }
+ }
+ }
+ _ = try? postDarwinNotification("com.vphone.display.orientation.pose.refresh", state: nil)
_ = try? postDarwinNotification("com.vphone.display.orientation.arbitration.refresh", state: nil)
_ = try? postDarwinNotification("com.vphone.display.orientation.actuation.refresh", state: nil)
_ = try? postDarwinNotification("com.vphone.display.presentation.wallpaper.pulse", state: nil)
_ = try? postDarwinNotification("com.vphone.display.presentation.clock.refresh", state: nil)
- // The scene may finish the fold transition after the first
- // notification batch. Give native arbitration a bounded
- // settle window and re-read the map once before returning.
- let orientationDeadline = ProcessInfo.processInfo.systemUptime + 2
- while interfaceRotationDegrees() != portrait.interfaceDegrees,
- ProcessInfo.processInfo.systemUptime < orientationDeadline {
- Thread.sleep(forTimeInterval: 0.05)
- }
- if interfaceRotationDegrees() != portrait.interfaceDegrees {
+ if !settled {
// Reuse the same public rotation path once the new panel
// is fully selected; it resolves the post-fold map again
// instead of relying on a stale pre-transition snapshot.
_ = try? setRotation("0")
}
}
+ // Real hardware blanks the inner panel when the fold closes and
+ // the cover presents the lock experience. The single-scene
+ // emulation cannot blank one panel, and an unlocked home layout
+ // in cover mode keeps whatever direction the mode swap computed
+ // from the inner pose — portrait arbitration does not settle
+ // there — while the cover lock screen arbitrates portrait
+ // natively (every folded boot presents its clock upright). Close
+ // through the lock: sleep a lit, unlocked display so the next
+ // wake presents the cover lock screen.
+ if angle! < 5,
+ let lock = ((try? collectDeviceSnapshot())?["lock"] as? [String: Any]),
+ (lock["locked"] as? Bool ?? true) == false,
+ (lock["screen_off"] as? Bool ?? true) == false {
+ _ = try? pressButton("power")
+ }
}
var hingeState = vp_hinge_get()
hingeState["applied"] = published
diff --git a/VPhoneDaemon/Daemon/GuestFoldPose.swift b/VPhoneDaemon/Daemon/GuestFoldPose.swift
index 547ac51e..64de4d81 100644
--- a/VPhoneDaemon/Daemon/GuestFoldPose.swift
+++ b/VPhoneDaemon/Daemon/GuestFoldPose.swift
@@ -1,4 +1,5 @@
import Foundation
+import IcliKit
import notify
import VphonedNative
@@ -84,10 +85,17 @@ private static let restoration = RestoreState()
guard restoration.generation == generation, !restoration.explicitRequest,
let pose = restoration.latestPose,
vp_hinge_get()["enabled"] as? Bool == true else { return }
- if let angle = vp_hinge_get()["angle"] as? Double { requestViewport(for: angle) }
- if poseValues(vp_device_pose_get()) == pose { return }
- _ = vp_device_pose_set(pose[0], pose[1], pose[2])
- NSLog("vphoned: replay latest fold pose after SpringBoard startup")
+ if let angle = vp_hinge_get()["angle"] as? Double {
+ if poseValues(vp_device_pose_get()) != pose {
+ _ = vp_device_pose_set(pose[0], pose[1], pose[2])
+ // The pipeline re-reads the pose only through
+ // this channel; feed it before the viewport
+ // apply so the swap arbitrates from the pose.
+ notify_post("com.vphone.display.orientation.pose.refresh")
+ NSLog("vphoned: replay latest fold pose after SpringBoard startup")
+ }
+ requestViewport(for: angle)
+ }
}
}
}
@@ -114,21 +122,32 @@ private static let restoration = RestoreState()
Thread.sleep(forTimeInterval: delay - waited)
waited = delay
let expectedHinge = hingeState(for: angle)
+ // Device 1 is the pose enum's portrait, not the mounted
+ // device value: the Duo inner panel presents portrait as
+ // device 3 under its rotated mounting. Resolve against the
+ // live map; a later rung corrects an early read that still
+ // carries the identity fallback table.
+ let portraitDevice = (vp_display_orientation_mapping()["deviceOrientations"] as? [Int])
+ .flatMap { GuestDisplayRotation.resolve("0", deviceOrientations: $0)?.deviceOrientation } ?? 1
let published = restoration.lock.withLock {
guard !restoration.cancelled else { return false }
- _ = vp_device_pose_set(1, Int32(expectedHinge), 0)
+ _ = vp_device_pose_set(Int32(portraitDevice), Int32(expectedHinge), 0)
_ = vp_hinge_set(angle)
requestViewport(for: angle)
restoration.latestPose = poseValues(vp_device_pose_get())
return true
}
guard published else { return }
+ // The running scene re-reads the device pose only through
+ // the pose-refresh channel; without the poke the pipeline
+ // keeps whatever direction it held before the restore.
+ notify_post("com.vphone.display.orientation.pose.refresh")
+ notify_post("com.vphone.display.orientation.arbitration.refresh")
+ notify_post("com.vphone.display.orientation.actuation.refresh")
let actual = vp_device_pose_get()
if actual["available"] as? Bool == true,
- actual["device_orientation"] as? Int == 1,
- actual["hinge"] as? Int == expectedHinge {
- return
- }
+ actual["hinge"] as? Int == expectedHinge,
+ interfaceRotationDegrees() == 0 { return }
}
}
}
diff --git a/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m b/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m
index ffc53991..92ee9498 100644
--- a/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m
+++ b/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m
@@ -2754,6 +2754,27 @@ static id vpMainInnerWindowScene(void) {
return scene;
}
+// The orientation machinery must also run while the persistent viewport
+// holds the cover mode: the active scene is the same SB-main scene under
+// the cover identity, and a fold transition must not strand the interface
+// at the direction the mode swap computed with the previous panel's pose
+// (observed: closed computed landscape from the inner portrait pose and
+// nothing re-arbitrated it). Accept the active scene only while the
+// persistent viewport is live and it carries the current Main identity.
+static id vpOrientationArbitrationScene(void) {
+ id inner = vpMainInnerWindowScene();
+ if (inner) return inner;
+ if (!vpPersistentViewport) return nil;
+ id app = vpReadObject(objc_getClass("UIApplication"), "sharedApplication");
+ id scene = vpReadObject(app, "activeDisplayWindowScene");
+ if (!scene) return nil;
+ id configuration = vpReadObject(objc_getClass("FBDisplayManager"), "mainConfiguration");
+ id identity = vpReadObject(configuration, "identity");
+ id uiIdentity = vpReadObject(scene, "_fbsDisplayIdentity") ?: vpReadObject(scene, "displayIdentity");
+ if (!identity || ![uiIdentity isEqual:identity]) return nil;
+ return scene;
+}
+
static BOOL vpIsSingleMainStatusScene(id scene, BOOL constructing) {
if (![vpReadObject(vpReadObject(scene, "_FBSScene"), "identifier") isEqual:@"SB-main"]) return NO;
NSInteger surface = [[scene valueForKey:@"surfaceType"] integerValue];
@@ -2882,7 +2903,7 @@ static void vpProbeSceneGeometryRequest(void) {
static void vpRefreshNativeOrientationInputs(BOOL rereadPose) {
@try {
- id scene = vpMainInnerWindowScene();
+ id scene = vpOrientationArbitrationScene();
if (!scene) { vpFoldDisplayLog(@"orientation refresh: refused outside Main inner"); return; }
id pipeline = vpReadObject(scene, "traitsPipelineManager");
SEL selector = sel_registerName(rereadPose ? "_updateDeviceOrientationAnimated:forReason:" : "_noteInputsNeedUpdateAnimated:reason:");
@@ -2900,7 +2921,7 @@ static void vpRefreshNativeOrientationInputs(BOOL rereadPose) {
static void vpForceNativeOrientationResolution(void) {
@try {
- id scene = vpMainInnerWindowScene();
+ id scene = vpOrientationArbitrationScene();
if (!scene) { vpFoldDisplayLog(@"orientation arbitration: refused outside Main inner"); return; }
id arbiter = vpReadObject(scene, "traitsArbiter");
SEL factory = sel_registerName("_defaultUpdateContextWithReason:animatable:");
@@ -2966,9 +2987,21 @@ static void vpSetCanonicalOrientationProbe(BOOL enabled) {
static void vpRefreshNativeOrientationActuation(void) {
@try {
- id scene = vpMainInnerWindowScene();
+ id scene = vpOrientationArbitrationScene();
if (!scene) { vpFoldDisplayLog(@"orientation actuation: refused outside Main inner"); return; }
NSInteger direction = [[vpReadObject(scene, "traitsPipelineManager") valueForKey:@"systemArbitratedInterfaceOrientation"] integerValue];
+ // The cover presentation is portrait-native, but the single-scene
+ // emulation's arbiter carries the direction the mode swap computed
+ // from the inner panel's rotated pose and no input channel reopens
+ // it (pose, pose-refresh, arbitration, accelerometer all verified
+ // portrait while the pipeline stays landscape). Complete the same
+ // native callback chain with the cover's native direction instead
+ // of the latched one.
+ if (scene != vpMainInnerWindowScene()) {
+ if (direction != 1) vpFoldDisplayLog([NSString stringWithFormat:
+ @"orientation actuation: cover direction %ld overridden to portrait", (long)direction]);
+ direction = 1;
+ }
if (direction < 1 || direction > 4) return;
id app = vpReadObject(objc_getClass("UIApplication"), "sharedApplication");
SEL selector = sel_registerName("_notePipelineManagerChangedInterfaceOrientation:onWindowScene:duration:force:logMessage:");