UTMDataExtension.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. @available(iOS 14, *)
  18. extension UTMData {
  19. private func createDisplay(vm: UTMVirtualMachine) -> VMDisplayViewController {
  20. let qvm = vm as! UTMQemuVirtualMachine
  21. if qvm.qemuConfig.displayConsoleOnly {
  22. let vc = VMDisplayTerminalViewController()
  23. vm.delegate = vc
  24. vc.vm = qvm
  25. vc.setupSubviews()
  26. vc.virtualMachine(vm, transitionTo: vm.state)
  27. return vc
  28. } else {
  29. let vc = VMDisplayMetalViewController()
  30. vm.delegate = vc
  31. vc.vm = qvm
  32. vc.setupSubviews()
  33. vc.virtualMachine(vm, transitionTo: vm.state)
  34. return vc
  35. }
  36. }
  37. func run(vm: UTMVirtualMachine) {
  38. guard let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first else {
  39. logger.error("Cannot find key window")
  40. return
  41. }
  42. let vc = self.createDisplay(vm: vm)
  43. self.vmVC = vc
  44. window.rootViewController = vc
  45. window.makeKeyAndVisible()
  46. let options: UIView.AnimationOptions = .transitionCrossDissolve
  47. let duration: TimeInterval = 0.3
  48. UIView.transition(with: window, duration: duration, options: options, animations: {}, completion: nil)
  49. }
  50. func stop(vm: UTMVirtualMachine) throws {
  51. if vm.viewState.suspended {
  52. guard vm.deleteSaveVM() else {
  53. throw NSLocalizedString("Failed to delete saved state.", comment: "UTMDataExtension")
  54. }
  55. }
  56. }
  57. func tryClickAtPoint(point: CGPoint, button: CSInputButton) {
  58. if let vc = vmVC as? VMDisplayMetalViewController, let input = vc.vmInput {
  59. input.sendMouseButton(button, pressed: true)
  60. DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
  61. input.sendMouseButton(button, pressed: false)
  62. }
  63. }
  64. }
  65. func trySendTextSpice(_ text: String) {
  66. if let vc = vmVC as? VMDisplayMetalViewController {
  67. vc.keyboardView.insertText(text)
  68. }
  69. }
  70. }