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