mirror of
https://github.com/utmapp/UTM.git
synced 2026-10-12 02:24:01 +00:00
Now that the deployment targets are iOS 15 and macOS 12, every guard, fallback and workaround for an older release is dead code that only obscures what the supported path is. Remove them: - Drop the `@available` attributes for iOS 15, macOS 11 and macOS 12, and unwrap the matching `#available`/`#unavailable` guards in Swift and the `@available`/`API_AVAILABLE`/`NS_AVAILABLE_IOS` ones in Objective-C. - Delete the legacy-only views and helpers left unreachable by that: NumberTextFieldOld, VMConfigNetworkPortForwardLegacyView, VMContextMenuModifier.bodyBigSur, the nonbrokenSheet, uniqued, customBadge, borderedList and legacySettingsToolbar shims, the .unsupported BootloaderSelection case, and the iOS 14 sacrificial version Section in the QEMU settings list. - jb_has_cs_execseg_allow_unsigned() has returned false for every host since iOS 14.4, so drop it along with the is_device_A12_or_newer() helper that only it used. The entitlement parser no longer needs to copy the blob either, since that copy existed only so the psychic paper needle could be blanked in place. - VMDisplayTerminalViewController tracked the keyboard frame by hand for iOS 14. `useAutoLayout` is a constant that cannot be overridden, so nothing ever registered the notification observers and only the keyboardLayoutGuide path is reachable. - VMConfigQEMUView's additionalArguments is now referenced on iOS only, which leaves its macOS branch, and the macOS 11 ForEach crash workaround inside it, unreachable. - Raise the Cydia package floor and the postinst/prerm helpers to match the new minimum. Two of the removed guards dropped an error path rather than just a branch. The shared directory `featureNotSupported` throw covered VZVirtioFileSystemDeviceConfiguration and friends being macOS 12+, which no longer applies to any supported host. The macOS guest `platformUnsupported` throw when decoding a boot config on Intel only fired on macOS 11; the Apple Silicon requirement it really guards is enforced unconditionally in UTMAppleConfigurationSystem.fillVZConfiguration, which both the start and the save path go through. Assisted-by: Claude:claude-opus-5
222 lines
8.8 KiB
Swift
222 lines
8.8 KiB
Swift
//
|
|
// Copyright © 2022 osy. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
import Foundation
|
|
import Virtualization
|
|
|
|
/// Device settings.
|
|
@available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
|
|
struct UTMAppleConfigurationVirtualization: Codable {
|
|
enum PointerDevice: String, CaseIterable, QEMUConstant {
|
|
case disabled = "Disabled"
|
|
case mouse = "Mouse"
|
|
case trackpad = "Trackpad"
|
|
|
|
var prettyValue: String {
|
|
switch self {
|
|
case .disabled: return NSLocalizedString("Disabled", comment: "UTMAppleConfigurationDevices")
|
|
case .mouse: return NSLocalizedString("Generic Mouse", comment: "UTMAppleConfigurationDevices")
|
|
case .trackpad: return NSLocalizedString("Mac Trackpad (macOS 13+)", comment: "UTMAppleConfigurationDevices")
|
|
}
|
|
}
|
|
}
|
|
|
|
enum KeyboardDevice: String, CaseIterable, QEMUConstant {
|
|
case disabled = "Disabled"
|
|
case generic = "Generic"
|
|
case mac = "Mac"
|
|
|
|
var prettyValue: String {
|
|
switch self {
|
|
case .disabled: return NSLocalizedString("Disabled", comment: "UTMAppleConfigurationDevices")
|
|
case .generic: return NSLocalizedString("Generic USB", comment: "UTMAppleConfigurationDevices")
|
|
case .mac: return NSLocalizedString("Mac Keyboard (macOS 14+)", comment: "UTMAppleConfigurationDevices")
|
|
}
|
|
}
|
|
}
|
|
|
|
var hasAudio: Bool = false
|
|
|
|
var hasBalloon: Bool = true
|
|
|
|
var hasEntropy: Bool = true
|
|
|
|
var keyboard: KeyboardDevice = .disabled
|
|
|
|
var pointer: PointerDevice = .disabled
|
|
|
|
var hasRosetta: Bool?
|
|
|
|
var hasClipboardSharing: Bool = false
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case hasAudio = "Audio"
|
|
case hasBalloon = "Balloon"
|
|
case hasEntropy = "Entropy"
|
|
case keyboard = "Keyboard"
|
|
case pointer = "Pointer"
|
|
case hasTrackpad = "Trackpad"
|
|
case rosetta = "Rosetta"
|
|
case hasClipboardSharing = "ClipboardSharing"
|
|
}
|
|
|
|
init() {
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let values = try decoder.container(keyedBy: CodingKeys.self)
|
|
hasAudio = try values.decode(Bool.self, forKey: .hasAudio)
|
|
hasBalloon = try values.decode(Bool.self, forKey: .hasBalloon)
|
|
hasEntropy = try values.decode(Bool.self, forKey: .hasEntropy)
|
|
if let hasKeyboard = try? values.decode(Bool.self, forKey: .keyboard) {
|
|
keyboard = hasKeyboard ? .generic : .disabled
|
|
} else {
|
|
keyboard = try values.decode(KeyboardDevice.self, forKey: .keyboard)
|
|
}
|
|
if let hasPointer = try? values.decode(Bool.self, forKey: .pointer) {
|
|
let hasTrackpad = try values.decodeIfPresent(Bool.self, forKey: .hasTrackpad) ?? false
|
|
pointer = hasTrackpad ? .trackpad : hasPointer ? .mouse : .disabled
|
|
} else {
|
|
pointer = try values.decode(PointerDevice.self, forKey: .pointer)
|
|
}
|
|
if #available(macOS 13, *) {
|
|
hasRosetta = try values.decodeIfPresent(Bool.self, forKey: .rosetta)
|
|
hasClipboardSharing = try values.decodeIfPresent(Bool.self, forKey: .hasClipboardSharing) ?? false
|
|
}
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(hasAudio, forKey: .hasAudio)
|
|
try container.encode(hasBalloon, forKey: .hasBalloon)
|
|
try container.encode(hasEntropy, forKey: .hasEntropy)
|
|
try container.encode(keyboard, forKey: .keyboard)
|
|
try container.encode(pointer, forKey: .pointer)
|
|
try container.encodeIfPresent(hasRosetta, forKey: .rosetta)
|
|
try container.encode(hasClipboardSharing, forKey: .hasClipboardSharing)
|
|
}
|
|
}
|
|
|
|
// MARK: - Conversion of old config format
|
|
|
|
@available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
|
|
extension UTMAppleConfigurationVirtualization {
|
|
init(migrating oldConfig: UTMLegacyAppleConfiguration) {
|
|
self.init()
|
|
hasBalloon = oldConfig.isBalloonEnabled
|
|
hasEntropy = oldConfig.isEntropyEnabled
|
|
hasAudio = oldConfig.isAudioEnabled
|
|
keyboard = oldConfig.isKeyboardEnabled ? .generic : .disabled
|
|
pointer = oldConfig.isPointingEnabled ? .mouse : .disabled
|
|
}
|
|
}
|
|
|
|
// MARK: - Creating Apple config
|
|
|
|
@available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
|
|
extension UTMAppleConfigurationVirtualization {
|
|
func fillVZConfiguration(_ vzconfig: VZVirtualMachineConfiguration, isMacOSGuest: Bool = false) throws {
|
|
if hasBalloon && !isMacOSGuest {
|
|
vzconfig.memoryBalloonDevices = [VZVirtioTraditionalMemoryBalloonDeviceConfiguration()]
|
|
}
|
|
if hasEntropy {
|
|
vzconfig.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
|
|
}
|
|
if hasAudio {
|
|
let audioConfiguration = VZVirtioSoundDeviceConfiguration()
|
|
let audioInput = VZVirtioSoundDeviceInputStreamConfiguration()
|
|
audioInput.source = VZHostAudioInputStreamSource()
|
|
let audioOutput = VZVirtioSoundDeviceOutputStreamConfiguration()
|
|
audioOutput.sink = VZHostAudioOutputStreamSink()
|
|
audioConfiguration.streams = [audioInput, audioOutput]
|
|
vzconfig.audioDevices = [audioConfiguration]
|
|
}
|
|
if keyboard != .disabled {
|
|
vzconfig.keyboards = [VZUSBKeyboardConfiguration()]
|
|
#if arch(arm64)
|
|
if #available(macOS 14, *), isMacOSGuest && keyboard == .mac {
|
|
vzconfig.keyboards = [VZMacKeyboardConfiguration()]
|
|
}
|
|
#endif
|
|
}
|
|
if pointer != .disabled {
|
|
vzconfig.pointingDevices = [VZUSBScreenCoordinatePointingDeviceConfiguration()]
|
|
#if arch(arm64)
|
|
if #available(macOS 13, *), isMacOSGuest && pointer == .trackpad {
|
|
// replace with trackpad device
|
|
vzconfig.pointingDevices = [VZMacTrackpadConfiguration()]
|
|
}
|
|
#endif
|
|
}
|
|
if #available(macOS 13, *) {
|
|
#if arch(arm64)
|
|
if hasRosetta == true {
|
|
let rosettaDirectoryShare = try VZLinuxRosettaDirectoryShare()
|
|
if #available(macOS 14, *) {
|
|
// enable cache if possible
|
|
try? rosettaDirectoryShare.setCachingOptions(.defaultUnixSocket)
|
|
}
|
|
let fileSystemDevice = VZVirtioFileSystemDeviceConfiguration(tag: "rosetta")
|
|
fileSystemDevice.share = rosettaDirectoryShare
|
|
vzconfig.directorySharingDevices.append(fileSystemDevice)
|
|
}
|
|
#else
|
|
if hasRosetta == true {
|
|
throw UTMAppleConfigurationError.rosettaNotSupported
|
|
}
|
|
#endif
|
|
if hasClipboardSharing {
|
|
let spiceClipboardAgent = VZSpiceAgentPortAttachment()
|
|
spiceClipboardAgent.sharesClipboard = true
|
|
let consolePort = VZVirtioConsolePortConfiguration()
|
|
consolePort.name = VZSpiceAgentPortAttachment.spiceAgentPortName
|
|
consolePort.attachment = spiceClipboardAgent
|
|
consolePort.isConsole = false
|
|
let consoleDevice = VZVirtioConsoleDeviceConfiguration()
|
|
consoleDevice.ports[0] = consolePort
|
|
vzconfig.consoleDevices.append(consoleDevice)
|
|
}
|
|
} else {
|
|
if hasRosetta == true || hasClipboardSharing {
|
|
throw UTMAppleConfigurationError.featureNotSupported
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: prepare save
|
|
extension UTMAppleConfigurationVirtualization {
|
|
func prepareSave(for packageURL: URL) async throws {
|
|
if #available(macOS 13, *), hasRosetta == true {
|
|
try await installRosetta()
|
|
}
|
|
}
|
|
|
|
@available(macOS 13, *)
|
|
private func installRosetta() async throws {
|
|
#if arch(arm64)
|
|
let rosettaAvailability = VZLinuxRosettaDirectoryShare.availability
|
|
if rosettaAvailability == .notSupported {
|
|
throw UTMAppleConfigurationError.rosettaNotSupported
|
|
} else if rosettaAvailability == .notInstalled {
|
|
try await VZLinuxRosettaDirectoryShare.installRosetta()
|
|
}
|
|
#else
|
|
throw UTMAppleConfigurationError.rosettaNotSupported
|
|
#endif
|
|
}
|
|
}
|