Home | History | Annotate | Line # | Download | only in ctl
ctl.c revision 1.1
      1 /*	$NetBSD: ctl.c,v 1.1 1997/05/13 17:35:53 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 REGENTS OR CONTRIBUTORS BE
     29  * 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 <fcntl.h>
     40 #include <err.h>
     41 #include <unistd.h>
     42 #include <string.h>
     43 #include <sys/types.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/audioio.h>
     46 
     47 FILE *out = stdout;
     48 
     49 char *prog;
     50 
     51 audio_device_t adev;
     52 
     53 audio_info_t info;
     54 
     55 char encbuf[1000];
     56 
     57 int fullduplex, rerror;
     58 
     59 struct field {
     60 	char *name;
     61 	void *valp;
     62 	int format;
     63 #define STRING 1
     64 #define INT 2
     65 #define UINT 3
     66 #define P_R 4
     67 #define ULONG 5
     68 #define UCHAR 6
     69 #define ENC 7
     70 	char readonly;
     71 } fields[] = {
     72     { "name", 			&adev.name, 		STRING, 1 },
     73     { "version",		&adev.version,		STRING, 1 },
     74     { "config",			&adev.config,		STRING, 1 },
     75     { "encodings",		encbuf,			STRING, 1 },
     76     { "full_duplex",		&fullduplex,		INT,    0 },
     77     { "blocksize",		&info.blocksize,	UINT,	0 },
     78     { "hiwat",			&info.hiwat,		UINT,	0 },
     79     { "lowat",			&info.lowat,		UINT,	0 },
     80     { "backlog",		&info.backlog,		UINT,	0 },
     81     { "mode",			&info.mode,		P_R,	1 },
     82     { "play.rate",		&info.play.sample_rate,	UINT,	0 },
     83     { "play.channels",		&info.play.channels,	UINT,	0 },
     84     { "play.precision",		&info.play.precision,	UINT,	0 },
     85     { "play.encoding",		&info.play.encoding,	ENC,	0 },
     86     { "play.gain",		&info.play.gain,	UINT,	0 },
     87     { "play.port",		&info.play.port,	UINT,	0 },
     88     { "play.seek",		&info.play.seek,	ULONG,	1 },
     89     { "play.samples",		&info.play.samples,	UINT,	1 },
     90     { "play.eof",		&info.play.eof,		UINT,	1 },
     91     { "play.pause",		&info.play.pause,	UCHAR,	0 },
     92     { "play.error",		&info.play.error,	UCHAR,	1 },
     93     { "play.waiting",		&info.play.waiting,	UCHAR,	1 },
     94     { "play.open",		&info.play.open,	UCHAR,	1 },
     95     { "play.active",		&info.play.active,	UCHAR,	1 },
     96     { "record.rate",		&info.record.sample_rate,UINT,	0 },
     97     { "record.channels",	&info.record.channels,	UINT,	0 },
     98     { "record.precision",	&info.record.precision,	UINT,	0 },
     99     { "record.encoding",	&info.record.encoding,	ENC,	0 },
    100     { "record.gain",		&info.record.gain,	UINT,	0 },
    101     { "record.port",		&info.record.port,	UINT,	0 },
    102     { "record.seek",		&info.record.seek,	ULONG,	1 },
    103     { "record.samples",		&info.record.samples,	UINT,	1 },
    104     { "record.eof",		&info.record.eof,	UINT,	1 },
    105     { "record.pause",		&info.record.pause,	UCHAR,	0 },
    106     { "record.error",		&info.record.error,	UCHAR,	1 },
    107     { "record.waiting",		&info.record.waiting,	UCHAR,	1 },
    108     { "record.open",		&info.record.open,	UCHAR,	1 },
    109     { "record.active",		&info.record.active,	UCHAR,	1 },
    110     { "record.errors",		&rerror,		INT,	1 },
    111     { 0 }
    112 };
    113 
    114 struct {
    115 	char *ename;
    116 	int eno;
    117 } encs[] = {
    118 	{ "ulaw", AUDIO_ENCODING_ULAW },
    119 	{ "alaw", AUDIO_ENCODING_ALAW },
    120 	{ "linear", AUDIO_ENCODING_LINEAR },
    121 	{ "ulinear", AUDIO_ENCODING_ULINEAR },
    122 	{ "adpcm", AUDIO_ENCODING_ADPCM },
    123 	{ "ADPCM", AUDIO_ENCODING_ADPCM },
    124 	{ "mulaw", AUDIO_ENCODING_ULAW },
    125 	{ "linear_le", AUDIO_ENCODING_LINEAR_LE },
    126 	{ "ulinear_le", AUDIO_ENCODING_ULINEAR_LE },
    127 	{ "linear_be", AUDIO_ENCODING_LINEAR_BE },
    128 	{ "ulinear_be", AUDIO_ENCODING_ULINEAR_BE },
    129 	{ 0 }
    130 };
    131 
    132 struct field *
    133 findfield(char *name)
    134 {
    135 	int i;
    136 	for(i = 0; fields[i].name; i++)
    137 		if (strcmp(fields[i].name, name) == 0)
    138 			return &fields[i];
    139 	return 0;
    140 }
    141 
    142 void
    143 prfield(struct field *p, char *sep)
    144 {
    145 	u_int v;
    146 	char *cm;
    147 	int i;
    148 
    149 	if (sep)
    150 		fprintf(out, "%s%s", p->name, sep);
    151 	switch(p->format) {
    152 	case STRING:
    153 		fprintf(out, "%s", (char*)p->valp);
    154 		break;
    155 	case INT:
    156 		fprintf(out, "%d", *(int*)p->valp);
    157 		break;
    158 	case UINT:
    159 		fprintf(out, "%u", *(u_int*)p->valp);
    160 		break;
    161 	case UCHAR:
    162 		fprintf(out, "%u", *(u_char*)p->valp);
    163 		break;
    164 	case ULONG:
    165 		fprintf(out, "%lu", *(u_long*)p->valp);
    166 		break;
    167 	case P_R:
    168 		v = *(u_int*)p->valp;
    169 		cm = "";
    170 		if (v & AUMODE_PLAY) {
    171 			if (v & AUMODE_PLAY_ALL)
    172 				fprintf(out, "play");
    173 			else
    174 				fprintf(out, "playsync");
    175 			cm = ",";
    176 		}
    177 		if (v & AUMODE_RECORD)
    178 			fprintf(out, "%srecord", cm);
    179 		break;
    180 	case ENC:
    181 		v = *(u_int*)p->valp;
    182 		for(i = 0; encs[i].ename; i++)
    183 			if (encs[i].eno == v)
    184 				break;
    185 		if (encs[i].ename)
    186 			fprintf(out, "%s", encs[i].ename);
    187 		else
    188 			fprintf(out, "%u", v);
    189 		break;
    190 	default:
    191 		errx(1, "Invalid format.");
    192 	}
    193 }
    194 
    195 void
    196 rdfield(struct field *p, char *q, char *sep)
    197 {
    198 	u_int v;
    199 	int i;
    200 
    201 #if 0
    202 	if (sep)
    203 		prfield(p, ": ");
    204 #else
    205 	if (sep)
    206 		fprintf(out, "%s: ", p->name);
    207 #endif
    208 	switch(p->format) {
    209 	case UINT:
    210 		if (sscanf(q, "%u", p->valp) != 1)
    211 			warnx("Bad number %s", q);
    212 		break;
    213 	case ENC:
    214 		for(i = 0; encs[i].ename; i++)
    215 			if (strcmp(encs[i].ename, q) == 0)
    216 				break;
    217 		if (encs[i].ename)
    218 			*(u_int*)p->valp = encs[i].eno;
    219 		else
    220 			warnx("Unknown encoding: %s", q);
    221 		break;
    222 	default:
    223 		errx(1, "Invalid format.");
    224 	}
    225 	if (sep) {
    226 		fprintf(out, " -> ");
    227 		prfield(p, 0);
    228 		fprintf(out, "\n");
    229 	}
    230 }
    231 
    232 void
    233 main(int argc, char **argv)
    234 {
    235 	int fd, r, i, ch, pos;
    236 	int aflag = 0, wflag = 0;
    237 	char *file = "/dev/sound";
    238 	char *sep = "=";
    239 
    240 	prog = *argv;
    241 
    242 	while ((ch = getopt(argc, argv, "af:nw")) != -1) {
    243 		switch(ch) {
    244 		case 'a':
    245 			aflag++;
    246 			break;
    247 		case 'w':
    248 			wflag++;
    249 			break;
    250 		case 'n':
    251 			sep = 0;;
    252 			break;
    253 		case 'f':
    254 			file = optarg;
    255 			 /* XXX this test should be more clever */
    256 			if (strcmp(file, "/dev/stdout") == 0)
    257 				out = stderr;
    258 			break;
    259 		case '?':
    260 		default:
    261 		usage:
    262 		fprintf(out, "%s [-f file] [-n] name ...\n", prog);
    263 		fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
    264 		fprintf(out, "%s [-f file] [-n] -a\n", prog);
    265 		exit(0);
    266 		}
    267 	}
    268 	argc -= optind;
    269 	argv += optind;
    270 
    271 	fd = open(file, O_RDWR);
    272 	if (fd < 0)
    273 		fd = open(file, O_WRONLY);
    274 	if (fd < 0)
    275 		fd = open(file, O_RDONLY);
    276 	if (fd < 0)
    277 		err(1, "%s", file);
    278 
    279 	if (!wflag) {
    280 		if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
    281 			err(1, NULL);
    282 		for(pos = 0, i = 0;; i++) {
    283 			audio_encoding_t enc;
    284 			enc.index = i;
    285 			if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
    286 				break;
    287 			if (pos)
    288 				encbuf[pos++] = ',';
    289 			sprintf(encbuf+pos, "%s:%d%s", enc.name,
    290 			        enc.precision,
    291 				enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
    292 			pos += strlen(encbuf+pos);
    293 		}
    294 		if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
    295 			err(1, NULL);
    296 		if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
    297 			err(1, NULL);
    298 		if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
    299 			err(1, NULL);
    300 	}
    301 
    302 	if (argc == 0 && aflag && !wflag) {
    303 		for(i = 0; fields[i].name; i++) {
    304 			prfield(&fields[i], sep);
    305 			fprintf(out, "\n");
    306 		}
    307 	} else if (argc > 0 && !aflag) {
    308 		struct field *p;
    309 		if (wflag) {
    310 			AUDIO_INITINFO(&info);
    311 			while(argc--) {
    312 				char *q;
    313 
    314 				q = strchr(*argv, '=');
    315 				if (q) {
    316 					*q++ = 0;
    317 					p = findfield(*argv);
    318 					if (p == 0)
    319 						warnx("field %s does not exist", *argv);
    320 					else {
    321 						if (p->readonly)
    322 							warnx("%s is read only", *argv);
    323 						else {
    324 							rdfield(p, q, sep);
    325 							if (p->valp == &fullduplex)
    326 								if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
    327 									err(1, "set failed");
    328 						}
    329 					}
    330 				} else {
    331 					warnx("No `=' in %s", *argv);
    332 				}
    333 				argv++;
    334 			}
    335 			if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    336 				err(1, "set failed");
    337 		} else {
    338 			while(argc--) {
    339 				p = findfield(*argv);
    340 				if (p == 0)
    341 					warnx("field %s does not exist", *argv);
    342 				else
    343 					prfield(p, sep), fprintf(out, "\n");
    344 				argv++;
    345 			}
    346 		}
    347 	} else
    348 		goto usage;
    349 	exit(0);
    350 }
    351 
    352