| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <getopt.h> |
| 10 |
#include <alsa/asoundlib.h> |
| 11 |
#include <glib.h> |
| 12 |
|
| 13 |
#define CONFIG_SECTION_CONTROLS "controls" |
| 14 |
|
| 15 |
struct cli_elem { |
| 16 |
int numid; |
| 17 |
char *name; |
| 18 |
int type; |
| 19 |
int count; |
| 20 |
}; |
| 21 |
|
| 22 |
static void print_usage(void) { |
| 23 |
fprintf(stderr, "alsa-scarlett-config: Load configuration to Focusrite device\n"); |
| 24 |
fprintf(stderr, "\n"); |
| 25 |
fprintf(stderr, "Usage: alsa-scarlett-config [-d <device>] <config.conf>\n"); |
| 26 |
fprintf(stderr, " alsa-scarlett-config -l\n"); |
| 27 |
fprintf(stderr, "\n"); |
| 28 |
fprintf(stderr, "Options:\n"); |
| 29 |
fprintf(stderr, " -d, --device <device> ALSA device (auto-detected if only one)\n"); |
| 30 |
fprintf(stderr, " -l, --list List available Focusrite devices\n"); |
| 31 |
fprintf(stderr, " -v, --verbose Show each control as it's set\n"); |
| 32 |
fprintf(stderr, " -n, --dry-run Parse config but don't apply changes\n"); |
| 33 |
fprintf(stderr, " -h, --help Show this help\n"); |
| 34 |
} |
| 35 |
|
| 36 |
static int is_focusrite_card(const char *name) { |
| 37 |
return strncmp(name, "Scarlett", 8) == 0 || |
| 38 |
strncmp(name, "Clarett", 7) == 0 || |
| 39 |
strncmp(name, "Vocaster", 8) == 0; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static int find_focusrite_devices(char *out_device, size_t out_size) { |
| 45 |
snd_ctl_card_info_t *info; |
| 46 |
snd_ctl_t *ctl; |
| 47 |
int card_num = -1; |
| 48 |
int count = 0; |
| 49 |
int found_card = -1; |
| 50 |
|
| 51 |
snd_ctl_card_info_alloca(&info); |
| 52 |
|
| 53 |
while (snd_card_next(&card_num) >= 0 && card_num >= 0) { |
| 54 |
char device[32]; |
| 55 |
snprintf(device, sizeof(device), "hw:%d", card_num); |
| 56 |
|
| 57 |
if (snd_ctl_open(&ctl, device, 0) < 0) |
| 58 |
continue; |
| 59 |
|
| 60 |
if (snd_ctl_card_info(ctl, info) >= 0) { |
| 61 |
const char *name = snd_ctl_card_info_get_name(info); |
| 62 |
if (is_focusrite_card(name)) { |
| 63 |
count++; |
| 64 |
found_card = card_num; |
| 65 |
} |
| 66 |
} |
| 67 |
snd_ctl_close(ctl); |
| 68 |
} |
| 69 |
|
| 70 |
if (out_device && count == 1) |
| 71 |
snprintf(out_device, out_size, "hw:%d", found_card); |
| 72 |
|
| 73 |
return count; |
| 74 |
} |
| 75 |
|
| 76 |
static void list_devices(void) { |
| 77 |
snd_ctl_card_info_t *info; |
| 78 |
snd_ctl_t *ctl; |
| 79 |
int card_num = -1; |
| 80 |
int found = 0; |
| 81 |
|
| 82 |
snd_ctl_card_info_alloca(&info); |
| 83 |
|
| 84 |
printf("Available Focusrite devices:\n"); |
| 85 |
|
| 86 |
while (snd_card_next(&card_num) >= 0 && card_num >= 0) { |
| 87 |
char device[32]; |
| 88 |
snprintf(device, sizeof(device), "hw:%d", card_num); |
| 89 |
|
| 90 |
if (snd_ctl_open(&ctl, device, 0) < 0) |
| 91 |
continue; |
| 92 |
|
| 93 |
if (snd_ctl_card_info(ctl, info) >= 0) { |
| 94 |
const char *name = snd_ctl_card_info_get_name(info); |
| 95 |
if (is_focusrite_card(name)) { |
| 96 |
printf(" hw:%d %s\n", card_num, name); |
| 97 |
found = 1; |
| 98 |
} |
| 99 |
} |
| 100 |
snd_ctl_close(ctl); |
| 101 |
} |
| 102 |
|
| 103 |
if (!found) |
| 104 |
printf(" (none found)\n"); |
| 105 |
} |
| 106 |
|
| 107 |
static struct cli_elem *find_elem_by_name(GArray *elems, const char *name) { |
| 108 |
for (guint i = 0; i < elems->len; i++) { |
| 109 |
struct cli_elem *elem = &g_array_index(elems, struct cli_elem, i); |
| 110 |
if (strcmp(elem->name, name) == 0) |
| 111 |
return elem; |
| 112 |
} |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
static GArray *scan_elements(snd_ctl_t *handle) { |
| 117 |
snd_ctl_elem_list_t *list; |
| 118 |
snd_ctl_elem_info_t *elem_info; |
| 119 |
int count; |
| 120 |
|
| 121 |
snd_ctl_elem_list_alloca(&list); |
| 122 |
snd_ctl_elem_info_alloca(&elem_info); |
| 123 |
|
| 124 |
snd_ctl_elem_list(handle, list); |
| 125 |
count = snd_ctl_elem_list_get_count(list); |
| 126 |
snd_ctl_elem_list_alloc_space(list, count); |
| 127 |
snd_ctl_elem_list(handle, list); |
| 128 |
|
| 129 |
GArray *elems = g_array_new(FALSE, TRUE, sizeof(struct cli_elem)); |
| 130 |
|
| 131 |
for (int i = 0; i < count; i++) { |
| 132 |
int numid = snd_ctl_elem_list_get_numid(list, i); |
| 133 |
|
| 134 |
snd_ctl_elem_info_set_numid(elem_info, numid); |
| 135 |
if (snd_ctl_elem_info(handle, elem_info) < 0) |
| 136 |
continue; |
| 137 |
|
| 138 |
int type = snd_ctl_elem_info_get_type(elem_info); |
| 139 |
|
| 140 |
if (type != SND_CTL_ELEM_TYPE_BOOLEAN && |
| 141 |
type != SND_CTL_ELEM_TYPE_ENUMERATED && |
| 142 |
type != SND_CTL_ELEM_TYPE_INTEGER && |
| 143 |
type != SND_CTL_ELEM_TYPE_BYTES) |
| 144 |
continue; |
| 145 |
|
| 146 |
if (!snd_ctl_elem_info_is_writable(elem_info)) |
| 147 |
continue; |
| 148 |
|
| 149 |
struct cli_elem elem = { |
| 150 |
.numid = numid, |
| 151 |
.name = strdup(snd_ctl_elem_info_get_name(elem_info)), |
| 152 |
.type = type, |
| 153 |
.count = snd_ctl_elem_info_get_count(elem_info) |
| 154 |
}; |
| 155 |
|
| 156 |
g_array_append_val(elems, elem); |
| 157 |
} |
| 158 |
|
| 159 |
snd_ctl_elem_list_free_space(list); |
| 160 |
return elems; |
| 161 |
} |
| 162 |
|
| 163 |
static int get_enum_item_by_name( |
| 164 |
snd_ctl_t *handle, int numid, const char *item_name |
| 165 |
) { |
| 166 |
snd_ctl_elem_info_t *info; |
| 167 |
snd_ctl_elem_info_alloca(&info); |
| 168 |
snd_ctl_elem_info_set_numid(info, numid); |
| 169 |
|
| 170 |
if (snd_ctl_elem_info(handle, info) < 0) |
| 171 |
return -1; |
| 172 |
|
| 173 |
int items = snd_ctl_elem_info_get_items(info); |
| 174 |
for (int i = 0; i < items; i++) { |
| 175 |
snd_ctl_elem_info_set_item(info, i); |
| 176 |
if (snd_ctl_elem_info(handle, info) < 0) |
| 177 |
continue; |
| 178 |
const char *name = snd_ctl_elem_info_get_item_name(info); |
| 179 |
if (strcmp(name, item_name) == 0) |
| 180 |
return i; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
char *end; |
| 185 |
long v = strtol(item_name, &end, 10); |
| 186 |
if (end != item_name && *end == '\0') |
| 187 |
return (int)v; |
| 188 |
|
| 189 |
return -1; |
| 190 |
} |
| 191 |
|
| 192 |
static int set_elem_value( |
| 193 |
snd_ctl_t *handle, |
| 194 |
struct cli_elem *elem, |
| 195 |
const char *str, |
| 196 |
int verbose, |
| 197 |
int dry_run |
| 198 |
) { |
| 199 |
snd_ctl_elem_value_t *value; |
| 200 |
snd_ctl_elem_value_alloca(&value); |
| 201 |
snd_ctl_elem_value_set_numid(value, elem->numid); |
| 202 |
|
| 203 |
if (elem->type == SND_CTL_ELEM_TYPE_BOOLEAN) { |
| 204 |
int v = (strcmp(str, "true") == 0 || strcmp(str, "1") == 0) ? 1 : 0; |
| 205 |
snd_ctl_elem_value_set_boolean(value, 0, v); |
| 206 |
|
| 207 |
if (verbose) |
| 208 |
printf(" %s = %s\n", elem->name, v ? "true" : "false"); |
| 209 |
|
| 210 |
} else if (elem->type == SND_CTL_ELEM_TYPE_ENUMERATED) { |
| 211 |
int v = get_enum_item_by_name(handle, elem->numid, str); |
| 212 |
if (v < 0) { |
| 213 |
fprintf(stderr, "Warning: Unknown enum value '%s' for '%s'\n", |
| 214 |
str, elem->name); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
snd_ctl_elem_value_set_enumerated(value, 0, v); |
| 218 |
|
| 219 |
if (verbose) |
| 220 |
printf(" %s = %s\n", elem->name, str); |
| 221 |
|
| 222 |
} else if (elem->type == SND_CTL_ELEM_TYPE_INTEGER) { |
| 223 |
|
| 224 |
const char *p = str; |
| 225 |
int idx = 0; |
| 226 |
|
| 227 |
while (*p && idx < elem->count) { |
| 228 |
char *end; |
| 229 |
long v = strtol(p, &end, 10); |
| 230 |
if (end == p) |
| 231 |
break; |
| 232 |
snd_ctl_elem_value_set_integer(value, idx++, v); |
| 233 |
p = end; |
| 234 |
if (*p == ',') |
| 235 |
p++; |
| 236 |
} |
| 237 |
|
| 238 |
if (verbose) |
| 239 |
printf(" %s = %s\n", elem->name, str); |
| 240 |
|
| 241 |
} else if (elem->type == SND_CTL_ELEM_TYPE_BYTES) { |
| 242 |
size_t len = strlen(str); |
| 243 |
if (len > (size_t)elem->count) |
| 244 |
len = elem->count; |
| 245 |
|
| 246 |
|
| 247 |
char buf[256] = {0}; |
| 248 |
memcpy(buf, str, len); |
| 249 |
snd_ctl_elem_set_bytes(value, buf, elem->count); |
| 250 |
|
| 251 |
if (verbose) |
| 252 |
printf(" %s = \"%s\"\n", elem->name, str); |
| 253 |
|
| 254 |
} else { |
| 255 |
return -1; |
| 256 |
} |
| 257 |
|
| 258 |
if (!dry_run) { |
| 259 |
int err = snd_ctl_elem_write(handle, value); |
| 260 |
if (err < 0) { |
| 261 |
fprintf(stderr, "Warning: Failed to set '%s': %s\n", |
| 262 |
elem->name, snd_strerror(err)); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
static int load_config( |
| 271 |
snd_ctl_t *handle, |
| 272 |
GArray *elems, |
| 273 |
const char *path, |
| 274 |
int verbose, |
| 275 |
int dry_run |
| 276 |
) { |
| 277 |
GKeyFile *key_file = g_key_file_new(); |
| 278 |
GError *error = NULL; |
| 279 |
|
| 280 |
if (!g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, &error)) { |
| 281 |
fprintf(stderr, "Error loading config: %s\n", |
| 282 |
error ? error->message : "unknown error"); |
| 283 |
g_key_file_free(key_file); |
| 284 |
if (error) |
| 285 |
g_error_free(error); |
| 286 |
return -1; |
| 287 |
} |
| 288 |
|
| 289 |
gsize num_keys; |
| 290 |
gchar **keys = g_key_file_get_keys( |
| 291 |
key_file, CONFIG_SECTION_CONTROLS, &num_keys, NULL |
| 292 |
); |
| 293 |
|
| 294 |
if (!keys) { |
| 295 |
fprintf(stderr, "No [controls] section in config file\n"); |
| 296 |
g_key_file_free(key_file); |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
int set_count = 0; |
| 301 |
int skip_count = 0; |
| 302 |
|
| 303 |
|
| 304 |
for (int pass = 0; pass < 2; pass++) { |
| 305 |
for (gsize i = 0; i < num_keys; i++) { |
| 306 |
gchar *value = g_key_file_get_string( |
| 307 |
key_file, CONFIG_SECTION_CONTROLS, keys[i], NULL |
| 308 |
); |
| 309 |
|
| 310 |
if (!value) |
| 311 |
continue; |
| 312 |
|
| 313 |
struct cli_elem *elem = find_elem_by_name(elems, keys[i]); |
| 314 |
if (!elem) { |
| 315 |
if (pass == 1 && verbose) |
| 316 |
fprintf(stderr, "Warning: Unknown control '%s'\n", keys[i]); |
| 317 |
skip_count++; |
| 318 |
g_free(value); |
| 319 |
continue; |
| 320 |
} |
| 321 |
|
| 322 |
if (set_elem_value(handle, elem, value, verbose && pass == 0, dry_run) == 0) |
| 323 |
set_count++; |
| 324 |
|
| 325 |
g_free(value); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
g_strfreev(keys); |
| 330 |
g_key_file_free(key_file); |
| 331 |
|
| 332 |
printf("Applied %d controls", set_count); |
| 333 |
if (skip_count > 0) |
| 334 |
printf(" (%d skipped)", skip_count / 2); |
| 335 |
if (dry_run) |
| 336 |
printf(" [dry-run]"); |
| 337 |
printf("\n"); |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
static void free_elements(GArray *elems) { |
| 343 |
for (guint i = 0; i < elems->len; i++) { |
| 344 |
struct cli_elem *elem = &g_array_index(elems, struct cli_elem, i); |
| 345 |
free(elem->name); |
| 346 |
} |
| 347 |
g_array_free(elems, TRUE); |
| 348 |
} |
| 349 |
|
| 350 |
int main(int argc, char **argv) { |
| 351 |
static struct option long_options[] = { |
| 352 |
{"device", required_argument, 0, 'd'}, |
| 353 |
{"list", no_argument, 0, 'l'}, |
| 354 |
{"verbose", no_argument, 0, 'v'}, |
| 355 |
{"dry-run", no_argument, 0, 'n'}, |
| 356 |
{"help", no_argument, 0, 'h'}, |
| 357 |
{0, 0, 0, 0} |
| 358 |
}; |
| 359 |
|
| 360 |
char *device = NULL; |
| 361 |
int do_list = 0; |
| 362 |
int verbose = 0; |
| 363 |
int dry_run = 0; |
| 364 |
int opt; |
| 365 |
|
| 366 |
while ((opt = getopt_long(argc, argv, "d:lvnh", long_options, NULL)) != -1) { |
| 367 |
switch (opt) { |
| 368 |
case 'd': |
| 369 |
device = optarg; |
| 370 |
break; |
| 371 |
case 'l': |
| 372 |
do_list = 1; |
| 373 |
break; |
| 374 |
case 'v': |
| 375 |
verbose = 1; |
| 376 |
break; |
| 377 |
case 'n': |
| 378 |
dry_run = 1; |
| 379 |
break; |
| 380 |
case 'h': |
| 381 |
print_usage(); |
| 382 |
return 0; |
| 383 |
default: |
| 384 |
print_usage(); |
| 385 |
return 1; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
if (do_list) { |
| 390 |
list_devices(); |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
if (optind >= argc) { |
| 395 |
print_usage(); |
| 396 |
return 1; |
| 397 |
} |
| 398 |
|
| 399 |
const char *config_path = argv[optind]; |
| 400 |
|
| 401 |
|
| 402 |
char auto_device[32]; |
| 403 |
if (!device) { |
| 404 |
int count = find_focusrite_devices(auto_device, sizeof(auto_device)); |
| 405 |
if (count == 0) { |
| 406 |
fprintf(stderr, "Error: No Focusrite devices found\n"); |
| 407 |
return 1; |
| 408 |
} else if (count > 1) { |
| 409 |
fprintf(stderr, "Error: Multiple Focusrite devices found, use -d to specify\n"); |
| 410 |
list_devices(); |
| 411 |
return 1; |
| 412 |
} |
| 413 |
device = auto_device; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
if (!g_file_test(config_path, G_FILE_TEST_EXISTS)) { |
| 418 |
fprintf(stderr, "Error: Config file not found: %s\n", config_path); |
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if (!g_str_has_suffix(config_path, ".conf")) { |
| 424 |
fprintf(stderr, "Error: Config file must have .conf extension\n"); |
| 425 |
return 1; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
snd_ctl_t *handle; |
| 430 |
int err = snd_ctl_open(&handle, device, 0); |
| 431 |
if (err < 0) { |
| 432 |
fprintf(stderr, "Error opening device %s: %s\n", device, snd_strerror(err)); |
| 433 |
return 1; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
snd_ctl_card_info_t *info; |
| 438 |
snd_ctl_card_info_alloca(&info); |
| 439 |
if (snd_ctl_card_info(handle, info) >= 0) { |
| 440 |
const char *name = snd_ctl_card_info_get_name(info); |
| 441 |
if (!is_focusrite_card(name)) { |
| 442 |
fprintf(stderr, "Warning: %s is not a recognised Focusrite device\n", |
| 443 |
name); |
| 444 |
} else if (verbose) { |
| 445 |
printf("Device: %s\n", name); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
GArray *elems = scan_elements(handle); |
| 451 |
if (elems->len == 0) { |
| 452 |
fprintf(stderr, "Error: No writable controls found on device\n"); |
| 453 |
free_elements(elems); |
| 454 |
snd_ctl_close(handle); |
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
if (verbose) |
| 459 |
printf("Found %d writable controls\n", elems->len); |
| 460 |
|
| 461 |
|
| 462 |
int result = load_config(handle, elems, config_path, verbose, dry_run); |
| 463 |
|
| 464 |
free_elements(elems); |
| 465 |
snd_ctl_close(handle); |
| 466 |
|
| 467 |
return result < 0 ? 1 : 0; |
| 468 |
} |