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