UTMAppleConfigurationDisplay.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import Virtualization
  18. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  19. @available(macOS 11, *)
  20. struct UTMAppleConfigurationDisplay: Codable, Identifiable {
  21. var widthInPixels: Int = 1920
  22. var heightInPixels: Int = 1200
  23. var pixelsPerInch: Int = 80
  24. let id = UUID()
  25. enum CodingKeys: String, CodingKey {
  26. case widthInPixels = "WidthPixels"
  27. case heightInPixels = "HeightPixels"
  28. case pixelsPerInch = "PixelsPerInch"
  29. }
  30. init() {
  31. }
  32. init(width: Int, height: Int, ppi: Int = 80) {
  33. widthInPixels = width
  34. heightInPixels = height
  35. pixelsPerInch = ppi
  36. }
  37. init(from decoder: Decoder) throws {
  38. let values = try decoder.container(keyedBy: CodingKeys.self)
  39. widthInPixels = try values.decode(Int.self, forKey: .widthInPixels)
  40. heightInPixels = try values.decode(Int.self, forKey: .heightInPixels)
  41. pixelsPerInch = try values.decode(Int.self, forKey: .pixelsPerInch)
  42. }
  43. func encode(to encoder: Encoder) throws {
  44. var container = encoder.container(keyedBy: CodingKeys.self)
  45. try container.encode(widthInPixels, forKey: .widthInPixels)
  46. try container.encode(heightInPixels, forKey: .heightInPixels)
  47. try container.encode(pixelsPerInch, forKey: .pixelsPerInch)
  48. }
  49. #if arch(arm64)
  50. @available(macOS 12, *)
  51. init(from config: VZMacGraphicsDisplayConfiguration) {
  52. widthInPixels = config.widthInPixels
  53. heightInPixels = config.heightInPixels
  54. pixelsPerInch = config.pixelsPerInch
  55. }
  56. @available(macOS 12, *)
  57. func vzMacDisplay() -> VZMacGraphicsDisplayConfiguration {
  58. VZMacGraphicsDisplayConfiguration(widthInPixels: widthInPixels,
  59. heightInPixels: heightInPixels,
  60. pixelsPerInch: pixelsPerInch)
  61. }
  62. #endif
  63. @available(macOS 13, *)
  64. func vzVirtioDisplay() -> VZVirtioGraphicsScanoutConfiguration {
  65. VZVirtioGraphicsScanoutConfiguration(widthInPixels: widthInPixels,
  66. heightInPixels: heightInPixels)
  67. }
  68. }
  69. // MARK: - Conversion of old config format
  70. #if arch(arm64)
  71. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  72. @available(macOS 12, *)
  73. extension UTMAppleConfigurationDisplay {
  74. init(migrating oldDisplay: Display) {
  75. widthInPixels = oldDisplay.widthInPixels
  76. heightInPixels = oldDisplay.heightInPixels
  77. pixelsPerInch = oldDisplay.pixelsPerInch
  78. }
  79. }
  80. #endif