Run a virtual iPhone on an Apple Silicon Mac.
Swift
Ask before the VM window or ⌘Q stops a running machine
Jackya39ad4b99f5e
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstringsindex b0350384..1c716a11 100644--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings@@ -242,6 +242,76 @@}}},+ "Stop the Virtual Machine?" : {+ "extractionState" : "manual",+ "localizations" : {+ "en" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Stop the Virtual Machine?"+ }+ },+ "ja" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "仮想マシンを停止しますか?"+ }+ },+ "ko" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "가상 머신을 중지하시겠습니까?"+ }+ },+ "vi" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Dừng máy ảo?"+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "要停止虚拟机吗?"+ }+ }+ }+ },+ "Quitting turns the guest off at once, as pulling the power would. Anything not saved in the guest is lost." : {+ "extractionState" : "manual",+ "localizations" : {+ "en" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Quitting turns the guest off at once, as pulling the power would. Anything not saved in the guest is lost."+ }+ },+ "ja" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "終了すると、電源を抜いたときのようにゲストがすぐにオフになります。ゲストで保存していない内容は失われます。"+ }+ },+ "ko" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "종료하면 전원을 뽑은 것처럼 게스트가 즉시 꺼집니다. 게스트에서 저장하지 않은 내용은 모두 사라집니다."+ }+ },+ "vi" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Thoát sẽ tắt máy khách ngay lập tức, giống như rút nguồn. Mọi thứ chưa lưu trong máy khách sẽ bị mất."+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "退出会立即关闭客户机,如同拔掉电源。客户机中未存储的内容将会丢失。"+ }+ }+ }+ },"The virtual machine stopped and the keyboard setting was saved. Launch this machine again. Unable to restart: %@" : {"extractionState" : "manual","localizations" : {diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swiftindex e3174db0..e0a51d78 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneVirtualMachineWindowController.swift@@ -4,7 +4,7 @@ import Virtualizationimport VPhoneCoreKit@MainActor-class VPhoneVirtualMachineWindowController: NSObject {+class VPhoneVirtualMachineWindowController: NSObject, NSWindowDelegate {private var windowController: NSWindowController?private weak var control: VPhoneGuestControl?private weak var virtualMachineView: VPhoneVirtualMachineView?@@ -62,6 +62,7 @@ class VPhoneVirtualMachineWindowController: NSObject {)window.isReleasedWhenClosed = false+ window.delegate = selfwindow.level = .normalVPhoneAlert.hostWindow = windowwindow.contentAspectRatio = windowSize@@ -131,6 +132,16 @@ class VPhoneVirtualMachineWindowController: NSObject {setFrameRateDisplay(VPhoneFrameRateDisplay.isEnabled)}+ // MARK: - Close++ /// Closing the display window stops the VM, so the close button asks the+ /// same question as ⌘Q, with the window still up, instead of closing+ /// first. `closeForRestart` closes without asking.+ func windowShouldClose(_: NSWindow) -> Bool {+ NSApp.terminate(nil)+ return false+ }+/// Disconnect the old display and input devices before rebuilding the VM.func closeForRestart() {timers.forEach { $0.invalidate() }diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swiftindex 29a10585..9c146695 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift@@ -22,6 +22,10 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {private var sigintSource: DispatchSourceSignal?private var didAttemptAutoInstall = falseprivate var isRestartingVirtualMachine = false+ /// Set when the stop was asked for without the window or ⌘Q: a SIGINT+ /// from `vm stop` or Launchpad, which must not wait for an answer.+ private var stopConfirmed = false+ private var isConfirmingStop = falseinit(command: VPhoneBootCommand) {self.command = command@@ -42,9 +46,15 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {signal(SIGINT, SIG_IGN)let src = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)- src.setEventHandler {+ src.setEventHandler { [weak self] inprint("\n[vphone] SIGINT — shutting down")- NSApp.terminate(nil)+ self?.stopConfirmed = true+ if self?.isConfirmingStop == true {+ // A quit is waiting on the question; this answers it.+ NSApp.reply(toApplicationShouldTerminate: true)+ } else {+ NSApp.terminate(nil)+ }}src.activate()sigintSource = src@@ -478,6 +488,25 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {stopControlServices()}+ /// Quitting turns the guest off at once, so a quit from the window or ⌘Q+ /// asks first while the VM runs. A SIGINT stop, a headless VM and a VM+ /// that never started quit straight away.+ func applicationShouldTerminate(_: NSApplication) -> NSApplication.TerminateReply {+ guard !command.noGraphics, !stopConfirmed, vm != nil else { return .terminateNow }+ guard !isConfirmingStop else { return .terminateCancel }+ isConfirmingStop = true+ VPhoneAlert.present(+ title: "Stop the Virtual Machine?",+ message: "Quitting turns the guest off at once, as pulling the power would. Anything not saved in the guest is lost.",+ style: .warning,+ buttons: ["Stop", "Cancel"],+ ) { [weak self] response in+ self?.isConfirmingStop = false+ NSApp.reply(toApplicationShouldTerminate: response == .alertFirstButtonReturn)+ }+ return .terminateLater+ }+func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {!command.noGraphics && !isRestartingVirtualMachine}