소스 검색

scripting: properly create vm window on start

osy 2 년 전
부모
커밋
1b48c64967
3개의 변경된 파일13개의 추가작업 그리고 8개의 파일을 삭제
  1. 7 3
      Platform/macOS/UTMDataExtension.swift
  2. 5 5
      Platform/macOS/VMHeadlessSessionState.swift
  3. 1 0
      Scripting/UTMScriptingVirtualMachineImpl.swift

+ 7 - 3
Platform/macOS/UTMDataExtension.swift

@@ -19,7 +19,7 @@ import Carbon.HIToolbox
 
 
 @available(macOS 11, *)
 @available(macOS 11, *)
 extension UTMData {
 extension UTMData {
-    @MainActor func run(vm: UTMVirtualMachine) {
+    @MainActor func run(vm: UTMVirtualMachine, startImmediately: Bool = true) {
         var window: Any? = vmWindows[vm]
         var window: Any? = vmWindows[vm]
         if window == nil {
         if window == nil {
             let close = { (notification: Notification) -> Void in
             let close = { (notification: Notification) -> Void in
@@ -59,10 +59,14 @@ extension UTMData {
             vm.delegate = unwrappedWindow
             vm.delegate = unwrappedWindow
             unwrappedWindow.showWindow(nil)
             unwrappedWindow.showWindow(nil)
             unwrappedWindow.window!.makeMain()
             unwrappedWindow.window!.makeMain()
-            unwrappedWindow.requestAutoStart()
+            if startImmediately {
+                unwrappedWindow.requestAutoStart()
+            }
         } else if let unwrappedWindow = window as? VMHeadlessSessionState {
         } else if let unwrappedWindow = window as? VMHeadlessSessionState {
             vmWindows[vm] = unwrappedWindow
             vmWindows[vm] = unwrappedWindow
-            unwrappedWindow.start()
+            if startImmediately {
+                vm.requestVmStart()
+            }
         } else {
         } else {
             logger.critical("Failed to create window controller.")
             logger.critical("Failed to create window controller.")
         }
         }

+ 5 - 5
Platform/macOS/VMHeadlessSessionState.swift

@@ -41,10 +41,11 @@ extension VMHeadlessSessionState: UTMVirtualMachineDelegate {
             vmState = state
             vmState = state
             if state == .vmStarted {
             if state == .vmStarted {
                 hasStarted = true
                 hasStarted = true
+                didStart()
             }
             }
             if state == .vmStopped {
             if state == .vmStopped {
                 if hasStarted {
                 if hasStarted {
-                    stop() // graceful exit
+                    didStop() // graceful exit
                 }
                 }
                 hasStarted = false
                 hasStarted = false
             }
             }
@@ -57,19 +58,18 @@ extension VMHeadlessSessionState: UTMVirtualMachineDelegate {
             NotificationCenter.default.post(name: .vmSessionError, object: nil, userInfo: ["Session": self, "Message": message])
             NotificationCenter.default.post(name: .vmSessionError, object: nil, userInfo: ["Session": self, "Message": message])
             if !hasStarted {
             if !hasStarted {
                 // if we got an error and haven't started, then cleanup
                 // if we got an error and haven't started, then cleanup
-                stop()
+                didStop()
             }
             }
         }
         }
     }
     }
 }
 }
 
 
 extension VMHeadlessSessionState {
 extension VMHeadlessSessionState {
-    func start() {
+    private func didStart() {
         NotificationCenter.default.post(name: .vmSessionCreated, object: nil, userInfo: ["Session": self])
         NotificationCenter.default.post(name: .vmSessionCreated, object: nil, userInfo: ["Session": self])
-        vm.requestVmStart()
     }
     }
     
     
-    func stop() {
+    private func didStop() {
         NotificationCenter.default.post(name: .vmSessionEnded, object: nil, userInfo: ["Session": self])
         NotificationCenter.default.post(name: .vmSessionEnded, object: nil, userInfo: ["Session": self])
     }
     }
 }
 }

+ 1 - 0
Scripting/UTMScriptingVirtualMachineImpl.swift

@@ -122,6 +122,7 @@ class UTMScriptingVirtualMachineImpl: NSObject {
     
     
     @objc func start(_ command: NSScriptCommand) {
     @objc func start(_ command: NSScriptCommand) {
         withScriptCommand(command) { [self] in
         withScriptCommand(command) { [self] in
+            data.run(vm: vm, startImmediately: false)
             if vm.state == .vmStopped {
             if vm.state == .vmStopped {
                 try await vm.vmStart()
                 try await vm.vmStart()
             } else if vm.state == .vmPaused {
             } else if vm.state == .vmPaused {