Validate Suspendability (#797)

And show "Suspend" menu item based on `--suspnedable` flag

Fixes #796
This commit is contained in:
Fedor Korotkov 2024-04-22 12:07:01 +02:00 committed by GitHub
parent 8bc2e99f63
commit c6e99345cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 5 deletions

View File

@ -164,6 +164,10 @@ struct Run: AsyncParsableCommand {
}
if suspendable {
let config = try VMConfig.init(fromURL: vmDir.configURL)
if !(config.platform is PlatformSuspendable) {
throw ValidationError("You can only suspend macOS VMs")
}
if dir.count > 0 {
throw ValidationError("Suspending VMs with shared directories is not supported")
}
@ -557,7 +561,7 @@ struct Run: AsyncParsableCommand {
nsApp.activate(ignoringOtherApps: true)
struct MainApp: App {
static var disappearSignal: Int32 = SIGINT
static var suspendable: Bool = false
static var capturesSystemKeys: Bool = false
@NSApplicationDelegateAdaptor private var appDelegate: MinimalMenuAppDelegate
@ -568,7 +572,7 @@ struct Run: AsyncParsableCommand {
VMView(vm: vm!, capturesSystemKeys: MainApp.capturesSystemKeys).onAppear {
NSWindow.allowsAutomaticWindowTabbing = false
}.onDisappear {
let ret = kill(getpid(), MainApp.disappearSignal)
let ret = kill(getpid(), MainApp.suspendable ? SIGUSR1 : SIGINT)
if ret != 0 {
// Fallback to the old termination method that doesn't
// propagate the cancellation to Task's in case graceful
@ -605,8 +609,10 @@ struct Run: AsyncParsableCommand {
Task { try vm!.virtualMachine.requestStop() }
}
if #available(macOS 14, *) {
Button("Suspend") {
kill(getpid(), SIGUSR1)
if (MainApp.suspendable) {
Button("Suspend") {
kill(getpid(), SIGUSR1)
}
}
}
}
@ -614,7 +620,7 @@ struct Run: AsyncParsableCommand {
}
}
MainApp.disappearSignal = suspendable ? SIGUSR1 : SIGINT
MainApp.suspendable = suspendable
MainApp.capturesSystemKeys = captureSystemKeys
MainApp.main()
}