Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: keep the always-on Lock Screen visible in the VM window

Marvae84c12c95d29a
diff --git a/VPhoneDaemon/Daemon/GuestAPI+Device.swift b/VPhoneDaemon/Daemon/GuestAPI+Device.swift
index 883f3598..d6a4c0a5 100644
--- a/VPhoneDaemon/Daemon/GuestAPI+Device.swift
+++ b/VPhoneDaemon/Daemon/GuestAPI+Device.swift
@@ -180,7 +180,8 @@ extension GuestAPI {
reply["fold_angle"] = angle
// The window covers the panel while it is off: coming on, it
// shows its last frame from before until the guest draws again.
- reply["screen_off"] = GuestScreenUnlock.screenIsOff
+ // The always-on Lock Screen keeps the panel on, so it stays shown.
+ reply["screen_off"] = GuestScreenUnlock.panelIsDark
}
return reply
}
diff --git a/VPhoneDaemon/Daemon/GuestScreenUnlock.swift b/VPhoneDaemon/Daemon/GuestScreenUnlock.swift
index db8d9231..d729ad5e 100644
--- a/VPhoneDaemon/Daemon/GuestScreenUnlock.swift
+++ b/VPhoneDaemon/Daemon/GuestScreenUnlock.swift
@@ -176,6 +176,21 @@ enum GuestScreenUnlock {
/// Whether SpringBoard has the screen off.
static var screenIsOff: Bool { state().screenOff }
+ /// Whether a foldable guest's panel is dark. SpringBoard reports the screen
+ /// off both when it turns the panel off and when it dims it to the
+ /// always-on Lock Screen, which keeps the panel on and drawing.
+ /// FoldDisplay's state tells them apart: 1 while the panel has stayed on
+ /// since SpringBoard last lit the screen, 2 once it has gone off, 0 when
+ /// FoldDisplay has not said (then the screen state alone decides).
+ static var panelIsDark: Bool {
+ guard screenIsOff else { return false }
+ var token: Int32 = 0
+ guard notify_register_check("com.vphone.folddisplay.panel-dark", &token) == 0 else { return true }
+ defer { notify_cancel(token) }
+ var value: UInt64 = 0
+ return notify_get_state(token, &value) != 0 || value != 1
+ }
+
private struct State {
let locked: Bool
let screenOff: Bool
diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
index 1518653d..ab21038f 100644
--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
@@ -42,10 +42,53 @@ static unsigned int vpStateControlDisplayId(id control) {
// When the panel last came on (system uptime), for vpFollowContentModeTransition.
static NSTimeInterval vpPanelOnAt;
+// SpringBoard reports the screen blanked (hasBlankedScreen) both when it turns
+// the panel off and when it dims it to the always-on Lock Screen, which keeps
+// the panel on and drawing. The VM window covers the panel while the screen
+// is off (display.orientation's screen_off) so that, coming on, it does not
+// show the frame from before it went off; vphoned reads this state so that it
+// does not cover the always-on Lock Screen too. The panel counts as dark from
+// going off until SpringBoard lights the screen again, so a wake from off is
+// covered as before.
+static int vpBlankedToken = -1, vpPanelDarkToken = -1;
+static BOOL vpPanelWentDark;
+
+static void vpPublishPanelDark(void) {
+ if (vpPanelDarkToken >= 0) notify_set_state(vpPanelDarkToken, vpPanelWentDark ? 2 : 1);
+}
+
+static BOOL vpScreenBlanked(void) {
+ uint64_t blanked = 0;
+ if (vpBlankedToken >= 0) notify_get_state(vpBlankedToken, &blanked);
+ return blanked != 0;
+}
+
+static void vpFollowPanelPower(long long state) {
+ if (!state) vpPanelWentDark = YES;
+ else if (!vpScreenBlanked()) vpPanelWentDark = NO;
+ vpPublishPanelDark();
+}
+
+static void vpInstallPanelDark(void) {
+ if (notify_register_check(vpPanelDarkNotification, &vpPanelDarkToken) != NOTIFY_STATUS_OK) {
+ vpPanelDarkToken = -1;
+ return;
+ }
+ notify_register_dispatch("com.apple.springboard.hasBlankedScreen", &vpBlankedToken, dispatch_get_main_queue(), ^(int token) {
+ (void)token;
+ if (vpScreenBlanked() || !vpPrimaryAppliedState) return;
+ vpPanelWentDark = NO;
+ vpPublishPanelDark();
+ });
+ vpPanelWentDark = !vpPrimaryAppliedState;
+ vpPublishPanelDark();
+}
+
static void vpApplyPanelState(id control, long long state, long long *applied, id completion) {
if (!control || (state == *applied && !completion)) return;
if (control == vpPrimaryStateControl && state && !*applied) vpPanelOnAt = NSProcessInfo.processInfo.systemUptime;
*applied = state;
+ if (control == vpPrimaryStateControl) vpFollowPanelPower(state);
((void (*)(id, SEL, long long, id))vpOriginalTransitionDisplayState)(control,
sel_registerName("transitionToDisplayState:withCompletion:"), state, completion);
}
@@ -181,6 +224,7 @@ void vpInstallSharedPanel(void) {
return;
}
vpOriginalTransitionDisplayState = method_setImplementation(transition, (IMP)vpSharedScanoutTransition);
+ vpInstallPanelDark();
vpInstallContentModeOwnership();
vpFollowInnerPages();
vpSetFoldFadeToBlack(NO);
diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h
index 5ebb71b8..43199c03 100644
--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h
+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h
@@ -25,6 +25,9 @@
extern NSString *const vpInnerDisplayName;
/// Its state is the context id backboardd shows the inner display's pages in, 0 for none.
extern const char *const vpInnerPagesNotification;
+/// Its state is 1 while the panel has stayed on since SpringBoard last lit the
+/// screen (the always-on Lock Screen), 2 once it has gone off, 0 unknown.
+extern const char *const vpPanelDarkNotification;
void vpFoldDisplayLog(NSString *line);
id vpReadObject(id object, const char *name);
id vpReadObjectIvar(id object, const char *name);
diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplaySupport.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplaySupport.m
index 9ef4a3c1..79198777 100644
--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplaySupport.m
+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplaySupport.m
@@ -5,6 +5,7 @@
NSString *const vpInnerDisplayName = @"VPhone Duo Inner";
const char *const vpInnerPagesNotification = "com.vphone.folddisplay.inner-pages";
+const char *const vpPanelDarkNotification = "com.vphone.folddisplay.panel-dark";
void vpFoldDisplayLog(NSString *line) {
int fd = open("/var/mobile/Library/Caches/vphone-folddisplay.log", O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0666);