Run a virtual iPhone on an Apple Silicon Mac.
Swift
Offer Apple TV guests in Launchpad's New Machine sheet
Jackyaf9a4e67daf7
diff --git a/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift b/VPhoneLaunchpad/Tests/MachineProductTypeTests.swiftindex 8edd42f6..18905671 100644--- a/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift+++ b/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift@@ -30,6 +30,9 @@ struct MachineProductTypeTests {let phone = root.appendingPathComponent("research-01", isDirectory: true)try manager.createDirectory(at: phone.appendingPathComponent("FirmwareOriginals/iPhone17,3_27.0_24A435_Restore"), withIntermediateDirectories: true)expect(VPhoneLaunchpadMachine.guestProductType(in: phone) == "iPhone17,3", "iPhone restore tree")+ let tv = root.appendingPathComponent("tv-01", isDirectory: true)+ try manager.createDirectory(at: tv.appendingPathComponent("FirmwareOriginals/iPhoneOS_AppleTV5,3_26.6_23L773_Restore"), withIntermediateDirectories: true)+ expect(VPhoneLaunchpadMachine.guestProductType(in: tv) == "AppleTV5,3", "Apple TV restore tree")expect(VPhoneLaunchpadMachine.guestProductType(in: root.appendingPathComponent("missing")) == nil, "nothing to read")// restore-info.json's device is the restore identity's, not the guest's.@@ -42,6 +45,8 @@ struct MachineProductTypeTests {expect(machine.osName == "iOS" && !machine.isPad, "unknown type reads as iOS")machine.guestProductType = VPhoneLaunchpadMachine.guestProductType(in: pad)expect(machine.isPad && machine.osName == "iPadOS", "iPad guest")+ machine.guestProductType = VPhoneLaunchpadMachine.guestProductType(in: tv)+ expect(machine.isAppleTV && !machine.isPad && machine.osName == "tvOS", "Apple TV guest")print("Machine product type tests passed: config, restore trees, OS name")}}diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Control/VPhoneLaunchpadControlCommands.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Control/VPhoneLaunchpadControlCommands.swiftindex 211f8c59..a3cf4fa6 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Control/VPhoneLaunchpadControlCommands.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Control/VPhoneLaunchpadControlCommands.swift@@ -585,9 +585,27 @@ struct VPhoneLaunchpadControlCommands {}return value}- let (patches, guestPatches) = try await Self.patches(request, commandLine: commandLine, emit: emit)- let usesTemplate = !request.flag("no-template")- let slimming = try Self.slimming(request, usesTemplate: usesTemplate)+ // An Apple TV is restored on its own with the tvOS preset: templates+ // and slimming drive iOS Setup Assistant and its apps.+ let isAppleTV = device?.hasPrefix("AppleTV") == true+ let (patches, guestPatches) = try await Self.patches(+ request,+ commandLine: commandLine,+ defaultPreset: VPhoneLaunchpadPatchSelection.defaultPreset(forDevice: device),+ emit: emit,+ )+ let usesTemplate = !request.flag("no-template") && !isAppleTV+ let slimming: VPhoneLaunchpadSlimming+ if isAppleTV {+ if !Self.slimmingSwitches(request).isEmpty {+ throw VPhoneLaunchpadError("An Apple TV is created without a template; the slimming switches do not apply.")+ }+ var off = VPhoneLaunchpadSlimming()+ off.slim = false+ slimming = off+ } else {+ slimming = try Self.slimming(request, usesTemplate: usesTemplate)+ }let options = try VPhoneLaunchpadCreationPipeline.Options(name: machine.name,libraryRoot: machine.libraryRoot,@@ -616,12 +634,11 @@ struct VPhoneLaunchpadControlCommands {private static func patches(_ request: VPhoneLaunchpadControlRequest,commandLine: VPhoneLaunchpadCommandLine,+ defaultPreset: String,emit: Emit,) async throws -> (VPhoneLaunchpadPatchSelection, guest: Set<String>) {var selection = VPhoneLaunchpadPatchSelection()- if let preset = request.option("preset") {- selection.preset = preset- }+ selection.preset = request.option("preset") ?? defaultPresetlet block = request.values("block")let allow = request.values("allow")guard !block.isEmpty || !allow.isEmpty else {@@ -658,6 +675,12 @@ struct VPhoneLaunchpadControlCommands {return (selection, overrides.guestPatches)}+ /// The slimming switches the request names.+ private static func slimmingSwitches(_ request: VPhoneLaunchpadControlRequest) -> [String] {+ ["slim", "trim", "keep-languages", "service-profile", "remove-apps", "keep-apps"].filter { request.option($0) != nil }+ + (request.flag("accounts-off") ? ["accounts-off"] : [])+ }+/// The template switches, refused where `vphone-cli vm create` refuses/// them: with `--no-template`, after `--slim off`, `--accounts-off`/// without the trimmed profile, an app outside the removable list.@@ -672,8 +695,7 @@ struct VPhoneLaunchpadControlCommands {default: throw VPhoneLaunchpadError("--\(option) takes on or off, not \(value).")}}- let parts = ["trim", "keep-languages", "service-profile", "remove-apps", "keep-apps"].filter { request.option($0) != nil }- + (request.flag("accounts-off") ? ["accounts-off"] : [])+ let parts = slimmingSwitches(request).filter { $0 != "slim" }if !usesTemplate, !parts.isEmpty || request.option("slim") != nil {throw VPhoneLaunchpadError("The slimming switches shape a template; --no-template creates the machine without one.")}diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstringsindex 2160406c..7f202c09 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings@@ -1,6 +1,24 @@{"sourceLanguage" : "en","strings" : {+ "An Apple TV is restored on its own, with keys of its own. Templates are made for iPhone and iPad." : {+ "localizations" : {+ "en" : { "stringUnit" : { "state" : "translated", "value" : "An Apple TV is restored on its own, with keys of its own. Templates are made for iPhone and iPad." } },+ "ja" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV は独自のキーで個別に復元されます。テンプレートは iPhone と iPad 用です。" } },+ "ko" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV는 자체 키로 개별 복원됩니다. 템플릿은 iPhone과 iPad용입니다." } },+ "vi" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV được khôi phục riêng, với khóa của riêng nó. Mẫu dành cho iPhone và iPad." } },+ "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV 单独恢复,使用自己的密钥。模板只用于 iPhone 和 iPad。" } }+ }+ },+ "An Apple TV keeps every file, service and app." : {+ "localizations" : {+ "en" : { "stringUnit" : { "state" : "translated", "value" : "An Apple TV keeps every file, service and app." } },+ "ja" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV はすべてのファイル、サービス、App を保持します。" } },+ "ko" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV는 모든 파일, 서비스, 앱을 유지합니다." } },+ "vi" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV giữ lại mọi tệp, dịch vụ và ứng dụng." } },+ "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "Apple TV 会保留所有文件、服务和 App。" } }+ }+ },"%@ Guest System" : {"localizations" : {"en" : {diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swiftindex a7c0bdb6..20a0a851 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift@@ -145,14 +145,20 @@ nonisolated struct VPhoneLaunchpadMachine: Decodable, Hashable, Identifiable, SeguestProductType?.hasPrefix("iPad") == true}- /// The guest OS's name: iPadOS on an iPad.+ /// True for an Apple TV guest.+ var isAppleTV: Bool {+ guestProductType?.hasPrefix("AppleTV") == true+ }++ /// The guest OS's name: iPadOS on an iPad, tvOS on an Apple TV.var osName: String {- isPad ? "iPadOS" : "iOS"+ isAppleTV ? "tvOS" : isPad ? "iPadOS" : "iOS"}/// config.plist's `guestProductType`, else the product type in the name/// of the restore tree `fw prepare` keeps in `FirmwareOriginals`- /// (`iPhone17,3_…_Restore`, or `iPhoneOS_iPad16,1_…_Restore` for an iPad),+ /// (`iPhone17,3_…_Restore`, or `iPhoneOS_iPad16,1_…_Restore` for an iPad+ /// and `iPhoneOS_AppleTV5,3_…_Restore` for an Apple TV),/// for configs written before the key.static func guestProductType(in folder: URL) -> String? {let config = NSDictionary(contentsOf: folder.appendingPathComponent("config.plist"))@@ -162,7 +168,7 @@ nonisolated struct VPhoneLaunchpadMachine: Decodable, Hashable, Identifiable, Selet originals = folder.appendingPathComponent("FirmwareOriginals", isDirectory: true)let names = (try? FileManager.default.contentsOfDirectory(atPath: originals.path)) ?? []for name in names.sorted() where name.hasSuffix("_Restore") {- if let type = name.split(separator: "_").first(where: { $0.wholeMatch(of: /(iPhone|iPad)\d+,\d+/) != nil }) {+ if let type = name.split(separator: "_").first(where: { $0.wholeMatch(of: /(iPhone|iPad|AppleTV)\d+,\d+/) != nil }) {return String(type)}}@@ -243,7 +249,7 @@ nonisolated struct VPhoneLaunchpadFirmwareCatalog: Decodable, Sendable {/// The product type `fw prepare --device` takes.let productType: Stringlet name: String- /// `iPhone` or `iPad`.+ /// `iPhone`, `iPad` or `appleTV`.let family: Stringlet pairings: [Pairing]@@ -255,6 +261,15 @@ nonisolated struct VPhoneLaunchpadFirmwareCatalog: Decodable, Sendable {family == "iPad"}+ var isAppleTV: Bool {+ family == "appleTV"+ }++ /// The system its IPSWs install.+ var osName: String {+ isAppleTV ? "tvOS" : isPad ? "iPadOS" : "iOS"+ }+/// The name with its product type, `iPhone 16 (iPhone17,3)`: Launchpad/// showed only the product type before, which reads as iPhone 17.var detailedName: String {diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swiftindex 4363b5f3..4e72f7ea 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift@@ -198,7 +198,7 @@ struct VPhoneLaunchpadNewMachineView: View {// Off when there is no template to clone; it can still be turned on// to build one..onChange(of: templateAvailable, initial: true) {- if let templateAvailable {+ if let templateAvailable, !isAppleTVGuest {usesTemplate = templateAvailable}}@@ -240,9 +240,9 @@ struct VPhoneLaunchpadNewMachineView: View {hardwarePageadvancedSections(.hardware)case .template:- VPhoneLaunchpadSlimmingSections(part: .template, usesTemplate: $usesTemplate, templateAvailable: templateAvailable, slimming: $slimming)+ VPhoneLaunchpadSlimmingSections(part: .template, usesTemplate: $usesTemplate, templateAvailable: templateAvailable, slimming: $slimming, forAppleTV: isAppleTVGuest)case .slimming:- VPhoneLaunchpadSlimmingSections(part: .slimming, usesTemplate: $usesTemplate, templateAvailable: templateAvailable, slimming: $slimming)+ VPhoneLaunchpadSlimmingSections(part: .slimming, usesTemplate: $usesTemplate, templateAvailable: templateAvailable, slimming: $slimming, forAppleTV: isAppleTVGuest)case .firmware:bundleSectionfirmwareSourceSection@@ -316,7 +316,7 @@ struct VPhoneLaunchpadNewMachineView: View {// The count is for choosing; the closed menu names the device.Text(verbatim: selectedGuest?.name ?? "")}- Picker(selectedGuest?.isPad == true ? "iPadOS" : "iOS", selection: $pairing) {+ Picker(LocalizedStringKey(selectedGuest?.osName ?? "iOS"), selection: $pairing) {ForEach((selectedGuest?.pairings ?? []).reversed()) { pairing inlet title = "\(pairing.ios.name) (\(pairing.build))"Text(verbatim: isDownloaded(pairing) ? "\(title) ✓" : title).tag(Optional(pairing.id))@@ -677,8 +677,29 @@ struct VPhoneLaunchpadNewMachineView: View {/// Select a guest device and its newest release.private func choose(_ productType: String?) {+ let wasAppleTV = isAppleTVGuest+ let wasGuest = guestguest = productTypepairing = selectedGuest?.defaultPairing?.id+ // Each Apple TV starts from a preset of its own.+ guard isAppleTVGuest != wasAppleTV || isAppleTVGuest && wasGuest != productType else { return }+ // An Apple TV guest is restored on its own with a tvOS preset:+ // templates and slimming drive iOS Setup Assistant and its apps.+ patches = VPhoneLaunchpadPatchSelection()+ if isAppleTVGuest {+ patches.preset = VPhoneLaunchpadPatchSelection.defaultPreset(forDevice: productType)+ usesTemplate = false+ slimming.slim = false+ } else {+ usesTemplate = templateAvailable ?? true+ slimming = VPhoneLaunchpadSlimming()+ }+ Task { await loadPatchCatalog() }+ }++ /// An Apple TV from the catalog, which is built without a template.+ private var isAppleTVGuest: Bool {+ !usesCustomSources && selectedGuest?.isAppleTV == true}/// Read again whenever the preset or the version changes: `inPreset`, which@@ -726,8 +747,9 @@ struct VPhoneLaunchpadNewMachineView: View {bundleVersion: bundleVersion,iphoneSource: iphone,cloudOSSource: cloudOS,- // An iPad IPSW often covers two sizes; name the one chosen.- device: usesCustomSources ? nil : selectedGuest.flatMap { $0.isPad ? $0.productType : nil },+ // An iPad IPSW often covers two sizes; name the one chosen. An+ // Apple TV is named too, so the restore tree and log say which.+ device: usesCustomSources ? nil : selectedGuest.flatMap { $0.isPad || $0.isAppleTV ? $0.productType : nil },cpuCount: cpu,memoryMB: memoryMB,diskSizeGB: diskSizeGB,diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swiftindex 8e89cde6..0f8e270f 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift@@ -275,6 +275,24 @@ nonisolated struct VPhoneLaunchpadPatchSelection: Hashable, Sendable {/// only add noise, so the pipeline omits the flag for this one value.static let defaultPreset = "standard"+ /// The preset an Apple TV guest gets, mirroring+ /// `VPhonePatchPreset.tvOSIdentifier`.+ static let tvOSPreset = "tvos"++ /// The preset an Apple TV 4K guest gets, mirroring+ /// `VPhonePatchPreset.tvOS4KIdentifier`: its arm64e userland comes from+ /// the OTA, with the paravirtual GPU driver.+ static let tvOS4KPreset = "tvos-4k"++ /// The preset a new machine of `productType` starts from.+ static func defaultPreset(forDevice productType: String?) -> String {+ switch productType {+ case "AppleTV14,1": tvOS4KPreset+ case let type? where type.hasPrefix("AppleTV"): tvOSPreset+ default: defaultPreset+ }+ }+var preset = defaultPreset/// Patches the preset turns on that this VM leaves off.var blocked: Set<String> = []diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadSlimmingView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadSlimmingView.swiftindex c719e5bf..94eda07c 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadSlimmingView.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadSlimmingView.swift@@ -15,6 +15,9 @@ struct VPhoneLaunchpadSlimmingSections: View {/// IPSWs, which the list cannot be matched against.var templateAvailable: Bool?@Binding var slimming: VPhoneLaunchpadSlimming+ /// An Apple TV is restored on its own and not slimmed: templates and+ /// slimming drive iOS Setup Assistant and its apps.+ var forAppleTV = false@State private var choosesApps = falsevar body: some View {@@ -24,8 +27,12 @@ struct VPhoneLaunchpadSlimmingSections: View {case .slimming:Section {Toggle("Slim System", isOn: $slimming.slim)+ .disabled(forAppleTV)} footer: {- if !usesTemplate, slimming.slim {+ if forAppleTV {+ Text("An Apple TV keeps every file, service and app.")+ .foregroundStyle(.secondary)+ } else if !usesTemplate, slimming.slim {Text("Without a template the machine is slimmed on its own, with a setup boot of its own. Setup Assistant is skipped.").foregroundStyle(.secondary)}@@ -38,20 +45,25 @@ struct VPhoneLaunchpadSlimmingSections: View {private var templateSection: some View {Section {Toggle("Use Template", isOn: $usesTemplate)+ .disabled(forAppleTV)} header: {Text("Template")} footer: {VStack(alignment: .leading, spacing: 4) {- if templateAvailable == false {- Text("No template is available for this firmware. Turning this on saves a sample of the machine in the template library.")- }- Text(usesTemplate- ? "Machines from one template share its SEP root secret and Data volume keys. Turn this off for a machine that needs keys of its own."- : slimming.slim- ? "The machine is restored on its own, with keys of its own."- : "The machine is restored on its own, with keys of its own. It starts at Setup Assistant.")- if usesTemplate, !slimming.slim {- Text("Slim System is off, so the template keeps every file, service and app.")+ if forAppleTV {+ Text("An Apple TV is restored on its own, with keys of its own. Templates are made for iPhone and iPad.")+ } else {+ if templateAvailable == false {+ Text("No template is available for this firmware. Turning this on saves a sample of the machine in the template library.")+ }+ Text(usesTemplate+ ? "Machines from one template share its SEP root secret and Data volume keys. Turn this off for a machine that needs keys of its own."+ : slimming.slim+ ? "The machine is restored on its own, with keys of its own."+ : "The machine is restored on its own, with keys of its own. It starts at Setup Assistant.")+ if usesTemplate, !slimming.slim {+ Text("Slim System is off, so the template keeps every file, service and app.")+ }}}.foregroundStyle(.secondary)