Annotation of src/usr.bin/audioctl/audioctl.c, Revision 1.26
1.26 ! ratchov 1: /* $OpenBSD$ */
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: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
31:
1.7 pvalchev 32: /*
33: * audioctl(1) - a program to control audio device.
34: */
35:
1.1 provos 36: #include <stdio.h>
1.3 provos 37: #include <stdlib.h>
1.1 provos 38: #include <fcntl.h>
39: #include <err.h>
40: #include <unistd.h>
41: #include <string.h>
42: #include <sys/types.h>
43: #include <sys/stat.h>
44: #include <sys/ioctl.h>
45: #include <sys/audioio.h>
46:
1.6 millert 47: struct field *findfield(char *name);
1.12 moritz 48: void prfield(struct field *p, const char *sep);
1.6 millert 49: void rdfield(struct field *p, char *q);
50: void getinfo(int fd);
51: void usage(void);
52: int main(int argc, char **argv);
1.1 provos 53:
54: FILE *out = stdout;
55:
56: audio_device_t adev;
57:
58: audio_info_t info;
59:
60: char encbuf[1000];
61:
1.18 jakemsr 62: int properties, fullduplex, perrors, rerrors;
1.1 provos 63:
64: struct field {
1.12 moritz 65: const char *name;
1.1 provos 66: void *valp;
67: int format;
68: #define STRING 1
69: #define INT 2
70: #define UINT 3
71: #define P_R 4
72: #define UCHAR 6
73: #define ENC 7
74: #define PROPS 8
75: #define XINT 9
76: char flags;
77: #define READONLY 1
78: #define ALIAS 2
79: #define SET 4
1.22 krw 80: u_int oldval;
1.1 provos 81: } fields[] = {
82: { "name", &adev.name, STRING, READONLY },
83: { "encodings", encbuf, STRING, READONLY },
84: { "properties", &properties, PROPS, READONLY },
85: { "hiwat", &info.hiwat, UINT, 0 },
86: { "lowat", &info.lowat, UINT, 0 },
87: { "mode", &info.mode, P_R, READONLY },
88: { "play.rate", &info.play.sample_rate, UINT, 0 },
89: { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
90: { "play.channels", &info.play.channels, UINT, 0 },
91: { "play.precision", &info.play.precision, UINT, 0 },
1.21 jakemsr 92: { "play.bps", &info.play.bps, UINT, 0 },
93: { "play.msb", &info.play.msb, UINT, 0 },
1.1 provos 94: { "play.encoding", &info.play.encoding, ENC, 0 },
95: { "play.samples", &info.play.samples, UINT, READONLY },
96: { "play.pause", &info.play.pause, UCHAR, 0 },
97: { "play.active", &info.play.active, UCHAR, READONLY },
1.17 ratchov 98: { "play.block_size", &info.play.block_size, UINT, 0 },
1.18 jakemsr 99: { "play.errors", &perrors, INT, READONLY },
1.1 provos 100: { "record.rate", &info.record.sample_rate,UINT, 0 },
101: { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
102: { "record.channels", &info.record.channels, UINT, 0 },
103: { "record.precision", &info.record.precision, UINT, 0 },
1.21 jakemsr 104: { "record.bps", &info.record.bps, UINT, 0 },
105: { "record.msb", &info.record.msb, UINT, 0 },
1.1 provos 106: { "record.encoding", &info.record.encoding, ENC, 0 },
107: { "record.samples", &info.record.samples, UINT, READONLY },
108: { "record.pause", &info.record.pause, UCHAR, 0 },
109: { "record.active", &info.record.active, UCHAR, READONLY },
1.17 ratchov 110: { "record.block_size", &info.record.block_size,UINT, 0 },
1.18 jakemsr 111: { "record.errors", &rerrors, INT, READONLY },
1.1 provos 112: { 0 }
113: };
114:
115: struct {
1.12 moritz 116: const char *ename;
117: u_int eno;
1.1 provos 118: } encs[] = {
119: { AudioEmulaw, AUDIO_ENCODING_ULAW },
120: { "ulaw", AUDIO_ENCODING_ULAW },
121: { AudioEalaw, AUDIO_ENCODING_ALAW },
122: { AudioEslinear, AUDIO_ENCODING_SLINEAR },
123: { "linear", AUDIO_ENCODING_SLINEAR },
124: { AudioEulinear, AUDIO_ENCODING_ULINEAR },
125: { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
126: { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
127: { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
128: { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
129: { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
130: { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
131: { 0 }
132: };
133:
134: static struct {
1.12 moritz 135: const char *name;
1.1 provos 136: u_int prop;
137: } props[] = {
138: { "full_duplex", AUDIO_PROP_FULLDUPLEX },
139: { "mmap", AUDIO_PROP_MMAP },
140: { "independent", AUDIO_PROP_INDEPENDENT },
141: { 0 }
142: };
143:
144: struct field *
1.7 pvalchev 145: findfield(char *name)
1.1 provos 146: {
147: int i;
1.15 sobrado 148: for (i = 0; fields[i].name; i++)
1.1 provos 149: if (strcmp(fields[i].name, name) == 0)
150: return &fields[i];
1.7 pvalchev 151: return (0);
1.1 provos 152: }
153:
1.23 deraadt 154: static void
1.20 ratchov 155: prval(u_int format, void *valp)
1.1 provos 156: {
157: u_int v;
1.12 moritz 158: const char *cm;
1.1 provos 159: int i;
160:
1.20 ratchov 161: switch (format) {
1.1 provos 162: case STRING:
1.20 ratchov 163: fprintf(out, "%s", (char *)valp);
1.1 provos 164: break;
165: case INT:
1.20 ratchov 166: fprintf(out, "%d", *(int *)valp);
1.1 provos 167: break;
168: case UINT:
1.20 ratchov 169: fprintf(out, "%u", *(u_int *)valp);
1.1 provos 170: break;
171: case XINT:
1.20 ratchov 172: fprintf(out, "0x%x", *(u_int *)valp);
1.1 provos 173: break;
174: case UCHAR:
1.20 ratchov 175: fprintf(out, "%u", *(u_char *)valp);
1.1 provos 176: break;
177: case P_R:
1.20 ratchov 178: v = *(u_int *)valp;
1.1 provos 179: cm = "";
180: if (v & AUMODE_PLAY) {
181: if (v & AUMODE_PLAY_ALL)
182: fprintf(out, "play");
183: else
184: fprintf(out, "playsync");
185: cm = ",";
186: }
187: if (v & AUMODE_RECORD)
188: fprintf(out, "%srecord", cm);
189: break;
190: case ENC:
1.20 ratchov 191: v = *(u_int *)valp;
1.15 sobrado 192: for (i = 0; encs[i].ename; i++)
1.1 provos 193: if (encs[i].eno == v)
194: break;
195: if (encs[i].ename)
196: fprintf(out, "%s", encs[i].ename);
197: else
198: fprintf(out, "%u", v);
199: break;
200: case PROPS:
1.20 ratchov 201: v = *(u_int *)valp;
1.1 provos 202: for (cm = "", i = 0; props[i].name; i++) {
203: if (v & props[i].prop) {
204: fprintf(out, "%s%s", cm, props[i].name);
205: cm = ",";
206: }
207: }
208: break;
209: default:
210: errx(1, "Invalid print format.");
211: }
212: }
213:
214: void
1.20 ratchov 215: prfield(struct field *p, const char *sep)
216: {
217: if (sep) {
218: fprintf(out, "%s", p->name);
219: if (p->flags & SET) {
220: fprintf(out, "%s", ": ");
221: prval(p->format, &p->oldval);
222: fprintf(out, " -> ");
223: } else
224: fprintf(out, "%s", sep);
225: }
226: prval(p->format, p->valp);
227: fprintf(out, "\n");
228: }
229:
230: void
1.7 pvalchev 231: rdfield(struct field *p, char *q)
1.1 provos 232: {
233: int i;
234: u_int u;
235:
1.15 sobrado 236: switch (p->format) {
1.1 provos 237: case UINT:
1.20 ratchov 238: p->oldval = *(u_int *)p->valp;
239: if (sscanf(q, "%u", (unsigned int *)p->valp) != 1) {
1.1 provos 240: warnx("Bad number %s", q);
1.20 ratchov 241: return;
242: }
1.1 provos 243: break;
244: case UCHAR:
1.20 ratchov 245: *(char *)&p->oldval = *(u_char *)p->valp;
246: if (sscanf(q, "%u", &u) != 1) {
1.1 provos 247: warnx("Bad number %s", q);
1.20 ratchov 248: return;
249: }
250: *(u_char *)p->valp = u;
1.1 provos 251: break;
252: case XINT:
1.20 ratchov 253: p->oldval = *(u_int *)p->valp;
1.1 provos 254: if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
1.20 ratchov 255: sscanf(q, "%x", (unsigned int *)p->valp) != 1) {
1.1 provos 256: warnx("Bad number %s", q);
1.20 ratchov 257: return;
258: }
1.1 provos 259: break;
260: case ENC:
1.20 ratchov 261: p->oldval = *(u_int *)p->valp;
1.15 sobrado 262: for (i = 0; encs[i].ename; i++)
1.1 provos 263: if (strcmp(encs[i].ename, q) == 0)
264: break;
265: if (encs[i].ename)
266: *(u_int*)p->valp = encs[i].eno;
1.20 ratchov 267: else {
1.1 provos 268: warnx("Unknown encoding: %s", q);
1.20 ratchov 269: return;
270: }
1.1 provos 271: break;
272: default:
273: errx(1, "Invalid read format.");
274: }
275: p->flags |= SET;
276: }
277:
278: void
1.7 pvalchev 279: getinfo(int fd)
1.1 provos 280: {
1.7 pvalchev 281: int pos = 0, i = 0;
1.1 provos 282:
283: if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
284: err(1, "AUDIO_GETDEV");
1.15 sobrado 285: for (;;) {
1.9 deraadt 286: audio_encoding_t enc;
287: enc.index = i++;
288: if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
289: break;
290: if (pos)
291: encbuf[pos++] = ',';
1.21 jakemsr 292: snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d:%d:%d%s",
293: enc.name, enc.precision, enc.bps, enc.msb,
1.9 deraadt 294: enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
295: pos += strlen(encbuf+pos);
1.1 provos 296: }
297: if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
298: err(1, "AUDIO_GETFD");
299: if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
300: err(1, "AUDIO_GETPROPS");
1.18 jakemsr 301: if (ioctl(fd, AUDIO_PERROR, &perrors) < 0)
302: err(1, "AUDIO_PERROR");
303: if (ioctl(fd, AUDIO_RERROR, &rerrors) < 0)
1.1 provos 304: err(1, "AUDIO_RERROR");
305: if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
306: err(1, "AUDIO_GETINFO");
307: }
308:
309: void
1.7 pvalchev 310: usage(void)
1.1 provos 311: {
1.7 pvalchev 312: extern char *__progname; /* from crt0.o */
313:
314: fprintf(stderr,
1.16 deraadt 315: "usage: %s [-an] [-f file]\n"
1.15 sobrado 316: " %s [-n] [-f file] name ...\n"
317: " %s [-n] [-f file] name=value ...\n",
318: __progname, __progname, __progname);
1.7 pvalchev 319:
1.1 provos 320: exit(1);
321: }
322:
323: int
1.7 pvalchev 324: main(int argc, char **argv)
1.1 provos 325: {
326: int fd, i, ch;
1.11 vincent 327: int aflag = 0, canwrite, writeinfo = 0;
1.1 provos 328: struct stat dstat, ostat;
1.11 vincent 329: struct field *p;
1.12 moritz 330: const char *file;
331: const char *sep = "=";
1.1 provos 332:
1.7 pvalchev 333: if ((file = getenv("AUDIOCTLDEVICE")) == 0 || *file == '\0')
1.3 provos 334: file = "/dev/audioctl";
1.1 provos 335:
336: while ((ch = getopt(argc, argv, "af:nw")) != -1) {
1.15 sobrado 337: switch (ch) {
1.1 provos 338: case 'a':
1.25 deraadt 339: aflag = 1;
1.1 provos 340: break;
341: case 'w':
1.11 vincent 342: /* backward compatibility */
1.1 provos 343: break;
344: case 'n':
345: sep = 0;
346: break;
347: case 'f':
348: file = optarg;
349: break;
350: default:
351: usage();
352: }
353: }
354: argc -= optind;
355: argv += optind;
1.16 deraadt 356:
357: if (argc == 0)
1.25 deraadt 358: aflag = 1;
1.11 vincent 359:
360: if ((fd = open(file, O_RDWR)) < 0) {
361: if ((fd = open(file, O_RDONLY)) < 0)
362: err(1, "%s", file);
363: canwrite = 0;
364: } else
365: canwrite = 1;
1.1 provos 366:
367: /* Check if stdout is the same device as the audio device. */
368: if (fstat(fd, &dstat) < 0)
369: err(1, "fstat au");
370: if (fstat(STDOUT_FILENO, &ostat) < 0)
371: err(1, "fstat stdout");
372: if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
373: major(dstat.st_dev) == major(ostat.st_dev) &&
374: minor(dstat.st_dev) == minor(ostat.st_dev))
375: /* We can't write to stdout so use stderr */
376: out = stderr;
377:
1.11 vincent 378: if (!argc && !aflag)
379: usage();
380:
381: getinfo(fd);
1.1 provos 382:
1.11 vincent 383: if (aflag) {
384: for (i = 0; fields[i].name; i++) {
1.1 provos 385: if (!(fields[i].flags & ALIAS)) {
386: prfield(&fields[i], sep);
387: }
388: }
1.11 vincent 389: } else {
390: while (argc--) {
391: char *q;
1.1 provos 392:
1.11 vincent 393: if ((q = strchr(*argv, '=')) != NULL) {
394: *q++ = 0;
395: p = findfield(*argv);
396: if (p == 0)
397: warnx("field `%s' does not exist", *argv);
398: else {
399: if (!canwrite)
400: errx(1, "%s: permission denied",
401: *argv);
402: if (p->flags & READONLY)
403: warnx("`%s' is read only", *argv);
1.1 provos 404: else {
1.11 vincent 405: rdfield(p, q);
406: if (p->valp == &fullduplex)
407: if (ioctl(fd, AUDIO_SETFD,
408: &fullduplex) < 0)
409: err(1, "set failed");
1.1 provos 410: }
1.11 vincent 411: writeinfo = 1;
1.1 provos 412: }
1.11 vincent 413: } else {
1.1 provos 414: p = findfield(*argv);
1.11 vincent 415: if (p == 0)
416: warnx("field %s does not exist", *argv);
417: else {
1.1 provos 418: prfield(p, sep);
419: }
1.11 vincent 420: }
421: argv++;
422: }
423: if (writeinfo && ioctl(fd, AUDIO_SETINFO, &info) < 0)
424: err(1, "set failed");
1.20 ratchov 425: getinfo(fd);
426: for (i = 0; fields[i].name; i++) {
427: if (fields[i].flags & SET) {
428: prfield(&fields[i], sep);
1.1 provos 429: }
430: }
1.11 vincent 431: }
1.1 provos 432: exit(0);
433: }