Run a virtual iPhone on an Apple Silicon Mac.
Swift
Let no stored ProductIDOverride decide, and store ours from vphoned
Jackye6c01aa05d21
diff --git a/Research/Guest/virtio_sound.md b/Research/Guest/virtio_sound.mdindex 8f9fc3ad..c8918cf5 100644--- a/Research/Guest/virtio_sound.md+++ b/Research/Guest/virtio_sound.md@@ -278,13 +278,12 @@ never do.### The fix-> **Superseded (2026-10-03).** Do not store this value. The plugin now-> supplies `ProductIDOverride = 8010` in-process on an iPad guest when nothing-> is stored (§6, "Nothing left to set by hand"), and a stored value wins over-> it. 198 has no ringtone-preview category, so with it tones stay silent. A-> guest that still carries a stored 198 should have it deleted:-> `settings.delete` with domain `com.apple.audio.virtualaudio`, key-> `ProductIDOverride`. What follows is how the override was found.+> **Superseded (2026-10-03).** Do not store this value. The plugin supplies+> `ProductIDOverride = 8010` in-process on an iPad or iPhone guest and vphoned+> stores the same (§6, "Nothing left to set by hand"). 198 has no+> ringtone-preview category, so with it tones stay silent; a guest that still+> carries a stored 198 needs nothing done, the stored value gives way to+> both. What follows is how the override was found.```vphone-launchpad-cli guest rpc <machine> settings.set \@@ -494,17 +493,37 @@ guest gets neither, so it has the whole speaker route or none of it):* the device UID is `PuffinOutput` unless `VPhoneVirtIOSoundDeviceUID` namesanother. Elsewhere the default stays `VPhoneVirtIOSound:0`, whichVirtualAudio leaves unclaimed;-* `ProductIDOverride` is set to 8010 in the `com.apple.audio.virtualaudio`- domain from `halInitializeWithPluginHost:` when nothing stored names one.- The plugin loads before VirtualAudio reads its defaults, and both are in- audiomxd, so the in-process value is the one it reads; audiomxd's sandbox- keeps the write off disk (`settings.get` still shows the domain empty),- which is why it is repeated every launch. Each launch logs one line after- `=== plugin load ===` saying what was done and whether VirtualAudio was- already mapped: `ProductIDOverride on 'iPhone99,11': unset, set to 8010 for- this launch; VirtualAudio not loaded yet`. 8010 rather than §4's 198- because 198's category map has no ringtone-preview entry- (`virtualaudio_speaker_route_throws.md`, layer 1).+* `ProductIDOverride` in the `com.apple.audio.virtualaudio` domain is 8010,+ from two places. The plugin sets it from `halInitializeWithPluginHost:` on+ every launch that does not find 8010 there. The plugin loads before+ VirtualAudio reads its defaults, and both are in audiomxd, so the+ in-process value is the one it reads; audiomxd's sandbox keeps the write+ off disk, which is why it is repeated every launch. Each launch logs one+ line after `=== plugin load ===` saying what was done and whether+ VirtualAudio was already mapped: `ProductIDOverride on 'iPhone99,11':+ unset, set to 8010 for this launch; VirtualAudio not loaded yet`. Nothing+ guarantees that order, so vphoned stores the same value when it starts+ (`VPhoneDaemon/Daemon/GuestVirtualAudioProduct.swift`, only on a guest that+ has the plugin): a stored value needs no order, and every later launch of+ audiomxd finds it. 8010 rather than §4's 198 because 198's category map+ has no ringtone-preview entry (`virtualaudio_speaker_route_throws.md`,+ layer 1).+* A value already stored gives way to both, so a guest that kept the 198 of+ §4 needs nothing deleted. `VPhoneVirtIOSoundProductID` in the plugin's+ settings (`com.apple.coreaudio`) names another ID for the plugin and+ vphoned alike; 0 there leaves VirtualAudio's key alone.++Measured on a new iPad16,1 / 26.6.2 guest (`avtest-ipad`, 2026-10-04):++| State | `vpquery.log` | audiomxd | `settings.get` |+| --- | --- | --- | --- |+| first boot, nothing set | `stored 8010, nothing to do; VirtualAudio not loaded yet` (vphoned was ahead of audiomxd) | — | 8010 |+| 198 stored by `settings.set`, audiomxd restarted | `stored 198, set to 8010 for this launch; VirtualAudio not loaded yet` | `Defaults key ProductIDOverride was defined to 8010`, `VA Init Status: 0` | 198 |+| the same guest restarted | `stored 8010, nothing to do; VirtualAudio not loaded yet` | — | 8010 |++What is left of the order: the first launch of audiomxd on a guest where+vphoned has not stored the value yet, which is the plugin's in-process set+alone, as before.Verified on an iPad16,1 / 26.6.2 guest with all the keys deleted and rebooted:VirtualAudio initializes, the boot chime and a ringtone preview start I/O on@@ -752,8 +771,8 @@ service.3. VirtualAudio initialized: `audiomxd` logs `Defaults key ProductIDOverridewas defined to 8010`, `PlugIn initialized ? 2` / `VA Init Status: 0`, no`PRECONDITION FAILURE`, and no `VirtualAudio PlugIn is not initialized yet`- when an app opens a session. The plugin supplies the override on an iPad- guest (§6); `vpquery.log` says so when it had to.+ when an app opens a session. The plugin and vphoned supply the override+ (§6); `vpquery.log` says what the plugin found and did.4. Sound on the host: audible, and still audible after a stop. Beyond theplugin's own defaults (§6) the speaker route needs the VirtualAudio patches(`virtualaudio_speaker_route_throws.md`). In `vpquery.log`, every stop —diff --git a/Research/vphoned_http_api.md b/Research/vphoned_http_api.mdindex 5074c5c0..5b3e1d93 100644--- a/Research/vphoned_http_api.md+++ b/Research/vphoned_http_api.md@@ -309,6 +309,14 @@ the answers they read, so the field is true from that removal until the guestrestarts, also across a vphoned restart in the same boot; vphoned does notrestart the guest itself. See `Research/Guest/virtio_sound.md` §7.+At startup vphoned also stores `ProductIDOverride` 8010 in+`com.apple.audio.virtualaudio` for user mobile, on an iPad or iPhone guest+that has the sound plugin (`VPhoneDaemon/Daemon/GuestVirtualAudioProduct.swift`).+VirtualAudio reads the key before it derives a ProductID, and the one it+derives on a VM never initializes. A value already stored is replaced;+`VPhoneVirtIOSoundProductID` in `com.apple.coreaudio` names another ID, and 0+there leaves the key alone. See `Research/Guest/virtio_sound.md` §6.+`screen.unlock` reads the lock state, then turns the display on with`SBSUndimScreen` (no toggle, unlike a power press) and presses Home to dismissthe Lock Screen: a passcode-free guest goes to the Home Screen, a guest with adiff --git a/VPhoneDaemon/Daemon/GuestVirtualAudioProduct.swift b/VPhoneDaemon/Daemon/GuestVirtualAudioProduct.swiftnew file mode 100644index 00000000..2d18f021--- /dev/null+++ b/VPhoneDaemon/Daemon/GuestVirtualAudioProduct.swift@@ -0,0 +1,70 @@+import Darwin+import Foundation+import IcliKit++/// Stores the ProductID VirtualAudio should route this guest with.+///+/// VirtualAudio reads `ProductIDOverride` in `com.apple.audio.virtualaudio`+/// before it derives a ProductID of its own, and the one it derives on a VM+/// never initializes: no sound anywhere. The sound plugin sets the key inside+/// audiomxd on every launch, which works as long as the plugin initializes+/// before VirtualAudio reads its defaults; audiomxd's sandbox keeps that write+/// from reaching disk. Nothing guarantees the order. A stored value needs+/// none, so vphoned stores the same one here, at startup: every launch of+/// audiomxd after this finds it whichever plugin comes first.+///+/// A value already stored gives way. The earlier recipe had 198 set by hand,+/// and a guest that kept it dropped every tone. Someone who wants another ID+/// sets `VPhoneVirtIOSoundProductID` in the plugin's settings instead, which+/// the plugin reads too; 0 there leaves VirtualAudio's key alone.+///+/// Only on a guest that has the plugin: the ID selects a speaker route, and+/// without the plugin there is no device for it.+///+/// See Research/Guest/virtio_sound.md §6.+enum GuestVirtualAudioProduct {+ private static let plugin = "/System/Library/Audio/Plug-Ins/HAL/VPhoneVirtIOSound.driver"+ private static let domain = "com.apple.audio.virtualaudio"+ private static let key = "ProductIDOverride"+ private static let settingsDomain = "com.apple.coreaudio"+ private static let settingsKey = "VPhoneVirtIOSoundProductID"++ static func storeOnStartup() {+ guard FileManager.default.fileExists(atPath: plugin) else { return }+ let product = product(machine: machine(), chosen: integer(settingsKey, in: settingsDomain))+ guard product != 0 else { return }+ let stored = integer(key, in: domain)+ guard stored != product else { return }+ do {+ _ = try writePreference(domain: domain, key: key, value: .int(Int64(product)))+ NSLog(+ "vphoned: VirtualAudio ProductIDOverride was %@, stored %d; audiomxd reads it at its next launch",+ stored.map(String.init) ?? "unset", product,+ )+ } catch {+ NSLog("vphoned: VirtualAudio ProductIDOverride: %@", String(describing: error))+ }+ }++ /// The same answer as the plugin's `VPGuestProductID`: 8010 on an iPad or+ /// iPhone guest, 0 (none) on anything else, unless one was chosen.+ static func product(machine: String, chosen: Int?) -> Int {+ if let chosen, chosen >= 0 { return chosen }+ return machine.hasPrefix("iPad") || machine.hasPrefix("iPhone") ? 8010 : 0+ }++ // MARK: - Helpers++ private static func integer(_ key: String, in domain: String) -> Int? {+ let value = CFPreferencesCopyValue(key as CFString, domain as CFString, "mobile" as CFString, kCFPreferencesAnyHost)+ return (value as? NSNumber)?.intValue+ }++ private static func machine() -> String {+ var size = 0+ guard sysctlbyname("hw.machine", nil, &size, nil, 0) == 0, size > 0 else { return "" }+ var bytes = [CChar](repeating: 0, count: size + 1)+ guard sysctlbyname("hw.machine", &bytes, &size, nil, 0) == 0 else { return "" }+ return String(cString: bytes)+ }+}diff --git a/VPhoneDaemon/Daemon/main.swift b/VPhoneDaemon/Daemon/main.swiftindex 0c948e15..ce75466e 100644--- a/VPhoneDaemon/Daemon/main.swift+++ b/VPhoneDaemon/Daemon/main.swift@@ -17,6 +17,8 @@ guard vp_native_watch_proxy() == 0 else { exit(1) }GuestLockScreenIdle.startOnStartup()// Before the server, so `/v1/health` reports a drop from its first answer.GuestMobileGestaltCache.dropIfStaleOnStartup()+// Before audiomxd is asked for sound again; see the type for what it cannot cover.+GuestVirtualAudioProduct.storeOnStartup()vp_vcam_start()// Multi-finger injection is resolved here so the first pinch is not the call// that pays for `dlopen`; a base without the symbols logs and stays a no-op.diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.mindex c43cc35b..3014ac5d 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m@@ -27,6 +27,9 @@// Mic" on an iPad or iPhone guest,// "VPhoneVirtIOSoundInput:0" elsewhere)// VPhoneVirtIOSoundNominalRate 44100 to start at the alternate rate+// VPhoneVirtIOSoundProductID the ProductID VirtualAudio is given+// (default 8010 on an iPad or iPhone+// guest); 0 leaves VirtualAudio's own// VPhoneVirtIOSoundLeadPeriods periods of silence queued ahead of the// mix at each start, 0 to 8@@ -242,9 +245,22 @@ static const char *VPMachine(void) {/// node's acoustic ID, and plays Safari, but a tone's route change dies there/// as it does with 198 on an iPad. Both defaults below key on this one/// answer, so a guest gets either the whole route or none of it.+///+/// `VPhoneVirtIOSoundProductID` in the plugin's settings replaces the answer,+/// for trying another ID; 0 there leaves VirtualAudio to itself. vphoned+/// reads the same key (`GuestVirtualAudioProduct`).static int VPGuestProductID(void) {- const char *machine = VPMachine();- return strncmp(machine, "iPad", 4) == 0 || strncmp(machine, "iPhone", 6) == 0 ? 8010 : 0;+ static int product;+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ const char *machine = VPMachine();+ product = strncmp(machine, "iPad", 4) == 0 || strncmp(machine, "iPhone", 6) == 0 ? 8010 : 0;+ id chosen = CFBridgingRelease(CFPreferencesCopyAppValue(CFSTR("VPhoneVirtIOSoundProductID"), kSettingsDomain));+ if ([chosen isKindOfClass:NSNumber.class] && [chosen intValue] >= 0) {+ product = [chosen intValue];+ }+ });+ return product;}static NSString *VPDeviceUID(ASDStreamDirection direction, unsigned index) {@@ -273,32 +289,40 @@ static NSString *VPDeviceUID(ASDStreamDirection direction, unsigned index) {/// Gives VirtualAudio `VPGuestProductID()` through its own defaults key,/// `ProductIDOverride` in `com.apple.audio.virtualaudio`, which it reads-/// before deriving one. This runs in audiomxd before VirtualAudio reads its-/// defaults, so the key is set here, in the process, on every launch that-/// finds none: audiomxd's sandbox keeps the write from reaching disk, and it-/// does not need to. A value someone stored (`settings.set`) is their choice-/// and stays.+/// before deriving one. The key is set here, in the process, on every launch+/// that does not find that value in it: audiomxd's sandbox keeps the write+/// from reaching disk. Whatever is stored gives way, so a guest that still+/// has 198 from the earlier recipe plays tones without anyone deleting it;+/// another ID is chosen with `VPhoneVirtIOSoundProductID`.///-/// Nothing guarantees that ordering, so the one line logged says what was-/// done and whether VirtualAudio was already mapped into audiomxd: one that-/// was not cannot have read its defaults yet; one that was may have.+/// Set in the process, the value reaches VirtualAudio only when this plugin+/// initializes before VirtualAudio reads its defaults. That has been the+/// order on every launch looked at, and nothing guarantees it, so vphoned+/// stores the same value where every later launch of audiomxd finds it+/// whatever the order (`GuestVirtualAudioProduct`). The one line logged says+/// what was done and whether VirtualAudio was already mapped into audiomxd:+/// one that was not cannot have read its defaults yet; one that was may have.static void VPEnsureVirtualAudioProduct(void) {BOOL virtualAudioLoaded = VPImageLoaded("/VirtualAudio.plugin/");NSString *outcome;int product = VPGuestProductID();if (product == 0) {- outcome = @"no ProductID for this guest, left unset";+ outcome = @"no ProductID for this guest, left as is";} else {CFStringRef domain = CFSTR("com.apple.audio.virtualaudio");CFStringRef key = CFSTR("ProductIDOverride");id existing = CFBridgingRelease(CFPreferencesCopyAppValue(key, domain));- if (existing) {- outcome = [NSString stringWithFormat:@"stored %@, left as is", existing];+ if ([existing isKindOfClass:NSNumber.class] && [existing intValue] == product) {+ outcome = [NSString stringWithFormat:@"stored %d, nothing to do", product];} else {CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &product);CFPreferencesSetAppValue(key, value, domain);CFRelease(value);- outcome = [NSString stringWithFormat:@"unset, set to %d for this launch", product];+ id now = CFBridgingRelease(CFPreferencesCopyAppValue(key, domain));+ outcome = [NSString stringWithFormat:@"%@, %@ %d for this launch",+ existing ? [NSString stringWithFormat:@"stored %@", existing] : @"unset",+ [now isKindOfClass:NSNumber.class] && [now intValue] == product ? @"set to" : @"could not be set to",+ product];}}const char *loaded = virtualAudioLoaded ? "already loaded" : "not loaded yet";