UTMDataExtension.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Foundation
  17. import Carbon.HIToolbox
  18. @available(macOS 11, *)
  19. extension UTMData {
  20. func run(vm: VMData, options: UTMVirtualMachineStartOptions = [], startImmediately: Bool = true) {
  21. var window: Any? = vmWindows[vm]
  22. if window == nil {
  23. let close = {
  24. self.vmWindows.removeValue(forKey: vm)
  25. window = nil
  26. }
  27. if let avm = vm.wrapped as? UTMAppleVirtualMachine {
  28. if avm.config.system.architecture == UTMAppleConfigurationSystem.currentArchitecture {
  29. let primarySerialIndex = avm.config.serials.firstIndex { $0.mode == .builtin }
  30. if let primarySerialIndex = primarySerialIndex {
  31. window = VMDisplayAppleTerminalWindowController(primaryForIndex: primarySerialIndex, vm: avm, onClose: close)
  32. }
  33. if #available(macOS 12, *), !avm.config.displays.isEmpty {
  34. window = VMDisplayAppleDisplayWindowController(vm: avm, onClose: close)
  35. } else if avm.config.displays.isEmpty && window == nil {
  36. window = VMHeadlessSessionState(for: avm, onStop: close)
  37. }
  38. }
  39. }
  40. if let qvm = vm.wrapped as? UTMQemuVirtualMachine {
  41. if !qvm.config.displays.isEmpty {
  42. window = VMDisplayQemuMetalWindowController(vm: qvm, onClose: close)
  43. } else if !qvm.config.serials.filter({ $0.mode == .builtin }).isEmpty {
  44. window = VMDisplayQemuTerminalWindowController(vm: qvm, onClose: close)
  45. } else {
  46. window = VMHeadlessSessionState(for: qvm, onStop: close)
  47. }
  48. }
  49. if window == nil {
  50. DispatchQueue.main.async {
  51. self.alertItem = .message(NSLocalizedString("This virtual machine cannot be run on this machine.", comment: "UTMDataExtension"))
  52. }
  53. }
  54. }
  55. if let unwrappedWindow = window as? VMDisplayWindowController {
  56. vmWindows[vm] = unwrappedWindow
  57. vm.wrapped!.delegate = unwrappedWindow
  58. unwrappedWindow.showWindow(nil)
  59. unwrappedWindow.window!.makeMain()
  60. if startImmediately {
  61. unwrappedWindow.requestAutoStart(options: options)
  62. }
  63. } else if let unwrappedWindow = window as? VMHeadlessSessionState {
  64. vmWindows[vm] = unwrappedWindow
  65. if startImmediately {
  66. if vm.wrapped!.state == .paused {
  67. vm.wrapped!.requestVmResume()
  68. } else if vm.wrapped!.state == .stopped {
  69. vm.wrapped!.requestVmStart(options: options)
  70. }
  71. }
  72. } else {
  73. logger.critical("Failed to create window controller.")
  74. }
  75. }
  76. /// Start a remote session and return SPICE server port.
  77. /// - Parameters:
  78. /// - vm: VM to start
  79. /// - options: Start options
  80. /// - server: Remote server
  81. /// - Returns: Port number to SPICE server
  82. func startRemote(vm: VMData, options: UTMVirtualMachineStartOptions, forClient client: UTMRemoteServer.Remote) async throws -> UTMRemoteMessageServer.StartVirtualMachine.ServerInformation {
  83. guard let wrapped = vm.wrapped as? UTMQemuVirtualMachine, type(of: wrapped).capabilities.supportsRemoteSession else {
  84. throw UTMDataError.unsupportedBackend
  85. }
  86. if let existingSession = vmWindows[vm] as? VMRemoteSessionState, let spiceServerInfo = wrapped.spiceServerInfo {
  87. if wrapped.state == .paused {
  88. try await wrapped.resume()
  89. }
  90. existingSession.client = client
  91. return spiceServerInfo
  92. }
  93. guard vmWindows[vm] == nil else {
  94. throw UTMDataError.virtualMachineUnavailable
  95. }
  96. let session = VMRemoteSessionState(for: wrapped, client: client) {
  97. self.vmWindows.removeValue(forKey: vm)
  98. }
  99. try await wrapped.start(options: options.union(.remoteSession))
  100. vmWindows[vm] = session
  101. guard let spiceServerInfo = wrapped.spiceServerInfo else {
  102. throw UTMDataError.unsupportedBackend
  103. }
  104. return spiceServerInfo
  105. }
  106. func stop(vm: VMData) {
  107. guard let wrapped = vm.wrapped else {
  108. return
  109. }
  110. Task {
  111. if wrapped.registryEntry.isSuspended {
  112. try? await wrapped.deleteSnapshot(name: nil)
  113. }
  114. try? await wrapped.stop(usingMethod: .force)
  115. await MainActor.run {
  116. self.close(vm: vm)
  117. }
  118. }
  119. }
  120. func close(vm: VMData) {
  121. if let window = vmWindows.removeValue(forKey: vm) as? VMDisplayWindowController {
  122. DispatchQueue.main.async {
  123. window.close()
  124. }
  125. }
  126. }
  127. }