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