Annotation of src/usr.bin/audioctl/audioctl.c, Revision 1.14
1.14 ! jakemsr 1: /* $OpenBSD: audioctl.c,v 1.13 2007/08/06 19:16:06 sobrado Exp $ */
1.3 provos 2: /* $NetBSD: audioctl.c,v 1.14 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
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: */
38:
1.7 pvalchev 39: /*
40: * audioctl(1) - a program to control audio device.
41: */
42:
1.1 provos 43: #include <stdio.h>
1.3 provos 44: #include <stdlib.h>
1.1 provos 45: #include <fcntl.h>
46: #include <err.h>
47: #include <unistd.h>
48: #include <string.h>
49: #include <sys/types.h>
50: #include <sys/stat.h>
51: #include <sys/ioctl.h>
52: #include <sys/audioio.h>
53:
1.6 millert 54: struct field *findfield(char *name);
1.12 moritz 55: void prfield(struct field *p, const char *sep);
1.6 millert 56: void rdfield(struct field *p, char *q);
57: void getinfo(int fd);
58: void usage(void);
59: int main(int argc, char **argv);
1.1 provos 60:
61: FILE *out = stdout;
62:
63: audio_device_t adev;
64:
65: audio_info_t info;
66:
67: char encbuf[1000];
68:
69: int properties, fullduplex, rerror;
70:
71: struct field {
1.12 moritz 72: const char *name;
1.1 provos 73: void *valp;
74: int format;
75: #define STRING 1
76: #define INT 2
77: #define UINT 3
78: #define P_R 4
79: #define ULONG 5
80: #define UCHAR 6
81: #define ENC 7
82: #define PROPS 8
83: #define XINT 9
84: char flags;
85: #define READONLY 1
86: #define ALIAS 2
87: #define SET 4
88: } fields[] = {
89: { "name", &adev.name, STRING, READONLY },
90: { "version", &adev.version, STRING, READONLY },
91: { "config", &adev.config, STRING, READONLY },
92: { "encodings", encbuf, STRING, READONLY },
93: { "properties", &properties, PROPS, READONLY },
1.4 deraadt 94: { "full_duplex", &fullduplex, UINT, 0 },
95: { "fullduplex", &fullduplex, UINT, 0 },
1.1 provos 96: { "blocksize", &info.blocksize, UINT, 0 },
97: { "hiwat", &info.hiwat, UINT, 0 },
98: { "lowat", &info.lowat, UINT, 0 },
1.14 ! jakemsr 99: { "output_muted", &info.output_muted, UCHAR, 0 },
1.1 provos 100: { "monitor_gain", &info.monitor_gain, UINT, 0 },
101: { "mode", &info.mode, P_R, READONLY },
102: { "play.rate", &info.play.sample_rate, UINT, 0 },
103: { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
104: { "play.channels", &info.play.channels, UINT, 0 },
105: { "play.precision", &info.play.precision, UINT, 0 },
106: { "play.encoding", &info.play.encoding, ENC, 0 },
107: { "play.gain", &info.play.gain, UINT, 0 },
108: { "play.balance", &info.play.balance, UCHAR, 0 },
109: { "play.port", &info.play.port, XINT, 0 },
110: { "play.avail_ports", &info.play.avail_ports, XINT, 0 },
1.8 naddy 111: { "play.seek", &info.play.seek, UINT, READONLY },
1.1 provos 112: { "play.samples", &info.play.samples, UINT, READONLY },
113: { "play.eof", &info.play.eof, UINT, READONLY },
114: { "play.pause", &info.play.pause, UCHAR, 0 },
115: { "play.error", &info.play.error, UCHAR, READONLY },
116: { "play.waiting", &info.play.waiting, UCHAR, READONLY },
117: { "play.open", &info.play.open, UCHAR, READONLY },
118: { "play.active", &info.play.active, UCHAR, READONLY },
119: { "play.buffer_size", &info.play.buffer_size, UINT, 0 },
120: { "record.rate", &info.record.sample_rate,UINT, 0 },
121: { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
122: { "record.channels", &info.record.channels, UINT, 0 },
123: { "record.precision", &info.record.precision, UINT, 0 },
124: { "record.encoding", &info.record.encoding, ENC, 0 },
125: { "record.gain", &info.record.gain, UINT, 0 },
126: { "record.balance", &info.record.balance, UCHAR, 0 },
127: { "record.port", &info.record.port, XINT, 0 },
128: { "record.avail_ports", &info.record.avail_ports,XINT, 0 },
1.8 naddy 129: { "record.seek", &info.record.seek, UINT, READONLY },
1.1 provos 130: { "record.samples", &info.record.samples, UINT, READONLY },
131: { "record.eof", &info.record.eof, UINT, READONLY },
132: { "record.pause", &info.record.pause, UCHAR, 0 },
133: { "record.error", &info.record.error, UCHAR, READONLY },
134: { "record.waiting", &info.record.waiting, UCHAR, READONLY },
135: { "record.open", &info.record.open, UCHAR, READONLY },
136: { "record.active", &info.record.active, UCHAR, READONLY },
137: { "record.buffer_size", &info.record.buffer_size,UINT, 0 },
138: { "record.errors", &rerror, INT, READONLY },
139: { 0 }
140: };
141:
142: struct {
1.12 moritz 143: const char *ename;
144: u_int eno;
1.1 provos 145: } encs[] = {
146: { AudioEmulaw, AUDIO_ENCODING_ULAW },
147: { "ulaw", AUDIO_ENCODING_ULAW },
148: { AudioEalaw, AUDIO_ENCODING_ALAW },
149: { AudioEslinear, AUDIO_ENCODING_SLINEAR },
150: { "linear", AUDIO_ENCODING_SLINEAR },
151: { AudioEulinear, AUDIO_ENCODING_ULINEAR },
152: { AudioEadpcm, AUDIO_ENCODING_ADPCM },
153: { "ADPCM", AUDIO_ENCODING_ADPCM },
154: { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
155: { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
156: { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
157: { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
158: { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
159: { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
160: { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
161: { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
162: { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
163: { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
164: { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
165: { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
166: { 0 }
167: };
168:
169: static struct {
1.12 moritz 170: const char *name;
1.1 provos 171: u_int prop;
172: } props[] = {
173: { "full_duplex", AUDIO_PROP_FULLDUPLEX },
174: { "mmap", AUDIO_PROP_MMAP },
175: { "independent", AUDIO_PROP_INDEPENDENT },
176: { 0 }
177: };
178:
179: struct field *
1.7 pvalchev 180: findfield(char *name)
1.1 provos 181: {
182: int i;
183: for(i = 0; fields[i].name; i++)
184: if (strcmp(fields[i].name, name) == 0)
185: return &fields[i];
1.7 pvalchev 186: return (0);
1.1 provos 187: }
188:
189: void
1.12 moritz 190: prfield(struct field *p, const char *sep)
1.1 provos 191: {
192: u_int v;
1.12 moritz 193: const char *cm;
1.1 provos 194: int i;
195:
196: if (sep)
197: fprintf(out, "%s%s", p->name, sep);
198: switch(p->format) {
199: case STRING:
200: fprintf(out, "%s", (char*)p->valp);
201: break;
202: case INT:
203: fprintf(out, "%d", *(int*)p->valp);
204: break;
205: case UINT:
206: fprintf(out, "%u", *(u_int*)p->valp);
207: break;
208: case XINT:
209: fprintf(out, "0x%x", *(u_int*)p->valp);
210: break;
211: case UCHAR:
212: fprintf(out, "%u", *(u_char*)p->valp);
213: break;
214: case ULONG:
215: fprintf(out, "%lu", *(u_long*)p->valp);
216: break;
217: case P_R:
218: v = *(u_int*)p->valp;
219: cm = "";
220: if (v & AUMODE_PLAY) {
221: if (v & AUMODE_PLAY_ALL)
222: fprintf(out, "play");
223: else
224: fprintf(out, "playsync");
225: cm = ",";
226: }
227: if (v & AUMODE_RECORD)
228: fprintf(out, "%srecord", cm);
229: break;
230: case ENC:
231: v = *(u_int*)p->valp;
232: for(i = 0; encs[i].ename; i++)
233: if (encs[i].eno == v)
234: break;
235: if (encs[i].ename)
236: fprintf(out, "%s", encs[i].ename);
237: else
238: fprintf(out, "%u", v);
239: break;
240: case PROPS:
241: v = *(u_int*)p->valp;
242: for (cm = "", i = 0; props[i].name; i++) {
243: if (v & props[i].prop) {
244: fprintf(out, "%s%s", cm, props[i].name);
245: cm = ",";
246: }
247: }
248: break;
249: default:
250: errx(1, "Invalid print format.");
251: }
252: }
253:
254: void
1.7 pvalchev 255: rdfield(struct field *p, char *q)
1.1 provos 256: {
257: int i;
258: u_int u;
259:
260: switch(p->format) {
261: case UINT:
262: if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
263: warnx("Bad number %s", q);
264: break;
265: case UCHAR:
266: if (sscanf(q, "%u", &u) != 1)
267: warnx("Bad number %s", q);
268: else
269: *(u_char *)p->valp = u;
270: break;
271: case XINT:
272: if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
273: sscanf(q, "%x", (unsigned int *)p->valp) != 1)
274: warnx("Bad number %s", q);
275: break;
276: case ENC:
277: for(i = 0; encs[i].ename; i++)
278: if (strcmp(encs[i].ename, q) == 0)
279: break;
280: if (encs[i].ename)
281: *(u_int*)p->valp = encs[i].eno;
282: else
283: warnx("Unknown encoding: %s", q);
284: break;
285: default:
286: errx(1, "Invalid read format.");
287: }
288: p->flags |= SET;
289: }
290:
291: void
1.7 pvalchev 292: getinfo(int fd)
1.1 provos 293: {
1.7 pvalchev 294: int pos = 0, i = 0;
1.1 provos 295:
296: if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
297: err(1, "AUDIO_GETDEV");
1.7 pvalchev 298: for(;;) {
1.9 deraadt 299: audio_encoding_t enc;
300: enc.index = i++;
301: if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
302: break;
303: if (pos)
304: encbuf[pos++] = ',';
305: snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
306: enc.name, enc.precision,
307: enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
308: pos += strlen(encbuf+pos);
1.1 provos 309: }
310: if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
311: err(1, "AUDIO_GETFD");
312: if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
313: err(1, "AUDIO_GETPROPS");
314: if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
315: err(1, "AUDIO_RERROR");
316: if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
317: err(1, "AUDIO_GETINFO");
318: }
319:
320: void
1.7 pvalchev 321: usage(void)
1.1 provos 322: {
1.7 pvalchev 323: extern char *__progname; /* from crt0.o */
324:
325: fprintf(stderr,
326: "usage: %s [-f file] [-n] -a\n"
1.13 sobrado 327: " %s [-f file] [-n] name ...\n"
328: " %s [-f file] [-n] name=value ...\n", __progname,
1.7 pvalchev 329: __progname, __progname);
330:
1.1 provos 331: exit(1);
332: }
333:
334: int
1.7 pvalchev 335: main(int argc, char **argv)
1.1 provos 336: {
337: int fd, i, ch;
1.11 vincent 338: int aflag = 0, canwrite, writeinfo = 0;
1.1 provos 339: struct stat dstat, ostat;
1.11 vincent 340: struct field *p;
1.12 moritz 341: const char *file;
342: const char *sep = "=";
1.1 provos 343:
1.7 pvalchev 344: if ((file = getenv("AUDIOCTLDEVICE")) == 0 || *file == '\0')
1.3 provos 345: file = "/dev/audioctl";
1.1 provos 346:
347: while ((ch = getopt(argc, argv, "af:nw")) != -1) {
348: switch(ch) {
349: case 'a':
350: aflag++;
351: break;
352: case 'w':
1.11 vincent 353: /* backward compatibility */
1.1 provos 354: break;
355: case 'n':
356: sep = 0;
357: break;
358: case 'f':
359: file = optarg;
360: break;
361: default:
362: usage();
363: }
364: }
365: argc -= optind;
366: argv += optind;
1.11 vincent 367:
368: if ((fd = open(file, O_RDWR)) < 0) {
369: if ((fd = open(file, O_RDONLY)) < 0)
370: err(1, "%s", file);
371: canwrite = 0;
372: } else
373: canwrite = 1;
1.1 provos 374:
375: /* Check if stdout is the same device as the audio device. */
376: if (fstat(fd, &dstat) < 0)
377: err(1, "fstat au");
378: if (fstat(STDOUT_FILENO, &ostat) < 0)
379: err(1, "fstat stdout");
380: if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
381: major(dstat.st_dev) == major(ostat.st_dev) &&
382: minor(dstat.st_dev) == minor(ostat.st_dev))
383: /* We can't write to stdout so use stderr */
384: out = stderr;
385:
1.11 vincent 386: if (!argc && !aflag)
387: usage();
388:
389: getinfo(fd);
1.1 provos 390:
1.11 vincent 391: if (aflag) {
392: for (i = 0; fields[i].name; i++) {
1.1 provos 393: if (!(fields[i].flags & ALIAS)) {
394: prfield(&fields[i], sep);
395: fprintf(out, "\n");
396: }
397: }
1.11 vincent 398: } else {
399: while (argc--) {
400: char *q;
1.1 provos 401:
1.11 vincent 402: if ((q = strchr(*argv, '=')) != NULL) {
403: *q++ = 0;
404: p = findfield(*argv);
405: if (p == 0)
406: warnx("field `%s' does not exist", *argv);
407: else {
408: if (!canwrite)
409: errx(1, "%s: permission denied",
410: *argv);
411: if (p->flags & READONLY)
412: warnx("`%s' is read only", *argv);
1.1 provos 413: else {
1.11 vincent 414: rdfield(p, q);
415: if (p->valp == &fullduplex)
416: if (ioctl(fd, AUDIO_SETFD,
417: &fullduplex) < 0)
418: err(1, "set failed");
1.1 provos 419: }
1.11 vincent 420: writeinfo = 1;
1.1 provos 421: }
1.11 vincent 422: } else {
1.1 provos 423: p = findfield(*argv);
1.11 vincent 424: if (p == 0)
425: warnx("field %s does not exist", *argv);
426: else {
1.1 provos 427: prfield(p, sep);
428: fprintf(out, "\n");
429: }
1.11 vincent 430: }
431: argv++;
432: }
433: if (writeinfo && ioctl(fd, AUDIO_SETINFO, &info) < 0)
434: err(1, "set failed");
435: if (sep) {
436: getinfo(fd);
437: for (i = 0; fields[i].name; i++) {
438: if (fields[i].flags & SET) {
439: fprintf(out, "%s: -> ", fields[i].name);
440: prfield(&fields[i], 0);
441: fprintf(out, "\n");
442: }
1.1 provos 443: }
444: }
1.11 vincent 445: }
1.1 provos 446: exit(0);
447: }