Home | History | Annotate | Line # | Download | only in ctl
ctl.c revision 1.21
      1 /*	$NetBSD: ctl.c,v 1.21 1998/11/25 22:17:08 augustss 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 <stdio.h>
     40 #include <stdlib.h>
     41 #include <fcntl.h>
     42 #include <err.h>
     43 #include <unistd.h>
     44 #include <string.h>
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/audioio.h>
     49 
     50 #define AUDIOCTL "/dev/audioctl0"
     51 #define OLD_AUDIOCTL "/dev/audioctl"
     52 
     53 struct field *findfield __P((char *name));
     54 void prfield __P((struct field *p, char *sep));
     55 void rdfield __P((struct field *p, char *q));
     56 void getinfo __P((int fd));
     57 void usage __P((void));
     58 int main __P((int argc, char **argv));
     59 
     60 FILE *out = stdout;
     61 
     62 char *prog;
     63 
     64 audio_device_t adev;
     65 
     66 audio_info_t info;
     67 
     68 char encbuf[1000];
     69 
     70 int properties, fullduplex, rerror;
     71 
     72 struct field {
     73 	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 	{ 0 }
    143 };
    144 
    145 struct {
    146 	char *ename;
    147 	int eno;
    148 } encs[] = {
    149 	{ AudioEmulaw,		AUDIO_ENCODING_ULAW },
    150 	{ "ulaw",		AUDIO_ENCODING_ULAW },
    151 	{ AudioEalaw, 		AUDIO_ENCODING_ALAW },
    152 	{ AudioEslinear,	AUDIO_ENCODING_SLINEAR },
    153 	{ "linear",		AUDIO_ENCODING_SLINEAR },
    154 	{ AudioEulinear,	AUDIO_ENCODING_ULINEAR },
    155 	{ AudioEadpcm,		AUDIO_ENCODING_ADPCM },
    156 	{ "ADPCM",		AUDIO_ENCODING_ADPCM },
    157 	{ AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE },
    158 	{ "linear_le",		AUDIO_ENCODING_SLINEAR_LE },
    159 	{ AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE },
    160 	{ AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE },
    161 	{ "linear_be",		AUDIO_ENCODING_SLINEAR_BE },
    162 	{ AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE },
    163 	{ AudioEmpeg_l1_stream,	AUDIO_ENCODING_MPEG_L1_STREAM },
    164 	{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
    165 	{ AudioEmpeg_l1_system,	AUDIO_ENCODING_MPEG_L1_SYSTEM },
    166 	{ AudioEmpeg_l2_stream,	AUDIO_ENCODING_MPEG_L2_STREAM },
    167 	{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
    168 	{ AudioEmpeg_l2_system,	AUDIO_ENCODING_MPEG_L2_SYSTEM },
    169 	{ 0 }
    170 };
    171 
    172 static struct {
    173 	char *name;
    174 	u_int prop;
    175 } props[] = {
    176 	{ "full_duplex",	AUDIO_PROP_FULLDUPLEX },
    177 	{ "mmap",		AUDIO_PROP_MMAP },
    178 	{ "independent",	AUDIO_PROP_INDEPENDENT },
    179 	{ 0 }
    180 };
    181 
    182 struct field *
    183 findfield(name)
    184 	char *name;
    185 {
    186 	int i;
    187 	for(i = 0; fields[i].name; i++)
    188 		if (strcmp(fields[i].name, name) == 0)
    189 			return &fields[i];
    190 	return 0;
    191 }
    192 
    193 void
    194 prfield(p, sep)
    195 	struct field *p;
    196 	char *sep;
    197 {
    198 	u_int v;
    199 	char *cm;
    200 	int i;
    201 
    202 	if (sep)
    203 		fprintf(out, "%s%s", p->name, sep);
    204 	switch(p->format) {
    205 	case STRING:
    206 		fprintf(out, "%s", (char*)p->valp);
    207 		break;
    208 	case INT:
    209 		fprintf(out, "%d", *(int*)p->valp);
    210 		break;
    211 	case UINT:
    212 		fprintf(out, "%u", *(u_int*)p->valp);
    213 		break;
    214 	case XINT:
    215 		fprintf(out, "0x%x", *(u_int*)p->valp);
    216 		break;
    217 	case UCHAR:
    218 		fprintf(out, "%u", *(u_char*)p->valp);
    219 		break;
    220 	case ULONG:
    221 		fprintf(out, "%lu", *(u_long*)p->valp);
    222 		break;
    223 	case P_R:
    224 		v = *(u_int*)p->valp;
    225 		cm = "";
    226 		if (v & AUMODE_PLAY) {
    227 			if (v & AUMODE_PLAY_ALL)
    228 				fprintf(out, "play");
    229 			else
    230 				fprintf(out, "playsync");
    231 			cm = ",";
    232 		}
    233 		if (v & AUMODE_RECORD)
    234 			fprintf(out, "%srecord", cm);
    235 		break;
    236 	case ENC:
    237 		v = *(u_int*)p->valp;
    238 		for(i = 0; encs[i].ename; i++)
    239 			if (encs[i].eno == v)
    240 				break;
    241 		if (encs[i].ename)
    242 			fprintf(out, "%s", encs[i].ename);
    243 		else
    244 			fprintf(out, "%u", v);
    245 		break;
    246 	case PROPS:
    247 		v = *(u_int*)p->valp;
    248 		for (cm = "", i = 0; props[i].name; i++) {
    249 			if (v & props[i].prop) {
    250 				fprintf(out, "%s%s", cm, props[i].name);
    251 				cm = ",";
    252 			}
    253 		}
    254 		break;
    255 	case FORMAT:
    256 		prfield(p + 1, 0);
    257 		fprintf(out, ",");
    258 		prfield(p + 3, 0);
    259 		fprintf(out, ",");
    260 		prfield(p + 4, 0);
    261 		fprintf(out, ",");
    262 		prfield(p + 5, 0);
    263 		break;
    264 	default:
    265 		errx(1, "Invalid print format.");
    266 	}
    267 }
    268 
    269 void
    270 rdfield(p, q)
    271 	struct field *p;
    272 	char *q;
    273 {
    274 	int i;
    275 	u_int u;
    276 	char *s;
    277 
    278 	switch(p->format) {
    279 	case UINT:
    280 		if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
    281 			errx(1, "Bad number: %s", q);
    282 		break;
    283 	case UCHAR:
    284 		if (sscanf(q, "%u", &u) != 1)
    285 			errx(1, "Bad number: %s", q);
    286 		else
    287 			*(u_char *)p->valp = u;
    288 		break;
    289 	case XINT:
    290 		if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
    291 		    sscanf(q, "%x", (unsigned int *)p->valp) != 1)
    292 			errx(1, "Bad number: %s", q);
    293 		break;
    294 	case ENC:
    295 		for(i = 0; encs[i].ename; i++)
    296 			if (strcmp(encs[i].ename, q) == 0)
    297 				break;
    298 		if (encs[i].ename)
    299 			*(u_int*)p->valp = encs[i].eno;
    300 		else
    301 			errx(1, "Unknown encoding: %s", q);
    302 		break;
    303 	case FORMAT:
    304 		s = strsep(&q, ",");
    305 		if (s)
    306 			rdfield(p + 1, s);
    307 		s = strsep(&q, ",");
    308 		if (s)
    309 			rdfield(p + 3, s);
    310 		s = strsep(&q, ",");
    311 		if (s)
    312 			rdfield(p + 4, s);
    313 		s = strsep(&q, ",");
    314 		if (s)
    315 			rdfield(p + 5, s);
    316 		if (!s || q)
    317 			errx(1, "Bad format");
    318 		break;
    319 	default:
    320 		errx(1, "Invalid read format.");
    321 	}
    322 	p->flags |= SET;
    323 }
    324 
    325 void
    326 getinfo(fd)
    327 	int fd;
    328 {
    329 	int pos, i;
    330 
    331 	if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
    332 		err(1, "AUDIO_GETDEV");
    333 	for (pos = 0, i = 0; ; i++) {
    334 		audio_encoding_t enc;
    335 		enc.index = i;
    336 		if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
    337 			break;
    338 		if (pos >= sizeof(encbuf)-1)
    339 			break;
    340 		if (pos)
    341 			encbuf[pos++] = ',';
    342 		if (pos >= sizeof(encbuf)-1)
    343 			break;
    344 		pos += snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
    345 			enc.name, enc.precision,
    346 			enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
    347 	}
    348 	if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
    349 		err(1, "AUDIO_GETFD");
    350 	if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
    351 		err(1, "AUDIO_GETPROPS");
    352 	if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
    353 		err(1, "AUDIO_RERROR");
    354 	if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
    355 		err(1, "AUDIO_GETINFO");
    356 }
    357 
    358 void
    359 usage()
    360 {
    361 	fprintf(out, "%s [-f file] [-n] name ...\n", prog);
    362 	fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
    363 	fprintf(out, "%s [-f file] [-n] -a\n", prog);
    364 	exit(1);
    365 }
    366 
    367 int
    368 main(argc, argv)
    369 	int argc;
    370 	char **argv;
    371 {
    372 	int fd, i, ch;
    373 	int aflag = 0, wflag = 0;
    374 	struct stat dstat, ostat;
    375 	const char *file;
    376 	char *sep = "=";
    377 
    378 	file = getenv("AUDIOCTLDEVICE");
    379 	if (file == 0)
    380 		file = AUDIOCTL;
    381 
    382 	prog = *argv;
    383 
    384 	while ((ch = getopt(argc, argv, "af:nw")) != -1) {
    385 		switch(ch) {
    386 		case 'a':
    387 			aflag++;
    388 			break;
    389 		case 'w':
    390 			wflag++;
    391 			break;
    392 		case 'n':
    393 			sep = 0;
    394 			break;
    395 		case 'f':
    396 			file = optarg;
    397 			break;
    398 		case '?':
    399 		default:
    400 			usage();
    401 		}
    402 	}
    403 	argc -= optind;
    404 	argv += optind;
    405 
    406 	fd = open(file, O_WRONLY);
    407 	if (fd < 0)
    408 		fd = open(file, O_RDONLY);
    409 #ifdef OLD_AUDIOCTL
    410         /* Allow the non-unit device to be used. */
    411         if (fd < 0 && file == AUDIOCTL) {
    412         	file = OLD_AUDIOCTL;
    413                 fd = open(file, O_WRONLY);
    414 		if (fd < 0)
    415 			fd = open(file, O_RDONLY);
    416         }
    417 #endif
    418 	if (fd < 0)
    419 		err(1, "%s", file);
    420 
    421 	/* Check if stdout is the same device as the audio device. */
    422 	if (fstat(fd, &dstat) < 0)
    423 		err(1, "fstat au");
    424 	if (fstat(STDOUT_FILENO, &ostat) < 0)
    425 		err(1, "fstat stdout");
    426 	if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
    427 	    major(dstat.st_dev) == major(ostat.st_dev) &&
    428 	    minor(dstat.st_dev) == minor(ostat.st_dev))
    429 		/* We can't write to stdout so use stderr */
    430 		out = stderr;
    431 
    432 	if (!wflag)
    433 		getinfo(fd);
    434 
    435 	if (argc == 0 && aflag && !wflag) {
    436 		for(i = 0; fields[i].name; i++) {
    437 			if (!(fields[i].flags & ALIAS)) {
    438 				prfield(&fields[i], sep);
    439 				fprintf(out, "\n");
    440 			}
    441 		}
    442 	} else if (argc > 0 && !aflag) {
    443 		struct field *p;
    444 		if (wflag) {
    445 			AUDIO_INITINFO(&info);
    446 			while(argc--) {
    447 				char *q;
    448 
    449 				q = strchr(*argv, '=');
    450 				if (q) {
    451 					*q++ = 0;
    452 					p = findfield(*argv);
    453 					if (p == 0)
    454 						warnx("field `%s' does not exist", *argv);
    455 					else {
    456 						if (p->flags & READONLY)
    457 							warnx("`%s' is read only", *argv);
    458 						else {
    459 							rdfield(p, q);
    460 							if (p->valp == &fullduplex)
    461 								if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
    462 									err(1, "set failed");
    463 						}
    464 					}
    465 				} else
    466 					warnx("No `=' in %s", *argv);
    467 				argv++;
    468 			}
    469 			if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    470 				err(1, "set failed");
    471 			if (sep) {
    472 				getinfo(fd);
    473 				for(i = 0; fields[i].name; i++) {
    474 					if (fields[i].flags & SET) {
    475 						fprintf(out, "%s: -> ", fields[i].name);
    476 						prfield(&fields[i], 0);
    477 						fprintf(out, "\n");
    478 					}
    479 				}
    480 			}
    481 		} else {
    482 			while(argc--) {
    483 				p = findfield(*argv);
    484 				if (p == 0) {
    485 					if (strchr(*argv, '='))
    486 						warnx("field %s does not exist (use -w to set a variable)", *argv);
    487 					else
    488 						warnx("field %s does not exist", *argv);
    489 				} else {
    490 					prfield(p, sep);
    491 					fprintf(out, "\n");
    492 				}
    493 				argv++;
    494 			}
    495 		}
    496 	} else
    497 		usage();
    498 	exit(0);
    499 }
    500