UTMQemuConfigurationSound.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 single audio device
  18. struct UTMQemuConfigurationSound: Codable, Identifiable {
  19. /// Hardware model to emulate.
  20. var hardware: any QEMUSoundDevice = QEMUSoundDevice_x86_64.AC97
  21. let id = UUID()
  22. enum CodingKeys: String, CodingKey {
  23. case hardware = "Hardware"
  24. }
  25. init() {
  26. }
  27. init(from decoder: Decoder) throws {
  28. let values = try decoder.container(keyedBy: CodingKeys.self)
  29. hardware = try values.decode(AnyQEMUConstant.self, forKey: .hardware)
  30. }
  31. func encode(to encoder: Encoder) throws {
  32. var container = encoder.container(keyedBy: CodingKeys.self)
  33. try container.encode(hardware.asAnyQEMUConstant(), forKey: .hardware)
  34. }
  35. }
  36. // MARK: - Default construction
  37. extension UTMQemuConfigurationSound {
  38. init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
  39. self.init()
  40. let rawTarget = target.rawValue
  41. if rawTarget.hasPrefix("pc") {
  42. hardware = QEMUSoundDevice_x86_64.AC97
  43. } else if rawTarget.hasPrefix("q35") || rawTarget.hasPrefix("virt-") || rawTarget == "virt" {
  44. hardware = QEMUSoundDevice_x86_64.intel_hda
  45. } else if rawTarget == "mac99" {
  46. hardware = QEMUSoundDevice_ppc.screamer
  47. } else {
  48. let cards = architecture.soundDeviceType.allRawValues
  49. if let first = cards.first {
  50. hardware = AnyQEMUConstant(rawValue: first)!
  51. } else {
  52. return nil
  53. }
  54. }
  55. }
  56. }
  57. // MARK: - Conversion of old config format
  58. extension UTMQemuConfigurationSound {
  59. init?(migrating oldConfig: UTMLegacyQemuConfiguration) {
  60. self.init()
  61. guard oldConfig.soundEnabled else {
  62. return nil
  63. }
  64. if oldConfig.soundCard == "ac97" { // change in case for this one device
  65. hardware = AnyQEMUConstant(rawValue: "AC97")!
  66. } else if let hardwareStr = oldConfig.soundCard {
  67. hardware = AnyQEMUConstant(rawValue: hardwareStr)!
  68. }
  69. }
  70. }