Home | History | Annotate | Line # | Download | only in ctl
ctl.c revision 1.15
      1 /*	$NetBSD: ctl.c,v 1.15 1998/07/13 15:11:03 augustss 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 	char flags;
     82 #define READONLY 1
     83 #define ALIAS 2
     84 #define SET 4
     85 } fields[] = {
     86 	{ "name", 		&adev.name, 		STRING, READONLY },
     87 	{ "version",		&adev.version,		STRING, READONLY },
     88 	{ "config",		&adev.config,		STRING, READONLY },
     89 	{ "encodings",		encbuf,			STRING, READONLY },
     90 	{ "properties",		&properties,		PROPS,	READONLY },
     91 	{ "full_duplex",	&fullduplex,		UINT,   0 },
     92 	{ "fullduplex",		&fullduplex,		UINT,   0 },
     93 	{ "blocksize",		&info.blocksize,	UINT,	0 },
     94 	{ "hiwat",		&info.hiwat,		UINT,	0 },
     95 	{ "lowat",		&info.lowat,		UINT,	0 },
     96 	{ "monitor_gain",	&info.monitor_gain,	UINT,	0 },
     97 	{ "mode",		&info.mode,		P_R,	READONLY },
     98 	{ "play.rate",		&info.play.sample_rate,	UINT,	0 },
     99 	{ "play.sample_rate",	&info.play.sample_rate,	UINT,	ALIAS },
    100 	{ "play.channels",	&info.play.channels,	UINT,	0 },
    101 	{ "play.precision",	&info.play.precision,	UINT,	0 },
    102 	{ "play.encoding",	&info.play.encoding,	ENC,	0 },
    103 	{ "play.gain",		&info.play.gain,	UINT,	0 },
    104 	{ "play.balance",	&info.play.balance,	UCHAR,	0 },
    105 	{ "play.port",		&info.play.port,	XINT,	0 },
    106 	{ "play.avail_ports",	&info.play.avail_ports,	XINT,	0 },
    107 	{ "play.seek",		&info.play.seek,	ULONG,	READONLY },
    108 	{ "play.samples",	&info.play.samples,	UINT,	READONLY },
    109 	{ "play.eof",		&info.play.eof,		UINT,	READONLY },
    110 	{ "play.pause",		&info.play.pause,	UCHAR,	0 },
    111 	{ "play.error",		&info.play.error,	UCHAR,	READONLY },
    112 	{ "play.waiting",	&info.play.waiting,	UCHAR,	READONLY },
    113 	{ "play.open",		&info.play.open,	UCHAR,	READONLY },
    114 	{ "play.active",	&info.play.active,	UCHAR,	READONLY },
    115 	{ "play.buffer_size",	&info.play.buffer_size,	UINT,	0 },
    116 	{ "record.rate",	&info.record.sample_rate,UINT,	0 },
    117 	{ "record.sample_rate",	&info.record.sample_rate,UINT,	ALIAS },
    118 	{ "record.channels",	&info.record.channels,	UINT,	0 },
    119 	{ "record.precision",	&info.record.precision,	UINT,	0 },
    120 	{ "record.encoding",	&info.record.encoding,	ENC,	0 },
    121 	{ "record.gain",	&info.record.gain,	UINT,	0 },
    122 	{ "record.balance",	&info.record.balance,	UCHAR,	0 },
    123 	{ "record.port",	&info.record.port,	XINT,	0 },
    124 	{ "record.avail_ports",	&info.record.avail_ports,XINT,	0 },
    125 	{ "record.seek",	&info.record.seek,	ULONG,	READONLY },
    126 	{ "record.samples",	&info.record.samples,	UINT,	READONLY },
    127 	{ "record.eof",		&info.record.eof,	UINT,	READONLY },
    128 	{ "record.pause",	&info.record.pause,	UCHAR,	0 },
    129 	{ "record.error",	&info.record.error,	UCHAR,	READONLY },
    130 	{ "record.waiting",	&info.record.waiting,	UCHAR,	READONLY },
    131 	{ "record.open",	&info.record.open,	UCHAR,	READONLY },
    132 	{ "record.active",	&info.record.active,	UCHAR,	READONLY },
    133 	{ "record.buffer_size",	&info.record.buffer_size,UINT,	0 },
    134 	{ "record.errors",	&rerror,		INT,	READONLY },
    135 	{ 0 }
    136 };
    137 
    138 struct {
    139 	char *ename;
    140 	int eno;
    141 } encs[] = {
    142 	{ AudioEmulaw,		AUDIO_ENCODING_ULAW },
    143 	{ "ulaw",		AUDIO_ENCODING_ULAW },
    144 	{ AudioEalaw, 		AUDIO_ENCODING_ALAW },
    145 	{ AudioEslinear,	AUDIO_ENCODING_SLINEAR },
    146 	{ "linear",		AUDIO_ENCODING_SLINEAR },
    147 	{ AudioEulinear,	AUDIO_ENCODING_ULINEAR },
    148 	{ AudioEadpcm,		AUDIO_ENCODING_ADPCM },
    149 	{ "ADPCM",		AUDIO_ENCODING_ADPCM },
    150 	{ AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE },
    151 	{ "linear_le",		AUDIO_ENCODING_SLINEAR_LE },
    152 	{ AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE },
    153 	{ AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE },
    154 	{ "linear_be",		AUDIO_ENCODING_SLINEAR_BE },
    155 	{ AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE },
    156 	{ AudioEmpeg_l1_stream,	AUDIO_ENCODING_MPEG_L1_STREAM },
    157 	{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
    158 	{ AudioEmpeg_l1_system,	AUDIO_ENCODING_MPEG_L1_SYSTEM },
    159 	{ AudioEmpeg_l2_stream,	AUDIO_ENCODING_MPEG_L2_STREAM },
    160 	{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
    161 	{ AudioEmpeg_l2_system,	AUDIO_ENCODING_MPEG_L2_SYSTEM },
    162 	{ 0 }
    163 };
    164 
    165 static struct {
    166 	char *name;
    167 	u_int prop;
    168 } props[] = {
    169 	{ "full_duplex",	AUDIO_PROP_FULLDUPLEX },
    170 	{ "mmap",		AUDIO_PROP_MMAP },
    171 	{ "independent",	AUDIO_PROP_INDEPENDENT },
    172 	{ 0 }
    173 };
    174 
    175 struct field *
    176 findfield(name)
    177 	char *name;
    178 {
    179 	int i;
    180 	for(i = 0; fields[i].name; i++)
    181 		if (strcmp(fields[i].name, name) == 0)
    182 			return &fields[i];
    183 	return 0;
    184 }
    185 
    186 void
    187 prfield(p, sep)
    188 	struct field *p;
    189 	char *sep;
    190 {
    191 	u_int v;
    192 	char *cm;
    193 	int i;
    194 
    195 	if (sep)
    196 		fprintf(out, "%s%s", p->name, sep);
    197 	switch(p->format) {
    198 	case STRING:
    199 		fprintf(out, "%s", (char*)p->valp);
    200 		break;
    201 	case INT:
    202 		fprintf(out, "%d", *(int*)p->valp);
    203 		break;
    204 	case UINT:
    205 		fprintf(out, "%u", *(u_int*)p->valp);
    206 		break;
    207 	case XINT:
    208 		fprintf(out, "0x%x", *(u_int*)p->valp);
    209 		break;
    210 	case UCHAR:
    211 		fprintf(out, "%u", *(u_char*)p->valp);
    212 		break;
    213 	case ULONG:
    214 		fprintf(out, "%lu", *(u_long*)p->valp);
    215 		break;
    216 	case P_R:
    217 		v = *(u_int*)p->valp;
    218 		cm = "";
    219 		if (v & AUMODE_PLAY) {
    220 			if (v & AUMODE_PLAY_ALL)
    221 				fprintf(out, "play");
    222 			else
    223 				fprintf(out, "playsync");
    224 			cm = ",";
    225 		}
    226 		if (v & AUMODE_RECORD)
    227 			fprintf(out, "%srecord", cm);
    228 		break;
    229 	case ENC:
    230 		v = *(u_int*)p->valp;
    231 		for(i = 0; encs[i].ename; i++)
    232 			if (encs[i].eno == v)
    233 				break;
    234 		if (encs[i].ename)
    235 			fprintf(out, "%s", encs[i].ename);
    236 		else
    237 			fprintf(out, "%u", v);
    238 		break;
    239 	case PROPS:
    240 		v = *(u_int*)p->valp;
    241 		for (cm = "", i = 0; props[i].name; i++) {
    242 			if (v & props[i].prop) {
    243 				fprintf(out, "%s%s", cm, props[i].name);
    244 				cm = ",";
    245 			}
    246 		}
    247 		break;
    248 	default:
    249 		errx(1, "Invalid print format.");
    250 	}
    251 }
    252 
    253 void
    254 rdfield(p, q)
    255 	struct field *p;
    256 	char *q;
    257 {
    258 	int i;
    259 	u_int u;
    260 
    261 	switch(p->format) {
    262 	case UINT:
    263 		if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
    264 			warnx("Bad number %s", q);
    265 		break;
    266 	case UCHAR:
    267 		if (sscanf(q, "%u", &u) != 1)
    268 			warnx("Bad number %s", q);
    269 		else
    270 			*(u_char *)p->valp = u;
    271 		break;
    272 	case XINT:
    273 		if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
    274 		    sscanf(q, "%x", (unsigned int *)p->valp) != 1)
    275 			warnx("Bad number %s", q);
    276 		break;
    277 	case ENC:
    278 		for(i = 0; encs[i].ename; i++)
    279 			if (strcmp(encs[i].ename, q) == 0)
    280 				break;
    281 		if (encs[i].ename)
    282 			*(u_int*)p->valp = encs[i].eno;
    283 		else
    284 			warnx("Unknown encoding: %s", q);
    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)
    306 			encbuf[pos++] = ',';
    307 		sprintf(encbuf+pos, "%s:%d%s", enc.name,
    308 			enc.precision,
    309 			enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
    310 		pos += strlen(encbuf+pos);
    311 	}
    312 	if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
    313 		err(1, "AUDIO_GETFD");
    314 	if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
    315 		err(1, "AUDIO_GETPROPS");
    316 	if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
    317 		err(1, "AUDIO_RERROR");
    318 	if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
    319 		err(1, "AUDIO_GETINFO");
    320 }
    321 
    322 void
    323 usage()
    324 {
    325 	fprintf(out, "%s [-f file] [-n] name ...\n", prog);
    326 	fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
    327 	fprintf(out, "%s [-f file] [-n] -a\n", prog);
    328 	exit(1);
    329 }
    330 
    331 int
    332 main(argc, argv)
    333 	int argc;
    334 	char **argv;
    335 {
    336 	int fd, i, ch;
    337 	int aflag = 0, wflag = 0;
    338 	struct stat dstat, ostat;
    339 	char *file;
    340 	char *sep = "=";
    341 
    342 	file = getenv("AUDIOCTLDEVICE");
    343 	if (file == 0)
    344 		file = "/dev/audioctl";
    345 
    346 	prog = *argv;
    347 
    348 	while ((ch = getopt(argc, argv, "af:nw")) != -1) {
    349 		switch(ch) {
    350 		case 'a':
    351 			aflag++;
    352 			break;
    353 		case 'w':
    354 			wflag++;
    355 			break;
    356 		case 'n':
    357 			sep = 0;
    358 			break;
    359 		case 'f':
    360 			file = optarg;
    361 			break;
    362 		case '?':
    363 		default:
    364 			usage();
    365 		}
    366 	}
    367 	argc -= optind;
    368 	argv += optind;
    369 
    370 	fd = open(file, O_WRONLY);
    371 	if (fd < 0)
    372 		fd = open(file, O_RDONLY);
    373 	if (fd < 0)
    374 		err(1, "%s", file);
    375 
    376 	/* Check if stdout is the same device as the audio device. */
    377 	if (fstat(fd, &dstat) < 0)
    378 		err(1, "fstat au");
    379 	if (fstat(STDOUT_FILENO, &ostat) < 0)
    380 		err(1, "fstat stdout");
    381 	if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
    382 	    major(dstat.st_dev) == major(ostat.st_dev) &&
    383 	    minor(dstat.st_dev) == minor(ostat.st_dev))
    384 		/* We can't write to stdout so use stderr */
    385 		out = stderr;
    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 				fprintf(out, "\n");
    395 			}
    396 		}
    397 	} else if (argc > 0 && !aflag) {
    398 		struct field *p;
    399 		if (wflag) {
    400 			AUDIO_INITINFO(&info);
    401 			while(argc--) {
    402 				char *q;
    403 
    404 				q = strchr(*argv, '=');
    405 				if (q) {
    406 					*q++ = 0;
    407 					p = findfield(*argv);
    408 					if (p == 0)
    409 						warnx("field `%s' does not exist", *argv);
    410 					else {
    411 						if (p->flags & READONLY)
    412 							warnx("`%s' is read only", *argv);
    413 						else {
    414 							rdfield(p, q);
    415 							if (p->valp == &fullduplex)
    416 								if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
    417 									err(1, "set failed");
    418 						}
    419 					}
    420 				} else
    421 					warnx("No `=' in %s", *argv);
    422 				argv++;
    423 			}
    424 			if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    425 				err(1, "set failed");
    426 			if (sep) {
    427 				getinfo(fd);
    428 				for(i = 0; fields[i].name; i++) {
    429 					if (fields[i].flags & SET) {
    430 						fprintf(out, "%s: -> ", fields[i].name);
    431 						prfield(&fields[i], 0);
    432 						fprintf(out, "\n");
    433 					}
    434 				}
    435 			}
    436 		} else {
    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