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