Home | History | Annotate | Line # | Download | only in ctl
ctl.c revision 1.8
      1 /*	$NetBSD: ctl.c,v 1.8 1997/08/25 19:03:12 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/stat.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/audioio.h>
     47 
     48 FILE *out = stdout;
     49 
     50 char *prog;
     51 
     52 audio_device_t adev;
     53 
     54 audio_info_t info;
     55 
     56 char encbuf[1000];
     57 
     58 int properties, fullduplex, rerror;
     59 
     60 struct field {
     61 	char *name;
     62 	void *valp;
     63 	int format;
     64 #define STRING 1
     65 #define INT 2
     66 #define UINT 3
     67 #define P_R 4
     68 #define ULONG 5
     69 #define UCHAR 6
     70 #define ENC 7
     71 #define PROPS 8
     72 	char flags;
     73 #define READONLY 1
     74 #define ALIAS 2
     75 #define SET 4
     76 } fields[] = {
     77     { "name", 			&adev.name, 		STRING, READONLY },
     78     { "version",		&adev.version,		STRING, READONLY },
     79     { "config",			&adev.config,		STRING, READONLY },
     80     { "encodings",		encbuf,			STRING, READONLY },
     81     { "properties",		&properties,		PROPS,	READONLY },
     82     { "full_duplex",		&fullduplex,		INT,    0 },
     83     { "buffersize",		&info.buffersize,	UINT,	0 },
     84     { "blocksize",		&info.blocksize,	UINT,	0 },
     85     { "hiwat",			&info.hiwat,		UINT,	0 },
     86     { "lowat",			&info.lowat,		UINT,	0 },
     87     { "backlog",		&info.backlog,		UINT,	0 },
     88     { "mode",			&info.mode,		P_R,	READONLY },
     89     { "play.rate",		&info.play.sample_rate,	UINT,	0 },
     90     { "play.sample_rate",	&info.play.sample_rate,	UINT,	ALIAS },
     91     { "play.channels",		&info.play.channels,	UINT,	0 },
     92     { "play.precision",		&info.play.precision,	UINT,	0 },
     93     { "play.encoding",		&info.play.encoding,	ENC,	0 },
     94     { "play.gain",		&info.play.gain,	UINT,	0 },
     95     { "play.port",		&info.play.port,	UINT,	0 },
     96     { "play.seek",		&info.play.seek,	ULONG,	READONLY },
     97     { "play.samples",		&info.play.samples,	UINT,	READONLY },
     98     { "play.eof",		&info.play.eof,		UINT,	READONLY },
     99     { "play.pause",		&info.play.pause,	UCHAR,	0 },
    100     { "play.error",		&info.play.error,	UCHAR,	READONLY },
    101     { "play.waiting",		&info.play.waiting,	UCHAR,	READONLY },
    102     { "play.open",		&info.play.open,	UCHAR,	READONLY },
    103     { "play.active",		&info.play.active,	UCHAR,	READONLY },
    104     { "record.rate",		&info.record.sample_rate,UINT,	0 },
    105     { "record.sample_rate",	&info.record.sample_rate,UINT,	ALIAS },
    106     { "record.channels",	&info.record.channels,	UINT,	0 },
    107     { "record.precision",	&info.record.precision,	UINT,	0 },
    108     { "record.encoding",	&info.record.encoding,	ENC,	0 },
    109     { "record.gain",		&info.record.gain,	UINT,	0 },
    110     { "record.port",		&info.record.port,	UINT,	0 },
    111     { "record.seek",		&info.record.seek,	ULONG,	READONLY },
    112     { "record.samples",		&info.record.samples,	UINT,	READONLY },
    113     { "record.eof",		&info.record.eof,	UINT,	READONLY },
    114     { "record.pause",		&info.record.pause,	UCHAR,	0 },
    115     { "record.error",		&info.record.error,	UCHAR,	READONLY },
    116     { "record.waiting",		&info.record.waiting,	UCHAR,	READONLY },
    117     { "record.open",		&info.record.open,	UCHAR,	READONLY },
    118     { "record.active",		&info.record.active,	UCHAR,	READONLY },
    119     { "record.errors",		&rerror,		INT,	READONLY },
    120     { 0 }
    121 };
    122 
    123 struct {
    124 	char *ename;
    125 	int eno;
    126 } encs[] = {
    127     { "ulaw",		AUDIO_ENCODING_ULAW },
    128     { "mulaw",		AUDIO_ENCODING_ULAW },
    129     { "alaw", 		AUDIO_ENCODING_ALAW },
    130     { "slinear",	AUDIO_ENCODING_SLINEAR },
    131     { "linear",		AUDIO_ENCODING_SLINEAR },
    132     { "ulinear",	AUDIO_ENCODING_ULINEAR },
    133     { "adpcm",		AUDIO_ENCODING_ADPCM },
    134     { "ADPCM",		AUDIO_ENCODING_ADPCM },
    135     { "slinear_le",	AUDIO_ENCODING_SLINEAR_LE },
    136     { "linear_le",	AUDIO_ENCODING_SLINEAR_LE },
    137     { "ulinear_le",	AUDIO_ENCODING_ULINEAR_LE },
    138     { "slinear_be",	AUDIO_ENCODING_SLINEAR_BE },
    139     { "linear_be",	AUDIO_ENCODING_SLINEAR_BE },
    140     { "ulinear_be",	AUDIO_ENCODING_ULINEAR_BE },
    141     { 0 }
    142 };
    143 
    144 struct field *
    145 findfield(char *name)
    146 {
    147     int i;
    148     for(i = 0; fields[i].name; i++)
    149 	if (strcmp(fields[i].name, name) == 0)
    150 	    return &fields[i];
    151     return 0;
    152 }
    153 
    154 void
    155 prfield(struct field *p, char *sep)
    156 {
    157     u_int v;
    158     char *cm;
    159     int i;
    160 
    161     if (sep)
    162 	fprintf(out, "%s%s", p->name, sep);
    163     switch(p->format) {
    164     case STRING:
    165 	fprintf(out, "%s", (char*)p->valp);
    166 	break;
    167     case INT:
    168 	fprintf(out, "%d", *(int*)p->valp);
    169 	break;
    170     case UINT:
    171 	fprintf(out, "%u", *(u_int*)p->valp);
    172 	break;
    173     case UCHAR:
    174 	fprintf(out, "%u", *(u_char*)p->valp);
    175 	break;
    176     case ULONG:
    177 	fprintf(out, "%lu", *(u_long*)p->valp);
    178 	break;
    179     case P_R:
    180 	v = *(u_int*)p->valp;
    181 	cm = "";
    182 	if (v & AUMODE_PLAY) {
    183 	    if (v & AUMODE_PLAY_ALL)
    184 		fprintf(out, "play");
    185 	    else
    186 		fprintf(out, "playsync");
    187 	    cm = ",";
    188 	}
    189 	if (v & AUMODE_RECORD)
    190 	    fprintf(out, "%srecord", cm);
    191 	break;
    192     case ENC:
    193 	v = *(u_int*)p->valp;
    194 	for(i = 0; encs[i].ename; i++)
    195 	    if (encs[i].eno == v)
    196 		break;
    197 	if (encs[i].ename)
    198 	    fprintf(out, "%s", encs[i].ename);
    199 	else
    200 	    fprintf(out, "%u", v);
    201 	break;
    202     case PROPS:
    203 	v = *(u_int*)p->valp;
    204 	cm = "";
    205 	if (v & AUDIO_PROP_FULLDUPLEX) {
    206 		fprintf(out, "%sfull_duplex", cm);
    207 		cm = ",";
    208 	}
    209 	if (v & AUDIO_PROP_MMAP) {
    210 		fprintf(out, "%smmap", cm);
    211 		cm = ",";
    212 	}
    213 	break;
    214     default:
    215 	errx(1, "Invalid format.");
    216     }
    217 }
    218 
    219 void
    220 rdfield(struct field *p, char *q)
    221 {
    222     int i;
    223 
    224     switch(p->format) {
    225     case UINT:
    226 	if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
    227 	    warnx("Bad number %s", q);
    228 	break;
    229     case ENC:
    230 	for(i = 0; encs[i].ename; i++)
    231 	    if (strcmp(encs[i].ename, q) == 0)
    232 		break;
    233 	if (encs[i].ename)
    234 	    *(u_int*)p->valp = encs[i].eno;
    235 	else
    236 	    warnx("Unknown encoding: %s", q);
    237 	break;
    238     default:
    239 	errx(1, "Invalid format.");
    240     }
    241     p->flags |= SET;
    242 }
    243 
    244 void
    245 getinfo(int fd)
    246 {
    247     int pos, i;
    248 
    249     if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
    250 	err(1, NULL);
    251     for(pos = 0, i = 0; ; i++) {
    252 	audio_encoding_t enc;
    253 	enc.index = i;
    254 	if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
    255 	    break;
    256 	if (pos)
    257 	    encbuf[pos++] = ',';
    258 	sprintf(encbuf+pos, "%s:%d%s", enc.name,
    259 		enc.precision,
    260 		enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
    261 	pos += strlen(encbuf+pos);
    262     }
    263     if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
    264 	err(1, NULL);
    265     if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
    266 	err(1, NULL);
    267     if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
    268 	err(1, NULL);
    269     if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
    270 	err(1, NULL);
    271 }
    272 
    273 void
    274 usage(void)
    275 {
    276     fprintf(out, "%s [-f file] [-n] name ...\n", prog);
    277     fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
    278     fprintf(out, "%s [-f file] [-n] -a\n", prog);
    279     exit(1);
    280 }
    281 
    282 void
    283 main(int argc, char **argv)
    284 {
    285     int fd, i, ch;
    286     int aflag = 0, wflag = 0;
    287     struct stat dstat, ostat;
    288     char *file = "/dev/audioctl";
    289     char *sep = "=";
    290 
    291     prog = *argv;
    292 
    293     while ((ch = getopt(argc, argv, "af:nw")) != -1) {
    294 	switch(ch) {
    295 	case 'a':
    296 	    aflag++;
    297 	    break;
    298 	case 'w':
    299 	    wflag++;
    300 	    break;
    301 	case 'n':
    302 	    sep = 0;
    303 	    break;
    304 	case 'f':
    305 	    file = optarg;
    306 	    break;
    307 	case '?':
    308 	default:
    309 	    usage();
    310 	}
    311     }
    312     argc -= optind;
    313     argv += optind;
    314 
    315     fd = open(file, O_WRONLY);
    316     if (fd < 0)
    317 	fd = open(file, O_RDONLY);
    318     if (fd < 0)
    319 	err(1, "%s", file);
    320 
    321     /* Check if stdout is the same device as the audio device. */
    322     if (fstat(fd, &dstat) < 0)
    323 	err(1, NULL);
    324     if (fstat(STDOUT_FILENO, &ostat) < 0)
    325 		err(1, NULL);
    326     if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
    327 	major(dstat.st_dev) == major(ostat.st_dev) &&
    328 	minor(dstat.st_dev) == minor(ostat.st_dev))
    329 	/* We can't write to stdout so use stderr */
    330 	out = stderr;
    331 
    332     if (!wflag)
    333 	getinfo(fd);
    334 
    335     if (argc == 0 && aflag && !wflag) {
    336 	for(i = 0; fields[i].name; i++) {
    337 	    if (!(fields[i].flags & ALIAS)) {
    338 		prfield(&fields[i], sep);
    339 		fprintf(out, "\n");
    340 	    }
    341 	}
    342     } else if (argc > 0 && !aflag) {
    343 	struct field *p;
    344 	if (wflag) {
    345 	    AUDIO_INITINFO(&info);
    346 	    while(argc--) {
    347 		char *q;
    348 
    349 		q = strchr(*argv, '=');
    350 		if (q) {
    351 		    *q++ = 0;
    352 		    p = findfield(*argv);
    353 		    if (p == 0)
    354 			warnx("field `%s' does not exist", *argv);
    355 		    else {
    356 			if (p->flags & READONLY)
    357 			    warnx("`%s' is read only", *argv);
    358 			else {
    359 			    rdfield(p, q);
    360 			    if (p->valp == &fullduplex)
    361 				if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
    362 				    err(1, "set failed");
    363 			}
    364 		    }
    365 		} else
    366 		    warnx("No `=' in %s", *argv);
    367 		argv++;
    368 	    }
    369 	    if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    370 		err(1, "set failed");
    371 	    if (sep) {
    372 		getinfo(fd);
    373 		for(i = 0; fields[i].name; i++) {
    374 		    if (fields[i].flags & SET) {
    375 			fprintf(out, "%s: -> ", fields[i].name);
    376 			prfield(&fields[i], 0);
    377 			fprintf(out, "\n");
    378 		    }
    379 		}
    380 	    }
    381 	} else {
    382 	    while(argc--) {
    383 		p = findfield(*argv);
    384 		if (p == 0) {
    385 		    if (strchr(*argv, '='))
    386 			warnx("field %s does not exist (use -w to set a variable)", *argv);
    387 		    else
    388 			warnx("field %s does not exist", *argv);
    389 		} else {
    390 		    prfield(p, sep);
    391 		    fprintf(out, "\n");
    392 		}
    393 		argv++;
    394 	    }
    395 	}
    396     } else
    397 	usage();
    398     exit(0);
    399 }
    400