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