UTMDataExtension.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 SwiftUI
  18. extension UTMData {
  19. func run(vm: VMData, options: UTMVirtualMachineStartOptions = []) {
  20. #if WITH_SOLO_VM
  21. guard VMSessionState.allActiveSessions.count == 0 else {
  22. logger.error("Session already started")
  23. return
  24. }
  25. #endif
  26. guard let wrapped = vm.wrapped else {
  27. return
  28. }
  29. if let session = VMSessionState.allActiveSessions.values.first(where: { $0.vm.id == wrapped.id }) {
  30. session.showWindow()
  31. } else if vm.isStopped || vm.isTakeoverAllowed {
  32. let session = VMSessionState(for: wrapped as! (any UTMSpiceVirtualMachine))
  33. session.start(options: options)
  34. } else {
  35. showErrorAlert(message: NSLocalizedString("This virtual machine is already running. In order to run it from this device, you must stop it first.", comment: "UTMDataExtension"))
  36. }
  37. }
  38. func stop(vm: VMData) {
  39. guard let wrapped = vm.wrapped else {
  40. return
  41. }
  42. if wrapped.registryEntry.isSuspended {
  43. wrapped.requestVmDeleteState()
  44. }
  45. wrapped.requestVmStop()
  46. }
  47. func close(vm: VMData) {
  48. // do nothing
  49. }
  50. }