audio: -audiodev command line option basic implementation
[qemu/ar7.git] / audio / audio_legacy.c
blobad3b781addf86a04e9e4148144d7da1d694ee6d6
1 /*
2 * QEMU Audio subsystem: legacy configuration handling
4 * Copyright (c) 2015-2019 Zoltán Kővágó <DirtY.iCE.hu@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "audio.h"
26 #include "audio_int.h"
27 #include "qemu-common.h"
28 #include "qemu/cutils.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-visit-audio.h"
31 #include "qapi/visitor-impl.h"
33 #define AUDIO_CAP "audio-legacy"
34 #include "audio_int.h"
36 static uint32_t toui32(const char *str)
38 unsigned long long ret;
39 if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) {
40 dolog("Invalid integer value `%s'\n", str);
41 exit(1);
43 return ret;
46 /* helper functions to convert env variables */
47 static void get_bool(const char *env, bool *dst, bool *has_dst)
49 const char *val = getenv(env);
50 if (val) {
51 *dst = toui32(val) != 0;
52 *has_dst = true;
56 static void get_int(const char *env, uint32_t *dst, bool *has_dst)
58 const char *val = getenv(env);
59 if (val) {
60 *dst = toui32(val);
61 *has_dst = true;
65 static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst)
67 const char *val = getenv(env);
68 if (val) {
69 size_t i;
70 for (i = 0; AudioFormat_lookup.size; ++i) {
71 if (strcasecmp(val, AudioFormat_lookup.array[i]) == 0) {
72 *dst = i;
73 *has_dst = true;
74 return;
78 dolog("Invalid audio format `%s'\n", val);
79 exit(1);
83 /* backend specific functions */
84 /* todo */
86 /* general */
87 static void handle_per_direction(
88 AudiodevPerDirectionOptions *pdo, const char *prefix)
90 char buf[64];
91 size_t len = strlen(prefix);
93 memcpy(buf, prefix, len);
94 strcpy(buf + len, "FIXED_SETTINGS");
95 get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings);
97 strcpy(buf + len, "FIXED_FREQ");
98 get_int(buf, &pdo->frequency, &pdo->has_frequency);
100 strcpy(buf + len, "FIXED_FMT");
101 get_fmt(buf, &pdo->format, &pdo->has_format);
103 strcpy(buf + len, "FIXED_CHANNELS");
104 get_int(buf, &pdo->channels, &pdo->has_channels);
106 strcpy(buf + len, "VOICES");
107 get_int(buf, &pdo->voices, &pdo->has_voices);
110 static AudiodevListEntry *legacy_opt(const char *drvname)
112 AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry));
113 e->dev = g_malloc0(sizeof(Audiodev));
114 e->dev->id = g_strdup(drvname);
115 e->dev->driver = qapi_enum_parse(
116 &AudiodevDriver_lookup, drvname, -1, &error_abort);
118 audio_create_pdos(e->dev);
120 handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_");
121 handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_");
123 get_int("QEMU_AUDIO_TIMER_PERIOD",
124 &e->dev->timer_period, &e->dev->has_timer_period);
126 return e;
129 AudiodevListHead audio_handle_legacy_opts(void)
131 const char *drvname = getenv("QEMU_AUDIO_DRV");
132 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
134 if (drvname) {
135 AudiodevListEntry *e;
136 audio_driver *driver = audio_driver_lookup(drvname);
137 if (!driver) {
138 dolog("Unknown audio driver `%s'\n", drvname);
139 exit(1);
141 e = legacy_opt(drvname);
142 QSIMPLEQ_INSERT_TAIL(&head, e, next);
143 } else {
144 for (int i = 0; audio_prio_list[i]; i++) {
145 audio_driver *driver = audio_driver_lookup(audio_prio_list[i]);
146 if (driver && driver->can_be_default) {
147 AudiodevListEntry *e = legacy_opt(driver->name);
148 QSIMPLEQ_INSERT_TAIL(&head, e, next);
151 if (QSIMPLEQ_EMPTY(&head)) {
152 dolog("Internal error: no default audio driver available\n");
153 exit(1);
157 return head;
160 /* visitor to print -audiodev option */
161 typedef struct {
162 Visitor visitor;
164 bool comma;
165 GList *path;
166 } LegacyPrintVisitor;
168 static void lv_start_struct(Visitor *v, const char *name, void **obj,
169 size_t size, Error **errp)
171 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
172 lv->path = g_list_append(lv->path, g_strdup(name));
175 static void lv_end_struct(Visitor *v, void **obj)
177 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
178 lv->path = g_list_delete_link(lv->path, g_list_last(lv->path));
181 static void lv_print_key(Visitor *v, const char *name)
183 GList *e;
184 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
185 if (lv->comma) {
186 putchar(',');
187 } else {
188 lv->comma = true;
191 for (e = lv->path; e; e = e->next) {
192 if (e->data) {
193 printf("%s.", (const char *) e->data);
197 printf("%s=", name);
200 static void lv_type_int64(Visitor *v, const char *name, int64_t *obj,
201 Error **errp)
203 lv_print_key(v, name);
204 printf("%" PRIi64, *obj);
207 static void lv_type_uint64(Visitor *v, const char *name, uint64_t *obj,
208 Error **errp)
210 lv_print_key(v, name);
211 printf("%" PRIu64, *obj);
214 static void lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
216 lv_print_key(v, name);
217 printf("%s", *obj ? "on" : "off");
220 static void lv_type_str(Visitor *v, const char *name, char **obj, Error **errp)
222 const char *str = *obj;
223 lv_print_key(v, name);
225 while (*str) {
226 if (*str == ',') {
227 putchar(',');
229 putchar(*str++);
233 static void lv_complete(Visitor *v, void *opaque)
235 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
236 assert(lv->path == NULL);
239 static void lv_free(Visitor *v)
241 LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
243 g_list_free_full(lv->path, g_free);
244 g_free(lv);
247 static Visitor *legacy_visitor_new(void)
249 LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor));
251 lv->visitor.start_struct = lv_start_struct;
252 lv->visitor.end_struct = lv_end_struct;
253 /* lists not supported */
254 lv->visitor.type_int64 = lv_type_int64;
255 lv->visitor.type_uint64 = lv_type_uint64;
256 lv->visitor.type_bool = lv_type_bool;
257 lv->visitor.type_str = lv_type_str;
259 lv->visitor.type = VISITOR_OUTPUT;
260 lv->visitor.complete = lv_complete;
261 lv->visitor.free = lv_free;
263 return &lv->visitor;
266 void audio_legacy_help(void)
268 AudiodevListHead head;
269 AudiodevListEntry *e;
271 printf("Environment variable based configuration deprecated.\n");
272 printf("Please use the new -audiodev option.\n");
274 head = audio_handle_legacy_opts();
275 printf("\nEquivalent -audiodev to your current environment variables:\n");
276 if (!getenv("QEMU_AUDIO_DRV")) {
277 printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
278 "possibilities)\n");
281 QSIMPLEQ_FOREACH(e, &head, next) {
282 Visitor *v;
283 Audiodev *dev = e->dev;
284 printf("-audiodev ");
286 v = legacy_visitor_new();
287 visit_type_Audiodev(v, NULL, &dev, &error_abort);
288 visit_free(v);
290 printf("\n");
292 audio_free_audiodev_list(&head);