Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: keep DeviceHub's hinge reading with its slider

Jackyaca39704a45b
diff --git a/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift b/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
index d2db72d8..d767cf2f 100644
--- a/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
+++ b/VPhoneDaemon/Daemon/GuestDeviceHubControl.swift
@@ -36,6 +36,15 @@ enum GuestDeviceHubControl {
guard let angle = (value as? NSNumber)?.doubleValue ?? (value as? String).flatMap(Double.init),
angle.isFinite
else { return }
+ // DeviceHub draws a closed or open hinge state at 0 or 180 degrees
+ // whatever its own animation shows, so a reading still at the old
+ // pose pulled its model back there between the end of the run and
+ // the fold. The reading follows the run; the fold then sets the
+ // hinge to where it ended, or back if it does not run.
+ if !vp_core_motion_hinge() {
+ let reading = min(max(angle, 0), 180)
+ vp_hinge_reading_publish(reading, Int32(GuestAPI.hingeState(for: reading)))
+ }
queue.async {
pending.generation &+= 1
let generation = pending.generation
@@ -45,6 +54,9 @@ enum GuestDeviceHubControl {
try GuestAPI.foldFromDeviceHub(angle: angle)
} catch {
NSLog("vphoned: DeviceHub fold to %.0f° failed: %@", angle, "\(error)")
+ if !vp_core_motion_hinge(), let held = (vp_hinge_get()["angle"] as? NSNumber)?.doubleValue {
+ vp_hinge_reading_publish(held, Int32(GuestAPI.hingeState(for: held)))
+ }
}
}
}
diff --git a/VPhoneDaemon/Native/Include/VphonedNative.h b/VPhoneDaemon/Native/Include/VphonedNative.h
index cf400a18..d8d75e2a 100644
--- a/VPhoneDaemon/Native/Include/VphonedNative.h
+++ b/VPhoneDaemon/Native/Include/VphonedNative.h
@@ -222,6 +222,10 @@ NSDictionary * _Nonnull vp_hinge_get(void);
/// dictionary with `source`, `type` and `value`) to `handler`, on a serial
/// queue of its own. False on any other device or without the HID API.
bool vp_devicehub_controls_start(void (^_Nonnull handler)(NSDictionary * _Nonnull control));
+/// Publish a hinge reading (degrees, and the hinge state it names) where the
+/// hinge stand-ins in dtdeviceinfod read the virtual hinge's newest sample,
+/// without moving the hinge.
+void vp_hinge_reading_publish(double angleDegrees, int hingeState);
/// Every display-ish IOService object the guest published, with its small
/// properties: what the display stack bound, for the foldable display work.
NSArray * _Nonnull vp_display_probe(void);
diff --git a/VPhoneDaemon/Native/vphoned_devicehub.m b/VPhoneDaemon/Native/vphoned_devicehub.m
index ce15effa..50f94fb3 100644
--- a/VPhoneDaemon/Native/vphoned_devicehub.m
+++ b/VPhoneDaemon/Native/vphoned_devicehub.m
@@ -18,6 +18,7 @@
*/
#import "Include/VphonedNative.h"
+#import "../../VPhoneGuestComponents/Hinge/VPhoneHingeState.h"
#include <dlfcn.h>
#include <string.h>
@@ -87,3 +88,16 @@ void vp_device_orientation_publish(int deviceOrientation) {
CFSTR("com.apple.backboardd"), CFSTR("mobile"), kCFPreferencesAnyHost);
CFPreferencesSynchronize(CFSTR("com.apple.backboardd"), CFSTR("mobile"), kCFPreferencesAnyHost);
}
+
+void vp_hinge_reading_publish(double angleDegrees, int hingeState) {
+ // The sample libvphonehinge writes as the hinge moves (phase 2, between
+ // the ends of a sweep); the hinge itself and its configuration are left
+ // alone.
+ NSDictionary *latest = @{
+ @"angle": @(angleDegrees), @"mechanical_angle": @(angleDegrees), @"velocity": @0,
+ @"state": @(hingeState), @"event_phase": @2, @"timestamp": @(NSDate.date.timeIntervalSince1970),
+ };
+ CFPreferencesSetValue(CFSTR(VP_HINGE_LATEST), (__bridge CFDictionaryRef)latest, CFSTR(VP_HINGE_DOMAIN),
+ CFSTR("mobile"), kCFPreferencesAnyHost);
+ CFPreferencesSynchronize(CFSTR(VP_HINGE_DOMAIN), CFSTR("mobile"), kCFPreferencesAnyHost);
+}