Annotation of src/usr.bin/mixerctl/mixerctl.c, Revision 1.21
1.21 ! millert 1: /* $OpenBSD: mixerctl.c,v 1.20 2005/02/07 14:29:10 millert 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.1 provos 135: m->un.value.level[1]);
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.18 millert 184: for (i = 0; i < p->infp->un.e.num_mem; i++)
1.19 otto 185: if (strcmp(p->infp->e_member_name, q) == 0)
1.1 provos 186: break;
187: if (i < p->infp->un.e.num_mem)
188: m->un.ord = p->infp->un.e.member[i].ord;
1.9 pvalchev 189: else
190: errx(1, "Bad enum value %s", q);
1.1 provos 191: break;
192: case AUDIO_MIXER_SET:
193: mask = 0;
1.20 millert 194: for (; q && *q; q = s) {
1.19 otto 195: if ((s = strchr(q, ',')) != NULL)
1.1 provos 196: *s++ = 0;
1.4 millert 197: for (i = 0; i < p->infp->un.s.num_mem; i++)
1.19 otto 198: if (strcmp(p->infp->s_member_name, q) == 0)
1.1 provos 199: break;
1.9 pvalchev 200: if (i < p->infp->un.s.num_mem)
1.1 provos 201: mask |= p->infp->un.s.member[i].mask;
1.9 pvalchev 202: else
203: errx(1, "Bad set value %s", q);
1.1 provos 204: }
205: m->un.mask = mask;
206: break;
207: case AUDIO_MIXER_VALUE:
208: if (m->un.value.num_channels == 1) {
1.20 millert 209: adjlevel(&q, &m->un.value.level[0], 0);
1.1 provos 210: } else {
1.20 millert 211: adjlevel(&q, &m->un.value.level[0], 1);
212: if (*q++ == ',')
213: adjlevel(&q, &m->un.value.level[1], 0);
214: else
215: m->un.value.level[1] = m->un.value.level[0];
1.1 provos 216: }
217: break;
218: default:
219: errx(1, "Invalid format.");
220: }
1.19 otto 221:
222: if (ioctl(fd, AUDIO_MIXER_WRITE, p->valp) < 0) {
223: warn("AUDIO_MIXER_WRITE");
1.21 ! millert 224: } else if (!quiet) {
1.19 otto 225: *p->valp = oldval;
226: prfield(p, ": ", 0);
227: if (ioctl(fd, AUDIO_MIXER_READ, p->valp) < 0) {
228: warn("AUDIO_MIXER_READ");
229: } else {
230: printf(" -> ");
231: prfield(p, NULL, 0);
232: printf("\n");
233: }
234: }
1.1 provos 235: }
236:
237: int
1.9 pvalchev 238: main(int argc, char **argv)
1.1 provos 239: {
240: int fd, i, j, ch, pos;
1.14 tedu 241: int aflag = 0, qflag = 0, vflag = 0;
1.3 provos 242: char *file;
1.1 provos 243: char *sep = "=";
244: mixer_devinfo_t dinfo;
1.16 tedu 245: int ndev;
1.1 provos 246:
1.9 pvalchev 247: if ((file = getenv("MIXERDEVICE")) == 0 || *file == '\0')
248: file = "/dev/mixer";
1.1 provos 249:
1.10 jfb 250: while ((ch = getopt(argc, argv, "af:nqvw")) != -1) {
1.1 provos 251: switch(ch) {
252: case 'a':
253: aflag++;
254: break;
255: case 'w':
1.14 tedu 256: /* compat */
1.1 provos 257: break;
258: case 'v':
259: vflag++;
260: break;
261: case 'n':
262: sep = 0;
263: break;
264: case 'f':
265: file = optarg;
266: break;
1.10 jfb 267: case 'q':
268: qflag = 1;
269: break;
1.1 provos 270: case '?':
271: default:
1.9 pvalchev 272: usage();
1.1 provos 273: }
274: }
275: argc -= optind;
276: argv += optind;
1.6 mickey 277:
1.14 tedu 278: if ((fd = open(file, O_RDWR)) == -1)
279: if ((fd = open(file, O_RDONLY)) == -1)
280: err(1, "%s", file);
1.1 provos 281:
1.18 millert 282: for (ndev = 0; ; ndev++) {
1.16 tedu 283: dinfo.index = ndev;
1.14 tedu 284: if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0)
285: break;
1.1 provos 286: }
1.5 mickey 287:
1.9 pvalchev 288: if (!ndev)
1.5 mickey 289: errx(1, "no mixer devices configured");
290:
1.9 pvalchev 291: if ((rfields = calloc(ndev, sizeof *rfields)) == NULL ||
292: (fields = calloc(ndev, sizeof *fields)) == NULL ||
293: (infos = calloc(ndev, sizeof *infos)) == NULL ||
294: (values = calloc(ndev, sizeof *values)) == NULL)
295: err(1, "calloc()");
1.1 provos 296:
1.18 millert 297: for (i = 0; i < ndev; i++) {
1.1 provos 298: infos[i].index = i;
1.17 millert 299: if (ioctl(fd, AUDIO_MIXER_DEVINFO, &infos[i]) < 0) {
1.19 otto 300: ndev--;
301: i--;
1.17 millert 302: continue;
303: }
1.1 provos 304: }
305:
1.18 millert 306: for (i = 0; i < ndev; i++) {
1.19 otto 307: strlcpy(rfields[i].name, infos[i].label.name, FIELD_NAME_MAX);
1.1 provos 308: rfields[i].valp = &values[i];
309: rfields[i].infp = &infos[i];
310: }
311:
1.18 millert 312: for (i = 0; i < ndev; i++) {
1.1 provos 313: values[i].dev = i;
314: values[i].type = infos[i].type;
315: if (infos[i].type != AUDIO_MIXER_CLASS) {
316: values[i].un.value.num_channels = 2;
317: if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0) {
318: values[i].un.value.num_channels = 1;
319: if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0)
320: err(1, "AUDIO_MIXER_READ");
321: }
322: }
323: }
324:
1.18 millert 325: for (j = i = 0; i < ndev; i++) {
1.1 provos 326: if (infos[i].type != AUDIO_MIXER_CLASS &&
327: infos[i].type != -1) {
328: fields[j++] = rfields[i];
1.18 millert 329: for (pos = infos[i].next; pos != AUDIO_MIXER_LAST;
1.1 provos 330: pos = infos[pos].next) {
331: fields[j] = rfields[pos];
1.19 otto 332: catstr(rfields[i].name, infos[pos].label.name,
333: fields[j].name);
1.1 provos 334: infos[pos].type = -1;
335: j++;
336: }
337: }
338: }
339:
1.18 millert 340: for (i = 0; i < j; i++) {
1.1 provos 341: int cls = fields[i].infp->mixer_class;
342: if (cls >= 0 && cls < ndev)
1.19 otto 343: catstr(infos[cls].label.name, fields[i].name,
344: fields[i].name);
1.1 provos 345: }
346:
1.14 tedu 347: if (!argc && aflag) {
1.19 otto 348: for (i = 0; fields[i].name[0] != '\0'; i++) {
1.1 provos 349: prfield(&fields[i], sep, vflag);
1.19 otto 350: printf("\n");
1.1 provos 351: }
352: } else if (argc > 0 && !aflag) {
353: struct field *p;
1.14 tedu 354:
1.19 otto 355: while (argc--) {
1.14 tedu 356: char *q;
357:
1.19 otto 358: ch = 0;
359: if ((q = strchr(*argv, '=')) != NULL) {
360: *q++ = '\0';
361: ch = 1;
362: }
363:
364: if ((p = findfield(*argv)) == NULL) {
365: warnx("field %s does not exist", *argv);
366: } else if (ch) {
1.21 ! millert 367: rdfield(fd, p, q, qflag);
1.14 tedu 368: } else {
1.19 otto 369: prfield(p, sep, vflag);
370: printf("\n");
1.1 provos 371: }
1.19 otto 372:
1.17 millert 373: argv++;
1.1 provos 374: }
375: } else
1.9 pvalchev 376: usage();
1.1 provos 377: exit(0);
1.9 pvalchev 378: }
379:
1.19 otto 380: __dead void
1.9 pvalchev 381: usage(void)
382: {
383: extern char *__progname; /* from crt0.o */
384:
385: fprintf(stderr,
1.13 jmc 386: "usage: %s [-nv] [-f file] -a\n"
387: " %s [-nv] [-f file] name [...]\n"
1.14 tedu 388: " %s [-q] [-f file] name=value [...]\n",
1.11 jsyn 389: __progname, __progname, __progname);
1.9 pvalchev 390:
391: exit(1);
1.1 provos 392: }