| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "alsa.h" |
| 5 |
#include "alsa-sim.h" |
| 6 |
#include "error.h" |
| 7 |
#include "window-iface.h" |
| 8 |
#include "optional-controls.h" |
| 9 |
#include "custom-names.h" |
| 10 |
#include "port-enable.h" |
| 11 |
#include "stereo-link.h" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static void get_and_check_first_compound( |
| 17 |
snd_config_t **config, |
| 18 |
const char *expected_id |
| 19 |
) { |
| 20 |
const char *id, *child_id; |
| 21 |
int err; |
| 22 |
|
| 23 |
err = snd_config_get_id(*config, &id); |
| 24 |
if (err < 0) |
| 25 |
fatal_alsa_error("snd_config_get_id error", err); |
| 26 |
|
| 27 |
if (snd_config_get_type(*config) != SND_CONFIG_TYPE_COMPOUND) { |
| 28 |
printf("config node '%s' is not of type compound\n", id); |
| 29 |
exit(1); |
| 30 |
} |
| 31 |
|
| 32 |
snd_config_iterator_t i = snd_config_iterator_first(*config); |
| 33 |
if (i == snd_config_iterator_end(*config)) { |
| 34 |
printf("compound config node '%s' has no children\n", id); |
| 35 |
exit(1); |
| 36 |
} |
| 37 |
|
| 38 |
snd_config_t *config_child = snd_config_iterator_entry(i); |
| 39 |
err = snd_config_get_id(config_child, &child_id); |
| 40 |
if (err < 0) |
| 41 |
fatal_alsa_error("snd_config_get_id error", err); |
| 42 |
|
| 43 |
if (snd_config_get_type(config_child) != SND_CONFIG_TYPE_COMPOUND) { |
| 44 |
printf("config node %s->%s is not of type compound\n", id, child_id); |
| 45 |
exit(1); |
| 46 |
} |
| 47 |
|
| 48 |
*config = config_child; |
| 49 |
|
| 50 |
if (!expected_id) |
| 51 |
return; |
| 52 |
|
| 53 |
if (!child_id) { |
| 54 |
printf("config node has no id\n"); |
| 55 |
exit(1); |
| 56 |
} |
| 57 |
|
| 58 |
if (strcmp(child_id, expected_id) != 0) { |
| 59 |
printf( |
| 60 |
"found config node %s->%s instead of %s\n", |
| 61 |
id, child_id, expected_id |
| 62 |
); |
| 63 |
exit(1); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static void alsa_parse_enum_items( |
| 68 |
snd_config_t *items, |
| 69 |
struct alsa_elem *elem |
| 70 |
) { |
| 71 |
int count = snd_config_is_array(items); |
| 72 |
if (count < 0) { |
| 73 |
printf("error: parse enum items array value %d\n", count); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
elem->item_count = count; |
| 78 |
elem->item_names = calloc(count, sizeof(char *)); |
| 79 |
|
| 80 |
int item_num = 0; |
| 81 |
|
| 82 |
snd_config_iterator_t i, next; |
| 83 |
snd_config_for_each(i, next, items) { |
| 84 |
snd_config_t *node = snd_config_iterator_entry(i); |
| 85 |
|
| 86 |
const char *key; |
| 87 |
|
| 88 |
int err = snd_config_get_id(node, &key); |
| 89 |
if (err < 0) |
| 90 |
fatal_alsa_error("snd_config_get_id error", err); |
| 91 |
int type = snd_config_get_type(node); |
| 92 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 93 |
printf("error: enum item %s type %d not string\n", key, type); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
const char *s; |
| 98 |
err = snd_config_get_string(node, &s); |
| 99 |
if (err < 0) |
| 100 |
fatal_alsa_error("snd_config_get_string error", err); |
| 101 |
|
| 102 |
elem->item_names[item_num++] = strdup(s); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static void alsa_parse_int_array( |
| 107 |
snd_config_t *node, |
| 108 |
long **int_values |
| 109 |
) { |
| 110 |
int count = snd_config_is_array(node); |
| 111 |
if (count < 0) { |
| 112 |
printf("error: parse int array value %d\n", count); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
*int_values = calloc(count, sizeof(long)); |
| 117 |
|
| 118 |
int item_num = 0; |
| 119 |
|
| 120 |
snd_config_iterator_t i, next; |
| 121 |
snd_config_for_each(i, next, node) { |
| 122 |
snd_config_t *node = snd_config_iterator_entry(i); |
| 123 |
|
| 124 |
const char *key; |
| 125 |
|
| 126 |
int err = snd_config_get_id(node, &key); |
| 127 |
if (err < 0) |
| 128 |
fatal_alsa_error("snd_config_get_id error", err); |
| 129 |
|
| 130 |
int type = snd_config_get_type(node); |
| 131 |
|
| 132 |
if (type == SND_CONFIG_TYPE_STRING) { |
| 133 |
const char *string_value; |
| 134 |
|
| 135 |
err = snd_config_get_string(node, &string_value); |
| 136 |
if (err < 0) |
| 137 |
fatal_alsa_error("snd_config_get_string error", err); |
| 138 |
|
| 139 |
if (strcmp(string_value, "true") == 0) |
| 140 |
(*int_values)[item_num++] = 1; |
| 141 |
|
| 142 |
} else if (type == SND_CONFIG_TYPE_INTEGER) { |
| 143 |
long int_value; |
| 144 |
err = snd_config_get_integer(node, &int_value); |
| 145 |
if (err < 0) |
| 146 |
fatal_alsa_error("snd_config_get_integer error", err); |
| 147 |
|
| 148 |
(*int_values)[item_num++] = int_value; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static void alsa_parse_comment_node( |
| 163 |
snd_config_t *comment, |
| 164 |
struct alsa_elem *elem |
| 165 |
) { |
| 166 |
snd_config_iterator_t i, next; |
| 167 |
snd_config_for_each(i, next, comment) { |
| 168 |
snd_config_t *node = snd_config_iterator_entry(i); |
| 169 |
|
| 170 |
const char *key; |
| 171 |
|
| 172 |
int err = snd_config_get_id(node, &key); |
| 173 |
if (err < 0) |
| 174 |
fatal_alsa_error("snd_config_get_id error", err); |
| 175 |
int type = snd_config_get_type(node); |
| 176 |
|
| 177 |
if (strcmp(key, "access") == 0) { |
| 178 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 179 |
printf("access type not string\n"); |
| 180 |
return; |
| 181 |
} |
| 182 |
const char *access; |
| 183 |
err = snd_config_get_string(node, &access); |
| 184 |
if (err < 0) |
| 185 |
fatal_alsa_error("snd_config_get_string error", err); |
| 186 |
if (strstr(access, "write")) |
| 187 |
elem->is_writable = 1; |
| 188 |
} else if (strcmp(key, "type") == 0) { |
| 189 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 190 |
printf("type type not string\n"); |
| 191 |
return; |
| 192 |
} |
| 193 |
const char *type; |
| 194 |
err = snd_config_get_string(node, &type); |
| 195 |
if (err < 0) |
| 196 |
fatal_alsa_error("snd_config_get_string error", err); |
| 197 |
if (strcmp(type, "BOOLEAN") == 0) |
| 198 |
elem->type = SND_CTL_ELEM_TYPE_BOOLEAN; |
| 199 |
else if (strcmp(type, "ENUMERATED") == 0) |
| 200 |
elem->type = SND_CTL_ELEM_TYPE_ENUMERATED; |
| 201 |
else if (strcmp(type, "INTEGER") == 0) |
| 202 |
elem->type = SND_CTL_ELEM_TYPE_INTEGER; |
| 203 |
} else if (strcmp(key, "count") == 0) { |
| 204 |
long count; |
| 205 |
|
| 206 |
err = snd_config_get_integer(node, &count); |
| 207 |
if (err < 0) |
| 208 |
fatal_alsa_error("snd_config_get_integer error", err); |
| 209 |
|
| 210 |
elem->count = count; |
| 211 |
} else if (strcmp(key, "item") == 0) { |
| 212 |
alsa_parse_enum_items(node, elem); |
| 213 |
} else if (strcmp(key, "range") == 0) { |
| 214 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 215 |
printf("range type not string\n"); |
| 216 |
return; |
| 217 |
} |
| 218 |
const char *range; |
| 219 |
err = snd_config_get_string(node, &range); |
| 220 |
if (err < 0) |
| 221 |
fatal_alsa_error("snd_config_get_string error", err); |
| 222 |
|
| 223 |
|
| 224 |
int min_val, max_val; |
| 225 |
if (sscanf(range, "%d - %d", &min_val, &max_val) == 2) { |
| 226 |
elem->min_val = min_val; |
| 227 |
elem->max_val = max_val; |
| 228 |
} |
| 229 |
} else if (strcmp(key, "dbmin") == 0) { |
| 230 |
if (type != SND_CONFIG_TYPE_INTEGER) { |
| 231 |
printf("dbmin type not integer\n"); |
| 232 |
return; |
| 233 |
} |
| 234 |
long dbmin; |
| 235 |
err = snd_config_get_integer(node, &dbmin); |
| 236 |
if (err < 0) |
| 237 |
fatal_alsa_error("snd_config_get_integer error", err); |
| 238 |
elem->min_cdB = dbmin; |
| 239 |
} else if (strcmp(key, "dbmax") == 0) { |
| 240 |
if (type != SND_CONFIG_TYPE_INTEGER) { |
| 241 |
printf("dbmax type not integer\n"); |
| 242 |
return; |
| 243 |
} |
| 244 |
long dbmax; |
| 245 |
err = snd_config_get_integer(node, &dbmax); |
| 246 |
if (err < 0) |
| 247 |
fatal_alsa_error("snd_config_get_integer error", err); |
| 248 |
elem->max_cdB = dbmax; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
static struct alsa_elem *add_sim_elem( |
| 255 |
struct alsa_card *card, const struct alsa_elem *elem |
| 256 |
) { |
| 257 |
struct alsa_elem *new_elem = malloc(sizeof(struct alsa_elem)); |
| 258 |
*new_elem = *elem; |
| 259 |
g_ptr_array_add(card->elems, new_elem); |
| 260 |
return new_elem; |
| 261 |
} |
| 262 |
|
| 263 |
static int alsa_config_to_new_elem( |
| 264 |
struct alsa_card *card, |
| 265 |
snd_config_t *config |
| 266 |
) { |
| 267 |
const char *s; |
| 268 |
int id; |
| 269 |
char *iface = NULL, *name = NULL; |
| 270 |
int seen_value; |
| 271 |
int value_type = -1; |
| 272 |
char *string_value = NULL; |
| 273 |
long int_value; |
| 274 |
long *int_values = NULL; |
| 275 |
int value_array_count = 0; |
| 276 |
int err; |
| 277 |
|
| 278 |
struct alsa_elem elem = {}; |
| 279 |
|
| 280 |
err = snd_config_get_id(config, &s); |
| 281 |
if (err < 0) |
| 282 |
fatal_alsa_error("snd_config_get_id error", err); |
| 283 |
id = atoi(s); |
| 284 |
|
| 285 |
|
| 286 |
snd_config_iterator_t i, next; |
| 287 |
snd_config_for_each(i, next, config) { |
| 288 |
snd_config_t *node = snd_config_iterator_entry(i); |
| 289 |
|
| 290 |
const char *key; |
| 291 |
|
| 292 |
err = snd_config_get_id(node, &key); |
| 293 |
if (err < 0) |
| 294 |
fatal_alsa_error("snd_config_get_id error", err); |
| 295 |
int type = snd_config_get_type(node); |
| 296 |
|
| 297 |
|
| 298 |
if (strcmp(key, "iface") == 0) { |
| 299 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 300 |
printf("iface type for %d is %d not string", id, type); |
| 301 |
goto fail; |
| 302 |
} |
| 303 |
|
| 304 |
err = snd_config_get_string(node, &s); |
| 305 |
if (err < 0) |
| 306 |
fatal_alsa_error("snd_config_get_string error", err); |
| 307 |
iface = strdup(s); |
| 308 |
|
| 309 |
|
| 310 |
} else if (strcmp(key, "name") == 0) { |
| 311 |
if (type != SND_CONFIG_TYPE_STRING) { |
| 312 |
printf("name type for %d is %d not string", id, type); |
| 313 |
goto fail; |
| 314 |
} |
| 315 |
|
| 316 |
err = snd_config_get_string(node, &s); |
| 317 |
if (err < 0) |
| 318 |
fatal_alsa_error("snd_config_get_string error", err); |
| 319 |
name = strdup(s); |
| 320 |
|
| 321 |
|
| 322 |
} else if (strcmp(key, "value") == 0) { |
| 323 |
seen_value = 1; |
| 324 |
value_type = type; |
| 325 |
|
| 326 |
if (type == SND_CONFIG_TYPE_INTEGER) { |
| 327 |
err = snd_config_get_integer(node, &int_value); |
| 328 |
if (err < 0) |
| 329 |
fatal_alsa_error("snd_config_get_integer error", err); |
| 330 |
} else if (type == SND_CONFIG_TYPE_STRING) { |
| 331 |
err = snd_config_get_string(node, &s); |
| 332 |
if (err < 0) |
| 333 |
fatal_alsa_error("snd_config_get_string error", err); |
| 334 |
string_value = strdup(s); |
| 335 |
} else if (type == SND_CONFIG_TYPE_COMPOUND) { |
| 336 |
value_array_count = snd_config_is_array(node); |
| 337 |
if (strcmp(name, "Level Meter") == 0) { |
| 338 |
elem.count = value_array_count; |
| 339 |
seen_value = 1; |
| 340 |
value_type = SND_CONFIG_TYPE_INTEGER; |
| 341 |
int_value = 0; |
| 342 |
} else { |
| 343 |
seen_value = 1; |
| 344 |
alsa_parse_int_array(node, &int_values); |
| 345 |
} |
| 346 |
} else { |
| 347 |
printf( |
| 348 |
"skipping value type for %d; is %d, not int or string\n", |
| 349 |
id, type |
| 350 |
); |
| 351 |
goto fail; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
} else if (strcmp(key, "comment") == 0) { |
| 356 |
alsa_parse_comment_node(node, &elem); |
| 357 |
|
| 358 |
|
| 359 |
} else if (strcmp(key, "index") == 0) { |
| 360 |
|
| 361 |
} else { |
| 362 |
printf("skipping unknown node %s for %d\n", key, id); |
| 363 |
goto fail; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
if (!iface) { |
| 369 |
printf("missing iface node in control id %d\n", id); |
| 370 |
goto fail; |
| 371 |
} |
| 372 |
if (strcmp(iface, "CARD") != 0 && |
| 373 |
strcmp(iface, "MIXER") != 0 && |
| 374 |
strcmp(iface, "PCM") != 0) |
| 375 |
goto fail; |
| 376 |
|
| 377 |
|
| 378 |
if (!name) { |
| 379 |
printf("missing name node in control id %d\n", id); |
| 380 |
goto fail; |
| 381 |
} |
| 382 |
if (!seen_value) { |
| 383 |
printf("missing value node in control id %d\n", id); |
| 384 |
goto fail; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if (value_type == SND_CONFIG_TYPE_INTEGER) { |
| 391 |
elem.value = int_value; |
| 392 |
|
| 393 |
|
| 394 |
} else if (value_type == SND_CONFIG_TYPE_STRING) { |
| 395 |
|
| 396 |
|
| 397 |
if (elem.type == SND_CTL_ELEM_TYPE_BOOLEAN) { |
| 398 |
if (strcmp(string_value, "true") == 0) |
| 399 |
elem.value = 1; |
| 400 |
|
| 401 |
|
| 402 |
} else if (elem.type == SND_CTL_ELEM_TYPE_ENUMERATED) { |
| 403 |
for (int i = 0; i < elem.item_count; i++) { |
| 404 |
if (strcmp(string_value, elem.item_names[i]) == 0) { |
| 405 |
elem.value = i; |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
} else { |
| 412 |
goto fail; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
elem.card = card; |
| 417 |
elem.numid = id; |
| 418 |
elem.name = name; |
| 419 |
|
| 420 |
if (int_values && strncmp(elem.name, "Master", 6) == 0) { |
| 421 |
|
| 422 |
int n = value_array_count < elem.count |
| 423 |
? value_array_count : elem.count; |
| 424 |
elem.count = 1; |
| 425 |
for (int i = 0; i < n; i++, elem.index++) { |
| 426 |
elem.value = int_values[i]; |
| 427 |
if (i > 0) |
| 428 |
elem.name = strdup(name); |
| 429 |
add_sim_elem(card, &elem); |
| 430 |
} |
| 431 |
} else if (int_values) { |
| 432 |
|
| 433 |
struct alsa_elem *new_elem = add_sim_elem(card, &elem); |
| 434 |
new_elem->values = malloc(elem.count * sizeof(long)); |
| 435 |
memcpy(new_elem->values, int_values, elem.count * sizeof(long)); |
| 436 |
} else { |
| 437 |
|
| 438 |
add_sim_elem(card, &elem); |
| 439 |
} |
| 440 |
|
| 441 |
free(iface); |
| 442 |
free(string_value); |
| 443 |
free(int_values); |
| 444 |
|
| 445 |
return 0; |
| 446 |
|
| 447 |
fail: |
| 448 |
free(iface); |
| 449 |
free(name); |
| 450 |
free(string_value); |
| 451 |
free(int_values); |
| 452 |
|
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
static void alsa_config_to_new_card( |
| 457 |
snd_config_t *top, |
| 458 |
struct alsa_card *card |
| 459 |
) { |
| 460 |
snd_config_t *config = top; |
| 461 |
|
| 462 |
|
| 463 |
get_and_check_first_compound(&config, "state"); |
| 464 |
get_and_check_first_compound(&config, NULL); |
| 465 |
get_and_check_first_compound(&config, "control"); |
| 466 |
|
| 467 |
|
| 468 |
snd_config_iterator_t i, next; |
| 469 |
snd_config_for_each(i, next, config) { |
| 470 |
snd_config_t *node = snd_config_iterator_entry(i); |
| 471 |
|
| 472 |
|
| 473 |
if (snd_config_get_type(config) != SND_CONFIG_TYPE_COMPOUND) |
| 474 |
continue; |
| 475 |
|
| 476 |
|
| 477 |
alsa_config_to_new_elem(card, node); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
static char *sim_card_name(const char *fn) { |
| 484 |
|
| 485 |
|
| 486 |
const char *base = strrchr(fn, '/'); |
| 487 |
char *name; |
| 488 |
if (base) |
| 489 |
name = strdup(base + 1); |
| 490 |
else |
| 491 |
name = strdup(fn); |
| 492 |
|
| 493 |
|
| 494 |
char *dot = strrchr(name, '.'); |
| 495 |
if (dot) |
| 496 |
*dot = '\0'; |
| 497 |
|
| 498 |
return name; |
| 499 |
} |
| 500 |
|
| 501 |
void create_sim_from_file(GtkWindow *w, char *fn) { |
| 502 |
snd_config_t *config; |
| 503 |
snd_input_t *in; |
| 504 |
|
| 505 |
int err; |
| 506 |
|
| 507 |
err = snd_config_top(&config); |
| 508 |
if (err < 0) |
| 509 |
fatal_alsa_error("snd_config_top error", err); |
| 510 |
|
| 511 |
err = snd_input_stdio_open(&in, fn, "r"); |
| 512 |
if (err < 0) { |
| 513 |
char *s = g_strdup_printf("Error opening %s: %s", fn, snd_strerror(err)); |
| 514 |
show_error(w, s); |
| 515 |
free(s); |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
err = snd_config_load(config, in); |
| 520 |
snd_input_close(in); |
| 521 |
if (err < 0) |
| 522 |
fatal_alsa_error("snd_config_load error", err); |
| 523 |
|
| 524 |
struct alsa_card *card = card_create(SIMULATED_CARD_NUM); |
| 525 |
card->name = sim_card_name(fn); |
| 526 |
card->serial = strdup(card->name); |
| 527 |
alsa_config_to_new_card(config, card); |
| 528 |
|
| 529 |
snd_config_delete(config); |
| 530 |
|
| 531 |
alsa_set_lr_nums(card); |
| 532 |
alsa_get_routing_controls(card); |
| 533 |
alsa_init_mixer_gains_cache(card); |
| 534 |
optional_controls_init(card); |
| 535 |
custom_names_init(card); |
| 536 |
port_enable_init(card); |
| 537 |
stereo_link_init(card); |
| 538 |
|
| 539 |
create_card_window(card); |
| 540 |
} |