| 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #include "qemu/osdep.h" |
| 25 | #include "hw/core/qdev.h" |
| 26 | #include "monitor/qdev.h" |
| 27 | #include "qemu/error-report.h" |
| 28 | #include "qapi/error.h" |
| 29 | #include "hw/core/qdev-properties.h" |
| 30 | #include "hw/audio/model.h" |
| 31 | |
| 32 | struct audio_model { |
| 33 | const char *name; |
| 34 | const char *descr; |
| 35 | const char *typename; |
| 36 | void (*init)(const char *audiodev); |
| 37 | }; |
| 38 | |
| 39 | static struct audio_model audio_models[9]; |
| 40 | static int audio_models_count; |
| 41 | |
| 42 | void audio_register_model_with_cb(const char *name, const char *descr, |
| 43 | void (*init_audio_model)(const char *audiodev)) |
| 44 | { |
| 45 | assert(audio_models_count < ARRAY_SIZE(audio_models) - 1); |
| 46 | audio_models[audio_models_count].name = name; |
| 47 | audio_models[audio_models_count].descr = descr; |
| 48 | audio_models[audio_models_count].init = init_audio_model; |
| 49 | audio_models_count++; |
| 50 | } |
| 51 | |
| 52 | void audio_register_model(const char *name, const char *descr, |
| 53 | const char *typename) |
| 54 | { |
| 55 | assert(audio_models_count < ARRAY_SIZE(audio_models) - 1); |
| 56 | audio_models[audio_models_count].name = name; |
| 57 | audio_models[audio_models_count].descr = descr; |
| 58 | audio_models[audio_models_count].typename = typename; |
| 59 | audio_models_count++; |
| 60 | } |
| 61 | |
| 62 | void audio_print_available_models(void) |
| 63 | { |
| 64 | struct audio_model *c; |
| 65 | |
| 66 | if (audio_models_count) { |
| 67 | printf("Valid audio device model names:\n"); |
| 68 | for (c = audio_models; c->name; ++c) { |
| 69 | printf("%-11s %s\n", c->name, c->descr); |
| 70 | } |
| 71 | } else { |
| 72 | printf("Machine has no user-selectable audio hardware " |
| 73 | "(it may or may not have always-present audio hardware).\n"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static struct audio_model *selected; |
| 78 | static const char *audiodev_id; |
| 79 | |
| 80 | void audio_set_model(const char *name, const char *audiodev) |
| 81 | { |
| 82 | struct audio_model *c; |
| 83 | |
| 84 | if (selected) { |
| 85 | error_report("only one -audio option is allowed"); |
| 86 | exit(1); |
| 87 | } |
| 88 | |
| 89 | for (c = audio_models; c->name; ++c) { |
| 90 | if (g_str_equal(c->name, name)) { |
| 91 | selected = c; |
| 92 | audiodev_id = audiodev; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (!c->name) { |
| 98 | error_report("Unknown audio device model `%s'", name); |
| 99 | audio_print_available_models(); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void audio_model_init(void) |
| 105 | { |
| 106 | struct audio_model *c = selected; |
| 107 | |
| 108 | if (!c) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (c->typename) { |
| 113 | DeviceState *dev = qdev_new(c->typename); |
| 114 | BusState *bus = qdev_find_default_bus(DEVICE_GET_CLASS(dev), &error_fatal); |
| 115 | qdev_prop_set_string(dev, "audiodev", audiodev_id); |
| 116 | qdev_realize_and_unref(dev, bus, &error_fatal); |
| 117 | } else { |
| 118 | c->init(audiodev_id); |
| 119 | } |
| 120 | } |