ctl.c revision 1.25 1 /* $NetBSD: ctl.c,v 1.25 2001/02/05 01:22:22 christos Exp $ */
2
3 /*
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) netbsd.org).
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
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <sys/audioio.h>
43
44 #include <err.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include <paths.h>
52
53 #include "libaudio.h"
54
55 struct field *findfield (char *name);
56 void prfield (struct field *p, char *sep);
57 void rdfield (struct field *p, char *q);
58 void getinfo (int fd);
59 void audioctl_write (int, int, char *[]);
60 void usage (void);
61 int main (int argc, char **argv);
62
63 FILE *out = stdout;
64
65 char *prog;
66
67 audio_device_t adev;
68
69 audio_info_t info;
70
71 char encbuf[1000];
72
73 int properties, fullduplex, rerror;
74
75 struct field {
76 char *name;
77 void *valp;
78 int format;
79 #define STRING 1
80 #define INT 2
81 #define UINT 3
82 #define P_R 4
83 #define ULONG 5 /* XXX obsolete now */
84 #define UCHAR 6
85 #define ENC 7
86 #define PROPS 8
87 #define XINT 9
88 #define FORMAT 10
89 char flags;
90 #define READONLY 1
91 #define ALIAS 2
92 #define SET 4
93 } fields[] = {
94 { "name", &adev.name, STRING, READONLY },
95 { "version", &adev.version, STRING, READONLY },
96 { "config", &adev.config, STRING, READONLY },
97 { "encodings", encbuf, STRING, READONLY },
98 { "properties", &properties, PROPS, READONLY },
99 { "full_duplex", &fullduplex, UINT, 0 },
100 { "fullduplex", &fullduplex, UINT, 0 },
101 { "blocksize", &info.blocksize, UINT, 0 },
102 { "hiwat", &info.hiwat, UINT, 0 },
103 { "lowat", &info.lowat, UINT, 0 },
104 { "monitor_gain", &info.monitor_gain, UINT, 0 },
105 { "mode", &info.mode, P_R, READONLY },
106 { "play", &info.play, FORMAT, ALIAS },
107 { "play.rate", &info.play.sample_rate, UINT, 0 },
108 { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
109 { "play.channels", &info.play.channels, UINT, 0 },
110 { "play.precision", &info.play.precision, UINT, 0 },
111 { "play.encoding", &info.play.encoding, ENC, 0 },
112 { "play.gain", &info.play.gain, UINT, 0 },
113 { "play.balance", &info.play.balance, UCHAR, 0 },
114 { "play.port", &info.play.port, XINT, 0 },
115 { "play.avail_ports", &info.play.avail_ports, XINT, 0 },
116 { "play.seek", &info.play.seek, UINT, READONLY },
117 { "play.samples", &info.play.samples, UINT, READONLY },
118 { "play.eof", &info.play.eof, UINT, READONLY },
119 { "play.pause", &info.play.pause, UCHAR, 0 },
120 { "play.error", &info.play.error, UCHAR, READONLY },
121 { "play.waiting", &info.play.waiting, UCHAR, READONLY },
122 { "play.open", &info.play.open, UCHAR, READONLY },
123 { "play.active", &info.play.active, UCHAR, READONLY },
124 { "play.buffer_size", &info.play.buffer_size, UINT, 0 },
125 { "record", &info.record, FORMAT, ALIAS },
126 { "record.rate", &info.record.sample_rate,UINT, 0 },
127 { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
128 { "record.channels", &info.record.channels, UINT, 0 },
129 { "record.precision", &info.record.precision, UINT, 0 },
130 { "record.encoding", &info.record.encoding, ENC, 0 },
131 { "record.gain", &info.record.gain, UINT, 0 },
132 { "record.balance", &info.record.balance, UCHAR, 0 },
133 { "record.port", &info.record.port, XINT, 0 },
134 { "record.avail_ports", &info.record.avail_ports,XINT, 0 },
135 { "record.seek", &info.record.seek, UINT, READONLY },
136 { "record.samples", &info.record.samples, UINT, READONLY },
137 { "record.eof", &info.record.eof, UINT, READONLY },
138 { "record.pause", &info.record.pause, UCHAR, 0 },
139 { "record.error", &info.record.error, UCHAR, READONLY },
140 { "record.waiting", &info.record.waiting, UCHAR, READONLY },
141 { "record.open", &info.record.open, UCHAR, READONLY },
142 { "record.active", &info.record.active, UCHAR, READONLY },
143 { "record.buffer_size", &info.record.buffer_size,UINT, 0 },
144 { "record.errors", &rerror, INT, READONLY },
145 { 0 }
146 };
147
148 static struct {
149 char *name;
150 u_int prop;
151 } props[] = {
152 { "full_duplex", AUDIO_PROP_FULLDUPLEX },
153 { "mmap", AUDIO_PROP_MMAP },
154 { "independent", AUDIO_PROP_INDEPENDENT },
155 { 0 }
156 };
157
158 extern char *__progname;
159
160 struct field *
161 findfield(name)
162 char *name;
163 {
164 int i;
165 for (i = 0; fields[i].name; i++)
166 if (strcmp(fields[i].name, name) == 0)
167 return &fields[i];
168 return 0;
169 }
170
171 void
172 prfield(p, sep)
173 struct field *p;
174 char *sep;
175 {
176 u_int v;
177 char *cm, *encstr;
178 int i;
179
180 if (sep)
181 fprintf(out, "%s%s", p->name, sep);
182 switch(p->format) {
183 case STRING:
184 fprintf(out, "%s", (char*)p->valp);
185 break;
186 case INT:
187 fprintf(out, "%d", *(int*)p->valp);
188 break;
189 case UINT:
190 fprintf(out, "%u", *(u_int*)p->valp);
191 break;
192 case XINT:
193 fprintf(out, "0x%x", *(u_int*)p->valp);
194 break;
195 case UCHAR:
196 fprintf(out, "%u", *(u_char*)p->valp);
197 break;
198 case ULONG:
199 fprintf(out, "%lu", *(u_long*)p->valp);
200 break;
201 case P_R:
202 v = *(u_int*)p->valp;
203 cm = "";
204 if (v & AUMODE_PLAY) {
205 if (v & AUMODE_PLAY_ALL)
206 fprintf(out, "play");
207 else
208 fprintf(out, "playsync");
209 cm = ",";
210 }
211 if (v & AUMODE_RECORD)
212 fprintf(out, "%srecord", cm);
213 break;
214 case ENC:
215 v = *(u_int*)p->valp;
216 encstr = audio_enc_from_val(v);
217 if (encstr)
218 fprintf(out, "%s", encstr);
219 else
220 fprintf(out, "%u", v);
221 break;
222 case PROPS:
223 v = *(u_int*)p->valp;
224 for (cm = "", i = 0; props[i].name; i++) {
225 if (v & props[i].prop) {
226 fprintf(out, "%s%s", cm, props[i].name);
227 cm = ",";
228 }
229 }
230 break;
231 case FORMAT:
232 prfield(p + 1, 0);
233 fprintf(out, ",");
234 prfield(p + 3, 0);
235 fprintf(out, ",");
236 prfield(p + 4, 0);
237 fprintf(out, ",");
238 prfield(p + 5, 0);
239 break;
240 default:
241 errx(1, "Invalid print format.");
242 }
243 }
244
245 void
246 rdfield(p, q)
247 struct field *p;
248 char *q;
249 {
250 int enc;
251 u_int u;
252 char *s;
253
254 switch(p->format) {
255 case UINT:
256 if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
257 errx(1, "Bad number: %s", q);
258 break;
259 case UCHAR:
260 if (sscanf(q, "%u", &u) != 1)
261 errx(1, "Bad number: %s", q);
262 else
263 *(u_char *)p->valp = u;
264 break;
265 case XINT:
266 if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
267 sscanf(q, "%x", (unsigned int *)p->valp) != 1)
268 errx(1, "Bad number: %s", q);
269 break;
270 case ENC:
271 enc = audio_enc_to_val(q);
272 if (enc >= 0)
273 *(u_int*)p->valp = enc;
274 else
275 errx(1, "Unknown encoding: %s", q);
276 break;
277 case FORMAT:
278 s = strsep(&q, ",");
279 if (s)
280 rdfield(p + 1, s);
281 s = strsep(&q, ",");
282 if (s)
283 rdfield(p + 3, s);
284 s = strsep(&q, ",");
285 if (s)
286 rdfield(p + 4, s);
287 s = strsep(&q, ",");
288 if (s)
289 rdfield(p + 5, s);
290 if (!s || q)
291 errx(1, "Bad format");
292 break;
293 default:
294 errx(1, "Invalid read format.");
295 }
296 p->flags |= SET;
297 }
298
299 void
300 getinfo(fd)
301 int fd;
302 {
303 int pos, i;
304
305 if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
306 err(1, "AUDIO_GETDEV");
307 for (pos = 0, i = 0; ; i++) {
308 audio_encoding_t enc;
309 enc.index = i;
310 if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
311 break;
312 if (pos >= sizeof(encbuf)-1)
313 break;
314 if (pos)
315 encbuf[pos++] = ',';
316 if (pos >= sizeof(encbuf)-1)
317 break;
318 pos += snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
319 enc.name, enc.precision,
320 enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
321 }
322 if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
323 err(1, "AUDIO_GETFD");
324 if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
325 err(1, "AUDIO_GETPROPS");
326 if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
327 err(1, "AUDIO_RERROR");
328 if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
329 err(1, "AUDIO_GETINFO");
330 }
331
332 void
333 usage()
334 {
335
336 fprintf(stderr, "Usage: %s [-f file] [-n] name ...\n", prog);
337 fprintf(stderr, "Usage: %s [-f file] [-n] -w name=value ...\n", prog);
338 fprintf(stderr, "Usage: %s [-f file] [-n] -a\n", prog);
339 exit(1);
340 }
341
342 int
343 main(argc, argv)
344 int argc;
345 char **argv;
346 {
347 int fd, i, ch;
348 int aflag = 0, wflag = 0;
349 struct stat dstat, ostat;
350 const char *file;
351 char *sep = "=";
352
353 file = getenv("AUDIOCTLDEVICE");
354 if (file == 0)
355 file = _PATH_AUDIOCTL;
356
357 prog = __progname;
358
359 while ((ch = getopt(argc, argv, "af:nw")) != -1) {
360 switch(ch) {
361 case 'a':
362 aflag++;
363 break;
364 case 'w':
365 wflag++;
366 break;
367 case 'n':
368 sep = 0;
369 break;
370 case 'f':
371 file = optarg;
372 break;
373 case '?':
374 default:
375 usage();
376 }
377 }
378 argc -= optind;
379 argv += optind;
380
381 fd = open(file, O_WRONLY);
382 if (fd < 0)
383 fd = open(file, O_RDONLY);
384 #ifdef _PATH_OAUDIOCTL
385 /* Allow the non-unit device to be used. */
386 if (fd < 0 && file == _PATH_AUDIOCTL) {
387 file = _PATH_OAUDIOCTL;
388 fd = open(file, O_WRONLY);
389 if (fd < 0)
390 fd = open(file, O_RDONLY);
391 }
392 #endif
393 if (fd < 0)
394 err(1, "%s", file);
395
396 /* Check if stdout is the same device as the audio device. */
397 if (fstat(fd, &dstat) < 0)
398 err(1, "fstat audioctl");
399 if (fstat(STDOUT_FILENO, &ostat) < 0)
400 err(1, "fstat stdout");
401 if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
402 major(dstat.st_dev) == major(ostat.st_dev) &&
403 minor(dstat.st_dev) == minor(ostat.st_dev))
404 /* We can't write to stdout so use stderr */
405 out = stderr;
406
407 if (!wflag)
408 getinfo(fd);
409
410 if (argc == 0 && aflag && !wflag) {
411 for (i = 0; fields[i].name; i++) {
412 if (!(fields[i].flags & ALIAS)) {
413 prfield(&fields[i], sep);
414 fprintf(out, "\n");
415 }
416 }
417 } else if (argc > 0 && !aflag) {
418 if (wflag) {
419 audioctl_write(fd, argc, argv);
420 if (sep) {
421 getinfo(fd);
422 for (i = 0; fields[i].name; i++) {
423 if (fields[i].flags & SET) {
424 fprintf(out, "%s: -> ", fields[i].name);
425 prfield(&fields[i], 0);
426 fprintf(out, "\n");
427 }
428 }
429 }
430 } else {
431 struct field *p;
432 while (argc--) {
433 p = findfield(*argv);
434 if (p == 0) {
435 if (strchr(*argv, '='))
436 warnx("field %s does not exist (use -w to set a variable)", *argv);
437 else
438 warnx("field %s does not exist", *argv);
439 } else {
440 prfield(p, sep);
441 fprintf(out, "\n");
442 }
443 argv++;
444 }
445 }
446 } else
447 usage();
448 exit(0);
449 }
450
451 void
452 audioctl_write(fd, argc, argv)
453 int fd;
454 int argc;
455 char *argv[];
456 {
457 struct field *p;
458
459 AUDIO_INITINFO(&info);
460 while (argc--) {
461 char *q;
462
463 q = strchr(*argv, '=');
464 if (q) {
465 *q++ = 0;
466 p = findfield(*argv);
467 if (p == 0)
468 warnx("field `%s' does not exist", *argv);
469 else {
470 if (p->flags & READONLY)
471 warnx("`%s' is read only", *argv);
472 else {
473 rdfield(p, q);
474 if (p->valp == &fullduplex)
475 if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
476 err(1, "set failed");
477 }
478 }
479 } else
480 warnx("No `=' in %s", *argv);
481 argv++;
482 }
483 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
484 err(1, "set failed");
485 }
486