Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: store DeviceHub's slider values in the hinge without resending them

Jackyfb040cd524d0
diff --git a/Research/Guest/devicehub_screen_viewing.md b/Research/Guest/devicehub_screen_viewing.md
index e06e876f..ceb5952a 100644
--- a/Research/Guest/devicehub_screen_viewing.md
+++ b/Research/Guest/devicehub_screen_viewing.md
@@ -676,8 +676,11 @@ DeviceHub reads the real managers. dtuhidd's "CoreDevice avpCustom" service
carries the same usage, so the relay takes DeviceHub's controls itself, as on
Apple's VM host, and the guest follows the slider as it moves. The virtual
hinge, which resends its stored values every 5 s for a restarted locationd,
-takes each slider value too: while it kept the old angle until a debounced
-fold, a resend landing inside a run sent the fold back. An opening run from
+stores each slider value quietly (a `quiet` configuration: no host-control
+event): while it kept the old angle until a debounced fold, a resend landing
+inside a run sent the fold back, and sending each value again reached the
+relay late, behind DeviceHub's own, and swung the hinge back and forth across
+the swap (the panel changed hands eight times on three clicks). An opening run from
closed lights the screen first, as `display.posture` does; with a sleeping
cover SpringBoard's pose provider commits no moving pose and the inner
display stayed black. The picker sets the hinge's orientation. The provider
diff --git a/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift b/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
index fd9d7653..a6b720a4 100644
--- a/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
+++ b/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
@@ -83,8 +83,10 @@ enum GuestDeviceHubControl {
/// avpCustom service carries the host controls' usage), as on Apple's VM
/// host, so the guest follows the slider as it moves. The virtual hinge,
/// which resends its stored angle every few seconds for a restarted
- /// locationd, takes each value too: a resend of the old angle in the
- /// middle of a run sent the fold back. Opening from closed lights the
+ /// locationd, stores each value too, quietly: a resend of the old angle
+ /// in the middle of a run sent the fold back, and sending each value
+ /// again reached the relay late, after DeviceHub's own, and swung the
+ /// hinge back and forth across the swap. Opening from closed lights the
/// screen first, as `display.posture` does: SpringBoard's pose provider
/// commits a moving pose only on display frames, and a sleeping cover
/// left the inner display black.
@@ -93,6 +95,6 @@ enum GuestDeviceHubControl {
GuestAPI.hingeState(for: held) == 1, GuestAPI.hingeState(for: angle) != 1 {
wake.async { GuestScreenUnlock.wakeDisplay() }
}
- _ = vp_hinge_set(angle, 0)
+ _ = vp_hinge_note(angle)
}
}
diff --git a/VPhoneDaemon/Native/Include/VphonedNative.h b/VPhoneDaemon/Native/Include/VphonedNative.h
index d8d75e2a..d24b2214 100644
--- a/VPhoneDaemon/Native/Include/VphonedNative.h
+++ b/VPhoneDaemon/Native/Include/VphonedNative.h
@@ -236,6 +236,10 @@ NSDictionary<NSString *, id> * _Nonnull vp_device_pose_get(void);
/// device orientation for the host controls (1 portrait … 6 face down), or 0
/// to keep the stored one.
BOOL vp_hinge_set(double angleDegrees, int orientation);
+/// Store `angleDegrees` as the hinge's angle, keeping its orientation, without
+/// a host-control event or waiting for the provider: the relay already has the
+/// angle, and the hinge keeps it for its periodic resend.
+bool vp_hinge_note(double angleDegrees);
/// Sweep the hinge to `targetDegrees` the way Apple's Duo tooling does: over
/// a profiled transition (0 continuous fold, 1 fade-in-out), `durationMs` < 0
/// the per-direction default (opening faster than closing), 0 an instant
diff --git a/VPhoneDaemon/Native/vphoned_pose.m b/VPhoneDaemon/Native/vphoned_pose.m
index 6d4c2f7b..c2e658f0 100644
--- a/VPhoneDaemon/Native/vphoned_pose.m
+++ b/VPhoneDaemon/Native/vphoned_pose.m
@@ -193,6 +193,16 @@ bool vp_hinge_set(double angleDegrees, int orientation) {
return vp_hinge_publish(VPhoneHingeEncode(state), angleDegrees);
}
+bool vp_hinge_note(double angleDegrees) {
+ if (!isfinite(angleDegrees) || angleDegrees < 0 || angleDegrees > 180)
+ return false;
+ VPhoneHingeState state = {true, false, angleDegrees, 0, 0, vp_hinge_orientation(0), true};
+ CFPreferencesSetValue(CFSTR(VP_HINGE_CONFIGURATION), (__bridge CFDictionaryRef)VPhoneHingeEncode(state),
+ CFSTR(VP_HINGE_DOMAIN), CFSTR("mobile"), kCFPreferencesAnyHost);
+ return CFPreferencesSynchronize(CFSTR(VP_HINGE_DOMAIN), CFSTR("mobile"), kCFPreferencesAnyHost) &&
+ notify_post(VP_HINGE_NOTIFICATION) == NOTIFY_STATUS_OK;
+}
+
bool vp_hinge_sweep(double targetDegrees, double durationMs, int profile, int orientation) {
if (!isfinite(targetDegrees) || targetDegrees < 0 || targetDegrees > 180 ||
!isfinite(durationMs) || durationMs < -1 || durationMs > 10000 ||
diff --git a/VPhoneGuestComponents/Hinge/VPhoneHingeState.h b/VPhoneGuestComponents/Hinge/VPhoneHingeState.h
index ac5f8703..ed0e5a9e 100644
--- a/VPhoneGuestComponents/Hinge/VPhoneHingeState.h
+++ b/VPhoneGuestComponents/Hinge/VPhoneHingeState.h
@@ -34,6 +34,10 @@ typedef struct {
// CoreMotion's device orientation, 1 portrait … 6 face down; 0 leaves
// the orientation the relay holds.
int32_t orientation;
+ // Stored without a host-control event: the relay already has the angle
+ // (DeviceHub sends it there itself), and the hinge only keeps it for its
+ // periodic resend.
+ BOOL quiet;
} VPhoneHingeState;
typedef enum {
@@ -95,8 +99,12 @@ static inline BOOL VPhoneHingeDecode(id value, VPhoneHingeState *state) {
if (orientation < 0 || orientation > 6 || orientation != [value[@"orientation"] doubleValue])
return NO;
}
+ id quietValue = value[@"quiet"];
+ if (quietValue != nil && (![quietValue isKindOfClass:NSNumber.class] ||
+ CFGetTypeID((__bridge CFTypeRef)quietValue) != CFBooleanGetTypeID()))
+ return NO;
*state = (VPhoneHingeState){
- [enabled boolValue], sweep, degrees, duration, profile, orientation,
+ [enabled boolValue], sweep, degrees, duration, profile, orientation, [quietValue boolValue],
};
return YES;
}
@@ -111,6 +119,8 @@ static inline NSDictionary *VPhoneHingeEncode(VPhoneHingeState state) {
} mutableCopy];
if (state.orientation)
encoded[@"orientation"] = @(state.orientation);
+ if (state.quiet)
+ encoded[@"quiet"] = @YES;
return encoded;
}
diff --git a/VPhoneGuestComponents/Hinge/libvphonehinge.m b/VPhoneGuestComponents/Hinge/libvphonehinge.m
index b6745fa2..0f692c3f 100644
--- a/VPhoneGuestComponents/Hinge/libvphonehinge.m
+++ b/VPhoneGuestComponents/Hinge/libvphonehinge.m
@@ -73,7 +73,7 @@
// HID service enumerates and on the rare tick it is down.
[self publishLatestAngle:self.currentAngle velocity:0
state:VPhoneHingeStateForAngle(self.currentAngle) phase:0];
- if (self.enumerated)
+ if (self.enumerated && !state.quiet)
[self dispatchSample];
}
if (state.orientation && state.orientation != self.orientation) {
diff --git a/VPhoneGuestComponents/Tests/HingeTests.m b/VPhoneGuestComponents/Tests/HingeTests.m
index bd06a960..ed57ab49 100644
--- a/VPhoneGuestComponents/Tests/HingeTests.m
+++ b/VPhoneGuestComponents/Tests/HingeTests.m
@@ -87,6 +87,17 @@ int main(void) {
bad[@"orientation"] = invalid;
assert(!VPhoneHingeDecode(bad, &state));
}
+ // A quiet update: a boolean, absent as no, encoded only when set.
+ NSMutableDictionary *quiet = [sample mutableCopy];
+ quiet[@"quiet"] = @YES;
+ assert(VPhoneHingeDecode(quiet, &state) && state.quiet);
+ assert([VPhoneHingeEncode(state) isEqual:quiet]);
+ assert(VPhoneHingeDecode(sample, &state) && !state.quiet);
+ for (id invalid in @[@1, @"yes"]) {
+ NSMutableDictionary *bad = [sample mutableCopy];
+ bad[@"quiet"] = invalid;
+ assert(!VPhoneHingeDecode(bad, &state));
+ }
assert(!VPhoneHingeDecode(@"flat", &state));
assert(VPhoneHingeInterval(nil) == 0);
assert(VPhoneHingeInterval(@YES) == 0);