Home | History | Annotate | Line # | Download | only in ctl
ctl.c revision 1.17
      1 /*	$NetBSD: ctl.c,v 1.17 1998/08/10 18:19:03 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <fcntl.h>
     41 #include <err.h>
     42 #include <unistd.h>
     43 #include <string.h>
     44 #include <sys/types.h>
     45 #include <sys/stat.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/audioio.h>
     48 
     49 struct field *findfield __P((char *name));
     50 void prfield __P((struct field *p, char *sep));
     51 void rdfield __P((struct field *p, char *q));
     52 void getinfo __P((int fd));
     53 void usage __P((void));
     54 int main __P((int argc, char **argv));
     55 
     56 FILE *out = stdout;
     57 
     58 char *prog;
     59 
     60 audio_device_t adev;
     61 
     62 audio_info_t info;
     63 
     64 char encbuf[1000];
     65 
     66 int properties, fullduplex, rerror;
     67 
     68 struct field {
     69 	char *name;
     70 	void *valp;
     71 	int format;
     72 #define STRING 1
     73 #define INT 2
     74 #define UINT 3
     75 #define P_R 4
     76 #define ULONG 5
     77 #define UCHAR 6
     78 #define ENC 7
     79 #define PROPS 8
     80 #define XINT 9
     81 #define	FORMAT 10
     82 	char flags;
     83 #define READONLY 1
     84 #define ALIAS 2
     85 #define SET 4
     86 } fields[] = {
     87 	{ "name", 		&adev.name, 		STRING, READONLY },
     88 	{ "version",		&adev.version,		STRING, READONLY },
     89 	{ "config",		&adev.config,		STRING, READONLY },
     90 	{ "encodings",		encbuf,			STRING, READONLY },
     91 	{ "properties",		&properties,		PROPS,	READONLY },
     92 	{ "full_duplex",	&fullduplex,		UINT,   0 },
     93 	{ "fullduplex",		&fullduplex,		UINT,   0 },
     94 	{ "blocksize",		&info.blocksize,	UINT,	0 },
     95 	{ "hiwat",		&info.hiwat,		UINT,	0 },
     96 	{ "lowat",		&info.lowat,		UINT,	0 },
     97 	{ "monitor_gain",	&info.monitor_gain,	UINT,	0 },
     98 	{ "mode",		&info.mode,		P_R,	READONLY },
     99 	{ "play",		&info.play,		FORMAT,	ALIAS },
    100 	{ "play.rate",		&info.play.sample_rate,	UINT,	0 },
    101 	{ "play.sample_rate",	&info.play.sample_rate,	UINT,	ALIAS },
    102 	{ "play.channels",	&info.play.channels,	UINT,	0 },
    103 	{ "play.precision",	&info.play.precision,	UINT,	0 },
    104 	{ "play.encoding",	&info.play.encoding,	ENC,	0 },
    105 	{ "play.gain",		&info.play.gain,	UINT,	0 },
    106 	{ "play.balance",	&info.play.balance,	UCHAR,	0 },
    107 	{ "play.port",		&info.play.port,	XINT,	0 },
    108 	{ "play.avail_ports",	&info.play.avail_ports,	XINT,	0 },
    109 	{ "play.seek",		&info.play.seek,	ULONG,	READONLY },
    110 	{ "play.samples",	&info.play.samples,	UINT,	READONLY },
    111 	{ "play.eof",		&info.play.eof,		UINT,	READONLY },
    112 	{ "play.pause",		&info.play.pause,	UCHAR,	0 },
    113 	{ "play.error",		&info.play.error,	UCHAR,	READONLY },
    114 	{ "play.waiting",	&info.play.waiting,	UCHAR,	READONLY },
    115 	{ "play.open",		&info.play.open,	UCHAR,	READONLY },
    116 	{ "play.active",	&info.play.active,	UCHAR,	READONLY },
    117 	{ "play.buffer_size",	&info.play.buffer_size,	UINT,	0 },
    118 	{ "record",		&info.record,		FORMAT,	ALIAS },
    119 	{ "record.rate",	&info.record.sample_rate,UINT,	0 },
    120 	{ "record.sample_rate",	&info.record.sample_rate,UINT,	ALIAS },
    121 	{ "record.channels",	&info.record.channels,	UINT,	0 },
    122 	{ "record.precision",	&info.record.precision,	UINT,	0 },
    123 	{ "record.encoding",	&info.record.encoding,	ENC,	0 },
    124 	{ "record.gain",	&info.record.gain,	UINT,	0 },
    125 	{ "record.balance",	&info.record.balance,	UCHAR,	0 },
    126 	{ "record.port",	&info.record.port,	XINT,	0 },
    127 	{ "record.avail_ports",	&info.record.avail_ports,XINT,	0 },
    128 	{ "record.seek",	&info.record.seek,	ULONG,	READONLY },
    129 	{ "record.samples",	&info.record.samples,	UINT,	READONLY },
    130 	{ "record.eof",		&info.record.eof,	UINT,	READONLY },
    131 	{ "record.pause",	&info.record.pause,	UCHAR,	0 },
    132 	{ "record.error",	&info.record.error,	UCHAR,	READONLY },
    133 	{ "record.waiting",	&info.record.waiting,	UCHAR,	READONLY },
    134 	{ "record.open",	&info.record.open,	UCHAR,	READONLY },
    135 	{ "record.active",	&info.record.active,	UCHAR,	READONLY },
    136 	{ "record.buffer_size",	&info.record.buffer_size,UINT,	0 },
    137 	{ "record.errors",	&rerror,		INT,	READONLY },
    138 	{ 0 }
    139 };
    140 
    141 struct {
    142 	char *ename;
    143 	int eno;
    144 } encs[] = {
    145 	{ AudioEmulaw,		AUDIO_ENCODING_ULAW },
    146 	{ "ulaw",		AUDIO_ENCODING_ULAW },
    147 	{ AudioEalaw, 		AUDIO_ENCODING_ALAW },
    148 	{ AudioEslinear,	AUDIO_ENCODING_SLINEAR },
    149 	{ "linear",		AUDIO_ENCODING_SLINEAR },
    150 	{ AudioEulinear,	AUDIO_ENCODING_ULINEAR },
    151 	{ AudioEadpcm,		AUDIO_ENCODING_ADPCM },
    152 	{ "ADPCM",		AUDIO_ENCODING_ADPCM },
    153 	{ AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE },
    154 	{ "linear_le",		AUDIO_ENCODING_SLINEAR_LE },
    155 	{ AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE },
    156 	{ AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE },
    157 	{ "linear_be",		AUDIO_ENCODING_SLINEAR_BE },
    158 	{ AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE },
    159 	{ AudioEmpeg_l1_stream,	AUDIO_ENCODING_MPEG_L1_STREAM },
    160 	{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
    161 	{ AudioEmpeg_l1_system,	AUDIO_ENCODING_MPEG_L1_SYSTEM },
    162 	{ AudioEmpeg_l2_stream,	AUDIO_ENCODING_MPEG_L2_STREAM },
    163 	{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
    164 	{ AudioEmpeg_l2_system,	AUDIO_ENCODING_MPEG_L2_SYSTEM },
    165 	{ 0 }
    166 };
    167 
    168 static struct {
    169 	char *name;
    170 	u_int prop;
    171 } props[] = {
    172 	{ "full_duplex",	AUDIO_PROP_FULLDUPLEX },
    173 	{ "mmap",		AUDIO_PROP_MMAP },
    174 	{ "independent",	AUDIO_PROP_INDEPENDENT },
    175 	{ 0 }
    176 };
    177 
    178 struct field *
    179 findfield(name)
    180 	char *name;
    181 {
    182 	int i;
    183 	for(i = 0; fields[i].name; i++)
    184 		if (strcmp(fields[i].name, name) == 0)
    185 			return &fields[i];
    186 	return 0;
    187 }
    188 
    189 void
    190 prfield(p, sep)
    191 	struct field *p;
    192 	char *sep;
    193 {
    194 	u_int v;
    195 	char *cm;
    196 	int i;
    197 
    198 	if (sep)
    199 		fprintf(out, "%s%s", p->name, sep);
    200 	switch(p->format) {
    201 	case STRING:
    202 		fprintf(out, "%s", (char*)p->valp);
    203 		break;
    204 	case INT:
    205 		fprintf(out, "%d", *(int*)p->valp);
    206 		break;
    207 	case UINT:
    208 		fprintf(out, "%u", *(u_int*)p->valp);
    209 		break;
    210 	case XINT:
    211 		fprintf(out, "0x%x", *(u_int*)p->valp);
    212 		break;
    213 	case UCHAR:
    214 		fprintf(out, "%u", *(u_char*)p->valp);
    215 		break;
    216 	case ULONG:
    217 		fprintf(out, "%lu", *(u_long*)p->valp);
    218 		break;
    219 	case P_R:
    220 		v = *(u_int*)p->valp;
    221 		cm = "";
    222 		if (v & AUMODE_PLAY) {
    223 			if (v & AUMODE_PLAY_ALL)
    224 				fprintf(out, "play");
    225 			else
    226 				fprintf(out, "playsync");
    227 			cm = ",";
    228 		}
    229 		if (v & AUMODE_RECORD)
    230 			fprintf(out, "%srecord", cm);
    231 		break;
    232 	case ENC:
    233 		v = *(u_int*)p->valp;
    234 		for(i = 0; encs[i].ename; i++)
    235 			if (encs[i].eno == v)
    236 				break;
    237 		if (encs[i].ename)
    238 			fprintf(out, "%s", encs[i].ename);
    239 		else
    240 			fprintf(out, "%u", v);
    241 		break;
    242 	case PROPS:
    243 		v = *(u_int*)p->valp;
    244 		for (cm = "", i = 0; props[i].name; i++) {
    245 			if (v & props[i].prop) {
    246 				fprintf(out, "%s%s", cm, props[i].name);
    247 				cm = ",";
    248 			}
    249 		}
    250 		break;
    251 	case FORMAT:
    252 		prfield(p + 1, 0);
    253 		fprintf(out, ",");
    254 		prfield(p + 3, 0);
    255 		fprintf(out, ",");
    256 		prfield(p + 4, 0);
    257 		fprintf(out, ",");
    258 		prfield(p + 5, 0);
    259 		break;
    260 	default:
    261 		errx(1, "Invalid print format.");
    262 	}
    263 }
    264 
    265 void
    266 rdfield(p, q)
    267 	struct field *p;
    268 	char *q;
    269 {
    270 	int i;
    271 	u_int u;
    272 	char *s;
    273 
    274 	switch(p->format) {
    275 	case UINT:
    276 		if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
    277 			errx(1, "Bad number: %s", q);
    278 		break;
    279 	case UCHAR:
    280 		if (sscanf(q, "%u", &u) != 1)
    281 			errx(1, "Bad number: %s", q);
    282 		else
    283 			*(u_char *)p->valp = u;
    284 		break;
    285 	case XINT:
    286 		if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
    287 		    sscanf(q, "%x", (unsigned int *)p->valp) != 1)
    288 			errx(1, "Bad number: %s", q);
    289 		break;
    290 	case ENC:
    291 		for(i = 0; encs[i].ename; i++)
    292 			if (strcmp(encs[i].ename, q) == 0)
    293 				break;
    294 		if (encs[i].ename)
    295 			*(u_int*)p->valp = encs[i].eno;
    296 		else
    297 			errx(1, "Unknown encoding: %s", q);
    298 		break;
    299 	case FORMAT:
    300 		s = strsep(&q, ",");
    301 		if (s)
    302 			rdfield(p + 1, s);
    303 		s = strsep(&q, ",");
    304 		if (s)
    305 			rdfield(p + 3, s);
    306 		s = strsep(&q, ",");
    307 		if (s)
    308 			rdfield(p + 4, s);
    309 		s = strsep(&q, ",");
    310 		if (s)
    311 			rdfield(p + 5, s);
    312 		if (!s || q)
    313 			errx(1, "Bad format");
    314 		break;
    315 	default:
    316 		errx(1, "Invalid read format.");
    317 	}
    318 	p->flags |= SET;
    319 }
    320 
    321 void
    322 getinfo(fd)
    323 	int fd;
    324 {
    325 	int pos, i;
    326 
    327 	if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
    328 		err(1, "AUDIO_GETDEV");
    329 	for (pos = 0, i = 0; ; i++) {
    330 		audio_encoding_t enc;
    331 		enc.index = i;
    332 		if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
    333 			break;
    334 		if (pos >= sizeof(encbuf)-1)
    335 			break;
    336 		if (pos)
    337 			encbuf[pos++] = ',';
    338 		if (pos >= sizeof(encbuf)-1)
    339 			break;
    340 		pos += snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
    341 			enc.name, enc.precision,
    342 			enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
    343 	}
    344 	if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
    345 		err(1, "AUDIO_GETFD");
    346 	if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
    347 		err(1, "AUDIO_GETPROPS");
    348 	if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
    349 		err(1, "AUDIO_RERROR");
    350 	if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
    351 		err(1, "AUDIO_GETINFO");
    352 }
    353 
    354 void
    355 usage()
    356 {
    357 	fprintf(out, "%s [-f file] [-n] name ...\n", prog);
    358 	fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
    359 	fprintf(out, "%s [-f file] [-n] -a\n", prog);
    360 	exit(1);
    361 }
    362 
    363 int
    364 main(argc, argv)
    365 	int argc;
    366 	char **argv;
    367 {
    368 	int fd, i, ch;
    369 	int aflag = 0, wflag = 0;
    370 	struct stat dstat, ostat;
    371 	const char *file;
    372 	char *sep = "=";
    373 
    374 	file = getenv("AUDIOCTLDEVICE");
    375 	if (file == 0)
    376 		file = "/dev/audioctl";
    377 
    378 	prog = *argv;
    379 
    380 	while ((ch = getopt(argc, argv, "af:nw")) != -1) {
    381 		switch(ch) {
    382 		case 'a':
    383 			aflag++;
    384 			break;
    385 		case 'w':
    386 			wflag++;
    387 			break;
    388 		case 'n':
    389 			sep = 0;
    390 			break;
    391 		case 'f':
    392 			file = optarg;
    393 			break;
    394 		case '?':
    395 		default:
    396 			usage();
    397 		}
    398 	}
    399 	argc -= optind;
    400 	argv += optind;
    401 
    402 	fd = open(file, O_WRONLY);
    403 	if (fd < 0)
    404 		fd = open(file, O_RDONLY);
    405 	if (fd < 0)
    406 		err(1, "%s", file);
    407 
    408 	/* Check if stdout is the same device as the audio device. */
    409 	if (fstat(fd, &dstat) < 0)
    410 		err(1, "fstat au");
    411 	if (fstat(STDOUT_FILENO, &ostat) < 0)
    412 		err(1, "fstat stdout");
    413 	if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
    414 	    major(dstat.st_dev) == major(ostat.st_dev) &&
    415 	    minor(dstat.st_dev) == minor(ostat.st_dev))
    416 		/* We can't write to stdout so use stderr */
    417 		out = stderr;
    418 
    419 	if (!wflag)
    420 		getinfo(fd);
    421 
    422 	if (argc == 0 && aflag && !wflag) {
    423 		for(i = 0; fields[i].name; i++) {
    424 			if (!(fields[i].flags & ALIAS)) {
    425 				prfield(&fields[i], sep);
    426 				fprintf(out, "\n");
    427 			}
    428 		}
    429 	} else if (argc > 0 && !aflag) {
    430 		struct field *p;
    431 		if (wflag) {
    432 			AUDIO_INITINFO(&info);
    433 			while(argc--) {
    434 				char *q;
    435 
    436 				q = strchr(*argv, '=');
    437 				if (q) {
    438 					*q++ = 0;
    439 					p = findfield(*argv);
    440 					if (p == 0)
    441 						warnx("field `%s' does not exist", *argv);
    442 					else {
    443 						if (p->flags & READONLY)
    444 							warnx("`%s' is read only", *argv);
    445 						else {
    446 							rdfield(p, q);
    447 							if (p->valp == &fullduplex)
    448 								if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
    449 									err(1, "set failed");
    450 						}
    451 					}
    452 				} else
    453 					warnx("No `=' in %s", *argv);
    454 				argv++;
    455 			}
    456 			if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    457 				err(1, "set failed");
    458 			if (sep) {
    459 				getinfo(fd);
    460 				for(i = 0; fields[i].name; i++) {
    461 					if (fields[i].flags & SET) {
    462 						fprintf(out, "%s: -> ", fields[i].name);
    463 						prfield(&fields[i], 0);
    464 						fprintf(out, "\n");
    465 					}
    466 				}
    467 			}
    468 		} else {
    469 			while(argc--) {
    470 				p = findfield(*argv);
    471 				if (p == 0) {
    472 					if (strchr(*argv, '='))
    473 						warnx("field %s does not exist (use -w to set a variable)", *argv);
    474 					else
    475 						warnx("field %s does not exist", *argv);
    476 				} else {
    477 					prfield(p, sep);
    478 					fprintf(out, "\n");
    479 				}
    480 				argv++;
    481 			}
    482 		}
    483 	} else
    484 		usage();
    485 	exit(0);
    486 }
    487