1 /* tinymix.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 ** * Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 ** * Redistributions in binary form must reproduce the above copyright
10 ** notice, this list of conditions and the following disclaimer in the
11 ** documentation and/or other materials provided with the distribution.
12 ** * Neither the name of The Android Open Source Project nor the names of
13 ** its contributors may be used to endorse or promote products derived
14 ** from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
28
29 #include <tinyalsa/asoundlib.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <limits.h>
36
37 #define OPTPARSE_IMPLEMENTATION
38 #include "optparse.h"
39
40 static void list_controls(struct mixer *mixer, int print_all);
41
42 static void print_control_values(struct mixer_ctl *control);
43 static void print_control_values_by_name_or_id(struct mixer *mixer, const char *name_or_id);
44
45 static int set_values(struct mixer *mixer, const char *control,
46 char **values, unsigned int num_values);
47
48 static void print_enum(struct mixer_ctl *ctl);
49
usage(void)50 void usage(void)
51 {
52 printf("usage: tinymix [options] <command>\n");
53 printf("options:\n");
54 printf("\t-h, --help : prints this help message and exits\n");
55 printf("\t-v, --version : prints this version of tinymix and exits\n");
56 printf("\t-D, --card NUMBER : specifies the card number of the mixer\n");
57 printf("\n");
58 printf("commands:\n");
59 printf("\tget NAME|ID : prints the values of a control\n");
60 printf("\tset NAME|ID VALUE(S) ... : sets the value of a control\n");
61 printf("\t\tVALUE(S): integers, percents, and relative values\n");
62 printf("\t\t\tIntegers: 0, 100, -100 ...\n");
63 printf("\t\t\tPercents: 0%%, 100%% ...\n");
64 printf("\t\t\tRelative values: 1+, 1-, 1%%+, 2%%+ ...\n");
65 printf("\tcontrols : lists controls of the mixer\n");
66 printf("\tcontents : lists controls of the mixer and their contents\n");
67 }
68
version(void)69 void version(void)
70 {
71 printf("tinymix version 2.0 (tinyalsa version %s)\n", TINYALSA_VERSION_STRING);
72 }
73
is_command(char * arg)74 static int is_command(char *arg) {
75 return strcmp(arg, "get") == 0 || strcmp(arg, "set") == 0 ||
76 strcmp(arg, "controls") == 0 || strcmp(arg, "contents") == 0;
77 }
78
find_command_position(int argc,char ** argv)79 static int find_command_position(int argc, char **argv)
80 {
81 for (int i = 0; i < argc; ++i) {
82 if (is_command(argv[i])) {
83 return i;
84 }
85 }
86 return -1;
87 }
88
main(int argc,char ** argv)89 int main(int argc, char **argv)
90 {
91 int card = 0;
92 struct optparse opts;
93 static struct optparse_long long_options[] = {
94 { "card", 'D', OPTPARSE_REQUIRED },
95 { "version", 'v', OPTPARSE_NONE },
96 { "help", 'h', OPTPARSE_NONE },
97 { 0, 0, 0 }
98 };
99
100 // optparse_long may change the order of the argv list. Duplicate one for parsing the options.
101 char **argv_options_list = (char **) calloc(argc + 1, sizeof(char *));
102 if (!argv_options_list) {
103 fprintf(stderr, "Failed to allocate options list\n");
104 return EXIT_FAILURE;
105 }
106
107 for (int i = 0; i < argc; ++i) {
108 argv_options_list[i] = argv[i];
109 }
110
111 optparse_init(&opts, argv_options_list);
112 /* Detect the end of the options. */
113 int c;
114 while ((c = optparse_long(&opts, long_options, NULL)) != -1) {
115 switch (c) {
116 case 'D':
117 card = atoi(opts.optarg);
118 break;
119 case 'h':
120 usage();
121 free(argv_options_list);
122 return EXIT_SUCCESS;
123 case 'v':
124 version();
125 free(argv_options_list);
126 return EXIT_SUCCESS;
127 case '?':
128 default:
129 break;
130 }
131 }
132 free(argv_options_list);
133
134 struct mixer *mixer = mixer_open(card);
135 if (!mixer) {
136 fprintf(stderr, "Failed to open mixer\n");
137 return EXIT_FAILURE;
138 }
139
140 int command_position = find_command_position(argc, argv);
141 if (command_position < 0) {
142 usage();
143 mixer_close(mixer);
144 return EXIT_FAILURE;
145 }
146
147 char *cmd = argv[command_position];
148 if (strcmp(cmd, "get") == 0) {
149 if (command_position + 1 >= argc) {
150 fprintf(stderr, "no control specified\n");
151 mixer_close(mixer);
152 return EXIT_FAILURE;
153 }
154 print_control_values_by_name_or_id(mixer, argv[command_position + 1]);
155 printf("\n");
156 } else if (strcmp(cmd, "set") == 0) {
157 if (command_position + 1 >= argc) {
158 fprintf(stderr, "no control specified\n");
159 mixer_close(mixer);
160 return EXIT_FAILURE;
161 }
162 if (command_position + 2 >= argc) {
163 fprintf(stderr, "no value(s) specified\n");
164 mixer_close(mixer);
165 return EXIT_FAILURE;
166 }
167 int res = set_values(mixer,
168 argv[command_position + 1],
169 &argv[command_position + 2],
170 argc - command_position - 2);
171 if (res != 0) {
172 mixer_close(mixer);
173 return EXIT_FAILURE;
174 }
175 } else if (strcmp(cmd, "controls") == 0) {
176 list_controls(mixer, 0);
177 } else if (strcmp(cmd, "contents") == 0) {
178 list_controls(mixer, 1);
179 } else {
180 fprintf(stderr, "unknown command '%s'\n", cmd);
181 usage();
182 mixer_close(mixer);
183 return EXIT_FAILURE;
184 }
185
186 mixer_close(mixer);
187 return EXIT_SUCCESS;
188 }
189
isnumber(const char * str)190 static int isnumber(const char *str) {
191 char *end;
192
193 if (str == NULL || strlen(str) == 0)
194 return 0;
195
196 strtol(str, &end, 0);
197 return strlen(end) == 0;
198 }
199
list_controls(struct mixer * mixer,int print_all)200 static void list_controls(struct mixer *mixer, int print_all)
201 {
202 struct mixer_ctl *ctl;
203 const char *name, *type;
204 unsigned int num_ctls, num_values, device;
205 unsigned int i;
206
207 num_ctls = mixer_get_num_ctls(mixer);
208
209 printf("Number of controls: %u\n", num_ctls);
210
211 if (print_all)
212 printf("ctl\ttype\tnum\t%-40s\tdevice\tvalue\n", "name");
213 else
214 printf("ctl\ttype\tnum\t%-40s\tdevice\n", "name");
215
216 for (i = 0; i < num_ctls; i++) {
217 ctl = mixer_get_ctl(mixer, i);
218
219 name = mixer_ctl_get_name(ctl);
220 type = mixer_ctl_get_type_string(ctl);
221 num_values = mixer_ctl_get_num_values(ctl);
222 device = mixer_ctl_get_device(ctl);
223 printf("%u\t%s\t%u\t%-40s\t%u", i, type, num_values, name, device);
224 if (print_all)
225 print_control_values(ctl);
226 printf("\n");
227 }
228 }
229
print_enum(struct mixer_ctl * ctl)230 static void print_enum(struct mixer_ctl *ctl)
231 {
232 unsigned int num_enums;
233 unsigned int i;
234 unsigned int value;
235 const char *string;
236
237 num_enums = mixer_ctl_get_num_enums(ctl);
238 value = mixer_ctl_get_value(ctl, 0);
239
240 for (i = 0; i < num_enums; i++) {
241 string = mixer_ctl_get_enum_string(ctl, i);
242 printf("%s%s, ", value == i ? "> " : "", string);
243 }
244 }
245
print_control_values_by_name_or_id(struct mixer * mixer,const char * name_or_id)246 static void print_control_values_by_name_or_id(struct mixer *mixer, const char *name_or_id)
247 {
248 struct mixer_ctl *ctl;
249
250 if (isnumber(name_or_id))
251 ctl = mixer_get_ctl(mixer, atoi(name_or_id));
252 else
253 ctl = mixer_get_ctl_by_name(mixer, name_or_id);
254
255 if (!ctl) {
256 fprintf(stderr, "Invalid mixer control\n");
257 return;
258 }
259
260 print_control_values(ctl);
261 }
262
print_control_values(struct mixer_ctl * control)263 static void print_control_values(struct mixer_ctl *control)
264 {
265 enum mixer_ctl_type type;
266 unsigned int num_values;
267 unsigned int i;
268 int min, max;
269 int ret;
270 char *buf = NULL;
271
272 type = mixer_ctl_get_type(control);
273 num_values = mixer_ctl_get_num_values(control);
274
275 if ((type == MIXER_CTL_TYPE_BYTE) && (num_values > 0)) {
276 buf = calloc(1, num_values);
277 if (buf == NULL) {
278 fprintf(stderr, "Failed to alloc mem for bytes %u\n", num_values);
279 return;
280 }
281
282 ret = mixer_ctl_get_array(control, buf, num_values);
283 if (ret < 0) {
284 fprintf(stderr, "Failed to mixer_ctl_get_array\n");
285 free(buf);
286 return;
287 }
288 }
289
290 for (i = 0; i < num_values; i++) {
291 switch (type)
292 {
293 case MIXER_CTL_TYPE_INT:
294 printf("%d", mixer_ctl_get_value(control, i));
295 break;
296 case MIXER_CTL_TYPE_BOOL:
297 printf("%s", mixer_ctl_get_value(control, i) ? "On" : "Off");
298 break;
299 case MIXER_CTL_TYPE_ENUM:
300 print_enum(control);
301 break;
302 case MIXER_CTL_TYPE_BYTE:
303 printf("%02hhx", buf[i]);
304 break;
305 default:
306 printf("unknown");
307 break;
308 };
309 if ((i + 1) < num_values) {
310 printf(", ");
311 }
312 }
313
314 if (type == MIXER_CTL_TYPE_INT) {
315 min = mixer_ctl_get_range_min(control);
316 max = mixer_ctl_get_range_max(control);
317 printf(" (range %d->%d)", min, max);
318 }
319
320 free(buf);
321 }
322
tinymix_set_byte_ctl(struct mixer_ctl * ctl,char ** values,unsigned int num_values)323 static void tinymix_set_byte_ctl(struct mixer_ctl *ctl,
324 char **values, unsigned int num_values)
325 {
326 int ret;
327 char *buf;
328 char *end;
329 unsigned int i;
330 long n;
331
332 buf = calloc(1, num_values);
333 if (buf == NULL) {
334 fprintf(stderr, "set_byte_ctl: Failed to alloc mem for bytes %u\n", num_values);
335 exit(EXIT_FAILURE);
336 }
337
338 for (i = 0; i < num_values; i++) {
339 errno = 0;
340 n = strtol(values[i], &end, 0);
341 if (*end) {
342 fprintf(stderr, "%s not an integer\n", values[i]);
343 goto fail;
344 }
345 if (errno) {
346 fprintf(stderr, "strtol: %s: %s\n", values[i],
347 strerror(errno));
348 goto fail;
349 }
350 if (n < 0 || n > 0xff) {
351 fprintf(stderr, "%s should be between [0, 0xff]\n",
352 values[i]);
353 goto fail;
354 }
355 buf[i] = n;
356 }
357
358 ret = mixer_ctl_set_array(ctl, buf, num_values);
359 if (ret < 0) {
360 fprintf(stderr, "Failed to set binary control\n");
361 goto fail;
362 }
363
364 free(buf);
365 return;
366
367 fail:
368 free(buf);
369 exit(EXIT_FAILURE);
370 }
371
is_int(const char * value)372 static int is_int(const char *value)
373 {
374 return value[0] >= '0' && value[0] <= '9';
375 }
376
377 struct parsed_int
378 {
379 /** Wether or not the integer was valid. */
380 int valid;
381 /** The value of the parsed integer. */
382 int value;
383 /** The number of characters that were parsed. */
384 unsigned int length;
385 /** The number of characters remaining in the string. */
386 unsigned int remaining_length;
387 /** The remaining characters (or suffix) of the integer. */
388 const char* remaining;
389 };
390
parse_int(const char * str)391 static struct parsed_int parse_int(const char* str)
392 {
393 struct parsed_int out = {
394 0 /* valid */,
395 0 /* value */,
396 0 /* length */,
397 0 /* remaining length */,
398 NULL /* remaining characters */
399 };
400
401 size_t length = strlen(str);
402 size_t i = 0;
403 int negative = 0;
404
405 if (i < length && str[i] == '-') {
406 negative = 1;
407 i++;
408 }
409
410 while (i < length) {
411 char c = str[i++];
412
413 if (c < '0' || c > '9') {
414 --i;
415 break;
416 }
417
418 out.value *= 10;
419 out.value += c - '0';
420
421 out.length++;
422 }
423
424 if (negative) {
425 out.value *= -1;
426 }
427
428 out.valid = out.length > 0;
429 out.remaining_length = length - out.length;
430 out.remaining = str + out.length;
431
432 return out;
433 }
434
435 struct control_value
436 {
437 int value;
438 int is_percent;
439 int is_relative;
440 };
441
to_control_value(const char * value_string)442 static struct control_value to_control_value(const char* value_string)
443 {
444 struct parsed_int parsed_int = parse_int(value_string);
445
446 struct control_value out = {
447 0 /* value */,
448 0 /* is percent */,
449 0 /* is relative */
450 };
451
452 out.value = parsed_int.value;
453
454 unsigned int i = 0;
455
456 if (parsed_int.remaining[i] == '%') {
457 out.is_percent = 1;
458 i++;
459 }
460
461 if (parsed_int.remaining[i] == '+') {
462 out.is_relative = 1;
463 } else if (parsed_int.remaining[i] == '-') {
464 out.is_relative = 1;
465 out.value *= -1;
466 }
467
468 return out;
469 }
470
set_control_value(struct mixer_ctl * ctl,unsigned int i,const struct control_value * value)471 static int set_control_value(struct mixer_ctl* ctl, unsigned int i,
472 const struct control_value* value)
473 {
474 int next_value = value->value;
475
476 if (value->is_relative) {
477
478 int prev_value = value->is_percent ? mixer_ctl_get_percent(ctl, i)
479 : mixer_ctl_get_value(ctl, i);
480
481 if (prev_value < 0) {
482 return prev_value;
483 }
484
485 next_value += prev_value;
486 }
487
488 return value->is_percent ? mixer_ctl_set_percent(ctl, i, next_value)
489 : mixer_ctl_set_value(ctl, i, next_value);
490 }
491
set_control_values(struct mixer_ctl * ctl,char ** values,unsigned int num_values)492 static int set_control_values(struct mixer_ctl* ctl,
493 char** values,
494 unsigned int num_values)
495 {
496 unsigned int num_ctl_values = mixer_ctl_get_num_values(ctl);
497
498 if (num_values == 1) {
499
500 /* Set all values the same */
501 struct control_value value = to_control_value(values[0]);
502
503 for (unsigned int i = 0; i < num_values; i++) {
504 int res = set_control_value(ctl, i, &value);
505 if (res != 0) {
506 fprintf(stderr, "Error: invalid value (%d%s%s)\n", value.value,
507 value.is_relative ? "r" : "", value.is_percent ? "%" : "");
508 return -1;
509 }
510 }
511
512 } else {
513
514 /* Set multiple values */
515 if (num_values > num_ctl_values) {
516 fprintf(stderr,
517 "Error: %u values given, but control only takes %u\n",
518 num_values, num_ctl_values);
519 return -1;
520 }
521
522 for (unsigned int i = 0; i < num_values; i++) {
523
524 struct control_value v = to_control_value(values[i]);
525
526 int res = set_control_value(ctl, i, &v);
527 if (res != 0) {
528 fprintf(stderr, "Error: invalid value (%d%s%s) for index %u\n", v.value,
529 v.is_relative ? "r" : "", v.is_percent ? "%" : "", i);
530 return -1;
531 }
532 }
533 }
534
535 return 0;
536 }
537
set_values(struct mixer * mixer,const char * control,char ** values,unsigned int num_values)538 static int set_values(struct mixer *mixer, const char *control,
539 char **values, unsigned int num_values)
540 {
541 struct mixer_ctl *ctl;
542 enum mixer_ctl_type type;
543
544 if (isnumber(control))
545 ctl = mixer_get_ctl(mixer, atoi(control));
546 else
547 ctl = mixer_get_ctl_by_name(mixer, control);
548
549 if (!ctl) {
550 fprintf(stderr, "Invalid mixer control\n");
551 return -1;
552 }
553
554 type = mixer_ctl_get_type(ctl);
555
556 if (type == MIXER_CTL_TYPE_BYTE) {
557 tinymix_set_byte_ctl(ctl, values, num_values);
558 return 0;
559 }
560
561 if (is_int(values[0])) {
562 set_control_values(ctl, values, num_values);
563 } else {
564 if (type == MIXER_CTL_TYPE_ENUM) {
565 if (num_values != 1) {
566 fprintf(stderr, "Enclose strings in quotes and try again\n");
567 return -1;
568 }
569 if (mixer_ctl_set_enum_by_string(ctl, values[0])) {
570 fprintf(stderr, "Error: invalid enum value\n");
571 return -1;
572 }
573 } else {
574 fprintf(stderr, "Error: only enum types can be set with strings\n");
575 return -1;
576 }
577 }
578
579 return 0;
580 }
581
582