1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
|
/*
* Advanced Linux Sound Architecture Control Program
* Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
* Jaroslav Kysela <perex@suse.cz>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "aconfig.h"
#include "version.h"
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <alsa/asoundlib.h>
#define SYS_ASOUNDRC "/etc/asound.state"
int debugflag = 0;
int force_restore = 0;
char *command;
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
#define error(...) do {\
fprintf(stderr, "%s: %s:%d: ", command, __FUNCTION__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
putc('\n', stderr); \
} while (0)
#else
#define error(args...) do {\
fprintf(stderr, "%s: %s:%d: ", command, __FUNCTION__, __LINE__); \
fprintf(stderr, ##args); \
putc('\n', stderr); \
} while (0)
#endif
static void help(void)
{
printf("Usage: alsactl <options> command\n");
printf("\nAvailable options:\n");
printf(" -h,--help this help\n");
printf(" -f,--file # configuration file (default " SYS_ASOUNDRC ")\n");
printf(" -F,--force try to restore the matching controls as much as possible\n");
printf(" -d,--debug debug mode\n");
printf(" -v,--version print version of this program\n");
printf("\nAvailable commands:\n");
printf(" store <card #> save current driver setup for one or each soundcards\n");
printf(" to configuration file\n");
printf(" restore<card #> load current driver setup for one or each soundcards\n");
printf(" from configuration file\n");
printf(" power [card #] [state]\n");
printf(" get/set power state for one or each soundcards\n");
}
char *id_str(snd_ctl_elem_id_t *id)
{
static char str[128];
assert(id);
sprintf(str, "%i,%i,%i,%s,%i",
snd_ctl_elem_id_get_interface(id),
snd_ctl_elem_id_get_device(id),
snd_ctl_elem_id_get_subdevice(id),
snd_ctl_elem_id_get_name(id),
snd_ctl_elem_id_get_index(id));
return str;
}
char *num_str(long n)
{
static char str[32];
sprintf(str, "%ld", n);
return str;
}
static int snd_config_integer_add(snd_config_t *father, char *id, long integer)
{
int err;
snd_config_t *leaf;
err = snd_config_make_integer(&leaf, id);
if (err < 0)
return err;
err = snd_config_add(father, leaf);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
err = snd_config_set_integer(leaf, integer);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
return 0;
}
static int snd_config_integer64_add(snd_config_t *father, char *id, long long integer)
{
int err;
snd_config_t *leaf;
err = snd_config_make_integer64(&leaf, id);
if (err < 0)
return err;
err = snd_config_add(father, leaf);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
err = snd_config_set_integer64(leaf, integer);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
return 0;
}
static int snd_config_string_add(snd_config_t *father, const char *id, const char *string)
{
int err;
snd_config_t *leaf;
err = snd_config_make_string(&leaf, id);
if (err < 0)
return err;
err = snd_config_add(father, leaf);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
err = snd_config_set_string(leaf, string);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
return 0;
}
static int snd_config_compound_add(snd_config_t *father, const char *id, int join,
snd_config_t **node)
{
int err;
snd_config_t *leaf;
err = snd_config_make_compound(&leaf, id, join);
if (err < 0)
return err;
err = snd_config_add(father, leaf);
if (err < 0) {
snd_config_delete(leaf);
return err;
}
*node = leaf;
return 0;
}
static int get_control(snd_ctl_t *handle, snd_ctl_elem_id_t *id, snd_config_t *top)
{
snd_ctl_elem_value_t *ctl;
snd_ctl_elem_info_t *info;
snd_config_t *control, *comment, *item, *value;
const char *s;
char buf[256];
unsigned int idx;
int err;
unsigned int device, subdevice, index;
const char *name;
snd_ctl_elem_type_t type;
unsigned int count;
snd_ctl_elem_value_alloca(&ctl);
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_info_set_id(info, id);
err = snd_ctl_elem_info(handle, info);
if (err < 0) {
error("Cannot read control info '%s': %s", id_str(id), snd_strerror(err));
return err;
}
if (!snd_ctl_elem_info_is_readable(info))
return 0;
snd_ctl_elem_value_set_id(ctl, id);
err = snd_ctl_elem_read(handle, ctl);
if (err < 0) {
error("Cannot read control '%s': %s", id_str(id), snd_strerror(err));
return err;
}
err = snd_config_compound_add(top, num_str(snd_ctl_elem_info_get_numid(info)), 0, &control);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
return err;
}
err = snd_config_compound_add(control, "comment", 1, &comment);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
return err;
}
buf[0] = '\0';
buf[1] = '\0';
if (snd_ctl_elem_info_is_readable(info))
strcat(buf, " read");
if (snd_ctl_elem_info_is_writable(info))
strcat(buf, " write");
if (snd_ctl_elem_info_is_inactive(info))
strcat(buf, " inactive");
if (snd_ctl_elem_info_is_volatile(info))
strcat(buf, " volatile");
if (snd_ctl_elem_info_is_locked(info))
strcat(buf, " locked");
if (snd_ctl_elem_info_is_user(info))
strcat(buf, " user");
err = snd_config_string_add(comment, "access", buf + 1);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
type = snd_ctl_elem_info_get_type(info);
device = snd_ctl_elem_info_get_device(info);
subdevice = snd_ctl_elem_info_get_subdevice(info);
index = snd_ctl_elem_info_get_index(info);
name = snd_ctl_elem_info_get_name(info);
count = snd_ctl_elem_info_get_count(info);
s = snd_ctl_elem_type_name(type);
err = snd_config_string_add(comment, "type", s);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
err = snd_config_integer_add(comment, "count", count);
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
switch (type) {
case SND_CTL_ELEM_TYPE_BOOLEAN:
break;
case SND_CTL_ELEM_TYPE_INTEGER:
{
long min = snd_ctl_elem_info_get_min(info);
long max = snd_ctl_elem_info_get_max(info);
long step = snd_ctl_elem_info_get_step(info);
if (step)
sprintf(buf, "%li - %li (step %li)", min, max, step);
else
sprintf(buf, "%li - %li", min, max);
err = snd_config_string_add(comment, "range", buf);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
break;
}
case SND_CTL_ELEM_TYPE_INTEGER64:
{
long long min = snd_ctl_elem_info_get_min64(info);
long long max = snd_ctl_elem_info_get_max64(info);
long long step = snd_ctl_elem_info_get_step64(info);
if (step)
sprintf(buf, "%Li - %Li (step %Li)", min, max, step);
else
sprintf(buf, "%Li - %Li", min, max);
err = snd_config_string_add(comment, "range", buf);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
break;
}
case SND_CTL_ELEM_TYPE_ENUMERATED:
{
unsigned int items;
err = snd_config_compound_add(comment, "item", 1, &item);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
return err;
}
items = snd_ctl_elem_info_get_items(info);
for (idx = 0; idx < items; idx++) {
snd_ctl_elem_info_set_item(info, idx);
err = snd_ctl_elem_info(handle, info);
if (err < 0) {
error("snd_ctl_card_info: %s", snd_strerror(err));
return err;
}
err = snd_config_string_add(item, num_str(idx), snd_ctl_elem_info_get_item_name(info));
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
}
break;
}
default:
break;
}
s = snd_ctl_elem_iface_name(snd_ctl_elem_info_get_interface(info));
err = snd_config_string_add(control, "iface", s);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
if (device != 0) {
err = snd_config_integer_add(control, "device", device);
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
}
if (subdevice != 0) {
err = snd_config_integer_add(control, "subdevice", subdevice);
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
}
err = snd_config_string_add(control, "name", name);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
if (index != 0) {
err = snd_config_integer_add(control, "index", index);
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
}
switch (type) {
case SND_CTL_ELEM_TYPE_BYTES:
case SND_CTL_ELEM_TYPE_IEC958:
{
size_t size = type == SND_CTL_ELEM_TYPE_BYTES ?
count : sizeof(snd_aes_iec958_t);
char buf[size * 2 + 1];
char *p = buf;
char *hex = "0123456789abcdef";
const unsigned char *bytes =
(const unsigned char *)snd_ctl_elem_value_get_bytes(ctl);
for (idx = 0; idx < size; idx++) {
int v = bytes[idx];
*p++ = hex[v >> 4];
*p++ = hex[v & 0x0f];
}
*p = '\0';
err = snd_config_string_add(control, "value", buf);
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
return 0;
}
default:
break;
}
if (count == 1) {
switch (type) {
case SND_CTL_ELEM_TYPE_BOOLEAN:
err = snd_config_string_add(control, "value", snd_ctl_elem_value_get_boolean(ctl, 0) ? "true" : "false");
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
return 0;
case SND_CTL_ELEM_TYPE_INTEGER:
err = snd_config_integer_add(control, "value", snd_ctl_elem_value_get_integer(ctl, 0));
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
return 0;
case SND_CTL_ELEM_TYPE_INTEGER64:
err = snd_config_integer64_add(control, "value", snd_ctl_elem_value_get_integer64(ctl, 0));
if (err < 0) {
error("snd_config_integer64_add: %s", snd_strerror(err));
return err;
}
return 0;
case SND_CTL_ELEM_TYPE_ENUMERATED:
{
unsigned int v = snd_ctl_elem_value_get_enumerated(ctl, 0);
snd_config_t *c;
err = snd_config_search(item, num_str(v), &c);
if (err == 0) {
err = snd_config_get_string(c, &s);
assert(err == 0);
err = snd_config_string_add(control, "value", s);
} else {
err = snd_config_integer_add(control, "value", v);
}
if (err < 0)
error("snd_config add: %s", snd_strerror(err));
return 0;
}
default:
error("Unknown control type: %d\n", type);
return -EINVAL;
}
}
err = snd_config_compound_add(control, "value", 1, &value);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
return err;
}
switch (type) {
case SND_CTL_ELEM_TYPE_BOOLEAN:
for (idx = 0; idx < count; idx++) {
err = snd_config_string_add(value, num_str(idx), snd_ctl_elem_value_get_boolean(ctl, idx) ? "true" : "false");
if (err < 0) {
error("snd_config_string_add: %s", snd_strerror(err));
return err;
}
}
break;
case SND_CTL_ELEM_TYPE_INTEGER:
for (idx = 0; idx < count; idx++) {
err = snd_config_integer_add(value, num_str(idx), snd_ctl_elem_value_get_integer(ctl, idx));
if (err < 0) {
error("snd_config_integer_add: %s", snd_strerror(err));
return err;
}
}
break;
case SND_CTL_ELEM_TYPE_INTEGER64:
for (idx = 0; idx < count; idx++) {
err = snd_config_integer64_add(value, num_str(idx), snd_ctl_elem_value_get_integer64(ctl, idx));
if (err < 0) {
error("snd_config_integer64_add: %s", snd_strerror(err));
return err;
}
}
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
for (idx = 0; idx < count; idx++) {
unsigned int v = snd_ctl_elem_value_get_enumerated(ctl, idx);
snd_config_t *c;
err = snd_config_search(item, num_str(v), &c);
if (err == 0) {
err = snd_config_get_string(c, &s);
assert(err == 0);
err = snd_config_string_add(value, num_str(idx), s);
} else {
err = snd_config_integer_add(value, num_str(idx), v);
}
if (err < 0) {
error("snd_config add: %s", snd_strerror(err));
return err;
}
}
break;
default:
error("Unknown control type: %d\n", type);
return -EINVAL;
}
return 0;
}
static int get_controls(int cardno, snd_config_t *top)
{
snd_ctl_t *handle;
snd_ctl_card_info_t *info;
snd_config_t *state, *card, *control;
snd_ctl_elem_list_t *list;
unsigned int idx;
int err;
char name[32];
unsigned int count;
const char *id;
snd_ctl_card_info_alloca(&info);
snd_ctl_elem_list_alloca(&list);
sprintf(name, "hw:%d", cardno);
err = snd_ctl_open(&handle, name, SND_CTL_READONLY);
if (err < 0) {
error("snd_ctl_open error: %s", snd_strerror(err));
return err;
}
err = snd_ctl_card_info(handle, info);
if (err < 0) {
error("snd_ctl_card_info error: %s", snd_strerror(err));
goto _close;
}
id = snd_ctl_card_info_get_id(info);
err = snd_config_search(top, "state", &state);
if (err == 0 &&
snd_config_get_type(state) != SND_CONFIG_TYPE_COMPOUND) {
error("config state node is not a compound");
err = -EINVAL;
goto _close;
}
if (err < 0) {
err = snd_config_compound_add(top, "state", 1, &state);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
goto _close;
}
}
err = snd_config_search(state, id, &card);
if (err == 0 &&
snd_config_get_type(card) != SND_CONFIG_TYPE_COMPOUND) {
error("config state.%s node is not a compound", id);
err = -EINVAL;
goto _close;
}
if (err < 0) {
err = snd_config_compound_add(state, id, 0, &card);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
goto _close;
}
}
err = snd_config_search(card, "control", &control);
if (err == 0) {
err = snd_config_delete(control);
if (err < 0) {
error("snd_config_delete: %s", snd_strerror(err));
goto _close;
}
}
err = snd_ctl_elem_list(handle, list);
if (err < 0) {
error("Cannot determine controls: %s", snd_strerror(err));
goto _close;
}
count = snd_ctl_elem_list_get_count(list);
if (count < 0) {
err = 0;
goto _close;
}
err = snd_config_compound_add(card, "control", count > 0, &control);
if (err < 0) {
error("snd_config_compound_add: %s", snd_strerror(err));
goto _close;
}
if (count == 0) {
err = 0;
goto _close;
}
snd_ctl_elem_list_set_offset(list, 0);
if (snd_ctl_elem_list_alloc_space(list, count) < 0) {
error("No enough memory...");
goto _close;
}
if ((err = snd_ctl_elem_list(handle, list)) < 0) {
error("Cannot determine controls (2): %s", snd_strerror(err));
goto _free;
}
for (idx = 0; idx < count; ++idx) {
snd_ctl_elem_id_t *id;
snd_ctl_elem_id_alloca(&id);
snd_ctl_elem_list_get_id(list, idx, id);
err = get_control(handle, id, control);
if (err < 0)
goto _free;
}
err = 0;
_free:
snd_ctl_elem_list_free_space(list);
_close:
snd_ctl_close(handle);
return err;
}
static long config_iface(snd_config_t *n)
{
long i;
long long li;
snd_ctl_elem_iface_t idx;
const char *str;
switch (snd_config_get_type(n)) {
case SND_CONFIG_TYPE_INTEGER:
snd_config_get_integer(n, &i);
return i;
case SND_CONFIG_TYPE_INTEGER64:
snd_config_get_integer64(n, &li);
return li;
case SND_CONFIG_TYPE_STRING:
snd_config_get_string(n, &str);
break;
default:
return -1;
}
for (idx = 0; idx <= SND_CTL_ELEM_IFACE_LAST; idx++) {
if (strcasecmp(snd_ctl_elem_iface_name(idx), str) == 0)
return idx;
}
return -1;
}
static int config_bool(snd_config_t *n)
{
const char *str;
long val;
long long lval;
switch (snd_config_get_type(n)) {
case SND_CONFIG_TYPE_INTEGER:
snd_config_get_integer(n, &val);
if (val < 0 || val > 1)
return -1;
return val;
case SND_CONFIG_TYPE_INTEGER64:
snd_config_get_integer64(n, &lval);
if (lval < 0 || lval > 1)
return -1;
return (int) lval;
case SND_CONFIG_TYPE_STRING:
snd_config_get_string(n, &str);
break;
default:
return -1;
}
if (strcmp(str, "on") == 0 || strcmp(str, "true") == 0)
return 1;
if (strcmp(str, "off") == 0 || strcmp(str, "false") == 0)
return 0;
return -1;
}
static int config_enumerated(snd_config_t *n, snd_ctl_t *handle,
snd_ctl_elem_info_t *info)
{
const char *str;
long val;
long long lval;
unsigned int idx, items;
switch (snd_config_get_type(n)) {
case SND_CONFIG_TYPE_INTEGER:
snd_config_get_integer(n, &val);
return val;
case SND_CONFIG_TYPE_INTEGER64:
snd_config_get_integer64(n, &lval);
return (int) lval;
case SND_CONFIG_TYPE_STRING:
snd_config_get_string(n, &str);
break;
default:
return -1;
}
items = snd_ctl_elem_info_get_items(info);
for (idx = 0; idx < items; idx++) {
int err;
snd_ctl_elem_info_set_item(info, idx);
err = snd_ctl_elem_info(handle, info);
if (err < 0) {
error("snd_ctl_elem_info: %s", snd_strerror(err));
return err;
}
if (strcmp(str, snd_ctl_elem_info_get_item_name(info)) == 0)
return idx;
}
return -1;
}
static int is_user_control(snd_config_t *conf)
{
snd_config_iterator_t i, next;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id, *s;
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "access") == 0) {
if (snd_config_get_string(n, &s) < 0)
return 0;
if (strstr(s, "user"))
return 1;
}
}
return 0;
}
static int add_user_control(snd_ctl_t *handle, snd_ctl_elem_info_t *info, snd_config_t *conf)
{
snd_ctl_elem_id_t *id;
snd_config_iterator_t i, next;
long imin, imax, istep;
snd_ctl_elem_type_t ctype;
unsigned int count;
int err;
imin = imax = istep = 0;
count = 0;
ctype = SND_CTL_ELEM_TYPE_NONE;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id, *type;
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "type") == 0) {
if ((err = snd_config_get_string(n, &type)) < 0)
return -EINVAL;
if (strcmp(type, "BOOLEAN") == 0)
ctype = SND_CTL_ELEM_TYPE_BOOLEAN;
else if (strcmp(type, "INTEGER") == 0)
ctype = SND_CTL_ELEM_TYPE_INTEGER;
else if (strcmp(type, "IEC958") == 0)
ctype = SND_CTL_ELEM_TYPE_IEC958;
else
return -EINVAL;
continue;
}
if (strcmp(id, "range") == 0) {
const char *s;
if ((err = snd_config_get_string(n, &s)) < 0)
return -EINVAL;
switch (ctype) {
case SND_CTL_ELEM_TYPE_INTEGER:
err = sscanf(s, "%li - %li (step %li)", &imin, &imax, &istep);
if (err != 3) {
istep = 0;
err = sscanf(s, "%li - %li", &imin, &imax);
if (err != 2)
return -EINVAL;
}
break;
default:
return -EINVAL;
}
continue;
}
if (strcmp(id, "count") == 0) {
long v;
if ((err = snd_config_get_integer(n, &v)) < 0)
return err;
count = v;
continue;
}
}
snd_ctl_elem_id_alloca(&id);
snd_ctl_elem_info_get_id(info, id);
if (count <= 0)
count = 1;
switch (ctype) {
case SND_CTL_ELEM_TYPE_INTEGER:
if (imin > imax || istep > imax - imin)
return -EINVAL;
err = snd_ctl_elem_add_integer(handle, id, count, imin, imax, istep);
break;
case SND_CTL_ELEM_TYPE_BOOLEAN:
err = snd_ctl_elem_add_boolean(handle, id, count);
break;
case SND_CTL_ELEM_TYPE_IEC958:
err = snd_ctl_elem_add_iec958(handle, id);
break;
default:
err = -EINVAL;
break;
}
if (err < 0)
return err;
return snd_ctl_elem_info(handle, info);
}
static int set_control(snd_ctl_t *handle, snd_config_t *control)
{
snd_ctl_elem_value_t *ctl;
snd_ctl_elem_info_t *info;
snd_config_iterator_t i, next;
unsigned int numid1;
snd_ctl_elem_iface_t iface = -1;
int iface1;
const char *name1;
unsigned int numid;
snd_ctl_elem_type_t type;
unsigned int count;
long device = -1;
long device1;
long subdevice = -1;
long subdevice1;
const char *name = NULL;
long index1;
long index = -1;
snd_config_t *value = NULL;
snd_config_t *comment = NULL;
long val;
long long lval;
unsigned int idx;
int err;
char *set;
const char *id;
snd_ctl_elem_value_alloca(&ctl);
snd_ctl_elem_info_alloca(&info);
if (snd_config_get_type(control) != SND_CONFIG_TYPE_COMPOUND) {
error("control is not a compound");
return -EINVAL;
}
err = snd_config_get_id(control, &id);
if (err < 0) {
error("unable to get id");
return -EINVAL;
}
numid = atoi(id);
snd_config_for_each(i, next, control) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *fld;
if (snd_config_get_id(n, &fld) < 0)
continue;
if (strcmp(fld, "comment") == 0) {
if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
comment = n;
continue;
}
if (strcmp(fld, "iface") == 0) {
iface = (snd_ctl_elem_iface_t)config_iface(n);
if (iface < 0) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
continue;
}
if (strcmp(fld, "device") == 0) {
if (snd_config_get_type(n) != SND_CONFIG_TYPE_INTEGER) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
snd_config_get_integer(n, &device);
continue;
}
if (strcmp(fld, "subdevice") == 0) {
if (snd_config_get_type(n) != SND_CONFIG_TYPE_INTEGER) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
snd_config_get_integer(n, &subdevice);
continue;
}
if (strcmp(fld, "name") == 0) {
if (snd_config_get_type(n) != SND_CONFIG_TYPE_STRING) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
snd_config_get_string(n, &name);
continue;
}
if (strcmp(fld, "index") == 0) {
if (snd_config_get_type(n) != SND_CONFIG_TYPE_INTEGER) {
error("control.%d.%s is invalid", numid, fld);
return -EINVAL;
}
snd_config_get_integer(n, &index);
continue;
}
if (strcmp(fld, "value") == 0) {
value = n;
continue;
}
error("unknown control.%d.%s field", numid, fld);
}
if (!value) {
error("missing control.%d.value", numid);
return -EINVAL;
}
if (device < 0)
device = 0;
if (subdevice < 0)
subdevice = 0;
if (index < 0)
index = 0;
err = -EINVAL;
if (! force_restore) {
snd_ctl_elem_info_set_numid(info, numid);
err = snd_ctl_elem_info(handle, info);
}
if (err < 0) {
if (iface >= 0 && name) {
snd_ctl_elem_info_set_numid(info, 0);
snd_ctl_elem_info_set_interface(info, iface);
snd_ctl_elem_info_set_device(info, device);
snd_ctl_elem_info_set_subdevice(info, subdevice);
snd_ctl_elem_info_set_name(info, name);
snd_ctl_elem_info_set_index(info, index);
err = snd_ctl_elem_info(handle, info);
if (err < 0 && comment && is_user_control(comment)) {
err = add_user_control(handle, info, comment);
if (err < 0) {
error("failed to add user control #%d (%s)",
numid, snd_strerror(err));
return err;
}
}
}
}
if (err < 0) {
error("failed to obtain info for control #%d (%s)", numid, snd_strerror(err));
return -ENOENT;
}
numid1 = snd_ctl_elem_info_get_numid(info);
iface1 = snd_ctl_elem_info_get_interface(info);
device1 = snd_ctl_elem_info_get_device(info);
subdevice1 = snd_ctl_elem_info_get_subdevice(info);
name1 = snd_ctl_elem_info_get_name(info);
index1 = snd_ctl_elem_info_get_index(info);
count = snd_ctl_elem_info_get_count(info);
type = snd_ctl_elem_info_get_type(info);
if (numid != numid1 && ! force_restore)
error("warning: numid mismatch (%d/%d) for control #%d",
numid, numid1, numid);
if (iface != iface1)
error("warning: iface mismatch (%d/%d) for control #%d", iface, iface1, numid);
if (device != device1)
error("warning: device mismatch (%ld/%ld) for control #%d", device, device1, numid);
if (subdevice != subdevice1)
error("warning: subdevice mismatch (%ld/%ld) for control #%d", subdevice, subdevice1, numid);
if (strcmp(name, name1))
error("warning: name mismatch (%s/%s) for control #%d", name, name1, numid);
if (index != index1)
error("warning: index mismatch (%ld/%ld) for control #%d", index, index1, numid);
#if 0
if (comment) {
check_comment_type(comment, type);
if (type == SND_CTL_ELEM_TYPE_INTEGER ||
type == SND_CTL_ELEM_TYPE_INTEGER64)
check_comment_range(comment, info);
}
#endif
if (!snd_ctl_elem_info_is_writable(info))
return 0;
snd_ctl_elem_value_set_numid(ctl, numid1);
if (count == 1) {
switch (type) {
case SND_CTL_ELEM_TYPE_BOOLEAN:
val = config_bool(value);
if (val >= 0) {
snd_ctl_elem_value_set_boolean(ctl, 0, val);
goto _ok;
}
break;
case SND_CTL_ELEM_TYPE_INTEGER:
err = snd_config_get_integer(value, &val);
if (err == 0) {
snd_ctl_elem_value_set_integer(ctl, 0, val);
goto _ok;
}
break;
case SND_CTL_ELEM_TYPE_INTEGER64:
err = snd_config_get_integer64(value, &lval);
if (err == 0) {
snd_ctl_elem_value_set_integer64(ctl, 0, lval);
goto _ok;
}
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
val = config_enumerated(value, handle, info);
if (val >= 0) {
snd_ctl_elem_value_set_enumerated(ctl, 0, val);
goto _ok;
}
break;
case SND_CTL_ELEM_TYPE_BYTES:
case SND_CTL_ELEM_TYPE_IEC958:
break;
default:
error("Unknow control type: %d", type);
return -EINVAL;
}
}
switch (type) {
case SND_CTL_ELEM_TYPE_BYTES:
case SND_CTL_ELEM_TYPE_IEC958:
{
const char *buf;
err = snd_config_get_string(value, &buf);
if (err >= 0) {
int c1 = 0;
int len = strlen(buf);
unsigned int idx = 0;
int size = type == SND_CTL_ELEM_TYPE_BYTES ?
count : sizeof(snd_aes_iec958_t);
if (size * 2 != len) {
error("bad control.%d.value contents\n", numid);
return -EINVAL;
}
while (*buf) {
int c = *buf++;
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'a' && c <= 'f')
c = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
c = c - 'A' + 10;
else {
error("bad control.%d.value contents\n", numid);
return -EINVAL;
}
if (idx % 2 == 1)
snd_ctl_elem_value_set_byte(ctl, idx / 2, c1 << 4 | c);
else
c1 = c;
idx++;
}
goto _ok;
}
}
default:
break;
}
if (snd_config_get_type(value) != SND_CONFIG_TYPE_COMPOUND) {
error("bad control.%d.value type", numid);
return -EINVAL;
}
set = (char*) alloca(count);
memset(set, 0, count);
snd_config_for_each(i, next, value) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < 0)
continue;
idx = atoi(id);
if (idx < 0 || idx >= count ||
set[idx]) {
error("bad control.%d.value index", numid);
return -EINVAL;
}
switch (type) {
case SND_CTL_ELEM_TYPE_BOOLEAN:
val = config_bool(n);
if (val < 0) {
error("bad control.%d.value.%d content", numid, idx);
return -EINVAL;
}
snd_ctl_elem_value_set_boolean(ctl, idx, val);
break;
case SND_CTL_ELEM_TYPE_INTEGER:
err = snd_config_get_integer(n, &val);
if (err < 0) {
error("bad control.%d.value.%d content", numid, idx);
return -EINVAL;
}
snd_ctl_elem_value_set_integer(ctl, idx, val);
break;
case SND_CTL_ELEM_TYPE_INTEGER64:
err = snd_config_get_integer64(n, &lval);
if (err < 0) {
error("bad control.%d.value.%d content", numid, idx);
return -EINVAL;
}
snd_ctl_elem_value_set_integer64(ctl, idx, lval);
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
val = config_enumerated(n, handle, info);
if (val < 0) {
error("bad control.%d.value.%d content", numid, idx);
return -EINVAL;
}
snd_ctl_elem_value_set_enumerated(ctl, idx, val);
break;
case SND_CTL_ELEM_TYPE_BYTES:
case SND_CTL_ELEM_TYPE_IEC958:
err = snd_config_get_integer(n, &val);
if (err < 0 || val < 0 || val > 255) {
error("bad control.%d.value.%d content", numid, idx);
return -EINVAL;
}
snd_ctl_elem_value_set_byte(ctl, idx, val);
break;
default:
break;
}
set[idx] = 1;
}
for (idx = 0; idx < count; ++idx) {
if (!set[idx]) {
error("control.%d.value.%d is not specified", numid, idx);
return -EINVAL;
}
}
_ok:
err = snd_ctl_elem_write(handle, ctl);
if (err < 0) {
error("Cannot write control '%d:%ld:%ld:%s:%ld' : %s", (int)iface, device, subdevice, name, index, snd_strerror(err));
return err;
}
return 0;
}
static int set_controls(int card, snd_config_t *top)
{
snd_ctl_t *handle;
snd_ctl_card_info_t *info;
snd_config_t *control;
snd_config_iterator_t i, next;
int err;
char name[32], tmpid[16];
const char *id;
snd_ctl_card_info_alloca(&info);
sprintf(name, "hw:%d", card);
err = snd_ctl_open(&handle, name, 0);
if (err < 0) {
error("snd_ctl_open error: %s", snd_strerror(err));
return err;
}
err = snd_ctl_card_info(handle, info);
if (err < 0) {
error("snd_ctl_card_info error: %s", snd_strerror(err));
goto _close;
}
id = snd_ctl_card_info_get_id(info);
err = snd_config_searchv(top, &control, "state", id, "control", 0);
if (err < 0) {
if (force_restore) {
sprintf(tmpid, "card%d", card);
err = snd_config_searchv(top, &control, "state", tmpid, "control", 0);
if (! err)
id = tmpid;
}
if (err < 0) {
err = 0;
fprintf(stderr, "No state is present for card %s\n", id);
goto _close;
}
id = tmpid;
}
if (snd_config_get_type(control) != SND_CONFIG_TYPE_COMPOUND) {
error("state.%s.control is not a compound\n", id);
return -EINVAL;
}
snd_config_for_each(i, next, control) {
snd_config_t *n = snd_config_iterator_entry(i);
err = set_control(handle, n);
if (err < 0 && ! force_restore)
goto _close;
}
_close:
snd_ctl_close(handle);
return err;
}
static int save_state(char *file, const char *cardname)
{
int err;
snd_config_t *config;
snd_input_t *in;
snd_output_t *out;
int stdio;
err = snd_config_top(&config);
if (err < 0) {
error("snd_config_top error: %s", snd_strerror(err));
return err;
}
stdio = !strcmp(file, "-");
if (!stdio && (err = snd_input_stdio_open(&in, file, "r")) >= 0) {
err = snd_config_load(config, in);
snd_input_close(in);
#if 0
if (err < 0) {
error("snd_config_load error: %s", snd_strerror(err));
return err;
}
#endif
}
if (!cardname) {
int card, first = 1;
card = -1;
/* find each installed soundcards */
while (1) {
if (snd_card_next(&card) < 0)
break;
if (card < 0) {
if (first) {
error("No soundcards found...");
return -ENODEV;
}
break;
}
first = 0;
if ((err = get_controls(card, config)))
return err;
}
} else {
int cardno;
cardno = snd_card_get_index(cardname);
if (cardno < 0) {
error("Cannot find soundcard '%s'...", cardname);
return cardno;
}
if ((err = get_controls(cardno, config))) {
return err;
}
}
if (stdio)
err = snd_output_stdio_attach(&out, stdout, 0);
else
err = snd_output_stdio_open(&out, file, "w");
if (err < 0) {
error("Cannot open %s for writing", file);
return -errno;
}
err = snd_config_save(config, out);
snd_output_close(out);
if (err < 0)
error("snd_config_save: %s", snd_strerror(err));
return 0;
}
int load_state(const char *file, const char *cardname)
{
int err;
snd_config_t *config;
snd_input_t *in;
int stdio;
err = snd_config_top(&config);
if (err < 0) {
error("snd_config_top error: %s", snd_strerror(err));
return err;
}
stdio = !strcmp(file, "-");
if (stdio)
err = snd_input_stdio_attach(&in, stdin, 0);
else
err = snd_input_stdio_open(&in, file, "r");
if (err >= 0) {
err = snd_config_load(config, in);
snd_input_close(in);
if (err < 0) {
error("snd_config_load error: %s", snd_strerror(err));
return err;
}
}
if (!cardname) {
int card, first = 1;
card = -1;
/* find each installed soundcards */
while (1) {
if (snd_card_next(&card) < 0)
break;
if (card < 0) {
if (first) {
error("No soundcards found...");
return -ENODEV;
}
break;
}
first = 0;
if ((err = set_controls(card, config)) && ! force_restore)
return err;
}
} else {
int cardno;
cardno = snd_card_get_index(cardname);
if (cardno < 0) {
error("Cannot find soundcard '%s'...", cardname);
return -ENODEV;
}
if ((err = set_controls(cardno, config)) && ! force_restore) {
return err;
}
}
return 0;
}
static int get_int_state(const char *str)
{
if (!strcasecmp(str, "on"))
return SND_CTL_POWER_D0;
if (!strcasecmp(str, "off"))
return SND_CTL_POWER_D3hot;
if (*str == 'D' || *str == 'd') {
str++;
if (!strcmp(str, "0"))
return SND_CTL_POWER_D0;
if (!strcmp(str, "1"))
return SND_CTL_POWER_D1;
if (!strcmp(str, "2"))
return SND_CTL_POWER_D2;
if (!strcmp(str, "3"))
return SND_CTL_POWER_D3;
if (!strcmp(str, "3hot"))
return SND_CTL_POWER_D3hot;
if (!strcmp(str, "3cold"))
return SND_CTL_POWER_D3cold;
}
return -1;
}
static const char *get_str_state(int power_state)
{
static char str[16];
switch (power_state) {
case SND_CTL_POWER_D0:
return "D0";
case SND_CTL_POWER_D1:
return "D1";
case SND_CTL_POWER_D2:
return "D2";
// return SND_CTL_POWER_D3; /* it's same as D3hot */
case SND_CTL_POWER_D3hot:
return "D3hot";
case SND_CTL_POWER_D3cold:
return "D3cold";
default:
sprintf(str, "???0x%x", power_state);
return str;
}
}
static int show_power(int cardno)
{
snd_ctl_t *handle;
char name[16];
unsigned int power_state;
int err;
sprintf(name, "hw:%d", cardno);
err = snd_ctl_open(&handle, name, 0);
if (err < 0) {
error("snd_ctl_open error: %s", snd_strerror(err));
return err;
}
err = snd_ctl_get_power_state(handle, &power_state);
if (err < 0) {
error("snd_ctl_get_power_state error: %s", snd_strerror(err));
snd_ctl_close(handle);
return err;
}
snd_ctl_close(handle);
printf("Power state for card #%d is %s\n", cardno, get_str_state(power_state));
return 0;
}
static int set_power(int cardno, unsigned int power_state)
{
snd_ctl_t *handle;
char name[16];
int err;
sprintf(name, "hw:%d", cardno);
err = snd_ctl_open(&handle, name, 0);
if (err < 0) {
error("snd_ctl_open error: %s", snd_strerror(err));
return err;
}
err = snd_ctl_set_power_state(handle, power_state);
if (err < 0) {
error("snd_ctl_set_power_state error: %s", snd_strerror(err));
snd_ctl_close(handle);
return err;
}
err = snd_ctl_get_power_state(handle, &power_state);
if (err < 0) {
error("snd_ctl_get_power_state error: %s", snd_strerror(err));
snd_ctl_close(handle);
return err;
}
snd_ctl_close(handle);
printf("Power state for card #%d is %s\n", cardno, get_str_state(power_state));
return 0;
}
static int power(const char *argv[], int argc)
{
int power_state, err;
if (argc == 0) { /* show status only */
int card, first = 1;
card = -1;
/* find each installed soundcards */
while (1) {
if (snd_card_next(&card) < 0)
break;
if (card < 0) {
if (first) {
error("No soundcards found...");
return -ENODEV;
}
break;
}
first = 0;
if ((err = show_power(card)) < 0)
return err;
}
return 0;
}
power_state = get_int_state(argv[0]);
if (power_state >= 0) {
int card, first = 1;
card = -1;
/* find each installed soundcards */
while (1) {
if (snd_card_next(&card) < 0)
break;
if (card < 0) {
if (first) {
error("No soundcards found...");
return -ENODEV;
}
break;
}
first = 0;
if ((err = set_power(card, power_state)))
return err;
}
} else {
int cardno;
cardno = snd_card_get_index(argv[0]);
if (cardno < 0) {
error("Cannot find soundcard '%s'...", argv[0]);
return -ENODEV;
}
if (argc > 1) {
power_state = get_int_state(argv[1]);
if (power_state < 0) {
error("Invalid power state '%s'...", argv[1]);
return -EINVAL;
}
if ((err = set_power(cardno, power_state)) < 0)
return err;
} else {
if ((err = show_power(cardno)) < 0)
return err;
}
}
return 0;
}
int main(int argc, char *argv[])
{
int morehelp;
struct option long_option[] =
{
{"help", 0, NULL, 'h'},
{"file", 1, NULL, 'f'},
{"force", 1, NULL, 'F'},
{"debug", 0, NULL, 'd'},
{"version", 0, NULL, 'v'},
{NULL, 0, NULL, 0},
};
char *cfgfile = SYS_ASOUNDRC;
int res;
command = argv[0];
morehelp = 0;
while (1) {
int c;
if ((c = getopt_long(argc, argv, "hf:Fdv", long_option, NULL)) < 0)
break;
switch (c) {
case 'h':
morehelp++;
break;
case 'f':
cfgfile = optarg;
break;
case 'F':
force_restore = 1;
break;
case 'd':
debugflag = 1;
break;
case 'v':
printf("alsactl version " SND_UTIL_VERSION_STR "\n");
return EXIT_SUCCESS;
case '?': // error msg already printed
morehelp++;
break;
default: // should never happen
fprintf(stderr,
"Invalid option '%c' (%d) not handled??\n", c, c);
}
}
if (morehelp) {
help();
return EXIT_SUCCESS;
}
if (argc - optind <= 0) {
fprintf(stderr, "alsactl: Specify command...\n");
return 0;
}
if (!strcmp(argv[optind], "store")) {
res = save_state(cfgfile,
argc - optind > 1 ? argv[optind + 1] : NULL);
} else if (!strcmp(argv[optind], "restore")) {
res = load_state(cfgfile,
argc - optind > 1 ? argv[optind + 1] : NULL);
} else if (!strcmp(argv[optind], "power")) {
res = power((const char **)argv + optind + 1, argc - optind - 1);
} else {
fprintf(stderr, "alsactl: Unknown command '%s'...\n",
argv[optind]);
res = -ENODEV;
}
return res < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
|