UTMQemuConfigurationDisplay.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright © 2022 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. /// Settings for a single display.
  18. struct UTMQemuConfigurationDisplay: Codable, Identifiable {
  19. /// Hardware card to emulate.
  20. var hardware: any QEMUDisplayDevice = QEMUDisplayDevice_x86_64.virtio_vga
  21. /// Only used for VGA devices.
  22. var vgaRamMib: Int?
  23. /// If true, attempt to use SPICE guest agent to change the display resolution automatically.
  24. var isDynamicResolution: Bool = true
  25. /// Filter to use when upscaling.
  26. var upscalingFilter: QEMUScaler = .nearest
  27. /// Filter to use when downscaling.
  28. var downscalingFilter: QEMUScaler = .linear
  29. /// If true, use the true (retina) resolution of the display. Otherwise, use the percieved resolution.
  30. var isNativeResolution: Bool = false
  31. let id = UUID()
  32. enum CodingKeys: String, CodingKey {
  33. case hardware = "Hardware"
  34. case vgaRamMib = "VgaRamMib"
  35. case isDynamicResolution = "DynamicResolution"
  36. case upscalingFilter = "UpscalingFilter"
  37. case downscalingFilter = "DownscalingFilter"
  38. case isNativeResolution = "NativeResolution"
  39. }
  40. init() {
  41. }
  42. init(from decoder: Decoder) throws {
  43. let values = try decoder.container(keyedBy: CodingKeys.self)
  44. hardware = try values.decode(AnyQEMUConstant.self, forKey: .hardware)
  45. vgaRamMib = try values.decodeIfPresent(Int.self, forKey: .vgaRamMib)
  46. isDynamicResolution = try values.decode(Bool.self, forKey: .isDynamicResolution)
  47. upscalingFilter = try values.decode(QEMUScaler.self, forKey: .upscalingFilter)
  48. downscalingFilter = try values.decode(QEMUScaler.self, forKey: .downscalingFilter)
  49. isNativeResolution = try values.decode(Bool.self, forKey: .isNativeResolution)
  50. }
  51. func encode(to encoder: Encoder) throws {
  52. var container = encoder.container(keyedBy: CodingKeys.self)
  53. try container.encode(hardware.asAnyQEMUConstant(), forKey: .hardware)
  54. try container.encodeIfPresent(vgaRamMib, forKey: .vgaRamMib)
  55. try container.encode(isDynamicResolution, forKey: .isDynamicResolution)
  56. try container.encode(upscalingFilter, forKey: .upscalingFilter)
  57. try container.encode(downscalingFilter, forKey: .downscalingFilter)
  58. try container.encode(isNativeResolution, forKey: .isNativeResolution)
  59. }
  60. }
  61. // MARK: - Default construction
  62. extension UTMQemuConfigurationDisplay {
  63. init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
  64. self.init()
  65. let rawTarget = target.rawValue
  66. if !architecture.hasAgentSupport || rawTarget == "isapc" {
  67. isDynamicResolution = false
  68. }
  69. if rawTarget.hasPrefix("pc") || rawTarget.hasPrefix("q35") {
  70. hardware = QEMUDisplayDevice_x86_64.virtio_vga
  71. } else if rawTarget == "isapc" {
  72. hardware = QEMUDisplayDevice_x86_64.isa_vga
  73. } else if rawTarget.hasPrefix("virt-") || rawTarget == "virt" {
  74. hardware = QEMUDisplayDevice_aarch64.virtio_ramfb
  75. } else {
  76. let cards = architecture.displayDeviceType.allRawValues
  77. if cards.contains("VGA") {
  78. hardware = AnyQEMUConstant(rawValue: "VGA")!
  79. } else if let first = cards.first {
  80. hardware = AnyQEMUConstant(rawValue: first)!
  81. } else {
  82. return nil
  83. }
  84. }
  85. }
  86. }
  87. // MARK: - Conversion of old config format
  88. extension UTMQemuConfigurationDisplay {
  89. init?(migrating oldConfig: UTMLegacyQemuConfiguration) {
  90. self.init()
  91. guard !oldConfig.displayConsoleOnly else {
  92. return nil
  93. }
  94. if let hardwareStr = oldConfig.displayCard {
  95. hardware = AnyQEMUConstant(rawValue: hardwareStr)!
  96. }
  97. isDynamicResolution = oldConfig.displayFitScreen
  98. isNativeResolution = oldConfig.displayRetina
  99. if let upscaler = convertScaler(from: oldConfig.displayUpscaler) {
  100. upscalingFilter = upscaler
  101. }
  102. if let downscaler = convertScaler(from: oldConfig.displayDownscaler) {
  103. downscalingFilter = downscaler
  104. }
  105. }
  106. private func convertScaler(from str: String?) -> QEMUScaler? {
  107. if str == "linear" {
  108. return .linear
  109. } else if str == "nearest" {
  110. return .nearest
  111. } else {
  112. return nil
  113. }
  114. }
  115. }