| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "alsa.h" |
| 5 |
#include "alsa-sim.h" |
| 6 |
#include "error.h" |
| 7 |
#include "file.h" |
| 8 |
#include "optional-state.h" |
| 9 |
#include "stringhelper.h" |
| 10 |
|
| 11 |
static void run_alsactl( |
| 12 |
struct alsa_card *card, |
| 13 |
char *cmd, |
| 14 |
char *fn |
| 15 |
) { |
| 16 |
GtkWindow *w = GTK_WINDOW(card->window_main); |
| 17 |
|
| 18 |
gchar *alsactl_path = g_find_program_in_path("alsactl"); |
| 19 |
|
| 20 |
if (!alsactl_path) |
| 21 |
alsactl_path = g_strdup("/usr/sbin/alsactl"); |
| 22 |
|
| 23 |
gchar *argv[] = { |
| 24 |
alsactl_path, cmd, card->device, "-I", "-f", fn, NULL |
| 25 |
}; |
| 26 |
|
| 27 |
gchar *stdout; |
| 28 |
gchar *stderr; |
| 29 |
gint exit_status; |
| 30 |
GError *error = NULL; |
| 31 |
|
| 32 |
gboolean result = g_spawn_sync( |
| 33 |
NULL, |
| 34 |
argv, |
| 35 |
NULL, |
| 36 |
G_SPAWN_SEARCH_PATH, |
| 37 |
NULL, |
| 38 |
NULL, |
| 39 |
&stdout, |
| 40 |
&stderr, |
| 41 |
&exit_status, |
| 42 |
&error |
| 43 |
); |
| 44 |
|
| 45 |
if (result && WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == 0) |
| 46 |
goto done; |
| 47 |
|
| 48 |
char *error_message = |
| 49 |
result |
| 50 |
? g_strdup_printf("%s\n%s", stdout, stderr) |
| 51 |
: g_strdup_printf("%s", error->message); |
| 52 |
|
| 53 |
char *msg = g_strdup_printf( |
| 54 |
"Error running “alsactl %s %s -f %s”: %s", |
| 55 |
cmd, card->device, fn, error_message |
| 56 |
); |
| 57 |
show_error(w, msg); |
| 58 |
g_free(msg); |
| 59 |
g_free(error_message); |
| 60 |
|
| 61 |
done: |
| 62 |
g_free(alsactl_path); |
| 63 |
g_free(stdout); |
| 64 |
g_free(stderr); |
| 65 |
if (error) |
| 66 |
g_error_free(error); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static char *elem_value_to_string(struct alsa_elem *elem) { |
| 71 |
int type = elem->type; |
| 72 |
|
| 73 |
if (type == SND_CTL_ELEM_TYPE_BOOLEAN) { |
| 74 |
return g_strdup(alsa_get_elem_value(elem) ? "true" : "false"); |
| 75 |
} else if (type == SND_CTL_ELEM_TYPE_ENUMERATED) { |
| 76 |
long value = alsa_get_elem_value(elem); |
| 77 |
char *item_name = alsa_get_item_name(elem, value); |
| 78 |
return g_strdup(item_name ? item_name : ""); |
| 79 |
} else if (type == SND_CTL_ELEM_TYPE_INTEGER) { |
| 80 |
int count = elem->count; |
| 81 |
|
| 82 |
|
| 83 |
if (count <= 1) |
| 84 |
return g_strdup_printf("%ld", alsa_get_elem_value(elem)); |
| 85 |
|
| 86 |
|
| 87 |
long *values = alsa_get_elem_int_values(elem); |
| 88 |
GString *str = g_string_new(NULL); |
| 89 |
|
| 90 |
for (int i = 0; i < count; i++) { |
| 91 |
if (i > 0) |
| 92 |
g_string_append_c(str, ','); |
| 93 |
g_string_append_printf(str, "%ld", values[i]); |
| 94 |
} |
| 95 |
|
| 96 |
free(values); |
| 97 |
return g_string_free(str, FALSE); |
| 98 |
} else if (type == SND_CTL_ELEM_TYPE_BYTES) { |
| 99 |
|
| 100 |
size_t size; |
| 101 |
const void *data = alsa_get_elem_bytes(elem, &size); |
| 102 |
if (!data || size == 0) |
| 103 |
return g_strdup(""); |
| 104 |
|
| 105 |
|
| 106 |
size_t len = strnlen(data, size); |
| 107 |
return g_strndup(data, len); |
| 108 |
} |
| 109 |
|
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static int should_save_elem(struct alsa_elem *elem) { |
| 115 |
|
| 116 |
if (alsa_get_elem_volatile(elem)) |
| 117 |
return 0; |
| 118 |
|
| 119 |
|
| 120 |
if (!alsa_get_elem_writable(elem)) |
| 121 |
return 0; |
| 122 |
|
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
int save_native(struct alsa_card *card, const char *path) { |
| 128 |
GKeyFile *key_file = g_key_file_new(); |
| 129 |
GError *error = NULL; |
| 130 |
|
| 131 |
|
| 132 |
if (card->serial && *card->serial) |
| 133 |
g_key_file_set_string( |
| 134 |
key_file, CONFIG_SECTION_DEVICE, "serial", card->serial |
| 135 |
); |
| 136 |
if (card->name) |
| 137 |
g_key_file_set_string( |
| 138 |
key_file, CONFIG_SECTION_DEVICE, "model", card->name |
| 139 |
); |
| 140 |
|
| 141 |
|
| 142 |
for (guint i = 0; i < card->elems->len; i++) { |
| 143 |
struct alsa_elem *elem = g_ptr_array_index(card->elems, i); |
| 144 |
|
| 145 |
if (!should_save_elem(elem)) |
| 146 |
continue; |
| 147 |
|
| 148 |
char *value_str = elem_value_to_string(elem); |
| 149 |
if (!value_str) |
| 150 |
continue; |
| 151 |
|
| 152 |
g_key_file_set_string( |
| 153 |
key_file, |
| 154 |
CONFIG_SECTION_CONTROLS, |
| 155 |
elem->name, |
| 156 |
value_str |
| 157 |
); |
| 158 |
|
| 159 |
g_free(value_str); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
int ret = 0; |
| 164 |
if (!g_key_file_save_to_file(key_file, path, &error)) { |
| 165 |
ret = -1; |
| 166 |
if (error) |
| 167 |
g_error_free(error); |
| 168 |
} |
| 169 |
|
| 170 |
g_key_file_free(key_file); |
| 171 |
return ret; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static int string_to_elem_value( |
| 176 |
struct alsa_elem *elem, |
| 177 |
const char *str, |
| 178 |
long *value |
| 179 |
) { |
| 180 |
int type = elem->type; |
| 181 |
|
| 182 |
if (type == SND_CTL_ELEM_TYPE_BOOLEAN) { |
| 183 |
if (strcmp(str, "true") == 0 || strcmp(str, "1") == 0) |
| 184 |
*value = 1; |
| 185 |
else |
| 186 |
*value = 0; |
| 187 |
return 0; |
| 188 |
} else if (type == SND_CTL_ELEM_TYPE_ENUMERATED) { |
| 189 |
|
| 190 |
int count = alsa_get_item_count(elem); |
| 191 |
for (int i = 0; i < count; i++) { |
| 192 |
char *item_name = alsa_get_item_name(elem, i); |
| 193 |
if (item_name && strcmp(item_name, str) == 0) { |
| 194 |
*value = i; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
char *end; |
| 200 |
long v = strtol(str, &end, 10); |
| 201 |
if (end != str && *end == '\0') { |
| 202 |
*value = v; |
| 203 |
return 0; |
| 204 |
} |
| 205 |
return -1; |
| 206 |
} else if (type == SND_CTL_ELEM_TYPE_INTEGER) { |
| 207 |
char *end; |
| 208 |
long v = strtol(str, &end, 10); |
| 209 |
if (end != str && (*end == '\0' || *end == ',')) { |
| 210 |
*value = v; |
| 211 |
return 0; |
| 212 |
} |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
return -1; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
static void set_elem_from_string(struct alsa_elem *elem, const char *str) { |
| 221 |
int type = elem->type; |
| 222 |
|
| 223 |
|
| 224 |
if (type == SND_CTL_ELEM_TYPE_INTEGER && elem->count > 1) { |
| 225 |
long *values = calloc(elem->count, sizeof(long)); |
| 226 |
const char *p = str; |
| 227 |
int i = 0; |
| 228 |
|
| 229 |
while (*p && i < elem->count) { |
| 230 |
char *end; |
| 231 |
values[i] = strtol(p, &end, 10); |
| 232 |
if (end == p) |
| 233 |
break; |
| 234 |
i++; |
| 235 |
p = end; |
| 236 |
if (*p == ',') |
| 237 |
p++; |
| 238 |
} |
| 239 |
|
| 240 |
if (i > 0) { |
| 241 |
alsa_set_elem_int_values(elem, values, i); |
| 242 |
alsa_elem_change(elem); |
| 243 |
} |
| 244 |
|
| 245 |
free(values); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if (type == SND_CTL_ELEM_TYPE_BYTES) { |
| 251 |
size_t len = strlen(str); |
| 252 |
alsa_set_elem_bytes(elem, str, len); |
| 253 |
|
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
long value; |
| 259 |
if (string_to_elem_value(elem, str, &value) == 0) { |
| 260 |
alsa_set_elem_value(elem, value); |
| 261 |
|
| 262 |
alsa_elem_change(elem); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
int load_native(struct alsa_card *card, const char *path) { |
| 268 |
GKeyFile *key_file = g_key_file_new(); |
| 269 |
GError *error = NULL; |
| 270 |
|
| 271 |
if (!g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, &error)) { |
| 272 |
g_key_file_free(key_file); |
| 273 |
if (error) |
| 274 |
g_error_free(error); |
| 275 |
return -1; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
gsize num_keys; |
| 280 |
gchar **keys = g_key_file_get_keys( |
| 281 |
key_file, CONFIG_SECTION_CONTROLS, &num_keys, NULL |
| 282 |
); |
| 283 |
|
| 284 |
if (!keys) { |
| 285 |
g_key_file_free(key_file); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
for (int pass = 0; pass < 2; pass++) { |
| 292 |
|
| 293 |
|
| 294 |
for (gsize i = 0; i < num_keys; i++) { |
| 295 |
gchar *value = g_key_file_get_string( |
| 296 |
key_file, CONFIG_SECTION_CONTROLS, keys[i], NULL |
| 297 |
); |
| 298 |
|
| 299 |
if (!value) |
| 300 |
continue; |
| 301 |
|
| 302 |
|
| 303 |
struct alsa_elem *elem = get_elem_by_name(card->elems, keys[i]); |
| 304 |
if (!elem) { |
| 305 |
g_free(value); |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
if (!alsa_get_elem_writable(elem)) { |
| 311 |
g_free(value); |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
set_elem_from_string(elem, value); |
| 317 |
|
| 318 |
g_free(value); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
g_strfreev(keys); |
| 323 |
g_key_file_free(key_file); |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
static GListModel *create_file_filters(void) { |
| 330 |
GListStore *filters = g_list_store_new(GTK_TYPE_FILE_FILTER); |
| 331 |
|
| 332 |
GtkFileFilter *conf_filter = gtk_file_filter_new(); |
| 333 |
gtk_file_filter_set_name(conf_filter, "alsa-scarlett-gui config (.conf)"); |
| 334 |
gtk_file_filter_add_pattern(conf_filter, "*.conf"); |
| 335 |
g_list_store_append(filters, conf_filter); |
| 336 |
g_object_unref(conf_filter); |
| 337 |
|
| 338 |
GtkFileFilter *state_filter = gtk_file_filter_new(); |
| 339 |
gtk_file_filter_set_name(state_filter, "alsactl state file (.state)"); |
| 340 |
gtk_file_filter_add_pattern(state_filter, "*.state"); |
| 341 |
g_list_store_append(filters, state_filter); |
| 342 |
g_object_unref(state_filter); |
| 343 |
|
| 344 |
return G_LIST_MODEL(filters); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
static GListModel *create_state_filter(void) { |
| 349 |
GListStore *filters = g_list_store_new(GTK_TYPE_FILE_FILTER); |
| 350 |
|
| 351 |
GtkFileFilter *filter = gtk_file_filter_new(); |
| 352 |
gtk_file_filter_set_name(filter, "alsactl state file (.state)"); |
| 353 |
gtk_file_filter_add_pattern(filter, "*.state"); |
| 354 |
g_list_store_append(filters, filter); |
| 355 |
g_object_unref(filter); |
| 356 |
|
| 357 |
return G_LIST_MODEL(filters); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
static void load_dialog_complete( |
| 362 |
GObject *source, |
| 363 |
GAsyncResult *result, |
| 364 |
gpointer data |
| 365 |
) { |
| 366 |
struct alsa_card *card = data; |
| 367 |
GtkWindow *w = GTK_WINDOW(card->window_main); |
| 368 |
GtkFileDialog *dialog = GTK_FILE_DIALOG(source); |
| 369 |
GError *error = NULL; |
| 370 |
|
| 371 |
GFile *file = gtk_file_dialog_open_finish(dialog, result, &error); |
| 372 |
if (!file) { |
| 373 |
if (error && !g_error_matches(error, GTK_DIALOG_ERROR, |
| 374 |
GTK_DIALOG_ERROR_DISMISSED)) |
| 375 |
show_error(w, error->message); |
| 376 |
g_clear_error(&error); |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
char *fn = g_file_get_path(file); |
| 381 |
|
| 382 |
|
| 383 |
if (string_ends_with(fn, ".conf")) { |
| 384 |
if (load_native(card, fn) < 0) { |
| 385 |
char *msg = g_strdup_printf("Error loading from %s", fn); |
| 386 |
show_error(w, msg); |
| 387 |
g_free(msg); |
| 388 |
} |
| 389 |
} else { |
| 390 |
run_alsactl(card, "restore", fn); |
| 391 |
} |
| 392 |
|
| 393 |
g_free(fn); |
| 394 |
g_object_unref(file); |
| 395 |
} |
| 396 |
|
| 397 |
void activate_load( |
| 398 |
GSimpleAction *action, |
| 399 |
GVariant *parameter, |
| 400 |
gpointer data |
| 401 |
) { |
| 402 |
struct alsa_card *card = data; |
| 403 |
|
| 404 |
GtkFileDialog *dialog = gtk_file_dialog_new(); |
| 405 |
gtk_file_dialog_set_title(dialog, "Load Configuration"); |
| 406 |
|
| 407 |
GListModel *filters = create_file_filters(); |
| 408 |
gtk_file_dialog_set_filters(dialog, filters); |
| 409 |
g_object_unref(filters); |
| 410 |
|
| 411 |
gtk_file_dialog_open( |
| 412 |
dialog, |
| 413 |
GTK_WINDOW(card->window_main), |
| 414 |
NULL, |
| 415 |
load_dialog_complete, |
| 416 |
card |
| 417 |
); |
| 418 |
|
| 419 |
g_object_unref(dialog); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
static void save_dialog_complete( |
| 424 |
GObject *source, |
| 425 |
GAsyncResult *result, |
| 426 |
gpointer data |
| 427 |
) { |
| 428 |
struct alsa_card *card = data; |
| 429 |
GtkWindow *w = GTK_WINDOW(card->window_main); |
| 430 |
GtkFileDialog *dialog = GTK_FILE_DIALOG(source); |
| 431 |
GError *error = NULL; |
| 432 |
|
| 433 |
GFile *file = gtk_file_dialog_save_finish(dialog, result, &error); |
| 434 |
if (!file) { |
| 435 |
if (error && !g_error_matches(error, GTK_DIALOG_ERROR, |
| 436 |
GTK_DIALOG_ERROR_DISMISSED)) |
| 437 |
show_error(w, error->message); |
| 438 |
g_clear_error(&error); |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
char *fn = g_file_get_path(file); |
| 443 |
|
| 444 |
|
| 445 |
int use_native = string_ends_with(fn, ".conf"); |
| 446 |
int use_state = string_ends_with(fn, ".state"); |
| 447 |
|
| 448 |
|
| 449 |
char *fn_with_ext; |
| 450 |
if (!use_native && !use_state) { |
| 451 |
fn_with_ext = g_strdup_printf("%s.conf", fn); |
| 452 |
use_native = 1; |
| 453 |
} else { |
| 454 |
fn_with_ext = g_strdup(fn); |
| 455 |
} |
| 456 |
|
| 457 |
if (use_native) { |
| 458 |
if (save_native(card, fn_with_ext) < 0) { |
| 459 |
char *msg = g_strdup_printf("Error saving to %s", fn_with_ext); |
| 460 |
show_error(w, msg); |
| 461 |
g_free(msg); |
| 462 |
} |
| 463 |
} else { |
| 464 |
run_alsactl(card, "store", fn_with_ext); |
| 465 |
} |
| 466 |
|
| 467 |
g_free(fn); |
| 468 |
g_free(fn_with_ext); |
| 469 |
g_object_unref(file); |
| 470 |
} |
| 471 |
|
| 472 |
void activate_save( |
| 473 |
GSimpleAction *action, |
| 474 |
GVariant *parameter, |
| 475 |
gpointer data |
| 476 |
) { |
| 477 |
struct alsa_card *card = data; |
| 478 |
|
| 479 |
GtkFileDialog *dialog = gtk_file_dialog_new(); |
| 480 |
gtk_file_dialog_set_title(dialog, "Save Configuration"); |
| 481 |
|
| 482 |
GListModel *filters = create_file_filters(); |
| 483 |
gtk_file_dialog_set_filters(dialog, filters); |
| 484 |
g_object_unref(filters); |
| 485 |
|
| 486 |
gtk_file_dialog_save( |
| 487 |
dialog, |
| 488 |
GTK_WINDOW(card->window_main), |
| 489 |
NULL, |
| 490 |
save_dialog_complete, |
| 491 |
card |
| 492 |
); |
| 493 |
|
| 494 |
g_object_unref(dialog); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
static void sim_dialog_complete( |
| 499 |
GObject *source, |
| 500 |
GAsyncResult *result, |
| 501 |
gpointer data |
| 502 |
) { |
| 503 |
GtkWindow *w = data; |
| 504 |
GtkFileDialog *dialog = GTK_FILE_DIALOG(source); |
| 505 |
GError *error = NULL; |
| 506 |
|
| 507 |
GFile *file = gtk_file_dialog_open_finish(dialog, result, &error); |
| 508 |
if (!file) { |
| 509 |
if (error && !g_error_matches(error, GTK_DIALOG_ERROR, |
| 510 |
GTK_DIALOG_ERROR_DISMISSED)) |
| 511 |
show_error(w, error->message); |
| 512 |
g_clear_error(&error); |
| 513 |
return; |
| 514 |
} |
| 515 |
|
| 516 |
char *fn = g_file_get_path(file); |
| 517 |
|
| 518 |
create_sim_from_file(w, fn); |
| 519 |
|
| 520 |
g_free(fn); |
| 521 |
g_object_unref(file); |
| 522 |
} |
| 523 |
|
| 524 |
void activate_sim( |
| 525 |
GSimpleAction *action, |
| 526 |
GVariant *parameter, |
| 527 |
gpointer data |
| 528 |
) { |
| 529 |
GtkWidget *w = data; |
| 530 |
|
| 531 |
GtkFileDialog *dialog = gtk_file_dialog_new(); |
| 532 |
gtk_file_dialog_set_title( |
| 533 |
dialog, "Load Configuration File for Interface Simulation" |
| 534 |
); |
| 535 |
|
| 536 |
GListModel *filters = create_state_filter(); |
| 537 |
gtk_file_dialog_set_filters(dialog, filters); |
| 538 |
g_object_unref(filters); |
| 539 |
|
| 540 |
gtk_file_dialog_open( |
| 541 |
dialog, |
| 542 |
GTK_WINDOW(w), |
| 543 |
NULL, |
| 544 |
sim_dialog_complete, |
| 545 |
w |
| 546 |
); |
| 547 |
|
| 548 |
g_object_unref(dialog); |
| 549 |
} |