Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

vphone-cli: guest-side touch injection for iOS 18 bases

zqxwceb90f699583f7
diff --git a/scripts/vphoned/vphoned.m b/scripts/vphoned/vphoned.m
index 589d718d..efd028d6 100644
--- a/scripts/vphoned/vphoned.m
+++ b/scripts/vphoned/vphoned.m
@@ -201,6 +201,14 @@ static NSDictionary *handle_command(NSDictionary *msg) {
return vp_make_response(@"ok", reqId);
}
+ if ([type isEqualToString:@"touch"]) {
+ int phase = [msg[@"phase"] intValue];
+ double x = [msg[@"x"] doubleValue];
+ double y = [msg[@"y"] doubleValue];
+ vp_hid_touch(phase, x, y);
+ return vp_make_response(@"ok", reqId);
+ }
+
if ([type isEqualToString:@"devmode"]) {
if (!vp_devmode_available()) {
NSMutableDictionary *r = vp_make_response(@"err", reqId);
@@ -331,6 +339,7 @@ static BOOL handle_client(int fd) {
[caps addObject:@"apps"];
[caps addObject:@"url"];
[caps addObject:@"settings"];
+ [caps addObject:@"touch"];
NSMutableDictionary *helloResp = [@{
@"v" : @PROTOCOL_VERSION,
@@ -338,6 +347,11 @@ static BOOL handle_client(int fd) {
@"name" : @"vphoned",
@"caps" : caps,
} mutableCopy];
+ NSOperatingSystemVersion osv =
+ [[NSProcessInfo processInfo] operatingSystemVersion];
+ helloResp[@"ios"] =
+ [NSString stringWithFormat:@"%ld.%ld.%ld", (long)osv.majorVersion,
+ (long)osv.minorVersion, (long)osv.patchVersion];
NSString *ip = primary_ipv4_address();
if (ip)
helloResp[@"ip"] = ip;
diff --git a/scripts/vphoned/vphoned_hid.h b/scripts/vphoned/vphoned_hid.h
index 510d616c..093e4bf7 100644
--- a/scripts/vphoned/vphoned_hid.h
+++ b/scripts/vphoned/vphoned_hid.h
@@ -16,3 +16,9 @@ void vp_hid_press(uint32_t page, uint32_t usage);
/// Send a single key down or key up event.
void vp_hid_key(uint32_t page, uint32_t usage, BOOL down);
+
+/// Inject a single-finger digitizer touch event.
+/// phase: 0 = down, 1 = move, 3 = up. x/y are normalized 0..1 with the
+/// origin at the top-left. Used for iOS 18 bases where the VZ USB touchscreen
+/// dext produces no digitizer events on the 26.x kernel.
+void vp_hid_touch(int phase, double x, double y);
diff --git a/scripts/vphoned/vphoned_hid.m b/scripts/vphoned/vphoned_hid.m
index 463641e9..9658bf98 100644
--- a/scripts/vphoned/vphoned_hid.m
+++ b/scripts/vphoned/vphoned_hid.m
@@ -5,6 +5,7 @@
typedef void *IOHIDEventSystemClientRef;
typedef void *IOHIDEventRef;
+typedef double IOHIDFloat;
static IOHIDEventSystemClientRef (*pCreate)(CFAllocatorRef);
static IOHIDEventRef (*pKeyboard)(CFAllocatorRef, uint64_t,
@@ -12,9 +13,30 @@ static IOHIDEventRef (*pKeyboard)(CFAllocatorRef, uint64_t,
static void (*pSetSender)(IOHIDEventRef, uint64_t);
static void (*pDispatch)(IOHIDEventSystemClientRef, IOHIDEventRef);
+// Digitizer (touch) event symbols — resolved lazily; touch is a no-op if absent.
+static IOHIDEventRef (*pDigitizer)(CFAllocatorRef, uint64_t, uint32_t, uint32_t,
+ uint32_t, uint32_t, uint32_t, IOHIDFloat,
+ IOHIDFloat, IOHIDFloat, IOHIDFloat, IOHIDFloat,
+ boolean_t, boolean_t, uint32_t);
+static IOHIDEventRef (*pFinger)(CFAllocatorRef, uint64_t, uint32_t, uint32_t,
+ uint32_t, IOHIDFloat, IOHIDFloat, IOHIDFloat,
+ IOHIDFloat, IOHIDFloat, boolean_t, boolean_t, uint32_t);
+static void (*pAppend)(IOHIDEventRef, IOHIDEventRef, uint32_t);
+static void (*pSetInt)(IOHIDEventRef, uint32_t, int);
+
static IOHIDEventSystemClientRef gClient;
static dispatch_queue_t gHIDQueue;
+// Digitizer event-mask bits and transducer types (IOHIDEventTypes.h).
+#define VP_DIG_RANGE 0x00000001u
+#define VP_DIG_TOUCH 0x00000002u
+#define VP_DIG_POSITION 0x00000004u
+#define VP_DIG_IDENTITY 0x00000020u
+#define VP_TRANSDUCER_HAND 1
+#define VP_TRANSDUCER_FINGER 2
+// kIOHIDEventFieldDigitizerIsDisplayIntegrated: (kIOHIDEventTypeDigitizer<<16)|offset.
+#define VP_FIELD_IS_DISPLAY_INTEGRATED ((((uint32_t)11) << 16) | 25)
+
BOOL vp_hid_load(void) {
void *h = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW);
if (!h) { NSLog(@"vphoned: dlopen IOKit failed"); return NO; }
@@ -24,10 +46,17 @@ BOOL vp_hid_load(void) {
pSetSender = dlsym(h, "IOHIDEventSetSenderID");
pDispatch = dlsym(h, "IOHIDEventSystemClientDispatchEvent");
+ pDigitizer = dlsym(h, "IOHIDEventCreateDigitizerEvent");
+ pFinger = dlsym(h, "IOHIDEventCreateDigitizerFingerEvent");
+ pAppend = dlsym(h, "IOHIDEventAppendEvent");
+ pSetInt = dlsym(h, "IOHIDEventSetIntegerValue");
+
if (!pCreate || !pKeyboard || !pSetSender || !pDispatch) {
NSLog(@"vphoned: missing IOKit symbols");
return NO;
}
+ if (!pDigitizer || !pFinger || !pAppend || !pSetInt)
+ NSLog(@"vphoned: digitizer symbols missing, touch injection disabled");
gClient = pCreate(kCFAllocatorDefault);
if (!gClient) { NSLog(@"vphoned: IOHIDEventSystemClientCreate returned NULL"); return NO; }
@@ -70,3 +99,47 @@ void vp_hid_key(uint32_t page, uint32_t usage, BOOL down) {
page, usage, down ? 1 : 0, 0);
if (ev) { send_hid_event(ev); CFRelease(ev); }
}
+
+// Build a display-integrated hand digitizer event carrying one finger and
+// dispatch it. Mirrors WebKit's HIDEventGenerator single-touch path.
+static void dispatch_digitizer(double x, double y, boolean_t range,
+ boolean_t touch, uint32_t mask) {
+ if (!pDigitizer || !pFinger || !pAppend || !pSetInt) return;
+
+ uint64_t ts = mach_absolute_time();
+ IOHIDEventRef parent = pDigitizer(kCFAllocatorDefault, ts, VP_TRANSDUCER_HAND,
+ 0, 0, mask, 0, x, y, 0, 0, 0, range, touch, 0);
+ if (!parent) return;
+ pSetInt(parent, VP_FIELD_IS_DISPLAY_INTEGRATED, 1);
+
+ IOHIDEventRef finger = pFinger(kCFAllocatorDefault, ts, 1, VP_TRANSDUCER_FINGER,
+ mask, x, y, 0, 0, 0, range, touch, 0);
+ if (finger) {
+ pSetInt(finger, VP_FIELD_IS_DISPLAY_INTEGRATED, 1);
+ pAppend(parent, finger, 0);
+ CFRelease(finger);
+ }
+
+ IOHIDEventRef strong = (IOHIDEventRef)CFRetain(parent);
+ dispatch_async(gHIDQueue, ^{
+ pSetSender(strong, 0x8000000817319372);
+ pDispatch(gClient, strong);
+ CFRelease(strong);
+ });
+ CFRelease(parent);
+}
+
+void vp_hid_touch(int phase, double x, double y) {
+ switch (phase) {
+ case 0: // down
+ dispatch_digitizer(x, y, 1, 1, VP_DIG_TOUCH | VP_DIG_IDENTITY);
+ break;
+ case 1: // move
+ dispatch_digitizer(x, y, 1, 1, VP_DIG_POSITION);
+ break;
+ case 3: // up
+ default:
+ dispatch_digitizer(x, y, 0, 0, VP_DIG_TOUCH | VP_DIG_IDENTITY);
+ break;
+ }
+}
diff --git a/sources/vphone-cli/VPhoneControl.swift b/sources/vphone-cli/VPhoneControl.swift
index 49c0a17d..47a4054d 100644
--- a/sources/vphone-cli/VPhoneControl.swift
+++ b/sources/vphone-cli/VPhoneControl.swift
@@ -28,6 +28,19 @@ class VPhoneControl {
private(set) var guestName = ""
private(set) var guestCaps: [String] = []
private(set) var guestIP: String?
+ /// Guest userland iOS version reported at handshake (e.g. "18.6.2"), if known.
+ private(set) var guestIOSVersion: String?
+
+ /// Whether touches should be injected guest-side via vphoned rather than the
+ /// VZ USB touchscreen. True for iOS 18 bases: on the 26.x kernel their USB
+ /// touchscreen dext receives reports but emits no digitizer events, so the
+ /// UI never sees touches. 26.x bases keep the native VZ multitouch path.
+ var useGuestTouchInjection: Bool {
+ guard isConnected, guestCaps.contains("touch"),
+ let major = guestIOSVersion.flatMap({ Int($0.split(separator: ".").first ?? "") })
+ else { return false }
+ return major < 26
+ }
/// Path to the signed vphoned binary. When set, enables auto-update.
var guestBinaryURL: URL?
@@ -201,6 +214,7 @@ class VPhoneControl {
let name = resp["name"] as? String ?? "unknown"
let caps = resp["caps"] as? [String] ?? []
let ip = resp["ip"] as? String
+ let iosVersion = resp["ios"] as? String
let needUpdate = resp["need_update"] as? Bool ?? false
Task { @MainActor in
@@ -216,9 +230,14 @@ class VPhoneControl {
self.guestName = name
self.guestCaps = caps
self.guestIP = ip
+ self.guestIOSVersion = iosVersion
self.isConnected = true
let ipSuffix = ip.map { " (\($0))" } ?? ""
- print("[control] connected to \(name) v\(version)\(ipSuffix), caps: \(caps)")
+ let iosSuffix = iosVersion.map { " iOS \($0)" } ?? ""
+ print("[control] connected to \(name) v\(version)\(ipSuffix)\(iosSuffix), caps: \(caps)")
+ if self.useGuestTouchInjection {
+ print("[control] guest-side touch injection enabled (iOS \(iosVersion ?? "?"))")
+ }
if needUpdate && self.variant != .less {
self.pushUpdate(fd: fd)
@@ -298,6 +317,24 @@ class VPhoneControl {
)
}
+ /// Inject a single-finger digitizer touch guest-side (bypasses VZ USB touch).
+ /// phase: 0 = down, 1 = move, 3 = up. x/y are normalized 0..1, top-left origin.
+ func sendTouch(phase: Int, x: Double, y: Double) {
+ nextRequestId += 1
+ let msg: [String: Any] = [
+ "v": Self.protocolVersion,
+ "t": "touch",
+ "id": String(nextRequestId, radix: 16),
+ "phase": phase,
+ "x": x,
+ "y": y,
+ ]
+ guard let fd = connection?.fileDescriptor, writeMessage(fd: fd, dict: msg) else {
+ print("[control] touch send failed (not connected)")
+ return
+ }
+ }
+
// MARK: - Developer Mode
struct DevModeStatus {
diff --git a/sources/vphone-cli/VPhoneVirtualMachineView.swift b/sources/vphone-cli/VPhoneVirtualMachineView.swift
index fd54b3c5..6d3f3f5e 100644
--- a/sources/vphone-cli/VPhoneVirtualMachineView.swift
+++ b/sources/vphone-cli/VPhoneVirtualMachineView.swift
@@ -253,12 +253,20 @@ class VPhoneVirtualMachineView: VZVirtualMachineView {
@discardableResult
private func sendTouchEvent(phase: Int, localPoint: NSPoint, timestamp: TimeInterval) -> Bool {
+ let normalizedPoint = normalizeCoordinate(localPoint)
+
+ // iOS 18 bases: the VZ USB touchscreen dext emits no digitizer events on
+ // the 26.x kernel, so route touches through vphoned's guest-side HID
+ // injection. 26.x bases fall through to the native VZ multitouch path.
+ if let control, control.useGuestTouchInjection {
+ control.sendTouch(phase: phase, x: Double(normalizedPoint.x), y: Double(normalizedPoint.y))
+ return true
+ }
+
guard let device = multiTouchDevice,
virtualMachine != nil
else { return false }
- let normalizedPoint = normalizeCoordinate(localPoint)
-
let touch = Dynamic._VZTouch(
view: self,
index: 0,