VMWizardStartView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright © 2021 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 SwiftUI
  17. #if canImport(Virtualization)
  18. import Virtualization
  19. #endif
  20. struct VMWizardStartView: View {
  21. @ObservedObject var wizardState: VMWizardState
  22. var isVirtualizationSupported: Bool {
  23. #if os(macOS)
  24. VZVirtualMachine.isSupported && !processIsTranslated()
  25. #else
  26. jb_has_hypervisor()
  27. #endif
  28. }
  29. var isEmulationSupported: Bool {
  30. #if !WITH_JIT
  31. true
  32. #else
  33. Main.jitAvailable
  34. #endif
  35. }
  36. var body: some View {
  37. VMWizardContent("Start") {
  38. Section {
  39. let virtButton = Button {
  40. wizardState.useVirtualization = true
  41. wizardState.next()
  42. } label: {
  43. HStack {
  44. Image(systemName: "hare")
  45. .font(.title)
  46. VStack(alignment: .leading, spacing: 10) {
  47. Text("Virtualize")
  48. .font(.title)
  49. Text("Faster, but can only run the native CPU architecture.")
  50. .font(.caption)
  51. }
  52. Spacer()
  53. }
  54. .padding()
  55. }
  56. .buttonStyle(.inList)
  57. .disabled(!isVirtualizationSupported)
  58. #if os(iOS) || os(visionOS)
  59. if #available(iOS 15, *) {
  60. virtButton
  61. } else {
  62. virtButton
  63. .opacity(isVirtualizationSupported ? 1 : 0.5)
  64. }
  65. #else
  66. virtButton
  67. #endif
  68. Button {
  69. wizardState.useVirtualization = false
  70. wizardState.next()
  71. } label: {
  72. HStack {
  73. Image(systemName: "tortoise")
  74. .font(.title)
  75. VStack(alignment: .leading, spacing: 10) {
  76. Text("Emulate")
  77. .font(.title)
  78. Text("Slower, but can run other CPU architectures.")
  79. .font(.caption)
  80. }
  81. Spacer()
  82. }
  83. .padding()
  84. }
  85. .buttonStyle(.inList)
  86. } header: {
  87. Text("Custom")
  88. } footer: {
  89. if !isEmulationSupported && !isVirtualizationSupported {
  90. Text("Your version of iOS does not support running VMs while unmodified. You must either run UTM while jailbroken or with a remote debugger attached. See https://getutm.app/install/ for more details.")
  91. } else if !isVirtualizationSupported {
  92. Text("Virtualization is not supported on your system.")
  93. } else if !isEmulationSupported {
  94. Text("This build does not emulation.")
  95. }
  96. }
  97. Section {
  98. Button {
  99. NotificationCenter.default.post(name: NSNotification.OpenVirtualMachine, object: nil)
  100. } label: {
  101. Label {
  102. Text("Open…")
  103. } icon: {
  104. Image(systemName: "doc")
  105. }
  106. }
  107. #if os(macOS)
  108. .buttonStyle(.link)
  109. #endif
  110. Link(destination: URL(string: "https://mac.getutm.app/gallery/")!) {
  111. Label {
  112. Text("Download prebuilt from UTM Gallery…")
  113. } icon: {
  114. Image(systemName: "arrow.down.doc")
  115. }
  116. }
  117. } header: {
  118. Text("Existing")
  119. }
  120. }
  121. }
  122. private func processIsTranslated() -> Bool {
  123. let key = "sysctl.proc_translated"
  124. var ret = Int32(0)
  125. var size: Int = 0
  126. sysctlbyname(key, nil, &size, nil, 0)
  127. let result = sysctlbyname(key, &ret, &size, nil, 0)
  128. if result == -1 {
  129. return false
  130. }
  131. return ret != 0
  132. }
  133. }
  134. struct VMWizardStartView_Previews: PreviewProvider {
  135. @StateObject static var wizardState = VMWizardState()
  136. static var previews: some View {
  137. VMWizardStartView(wizardState: wizardState)
  138. }
  139. }