Annotation of src/usr.bin/mixerctl/mixerctl.c, Revision 1.26
1.26 ! deraadt 1: /* $OpenBSD: mixerctl.c,v 1.25 2007/09/27 21:55:54 sobrado Exp $ */
1.3 provos 2: /* $NetBSD: mixerctl.c,v 1.11 1998/04/27 16:55:23 augustss Exp $ */
1.1 provos 3:
4: /*
5: * Copyright (c) 1997 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * Author: Lennart Augustsson, with some code and ideas from Chuck Cranor.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
1.9 pvalchev 38:
39: /*
40: * mixerctl(1) - a program to control audio mixing.
41: */
42:
1.19 otto 43: #include <sys/types.h>
44: #include <sys/ioctl.h>
45: #include <sys/audioio.h>
46:
47: #include <err.h>
1.20 millert 48: #include <errno.h>
1.19 otto 49: #include <fcntl.h>
1.20 millert 50: #include <limits.h>
1.1 provos 51: #include <stdio.h>
52: #include <stdlib.h>
1.19 otto 53: #include <string.h>
1.1 provos 54: #include <unistd.h>
55:
1.20 millert 56: struct field *findfield(char *);
57: void adjlevel(char **, u_char *, int);
1.19 otto 58: void catstr(char *, char *, char *);
59: void prfield(struct field *, char *, int);
1.21 millert 60: void rdfield(int, struct field *, char *, int);
1.19 otto 61: __dead void usage(void);
1.1 provos 62:
1.19 otto 63: #define FIELD_NAME_MAX 64
1.1 provos 64:
65: struct field {
1.19 otto 66: char name[FIELD_NAME_MAX];
1.1 provos 67: mixer_ctrl_t *valp;
68: mixer_devinfo_t *infp;
69: } *fields, *rfields;
70:
71: mixer_ctrl_t *values;
72: mixer_devinfo_t *infos;
73:
1.19 otto 74: void
75: catstr(char *p, char *q, char *out)
1.1 provos 76: {
1.19 otto 77: char tmp[FIELD_NAME_MAX];
1.8 deraadt 78:
1.19 otto 79: snprintf(tmp, FIELD_NAME_MAX, "%s.%s", p, q);
80: strlcpy(out, tmp, FIELD_NAME_MAX);
1.1 provos 81: }
82:
83: struct field *
1.9 pvalchev 84: findfield(char *name)
1.1 provos 85: {
86: int i;
1.19 otto 87: for (i = 0; fields[i].name[0] != '\0'; i++)
1.1 provos 88: if (strcmp(fields[i].name, name) == 0)
89: return &fields[i];
1.9 pvalchev 90: return (0);
1.1 provos 91: }
92:
1.19 otto 93: #define e_member_name un.e.member[i].label.name
94: #define s_member_name un.s.member[i].label.name
95:
1.1 provos 96: void
1.9 pvalchev 97: prfield(struct field *p, char *sep, int prvalset)
1.1 provos 98: {
99: mixer_ctrl_t *m;
100: int i, n;
101:
102: if (sep)
1.19 otto 103: printf("%s%s", p->name, sep);
1.1 provos 104: m = p->valp;
1.19 otto 105: switch (m->type) {
1.1 provos 106: case AUDIO_MIXER_ENUM:
1.18 millert 107: for (i = 0; i < p->infp->un.e.num_mem; i++)
1.1 provos 108: if (p->infp->un.e.member[i].ord == m->un.ord)
1.19 otto 109: printf("%s",
110: p->infp->e_member_name);
1.1 provos 111: if (prvalset) {
1.19 otto 112: printf(" [ ");
1.18 millert 113: for (i = 0; i < p->infp->un.e.num_mem; i++)
1.19 otto 114: printf("%s ", p->infp->e_member_name);
115: printf("]");
1.1 provos 116: }
117: break;
118: case AUDIO_MIXER_SET:
1.18 millert 119: for (n = i = 0; i < p->infp->un.s.num_mem; i++)
1.1 provos 120: if (m->un.mask & p->infp->un.s.member[i].mask)
1.19 otto 121: printf("%s%s", n++ ? "," : "",
122: p->infp->s_member_name);
1.1 provos 123: if (prvalset) {
1.19 otto 124: printf(" { ");
1.18 millert 125: for (i = 0; i < p->infp->un.s.num_mem; i++)
1.19 otto 126: printf("%s ", p->infp->s_member_name);
127: printf("}");
1.1 provos 128: }
129: break;
130: case AUDIO_MIXER_VALUE:
131: if (m->un.value.num_channels == 1)
1.19 otto 132: printf("%d", m->un.value.level[0]);
1.1 provos 133: else
1.19 otto 134: printf("%d,%d", m->un.value.level[0],
1.22 deraadt 135: m->un.value.level[1]);
1.1 provos 136: if (prvalset)
1.19 otto 137: printf(" %s", p->infp->un.v.units.name);
1.1 provos 138: break;
139: default:
140: errx(1, "Invalid format.");
141: }
142: }
143:
1.19 otto 144: void
1.20 millert 145: adjlevel(char **p, u_char *olevel, int more)
146: {
147: char *ep, *cp = *p;
148: long inc;
149: u_char level;
150:
151: if (*cp != '+' && *cp != '-')
152: *olevel = 0; /* absolute setting */
153:
154: errno = 0;
155: inc = strtol(cp, &ep, 10);
156: if (*cp == '\0' || (*ep != '\0' && *ep != ',') ||
157: (errno == ERANGE && (inc == LONG_MAX || inc == LONG_MIN)))
158: errx(1, "Bad number %s", cp);
159: if (*ep == ',' && !more)
160: errx(1, "Too many values");
161: *p = ep;
162:
163: if (inc < AUDIO_MIN_GAIN - *olevel)
164: level = AUDIO_MIN_GAIN;
165: else if (inc > AUDIO_MAX_GAIN - *olevel)
166: level = AUDIO_MAX_GAIN;
167: else
168: level = *olevel + inc;
169: *olevel = level;
170: }
171:
172: void
1.21 millert 173: rdfield(int fd, struct field *p, char *q, int quiet)
1.1 provos 174: {
1.19 otto 175: mixer_ctrl_t *m, oldval;
1.20 millert 176: int i, mask;
1.1 provos 177: char *s;
178:
1.19 otto 179: oldval = *p->valp;
1.1 provos 180: m = p->valp;
1.19 otto 181:
182: switch (m->type) {
1.1 provos 183: case AUDIO_MIXER_ENUM:
1.22 deraadt 184: if (strcmp(q, "toggle") == 0) {
185: for (i = 0; i < p->infp->un.e.num_mem; i++) {
186: if (m->un.ord == p->infp->un.e.member[i].ord)
187: break;
188: }
189: if (i < p->infp->un.e.num_mem)
190: i++;
191: else
192: i = 0;
193: m->un.ord = p->infp->un.e.member[i].ord;
194: break;
195: }
1.18 millert 196: for (i = 0; i < p->infp->un.e.num_mem; i++)
1.19 otto 197: if (strcmp(p->infp->e_member_name, q) == 0)
1.1 provos 198: break;
199: if (i < p->infp->un.e.num_mem)
200: m->un.ord = p->infp->un.e.member[i].ord;
1.9 pvalchev 201: else
1.22 deraadt 202: errx(1, "Bad enum value %s", q);
1.1 provos 203: break;
204: case AUDIO_MIXER_SET:
205: mask = 0;
1.20 millert 206: for (; q && *q; q = s) {
1.19 otto 207: if ((s = strchr(q, ',')) != NULL)
1.1 provos 208: *s++ = 0;
1.4 millert 209: for (i = 0; i < p->infp->un.s.num_mem; i++)
1.19 otto 210: if (strcmp(p->infp->s_member_name, q) == 0)
1.1 provos 211: break;
1.9 pvalchev 212: if (i < p->infp->un.s.num_mem)
1.1 provos 213: mask |= p->infp->un.s.member[i].mask;
1.9 pvalchev 214: else
1.22 deraadt 215: errx(1, "Bad set value %s", q);
1.1 provos 216: }
217: m->un.mask = mask;
218: break;
219: case AUDIO_MIXER_VALUE:
220: if (m->un.value.num_channels == 1) {
1.20 millert 221: adjlevel(&q, &m->un.value.level[0], 0);
1.1 provos 222: } else {
1.20 millert 223: adjlevel(&q, &m->un.value.level[0], 1);
224: if (*q++ == ',')
225: adjlevel(&q, &m->un.value.level[1], 0);
226: else
227: m->un.value.level[1] = m->un.value.level[0];
1.1 provos 228: }
229: break;
230: default:
231: errx(1, "Invalid format.");
232: }
1.19 otto 233:
234: if (ioctl(fd, AUDIO_MIXER_WRITE, p->valp) < 0) {
235: warn("AUDIO_MIXER_WRITE");
1.21 millert 236: } else if (!quiet) {
1.19 otto 237: *p->valp = oldval;
238: prfield(p, ": ", 0);
239: if (ioctl(fd, AUDIO_MIXER_READ, p->valp) < 0) {
240: warn("AUDIO_MIXER_READ");
241: } else {
242: printf(" -> ");
243: prfield(p, NULL, 0);
244: printf("\n");
245: }
246: }
1.1 provos 247: }
248:
249: int
1.9 pvalchev 250: main(int argc, char **argv)
1.1 provos 251: {
252: int fd, i, j, ch, pos;
1.22 deraadt 253: int aflag = 0, qflag = 0, vflag = 0, tflag = 0;
1.3 provos 254: char *file;
1.1 provos 255: char *sep = "=";
256: mixer_devinfo_t dinfo;
1.16 tedu 257: int ndev;
1.1 provos 258:
1.9 pvalchev 259: if ((file = getenv("MIXERDEVICE")) == 0 || *file == '\0')
1.22 deraadt 260: file = "/dev/mixer";
1.1 provos 261:
1.22 deraadt 262: while ((ch = getopt(argc, argv, "af:nqtvw")) != -1) {
1.25 sobrado 263: switch (ch) {
1.1 provos 264: case 'a':
265: aflag++;
266: break;
267: case 'w':
1.14 tedu 268: /* compat */
1.1 provos 269: break;
270: case 'v':
271: vflag++;
272: break;
273: case 'n':
274: sep = 0;
275: break;
276: case 'f':
277: file = optarg;
278: break;
1.10 jfb 279: case 'q':
280: qflag = 1;
281: break;
1.22 deraadt 282: case 't':
283: tflag = 1;
284: break;
1.1 provos 285: default:
1.9 pvalchev 286: usage();
1.1 provos 287: }
288: }
289: argc -= optind;
290: argv += optind;
1.6 mickey 291:
1.26 ! deraadt 292: if (argc == 0 && tflag == 0)
! 293: aflag++;
! 294:
1.14 tedu 295: if ((fd = open(file, O_RDWR)) == -1)
296: if ((fd = open(file, O_RDONLY)) == -1)
297: err(1, "%s", file);
1.1 provos 298:
1.18 millert 299: for (ndev = 0; ; ndev++) {
1.16 tedu 300: dinfo.index = ndev;
1.14 tedu 301: if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0)
302: break;
1.1 provos 303: }
1.5 mickey 304:
1.9 pvalchev 305: if (!ndev)
1.5 mickey 306: errx(1, "no mixer devices configured");
307:
1.9 pvalchev 308: if ((rfields = calloc(ndev, sizeof *rfields)) == NULL ||
309: (fields = calloc(ndev, sizeof *fields)) == NULL ||
310: (infos = calloc(ndev, sizeof *infos)) == NULL ||
311: (values = calloc(ndev, sizeof *values)) == NULL)
312: err(1, "calloc()");
1.1 provos 313:
1.18 millert 314: for (i = 0; i < ndev; i++) {
1.1 provos 315: infos[i].index = i;
1.17 millert 316: if (ioctl(fd, AUDIO_MIXER_DEVINFO, &infos[i]) < 0) {
1.19 otto 317: ndev--;
318: i--;
1.17 millert 319: continue;
320: }
1.1 provos 321: }
322:
1.18 millert 323: for (i = 0; i < ndev; i++) {
1.19 otto 324: strlcpy(rfields[i].name, infos[i].label.name, FIELD_NAME_MAX);
1.1 provos 325: rfields[i].valp = &values[i];
326: rfields[i].infp = &infos[i];
327: }
328:
1.18 millert 329: for (i = 0; i < ndev; i++) {
1.1 provos 330: values[i].dev = i;
331: values[i].type = infos[i].type;
332: if (infos[i].type != AUDIO_MIXER_CLASS) {
333: values[i].un.value.num_channels = 2;
334: if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0) {
335: values[i].un.value.num_channels = 1;
336: if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0)
337: err(1, "AUDIO_MIXER_READ");
338: }
339: }
340: }
341:
1.18 millert 342: for (j = i = 0; i < ndev; i++) {
1.1 provos 343: if (infos[i].type != AUDIO_MIXER_CLASS &&
344: infos[i].type != -1) {
345: fields[j++] = rfields[i];
1.18 millert 346: for (pos = infos[i].next; pos != AUDIO_MIXER_LAST;
1.1 provos 347: pos = infos[pos].next) {
348: fields[j] = rfields[pos];
1.19 otto 349: catstr(rfields[i].name, infos[pos].label.name,
350: fields[j].name);
1.1 provos 351: infos[pos].type = -1;
352: j++;
353: }
354: }
355: }
356:
1.18 millert 357: for (i = 0; i < j; i++) {
1.1 provos 358: int cls = fields[i].infp->mixer_class;
359: if (cls >= 0 && cls < ndev)
1.19 otto 360: catstr(infos[cls].label.name, fields[i].name,
361: fields[i].name);
1.1 provos 362: }
363:
1.14 tedu 364: if (!argc && aflag) {
1.19 otto 365: for (i = 0; fields[i].name[0] != '\0'; i++) {
1.1 provos 366: prfield(&fields[i], sep, vflag);
1.19 otto 367: printf("\n");
1.1 provos 368: }
369: } else if (argc > 0 && !aflag) {
370: struct field *p;
1.14 tedu 371:
1.19 otto 372: while (argc--) {
1.14 tedu 373: char *q;
374:
1.19 otto 375: ch = 0;
376: if ((q = strchr(*argv, '=')) != NULL) {
377: *q++ = '\0';
378: ch = 1;
379: }
380:
381: if ((p = findfield(*argv)) == NULL) {
382: warnx("field %s does not exist", *argv);
1.22 deraadt 383: } else if (ch || tflag) {
384: if (tflag && q == NULL)
385: q = "toggle";
1.21 millert 386: rdfield(fd, p, q, qflag);
1.14 tedu 387: } else {
1.19 otto 388: prfield(p, sep, vflag);
389: printf("\n");
1.1 provos 390: }
1.19 otto 391:
1.17 millert 392: argv++;
1.1 provos 393: }
394: } else
1.9 pvalchev 395: usage();
1.1 provos 396: exit(0);
1.9 pvalchev 397: }
398:
1.19 otto 399: __dead void
1.9 pvalchev 400: usage(void)
401: {
402: extern char *__progname; /* from crt0.o */
403:
404: fprintf(stderr,
1.26 ! deraadt 405: "usage: %s [-anv] [-f file]\n"
1.24 sobrado 406: " %s [-nv] [-f file] name ...\n"
407: " %s [-qt] [-f file] name ...\n"
1.25 sobrado 408: " %s [-q] [-f file] name=value ...\n",
1.23 deraadt 409: __progname, __progname, __progname, __progname);
1.9 pvalchev 410:
411: exit(1);
1.1 provos 412: }