UTMQemuConfigurationSound.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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") || rawTarget.hasPrefix("pseries") {
  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 if architecture == .m68k && rawTarget == QEMUTarget_m68k.q800.rawValue {
  48. hardware = QEMUSoundDevice_m68k.asc
  49. } else {
  50. let cards = architecture.soundDeviceType.allRawValues
  51. if let first = cards.first {
  52. hardware = AnyQEMUConstant(rawValue: first)!
  53. } else {
  54. return nil
  55. }
  56. }
  57. }
  58. }
  59. // MARK: - Conversion of old config format
  60. extension UTMQemuConfigurationSound {
  61. init?(migrating oldConfig: UTMLegacyQemuConfiguration) {
  62. self.init()
  63. guard oldConfig.soundEnabled else {
  64. return nil
  65. }
  66. if oldConfig.soundCard == "ac97" { // change in case for this one device
  67. hardware = AnyQEMUConstant(rawValue: "AC97")!
  68. } else if let hardwareStr = oldConfig.soundCard {
  69. hardware = AnyQEMUConstant(rawValue: hardwareStr)!
  70. }
  71. }
  72. }