Run a virtual iPhone on an Apple Silicon Mac.
Swift
Duo: inner display boots, routes touch and redraws on the shared panel
Jacky790f81e807fb
diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerBoot.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerBoot.mnew file mode 100644index 00000000..79cf8abb--- /dev/null+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerBoot.m@@ -0,0 +1,33 @@+// backboardd: the inner display from the render server's first moment.+//+// QuartzCore's shared_server_init creates the CAWindowServer (_init), finds+// the hardware displays (_detectDisplays), configures them, and only then+// registers the render server's name (CA::Render::Server::register_name),+// which is when SpringBoard and every other client can connect and read the+// display inventory. Adding the inner display after the server runs (a+// main-queue retry on +[CAWindowServer serverIfRunning]) lands after that+// first read: FrontBoard's display monitor, BacklightServices' backlight+// hosts and SpringBoard's embedded-scene gate all missed it until+// SpringBoard restarted. Adding it from _detectDisplays puts it in the+// inventory before any client exists. The server-ready retry in+// FoldDisplayInnerDisplay.m stays as the fallback for a server that was+// already running when this library loaded.++#import "FoldDisplayPrivate.h"++static IMP vpOriginalDetectDisplays;++static void vpDetectDisplaysWithInner(id self, SEL selector) {+ ((void (*)(id, SEL))vpOriginalDetectDisplays)(self, selector);+ if (vpAddInnerRenderDisplayToServer(self))+ vpFoldDisplayLog(@"inner display: added during render server display detection");+}++void vpInstallInnerBootDisplay(void) {+ Method detect = class_getInstanceMethod(objc_getClass("CAWindowServer"), sel_registerName("_detectDisplays"));+ if (!detect || strcmp(method_getTypeEncoding(detect), "v16@0:8") != 0) {+ vpFoldDisplayLog(@"inner display: render server display detection ABI unavailable");+ return;+ }+ vpOriginalDetectDisplays = method_setImplementation(detect, (IMP)vpDetectDisplaysWithInner);+}diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerDisplay.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerDisplay.mindex 134da3a4..f1bfee1d 100644--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerDisplay.m+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerDisplay.m@@ -30,31 +30,25 @@ BOOL vpInnerDisplayRequested(void) {- (instancetype)initWithOptions:(NSDictionary *)options;@end-// Adds the inner panel's render display once the render server runs. It is+// Adds the inner panel's render display to a render server, once. It is// never removed: FrontBoard keeps identities for the life of the monitor.-static void vpAddInnerRenderDisplay(unsigned int attempt) {+// Normally called from the server's display detection (FoldDisplayInnerBoot.m),+// so the display exists before any client reads the inventory.+BOOL vpAddInnerRenderDisplayToServer(id server) {+ if (vpInnerRenderDisplay || !server) return NO;@try {- id server = ((id (*)(id, SEL))objc_msgSend)(objc_getClass("CAWindowServer"), sel_registerName("serverIfRunning"));- if (!server) {- if (attempt < 30)- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(),- ^{ vpAddInnerRenderDisplay(attempt + 1); });- else- vpFoldDisplayLog(@"inner display: render server never ran");- return;- }Class cls = objc_getClass("CAWindowServerVirtualDisplay");Method initializer = class_getInstanceMethod(cls, sel_registerName("initWithOptions:"));if (!initializer || strcmp(method_getTypeEncoding(initializer), "@24@0:8@16") != 0) {vpFoldDisplayLog(@"inner display: virtual display initializer ABI unavailable");- return;+ return NO;}// The panel's native pixels, landscape: 951x669 points at 3x.id display = [(id<VPhoneInnerDisplayInitialization>)[cls alloc] initWithOptions:@{@"kCAVirtualDisplayWidth": @2853, @"kCAVirtualDisplayHeight": @2007,@"kCAVirtualDisplayUpdateRate": @60, @"kCAVirtualDisplayName": vpInnerDisplayName,}];- if (!display) { vpFoldDisplayLog(@"inner display: initializer returned nil"); return; }+ if (!display) { vpFoldDisplayLog(@"inner display: initializer returned nil"); return NO; }((void (*)(id, SEL, id))objc_msgSend)(server, sel_registerName("addDisplay:"), display);SEL setEnabled = sel_registerName("setEnabled:");Method enable = class_getInstanceMethod(object_getClass(display), setEnabled);@@ -65,9 +59,25 @@ static void vpAddInnerRenderDisplay(unsigned int attempt) {vpFoldDisplayLog([NSString stringWithFormat:@"inner display: render display added %@ uniqueId=%@", display,[display respondsToSelector:sel_registerName("uniqueId")] ? [display valueForKey:@"uniqueId"] : @"-"]);vpInspectRenderDisplays();+ return YES;} @catch (NSException *exception) {vpFoldDisplayLog([NSString stringWithFormat:@"inner display: render display exception %@", exception.reason]);}+ return NO;+}++// The fallback for a render server that was already running when this+// library loaded: add the display once the server answers.+static void vpAddInnerRenderDisplay(unsigned int attempt) {+ if (vpInnerRenderDisplay) return;+ id server = ((id (*)(id, SEL))objc_msgSend)(objc_getClass("CAWindowServer"), sel_registerName("serverIfRunning"));+ if (server)+ vpAddInnerRenderDisplayToServer(server);+ else if (attempt < 30)+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(),+ ^{ vpAddInnerRenderDisplay(attempt + 1); });+ else+ vpFoldDisplayLog(@"inner display: render server never ran");}// The surface type backboardd reports for a render display, from the@@ -116,6 +126,54 @@ static id vpInnerDigitizerState(id self, SEL selector, id service) {return state;}+// The inner panel is mounted at 270 degrees (device tree display1), so its+// digitizer reports positions in the panel's portrait frame, and the panel+// shows the inner display turned to match. This guest's single digitizer+// reports positions on the portrait cover panel, which backboardd would+// stretch onto the inner display's landscape space unturned; while the+// inner display has the digitizer, each finger's normalized position is+// turned by the mount first.+typedef struct __IOHIDEvent *VPHIDEventRef;+static IMP vpOriginalHandleDirectTouch;+static CFArrayRef (*vpHIDEventChildren)(VPHIDEventRef);+static uint32_t (*vpHIDEventType)(VPHIDEventRef);+static double (*vpHIDEventGetFloat)(VPHIDEventRef, uint32_t);+static void (*vpHIDEventSetFloat)(VPHIDEventRef, uint32_t, double);++static void vpTurnDigitizerPosition(VPHIDEventRef event) {+ static const uint32_t digitizer = 11, x = 11 << 16, y = (11 << 16) | 1;+ if (vpHIDEventType(event) == digitizer) {+ double nx = vpHIDEventGetFloat(event, x), ny = vpHIDEventGetFloat(event, y);+ vpHIDEventSetFloat(event, x, 1 - ny);+ vpHIDEventSetFloat(event, y, nx);+ }+ CFArrayRef children = vpHIDEventChildren(event);+ for (CFIndex index = 0; children && index < CFArrayGetCount(children); index++)+ vpTurnDigitizerPosition((VPHIDEventRef)CFArrayGetValueAtIndex(children, index));+}++static void vpInnerHandleDirectTouch(id self, SEL selector, void *event, id service) {+ if (event && vpInnerPanelLit()) vpTurnDigitizerPosition((VPHIDEventRef)event);+ ((void (*)(id, SEL, void *, id))vpOriginalHandleDirectTouch)(self, selector, event, service);+}++static void vpInstallInnerDigitizerTurn(Class processor) {+ Method handle = class_getInstanceMethod(processor, sel_registerName("_handleDirectTouchEvent:service:"));+ const char *encoding = handle ? method_getTypeEncoding(handle) : "missing";+ vpHIDEventChildren = dlsym(RTLD_DEFAULT, "IOHIDEventGetChildren");+ vpHIDEventType = dlsym(RTLD_DEFAULT, "IOHIDEventGetType");+ vpHIDEventGetFloat = dlsym(RTLD_DEFAULT, "IOHIDEventGetFloatValue");+ vpHIDEventSetFloat = dlsym(RTLD_DEFAULT, "IOHIDEventSetFloatValue");+ size_t length = strlen(encoding);+ if (!vpHIDEventChildren || !vpHIDEventType || !vpHIDEventGetFloat || !vpHIDEventSetFloat ||+ strncmp(encoding, "v32@0:8", 7) != 0 || length < 3 || strcmp(encoding + length - 3, "@24") != 0) {+ vpFoldDisplayLog([NSString stringWithFormat:@"inner display: digitizer turn ABI unavailable (%s)", encoding]);+ return;+ }+ vpOriginalHandleDirectTouch = method_setImplementation(handle, (IMP)vpInnerHandleDirectTouch);+ vpFoldDisplayLog([NSString stringWithFormat:@"inner display: digitizer turn installed (%s)", encoding]);+}+static void vpInstallInnerDigitizer(void) {Class processor = objc_getClass("BKHIDDirectTouchEventProcessor");Method uuid = class_getInstanceMethod(processor, sel_registerName("_queue_displayUUIDForDigitizerService:"));@@ -131,6 +189,7 @@ static void vpInstallInnerDigitizer(void) {vpOriginalDigitizerDisplayUUID = method_setImplementation(uuid, (IMP)vpInnerDigitizerDisplayUUID);vpOriginalDigitizerState = method_setImplementation(state, (IMP)vpInnerDigitizerState);vpFoldDisplayLog(@"inner display: digitizer routing installed");+ vpInstallInnerDigitizerTurn(processor);}// MARK: - SpringBoard: one scanout for two panels@@ -140,11 +199,13 @@ static void vpInstallInnerDigitizer(void) {// display on; closed, the reverse. On this guest the primary is the only// scanout, and open it has to show the inner display, which SpringBoard// composites onto it (vpShowInnerDisplay). So the primary is kept on while-// either panel wants it; a screen-off turns both off. The compositor-// follows the inner display's own state.+// either panel wants it; a screen-off turns both off. Closing does not turn+// the inner display off, so the inner display is lit only while the cover is+// not: the cover wins the shared panel. The compositor and backboardd's touch+// routing follow the inner display's applied state.static IMP vpOriginalTransitionDisplayState;-static __weak id vpPrimaryStateControl;-static long long vpPrimaryWantedState = 1, vpPrimaryAppliedState = 1, vpInnerWantedState;+static __weak id vpPrimaryStateControl, vpInnerStateControl;+static long long vpPrimaryWantedState = 1, vpPrimaryAppliedState = 1, vpInnerWantedState, vpInnerAppliedState = -1;static unsigned int vpStateControlDisplayId(id control) {Ivar ivar = class_getInstanceVariable(object_getClass(control), "_display_id");@@ -152,32 +213,41 @@ static unsigned int vpStateControlDisplayId(id control) {return *(unsigned int *)((uint8_t *)(__bridge void *)control + ivar_getOffset(ivar));}-static void vpApplyPrimaryState(id completion) {- id primary = vpPrimaryStateControl;- long long state = vpPrimaryWantedState || vpInnerWantedState ? 1 : 0;- if (!primary || (state == vpPrimaryAppliedState && !completion)) return;- vpPrimaryAppliedState = state;- ((void (*)(id, SEL, long long, id))vpOriginalTransitionDisplayState)(primary,+static void vpApplyPanelState(id control, long long state, long long *applied, id completion) {+ if (!control || (state == *applied && !completion)) return;+ *applied = state;+ ((void (*)(id, SEL, long long, id))vpOriginalTransitionDisplayState)(control,sel_registerName("transitionToDisplayState:withCompletion:"), state, completion);}+static void vpApplyPanelStates(id primaryCompletion, id innerCompletion) {+ vpApplyPanelState(vpPrimaryStateControl, vpPrimaryWantedState || vpInnerWantedState ? 1 : 0,+ &vpPrimaryAppliedState, primaryCompletion);+ long long inner = vpInnerWantedState && !vpPrimaryWantedState ? 1 : 0;+ BOOL changed = inner != vpInnerAppliedState;+ vpApplyPanelState(vpInnerStateControl, inner, &vpInnerAppliedState, innerCompletion);+ if (changed && vpInnerStateControl) dispatch_async(dispatch_get_main_queue(), ^{ vpShowInnerDisplay(inner != 0); });+}+static void vpSharedScanoutTransition(id self, SEL selector, long long state, id completion) {unsigned int displayId = vpStateControlDisplayId(self);vpFoldDisplayLog([NSString stringWithFormat:@"inner display: display %u wants state %lld", displayId, state]);if (displayId == 1) {vpPrimaryStateControl = self;vpPrimaryWantedState = state;- vpApplyPrimaryState(completion);+ vpApplyPanelStates(completion, nil);return;}- ((void (*)(id, SEL, long long, id))vpOriginalTransitionDisplayState)(self, selector, state, completion);id innerDisplay = nil;for (id display in vpReadObject(objc_getClass("CADisplay"), "displays") ?: @[])if ([[display valueForKey:@"displayId"] unsignedIntValue] == displayId && vpIsInnerDisplay(display)) innerDisplay = display;- if (!innerDisplay) return;+ if (!innerDisplay) {+ ((void (*)(id, SEL, long long, id))vpOriginalTransitionDisplayState)(self, selector, state, completion);+ return;+ }+ vpInnerStateControl = self;vpInnerWantedState = state;- vpApplyPrimaryState(nil);- dispatch_async(dispatch_get_main_queue(), ^{ vpShowInnerDisplay(state != 0); });+ vpApplyPanelStates(nil, completion);}static void vpInstallSharedScanout(void) {@@ -213,6 +283,7 @@ void vpLogRenderDisplayState(void) {void vpInstallInnerRenderDisplay(void) {if (!vpInnerDisplayRequested()) return;vpInstallInnerDigitizer();+ vpInstallInnerBootDisplay();dispatch_async(dispatch_get_main_queue(), ^{ vpAddInnerRenderDisplay(0); });}@@ -226,6 +297,7 @@ void vpInstallInnerRenderDisplay(void) {// UIKit-main-like, so only the real Main claims the cloning source.static IMP vpOriginalDisplayType, vpOriginalPointScale, vpOriginalExternal, vpOriginalConfigurationInit;static _Thread_local BOOL vpClassifyingConfiguration;+static id vpInnerIdentity;static BOOL vpIsInnerDisplay(id display) {@try {@@ -247,9 +319,33 @@ static id vpInnerConfigurationInit(id self, SEL selector, id display, id originavpClassifyingConfiguration = YES;id result = ((id (*)(id, SEL, id, id, BOOL, BOOL))vpOriginalConfigurationInit)(self, selector, display, original, isMain, assertIfInvalid);vpClassifyingConfiguration = NO;+ if (vpIsInnerDisplay(display) && !vpInnerIdentity) vpInnerIdentity = vpReadObject(result, "identity");return result;}+// Applications take the scene's display identity from SpringBoard. UIKit+// gives a screen the device idiom only when that identity is UIKit-main-like+// and leaves it unspecified (-1) otherwise; UIKit's Swift idiom switches+// (sheet presentation, scroll edge effects) trap on -1. The identity+// SpringBoard keeps stays as classified, so only Main claims the cloning+// source; the copy an application decodes says main-like.+static IMP vpOriginalIdentityEncode;++static void vpInnerIdentityEncode(id self, SEL selector, xpc_object_t dictionary) {+ ((void (*)(id, SEL, xpc_object_t))vpOriginalIdentityEncode)(self, selector, dictionary);+ if (dictionary && vpInnerIdentity && [self isEqual:vpInnerIdentity])+ xpc_dictionary_set_bool(dictionary, "mainLike", true);+}++static void vpInstallInnerIdentityEncode(void) {+ Method encode = class_getInstanceMethod(objc_getClass("FBSDisplayIdentity"), sel_registerName("encodeWithXPCDictionary:"));+ if (!encode || strcmp(method_getTypeEncoding(encode), "v24@0:8@16") != 0) {+ vpFoldDisplayLog(@"inner display: identity encoding ABI unavailable");+ return;+ }+ vpOriginalIdentityEncode = method_setImplementation(encode, (IMP)vpInnerIdentityEncode);+}+static NSUInteger vpInnerPointScale(id self, SEL selector) {return vpIsInnerDisplay(self) ? 3 : ((NSUInteger (*)(id, SEL))vpOriginalPointScale)(self, selector);}@@ -304,6 +400,7 @@ void vpInstallInnerDisplayAdapter(void) {if (!vpInnerDisplayRequested()) return;vpInstallSharedScanout();vpInstallInnerScreenIdiom();+ vpInstallInnerIdentityEncode();// Type 0 without the FrontBoard scope is a second UIKit-main-like// display, so no scope means no adapter.Method configurationInit = class_getInstanceMethod(objc_getClass("FBSDisplayConfiguration"),@@ -349,8 +446,28 @@ static id vpSpringBoardWindowScene(BOOL inner) {// SB-main, because open, SpringBoard gives the cover (Main) auxiliary// content and takes its scene's windows off screen. Touches on the hosted// windows reach their owners through backboardd's render-tree hit test.-static id vpInnerHostContext, vpInnerHostContainer;+//+// The render server redraws a display for commits to its own contexts only;+// a commit by an inner-display context (an application changing its pixels)+// leaves the hosting primary display on its last frame. So while shown, the+// container is touched once per primary frame: a near-1 opacity flip that+// damages all of it, without an implicit animation.+static id vpInnerHostContext, vpInnerHostContainer, vpInnerHostRefresh;static dispatch_source_t vpInnerHostTimer;++@interface VPhoneInnerHostRefresh : NSObject+@end++@implementation VPhoneInnerHostRefresh+- (void)refresh:(id)link {+ Class transaction = objc_getClass("CATransaction");+ ((void (*)(id, SEL))objc_msgSend)(transaction, sel_registerName("begin"));+ ((void (*)(id, SEL, BOOL))objc_msgSend)(transaction, sel_registerName("setDisableActions:"), YES);+ float opacity = ((float (*)(id, SEL))objc_msgSend)(vpInnerHostContainer, sel_registerName("opacity"));+ ((void (*)(id, SEL, float))objc_msgSend)(vpInnerHostContainer, sel_registerName("setOpacity:"), opacity < 1 ? 1 : 0.999f);+ ((void (*)(id, SEL))objc_msgSend)(transaction, sel_registerName("commit"));+}+@endstatic NSMutableDictionary<NSNumber *, id> *vpInnerHostLayers;static CGSize vpInnerHostPanel;@@ -432,6 +549,8 @@ void vpShowInnerDisplay(BOOL shown) {if (!vpInnerHostContext) return;dispatch_source_cancel(vpInnerHostTimer);vpInnerHostTimer = nil;+ ((void (*)(id, SEL))objc_msgSend)(vpInnerHostRefresh, sel_registerName("invalidate"));+ vpInnerHostRefresh = nil;((void (*)(id, SEL))objc_msgSend)(vpInnerHostContext, sel_registerName("invalidate"));vpInnerHostContext = vpInnerHostContainer = nil;vpInnerHostLayers = nil;@@ -470,6 +589,10 @@ void vpShowInnerDisplay(BOOL shown) {dispatch_source_set_timer(vpInnerHostTimer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 4), NSEC_PER_SEC / 4, NSEC_PER_SEC / 20);dispatch_source_set_event_handler(vpInnerHostTimer, ^{ vpSyncInnerHosts(); });dispatch_resume(vpInnerHostTimer);+ vpInnerHostRefresh = ((id (*)(id, SEL, id, SEL))objc_msgSend)(objc_getClass("CADisplayLink"),+ sel_registerName("displayLinkWithTarget:selector:"), [VPhoneInnerHostRefresh new], @selector(refresh:));+ ((void (*)(id, SEL, id, id))objc_msgSend)(vpInnerHostRefresh, sel_registerName("addToRunLoop:forMode:"),+ NSRunLoop.mainRunLoop, NSRunLoopCommonModes);vpFoldDisplayLog([NSString stringWithFormat:@"inner display: shown=1 context=%@ hosts=%lu",[context valueForKey:@"contextId"], (unsigned long)vpInnerHostLayers.count]);} @catch (NSException *exception) {@@ -642,6 +765,36 @@ void vpInspectInnerDisplay(void) {if (deviceClass && supports && lockRole && *lockRole)vpFoldDisplayLog([NSString stringWithFormat:@"inner display: PFCurrentDeviceClass=%ld embedded-as-class-1=%d",(long)deviceClass(), supports(*lockRole, @"DISPLAY_TYPE_EMBEDDED", 1)]);+ // Each scene's status bar windows (frame, provider, the status bar's+ // own frame) and its screen's inputs to the native provider lookup.+ for (id scene in [vpReadObject(app, "connectedScenes") allObjects] ?: @[]) {+ id screen = vpReadObject(scene, "screen");+ NSMutableString *line = [NSMutableString stringWithFormat:@"inner display: screen of %@",+ vpReadObject(vpReadObject(scene, "_FBSScene"), "identifier")];+ for (NSString *key in @[@"_nativeDisplayBounds", @"_exclusionArea", @"_displayCornerRadius", @"_referenceBounds"]) {+ id value = nil;+ @try { value = [screen valueForKey:key]; } @catch (__unused NSException *exception) { value = @"-"; }+ [line appendFormat:@" %@=%@", key, value];+ }+ for (id window in vpReadObject(scene, "windows") ?: @[]) {+ if (![NSStringFromClass([window class]) containsString:@"StatusBar"]) continue;+ id statusBar = nil;+ NSMutableArray *pending = [NSMutableArray arrayWithObject:window];+ while (pending.count && !statusBar) {+ id view = pending.lastObject;+ [pending removeLastObject];+ if ([view isKindOfClass:objc_getClass("STUIStatusBar")]) statusBar = view;+ else [pending addObjectsFromArray:[view valueForKey:@"subviews"] ?: @[]];+ }+ CGRect bounds = statusBar ? ((CGRect (*)(id, SEL))objc_msgSend)(statusBar, sel_registerName("bounds")) : CGRectZero;+ id hit = statusBar ? ((id (*)(id, SEL, CGPoint, id))objc_msgSend)(statusBar, sel_registerName("hitTest:withEvent:"),+ CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)), nil) : nil;+ [line appendFormat:@" | %@ frame=%@ STUIStatusBar=%@ provider=%@ middleHit=%@", NSStringFromClass([window class]),+ [window valueForKey:@"frame"], statusBar ? [statusBar valueForKey:@"frame"] : @"-",+ statusBar ? [statusBar valueForKey:@"_visualProviderClassName"] : @"-", NSStringFromClass([hit class])];+ }+ vpFoldDisplayLog(line);+ }for (id scene in [vpReadObject(app, "connectedScenes") allObjects] ?: @[]) {id configuration = nil;@try { configuration = vpReadObject(vpReadObject(scene, "_FBSScene"), "settings"); configuration = vpReadObject(configuration, "displayConfiguration"); }diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerTouch.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerTouch.mnew file mode 100644index 00000000..e7ae25a0--- /dev/null+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayInnerTouch.m@@ -0,0 +1,66 @@+// Spike: touches on the inner display reach its windows.+//+// backboardd routes the digitizer to the inner display while it is lit and+// its hit test names the inner window (FoldDisplayInnerDisplay.m). SpringBoard+// then sends the touch to a _UISystemGestureWindow first, and when that+// releases it, UIKit looks for the touch's window with+// -[UIWindow _targetWindowForPathIndex:atPoint:scenePointZ:forEvent:windowServerHitTestWindow:]:+// it hit-tests the point against the visible windows of the hit window's+// scene (+[UIWindow _hitTestToPoint:scenePointZ:forEvent:windowServerHitTestWindow:]).+// The answer was nil ("Unexpected nil window in latent system gesture client+// update", "to windows: 0"): the point is in the receiving window's space, a+// window of Main, and the inner scene's space is a different screen's.+//+// The compositor shows the inner scene's points one to one on the panel, so+// a point on Main's fixed coordinate space is the same point in the inner+// scene. When the native answer is nil for a hit window on the inner display,+// the point is taken through Main's fixed space and hit-tested again.++#import "FoldDisplayPrivate.h"++static IMP vpOriginalTargetWindow;+static unsigned int vpTouchLogCount;++static BOOL vpWindowIsOnInnerDisplay(id window) {+ id scene = vpReadObject(window, "windowScene");+ id configuration = vpReadObject(vpReadObject(vpReadObject(scene, "_FBSScene"), "settings"), "displayConfiguration");+ id identity = vpReadObject(configuration, "identity");+ return identity && ![[identity description] isEqual:@"Main"];+}++static id vpInnerTargetWindow(id self, SEL selector, NSInteger pathIndex, CGPoint point, double z, id event, id hitWindow) {+ id target = ((id (*)(id, SEL, NSInteger, CGPoint, double, id, id))vpOriginalTargetWindow)(self, selector, pathIndex, point, z, event, hitWindow);+ if (target || !hitWindow || !vpWindowIsOnInnerDisplay(hitWindow)) return target;+ @try {+ id fixed = vpReadObject(vpReadObject(self, "screen"), "fixedCoordinateSpace");+ CGPoint mapped = fixed ? ((CGPoint (*)(id, SEL, CGPoint, id))objc_msgSend)(self,+ sel_registerName("convertPoint:toCoordinateSpace:"), point, fixed) : point;+ target = ((id (*)(id, SEL, CGPoint, double, id, id))objc_msgSend)(objc_getClass("UIWindow"),+ sel_registerName("_hitTestToPoint:scenePointZ:forEvent:windowServerHitTestWindow:"), mapped, z, event, hitWindow);+ if (vpTouchLogCount++ < 24)+ vpFoldDisplayLog([NSString stringWithFormat:@"inner touch: receiver %@ point {%.1f, %.1f} -> {%.1f, %.1f} hit %@ -> %@",+ NSStringFromClass([self class]), point.x, point.y, mapped.x, mapped.y,+ NSStringFromClass([hitWindow class]), target ? NSStringFromClass([target class]) : @"nil"]);+ } @catch (NSException *exception) {+ vpFoldDisplayLog([NSString stringWithFormat:@"inner touch: exception %@", exception.reason]);+ }+ return target;+}++void vpInstallInnerTouch(void) {+ if (!vpInnerDisplayRequested()) return;+ Method target = class_getInstanceMethod(objc_getClass("UIWindow"),+ sel_registerName("_targetWindowForPathIndex:atPoint:scenePointZ:forEvent:windowServerHitTestWindow:"));+ Method hitTest = class_getClassMethod(objc_getClass("UIWindow"),+ sel_registerName("_hitTestToPoint:scenePointZ:forEvent:windowServerHitTestWindow:"));+ const char *targetEncoding = target ? method_getTypeEncoding(target) : "missing";+ const char *hitTestEncoding = hitTest ? method_getTypeEncoding(hitTest) : "missing";+ if (strcmp(targetEncoding, "@64@0:8q16{CGPoint=dd}24d40@48@56") != 0 ||+ strcmp(hitTestEncoding, "@56@0:8{CGPoint=dd}16d32@40@48") != 0) {+ vpFoldDisplayLog([NSString stringWithFormat:@"inner touch: target window ABI unavailable (%s, %s)",+ targetEncoding, hitTestEncoding]);+ return;+ }+ vpOriginalTargetWindow = method_setImplementation(target, (IMP)vpInnerTargetWindow);+ vpFoldDisplayLog(@"inner touch: target window adapter installed");+}diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.hindex 9ed7134d..ab26c576 100644--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPrivate.h@@ -18,6 +18,7 @@#include <string.h>#include <stdatomic.h>#include <notify.h>+#include <xpc/xpc.h>#include <sys/stat.h>#include <sys/sysctl.h>#include <unistd.h>@@ -128,6 +129,7 @@ void vpRefreshNativeClockSource(void);BOOL vpIsSingleMainStatusScene(id scene, BOOL constructing);void vpInstallSceneStatusPolicy(void);void vpInstallMainStatusLookup(void);+void vpInstallDualStatusLookup(void);void vpRefreshMainStatusRenderer(void);void vpRestoreMainStatusProfile(void);void vpPulseMainStatusProfile(void);@@ -184,11 +186,14 @@ void vpVirtualDisplayExperiment(BOOL create);BOOL vpInnerDisplayRequested(void);NSNumber *vpInnerDisplaySurfaceType(id deviceName);void vpInstallInnerRenderDisplay(void);+BOOL vpAddInnerRenderDisplayToServer(id server);+void vpInstallInnerBootDisplay(void);void vpLogRenderDisplayState(void);void vpShowInnerDisplay(BOOL shown);void vpToggleInnerPattern(void);void vpResolveInnerDisplay(void);void vpInstallInnerDisplayAdapter(void);+void vpInstallInnerTouch(void);void vpInspectInnerDisplay(void);// MARK: - diagnosticsdiff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayStatusBar.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayStatusBar.mindex e5acacfe..7995a16e 100644--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayStatusBar.m+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayStatusBar.m@@ -203,28 +203,25 @@ void vpInstallSceneStatusPolicy(void) {}@end-static Class vpMainStatusClassLookup(id screen, id info) {+// Asks the native, read-only provider factory which provider the given+// Duo panel takes, by showing it a preview of the screen with that panel's+// native pixels and an exclusion area of the profile's notch. Every screen+// on this guest carries Main's MobileGestalt panel metrics, so the factory+// would otherwise pick a generic island provider. Nil unless the factory+// itself selects the profile.+static Class vpDuoPanelStatusClass(id screen, id info, BOOL inner) {Class (*original)(id, id) = vpOriginalMainStatusClassLookup;- if (!vpMainStatusProfileEnabled || ![NSThread isMainThread]) return original(screen, info);- id root = vpReadObject(objc_getClass("FBDisplayManager"), "mainConfiguration");- id identity = vpReadObject(screen, "displayIdentity");- NSArray *bounds = vpSnapshotRect(root, "bounds");- BOOL inner = [bounds isEqual:@[@0, @0, @669, @951]];- BOOL cover = [bounds isEqual:@[@0, @0, @466, @678]];- if (![identity isEqual:vpReadObject(root, "identity")] ||- screen != vpReadObject(vpMainStatusProfileScene, "screen") ||- !(inner || cover)) return original(screen, info);Class profile = objc_getClass(inner ? "STUIStatusBarVisualProvider_AxisAlignableSplit2007" :"STUIStatusBarVisualProvider_AxisAlignableSplitV68Cover");SEL notchSelector = sel_registerName("notchSize");NSMethodSignature *notchSignature = [profile methodSignatureForSelector:notchSelector];- if (notchSignature.methodReturnLength != sizeof(CGSize)) return original(screen, info);+ if (notchSignature.methodReturnLength != sizeof(CGSize)) return Nil;CGSize notch = ((CGSize (*)(id, SEL))objc_msgSend)(profile, notchSelector);Class shapeClass = objc_getClass("UISDisplaySingleRectShape");SEL initializer = sel_registerName("initWithRect:");NSMethodSignature *shapeSignature = [shapeClass instanceMethodSignatureForSelector:initializer];if (shapeSignature.numberOfArguments != 3 ||- strcmp([shapeSignature getArgumentTypeAtIndex:2], "{CGRect={CGPoint=dd}{CGSize=dd}}") != 0) return original(screen, info);+ strcmp([shapeSignature getArgumentTypeAtIndex:2], "{CGRect={CGPoint=dd}{CGSize=dd}}") != 0) return Nil;VPhoneStatusScreenPreview *preview = [VPhoneStatusScreenPreview alloc];preview.source = screen;preview.referenceBounds = inner ? CGRectMake(0, 0, 2007, 2853) : CGRectMake(0, 0, 1398, 2034);@@ -232,12 +229,51 @@ static Class vpMainStatusClassLookup(id screen, id info) {preview.previewExclusion = ((id (*)(id, SEL, CGRect))objc_msgSend)([shapeClass alloc], initializer,CGRectMake(0, 0, notch.width, notch.height));Class selected = original(preview, info);- if (selected != profile) return original(screen, info);+ return selected == profile ? selected : Nil;+}++static Class vpMainStatusClassLookup(id screen, id info) {+ Class (*original)(id, id) = vpOriginalMainStatusClassLookup;+ if (!vpMainStatusProfileEnabled || ![NSThread isMainThread]) return original(screen, info);+ id root = vpReadObject(objc_getClass("FBDisplayManager"), "mainConfiguration");+ id identity = vpReadObject(screen, "displayIdentity");+ NSArray *bounds = vpSnapshotRect(root, "bounds");+ BOOL inner = [bounds isEqual:@[@0, @0, @669, @951]];+ BOOL cover = [bounds isEqual:@[@0, @0, @466, @678]];+ if (![identity isEqual:vpReadObject(root, "identity")] ||+ screen != vpReadObject(vpMainStatusProfileScene, "screen") ||+ !(inner || cover)) return original(screen, info);+ Class selected = vpDuoPanelStatusClass(screen, info, inner);+ if (!selected) return original(screen, info);if (vpMainStatusProfileLogCount++ < 8)vpFoldDisplayLog([NSString stringWithFormat:@"Main status profile: native %@ lookup accepted; original data/screen/identity retained", inner ? @"inner" : @"cover"]);return selected;}+// With the inner display published each screen is one panel: the inner+// display's screen is the inner panel and Main's is the cover.+static Class vpDualStatusClassLookup(id screen, id info) {+ Class (*original)(id, id) = vpOriginalMainStatusClassLookup;+ if (![NSThread isMainThread]) return original(screen, info);+ id identity = vpReadObject(vpReadObject(screen, "displayConfiguration"), "identity");+ if (!identity) return original(screen, info);+ BOOL inner = ![[identity description] isEqual:@"Main"];+ Class selected = vpDuoPanelStatusClass(screen, info, inner);+ if (vpMainStatusProfileLogCount++ < 8)+ vpFoldDisplayLog([NSString stringWithFormat:@"status bar: %@ screen takes %@", inner ? @"inner" : @"cover",+ selected ? NSStringFromClass(selected) : @"the native provider"]);+ return selected ?: original(screen, info);+}++void vpInstallDualStatusLookup(void) {+ void *target = dlsym(RTLD_DEFAULT, "STUIStatusBarGetVisualProviderClassForScreen");+ MISFixDetourResult (*install)(const char *, void *, void *, void **) = dlsym(RTLD_DEFAULT, "MISFixDetour");+ if (!target || !install) { vpFoldDisplayLog(@"status bar: native lookup/detour API unavailable"); return; }+ MISFixDetourResult result = install("Duo per-panel status profile", target, (void *)vpDualStatusClassLookup,+ &vpOriginalMainStatusClassLookup);+ vpFoldDisplayLog([NSString stringWithFormat:@"status bar: per-panel lookup detour result=%d", result]);+}+void vpInstallMainStatusLookup(void) {void *target = dlsym(RTLD_DEFAULT, "STUIStatusBarGetVisualProviderClassForScreen");MISFixDetourResult (*install)(const char *, void *, void *, void **) = dlsym(RTLD_DEFAULT, "MISFixDetour");@@ -405,6 +441,8 @@ void vpRestoreStatusProfileLookup(void) {}void vpPulseStatusProfileLookup(void) {+ // Single-display only: a published inner display has its own status bar.+ if (vpInnerDisplayRequested()) return;@try {if (vpStatusProfileLookupEnabled || !vpUIConfigPublished || vpMainInnerWindowScene() != vpUIConfigWindowScene) return;Method methods[4];diff --git a/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m b/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.mindex 7ebeb0aa..9bb17bce 100644--- a/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m+++ b/VPhoneGuestComponents/FoldDisplay/libvphonefolddisplay.m@@ -56,6 +56,7 @@ static void vpInstallBackboardProbes(void) {static void vpInstallSpringBoardHooks(void) {vpInstallInnerDisplayAdapter();+ vpInstallInnerTouch();// The round-16 landscape wallpaper gate is now part of the Duo startup// path. The one-shot marker remains accepted for older guests, but a// fresh boot must not silently fall back to the portrait wallpaper@@ -107,25 +108,36 @@ static void vpInstallSpringBoardHooks(void) {vpFoldDisplayLog(@"panel adapter: retired builtin promotion marker discarded");if (vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-shared-family.once"))vpInstallSharedInnerFamily();+ // With the inner display published, Main is the cover and the inner panel+ // has scenes of its own: each screen's status bar provider is chosen for+ // its own panel (vpInstallDualStatusLookup). The single-display profile+ // lookup and scene status policy below would pin Main to the inner+ // panel's profile instead.+ BOOL nativeStatus = vpInnerDisplayRequested();BOOL requestedMainStatus = vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-main-status.once");- if (vpAutomaticMainStatus || requestedMainStatus) vpInstallMainStatusLookup();+ if (nativeStatus) vpInstallDualStatusLookup();+ else if (vpAutomaticMainStatus || requestedMainStatus) vpInstallMainStatusLookup();if (vpAutomaticMainStatus) {- static int mainStatusStartupToken;- notify_register_dispatch("com.apple.springboard.finishedstartup", &mainStatusStartupToken, dispatch_get_main_queue(),- ^(int token) { vpPulseMainStatusProfile(); });- // The library may load before scene construction or after the startup- // notification. Bounded retries establish the existing Main renderer.- for (unsigned int retry = 1; retry <= 12; ++retry)- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, retry * NSEC_PER_SEC), dispatch_get_main_queue(),- ^{ vpPulseMainStatusProfile(); });+ if (!nativeStatus) {+ static int mainStatusStartupToken;+ notify_register_dispatch("com.apple.springboard.finishedstartup", &mainStatusStartupToken, dispatch_get_main_queue(),+ ^(int token) { vpPulseMainStatusProfile(); });+ // The library may load before scene construction or after the startup+ // notification. Bounded retries establish the existing Main renderer.+ for (unsigned int retry = 1; retry <= 12; ++retry)+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, retry * NSEC_PER_SEC), dispatch_get_main_queue(),+ ^{ vpPulseMainStatusProfile(); });+ }// The same ladder establishes the default quick-action glyph scale// on every lock-screen construction timing without any opt-in file.for (unsigned int retry = 1; retry <= 12; ++retry)dispatch_after(dispatch_time(DISPATCH_TIME_NOW, retry * NSEC_PER_SEC), dispatch_get_main_queue(),^{ vpApplyQuickActionGlyphScale(); });}- if (vpAutomaticMainStatus || vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-scene-status.once"))+ BOOL requestedSceneStatus = vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-scene-status.once");+ if (!nativeStatus && (vpAutomaticMainStatus || requestedSceneStatus))vpInstallSceneStatusPolicy();+ if (nativeStatus) vpFoldDisplayLog(@"status bar: native per-screen providers, single-display status shims not installed");if (vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-root-scene-policy.once"))vpFoldDisplayLog(@"root scene policy: retired global Main conversion marker discarded");if (vpConsumeDisplayExperimentFlag("/var/mobile/Library/Caches/vphone-duo-inner-family.once")) {