[BACK]Return to audioctl.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / audioctl

Annotation of src/usr.bin/audioctl/audioctl.c, Revision 1.23

1.23    ! deraadt     1: /*     $OpenBSD: audioctl.c,v 1.22 2012/07/27 00:21:17 krw Exp $       */
1.3       provos      2: /*     $NetBSD: audioctl.c,v 1.14 1998/04/27 16:55:23 augustss Exp $   */
1.1       provos      3:
                      4: /*
                      5:  * Copyright (c) 1997 The NetBSD Foundation, Inc.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Author: Lennart Augustsson
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
1.7       pvalchev   32: /*
                     33:  * audioctl(1) - a program to control audio device.
                     34:  */
                     35:
1.1       provos     36: #include <stdio.h>
1.3       provos     37: #include <stdlib.h>
1.1       provos     38: #include <fcntl.h>
                     39: #include <err.h>
                     40: #include <unistd.h>
                     41: #include <string.h>
                     42: #include <sys/types.h>
                     43: #include <sys/stat.h>
                     44: #include <sys/ioctl.h>
                     45: #include <sys/audioio.h>
                     46:
1.6       millert    47: struct field *findfield(char *name);
1.12      moritz     48: void prfield(struct field *p, const char *sep);
1.6       millert    49: void rdfield(struct field *p, char *q);
                     50: void getinfo(int fd);
                     51: void usage(void);
                     52: int main(int argc, char **argv);
1.1       provos     53:
                     54: FILE *out = stdout;
                     55:
                     56: audio_device_t adev;
                     57:
                     58: audio_info_t info;
                     59:
                     60: char encbuf[1000];
                     61:
1.18      jakemsr    62: int properties, fullduplex, perrors, rerrors;
1.1       provos     63:
                     64: struct field {
1.12      moritz     65:        const char *name;
1.1       provos     66:        void *valp;
                     67:        int format;
                     68: #define STRING 1
                     69: #define INT 2
                     70: #define UINT 3
                     71: #define P_R 4
                     72: #define UCHAR 6
                     73: #define ENC 7
                     74: #define PROPS 8
                     75: #define XINT 9
                     76:        char flags;
                     77: #define READONLY 1
                     78: #define ALIAS 2
                     79: #define SET 4
1.22      krw        80:        u_int oldval;
1.1       provos     81: } fields[] = {
                     82:        { "name",               &adev.name,             STRING, READONLY },
                     83:        { "version",            &adev.version,          STRING, READONLY },
                     84:        { "config",             &adev.config,           STRING, READONLY },
                     85:        { "encodings",          encbuf,                 STRING, READONLY },
                     86:        { "properties",         &properties,            PROPS,  READONLY },
1.4       deraadt    87:        { "full_duplex",        &fullduplex,            UINT,   0 },
                     88:        { "fullduplex",         &fullduplex,            UINT,   0 },
1.1       provos     89:        { "blocksize",          &info.blocksize,        UINT,   0 },
                     90:        { "hiwat",              &info.hiwat,            UINT,   0 },
                     91:        { "lowat",              &info.lowat,            UINT,   0 },
1.14      jakemsr    92:        { "output_muted",       &info.output_muted,     UCHAR,  0 },
1.1       provos     93:        { "monitor_gain",       &info.monitor_gain,     UINT,   0 },
                     94:        { "mode",               &info.mode,             P_R,    READONLY },
                     95:        { "play.rate",          &info.play.sample_rate, UINT,   0 },
                     96:        { "play.sample_rate",   &info.play.sample_rate, UINT,   ALIAS },
                     97:        { "play.channels",      &info.play.channels,    UINT,   0 },
                     98:        { "play.precision",     &info.play.precision,   UINT,   0 },
1.21      jakemsr    99:        { "play.bps",           &info.play.bps,         UINT,   0 },
                    100:        { "play.msb",           &info.play.msb,         UINT,   0 },
1.1       provos    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 },
1.8       naddy     106:        { "play.seek",          &info.play.seek,        UINT,   READONLY },
1.1       provos    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 },
1.17      ratchov   115:        { "play.block_size",    &info.play.block_size,  UINT,   0 },
1.18      jakemsr   116:        { "play.errors",        &perrors,               INT,    READONLY },
1.1       provos    117:        { "record.rate",        &info.record.sample_rate,UINT,  0 },
                    118:        { "record.sample_rate", &info.record.sample_rate,UINT,  ALIAS },
                    119:        { "record.channels",    &info.record.channels,  UINT,   0 },
                    120:        { "record.precision",   &info.record.precision, UINT,   0 },
1.21      jakemsr   121:        { "record.bps",         &info.record.bps,       UINT,   0 },
                    122:        { "record.msb",         &info.record.msb,       UINT,   0 },
1.1       provos    123:        { "record.encoding",    &info.record.encoding,  ENC,    0 },
                    124:        { "record.gain",        &info.record.gain,      UINT,   0 },
                    125:        { "record.balance",     &info.record.balance,   UCHAR,  0 },
                    126:        { "record.port",        &info.record.port,      XINT,   0 },
                    127:        { "record.avail_ports", &info.record.avail_ports,XINT,  0 },
1.8       naddy     128:        { "record.seek",        &info.record.seek,      UINT,   READONLY },
1.1       provos    129:        { "record.samples",     &info.record.samples,   UINT,   READONLY },
                    130:        { "record.eof",         &info.record.eof,       UINT,   READONLY },
                    131:        { "record.pause",       &info.record.pause,     UCHAR,  0 },
                    132:        { "record.error",       &info.record.error,     UCHAR,  READONLY },
                    133:        { "record.waiting",     &info.record.waiting,   UCHAR,  READONLY },
                    134:        { "record.open",        &info.record.open,      UCHAR,  READONLY },
                    135:        { "record.active",      &info.record.active,    UCHAR,  READONLY },
                    136:        { "record.buffer_size", &info.record.buffer_size,UINT,  0 },
1.17      ratchov   137:        { "record.block_size",  &info.record.block_size,UINT,   0 },
1.18      jakemsr   138:        { "record.errors",      &rerrors,               INT,    READONLY },
1.1       provos    139:        { 0 }
                    140: };
                    141:
                    142: struct {
1.12      moritz    143:        const char *ename;
                    144:        u_int eno;
1.1       provos    145: } encs[] = {
                    146:        { AudioEmulaw,          AUDIO_ENCODING_ULAW },
                    147:        { "ulaw",               AUDIO_ENCODING_ULAW },
                    148:        { AudioEalaw,           AUDIO_ENCODING_ALAW },
                    149:        { AudioEslinear,        AUDIO_ENCODING_SLINEAR },
                    150:        { "linear",             AUDIO_ENCODING_SLINEAR },
                    151:        { AudioEulinear,        AUDIO_ENCODING_ULINEAR },
                    152:        { AudioEadpcm,          AUDIO_ENCODING_ADPCM },
                    153:        { "ADPCM",              AUDIO_ENCODING_ADPCM },
                    154:        { AudioEslinear_le,     AUDIO_ENCODING_SLINEAR_LE },
                    155:        { "linear_le",          AUDIO_ENCODING_SLINEAR_LE },
                    156:        { AudioEulinear_le,     AUDIO_ENCODING_ULINEAR_LE },
                    157:        { AudioEslinear_be,     AUDIO_ENCODING_SLINEAR_BE },
                    158:        { "linear_be",          AUDIO_ENCODING_SLINEAR_BE },
                    159:        { AudioEulinear_be,     AUDIO_ENCODING_ULINEAR_BE },
                    160:        { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
                    161:        { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
                    162:        { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
                    163:        { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
                    164:        { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
                    165:        { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
                    166:        { 0 }
                    167: };
                    168:
                    169: static struct {
1.12      moritz    170:        const char *name;
1.1       provos    171:        u_int prop;
                    172: } props[] = {
                    173:        { "full_duplex",        AUDIO_PROP_FULLDUPLEX },
                    174:        { "mmap",               AUDIO_PROP_MMAP },
                    175:        { "independent",        AUDIO_PROP_INDEPENDENT },
                    176:        { 0 }
                    177: };
                    178:
                    179: struct field *
1.7       pvalchev  180: findfield(char *name)
1.1       provos    181: {
                    182:        int i;
1.15      sobrado   183:        for (i = 0; fields[i].name; i++)
1.1       provos    184:                if (strcmp(fields[i].name, name) == 0)
                    185:                        return &fields[i];
1.7       pvalchev  186:        return (0);
1.1       provos    187: }
                    188:
1.23    ! deraadt   189: static void
1.20      ratchov   190: prval(u_int format, void *valp)
1.1       provos    191: {
                    192:        u_int v;
1.12      moritz    193:        const char *cm;
1.1       provos    194:        int i;
                    195:
1.20      ratchov   196:        switch (format) {
1.1       provos    197:        case STRING:
1.20      ratchov   198:                fprintf(out, "%s", (char *)valp);
1.1       provos    199:                break;
                    200:        case INT:
1.20      ratchov   201:                fprintf(out, "%d", *(int *)valp);
1.1       provos    202:                break;
                    203:        case UINT:
1.20      ratchov   204:                fprintf(out, "%u", *(u_int *)valp);
1.1       provos    205:                break;
                    206:        case XINT:
1.20      ratchov   207:                fprintf(out, "0x%x", *(u_int *)valp);
1.1       provos    208:                break;
                    209:        case UCHAR:
1.20      ratchov   210:                fprintf(out, "%u", *(u_char *)valp);
1.1       provos    211:                break;
                    212:        case P_R:
1.20      ratchov   213:                v = *(u_int *)valp;
1.1       provos    214:                cm = "";
                    215:                if (v & AUMODE_PLAY) {
                    216:                        if (v & AUMODE_PLAY_ALL)
                    217:                                fprintf(out, "play");
                    218:                        else
                    219:                                fprintf(out, "playsync");
                    220:                        cm = ",";
                    221:                }
                    222:                if (v & AUMODE_RECORD)
                    223:                        fprintf(out, "%srecord", cm);
                    224:                break;
                    225:        case ENC:
1.20      ratchov   226:                v = *(u_int *)valp;
1.15      sobrado   227:                for (i = 0; encs[i].ename; i++)
1.1       provos    228:                        if (encs[i].eno == v)
                    229:                                break;
                    230:                if (encs[i].ename)
                    231:                        fprintf(out, "%s", encs[i].ename);
                    232:                else
                    233:                        fprintf(out, "%u", v);
                    234:                break;
                    235:        case PROPS:
1.20      ratchov   236:                v = *(u_int *)valp;
1.1       provos    237:                for (cm = "", i = 0; props[i].name; i++) {
                    238:                        if (v & props[i].prop) {
                    239:                                fprintf(out, "%s%s", cm, props[i].name);
                    240:                                cm = ",";
                    241:                        }
                    242:                }
                    243:                break;
                    244:        default:
                    245:                errx(1, "Invalid print format.");
                    246:        }
                    247: }
                    248:
                    249: void
1.20      ratchov   250: prfield(struct field *p, const char *sep)
                    251: {
                    252:        if (sep) {
                    253:                fprintf(out, "%s", p->name);
                    254:                if (p->flags & SET) {
                    255:                        fprintf(out, "%s", ": ");
                    256:                        prval(p->format, &p->oldval);
                    257:                        fprintf(out, " -> ");
                    258:                } else
                    259:                        fprintf(out, "%s", sep);
                    260:        }
                    261:        prval(p->format, p->valp);
                    262:        fprintf(out, "\n");
                    263: }
                    264:
                    265: void
1.7       pvalchev  266: rdfield(struct field *p, char *q)
1.1       provos    267: {
                    268:        int i;
                    269:        u_int u;
                    270:
1.15      sobrado   271:        switch (p->format) {
1.1       provos    272:        case UINT:
1.20      ratchov   273:                p->oldval = *(u_int *)p->valp;
                    274:                if (sscanf(q, "%u", (unsigned int *)p->valp) != 1) {
1.1       provos    275:                        warnx("Bad number %s", q);
1.20      ratchov   276:                        return;
                    277:                }
1.1       provos    278:                break;
                    279:        case UCHAR:
1.20      ratchov   280:                *(char *)&p->oldval = *(u_char *)p->valp;
                    281:                if (sscanf(q, "%u", &u) != 1) {
1.1       provos    282:                        warnx("Bad number %s", q);
1.20      ratchov   283:                        return;
                    284:                }
                    285:                *(u_char *)p->valp = u;
1.1       provos    286:                break;
                    287:        case XINT:
1.20      ratchov   288:                p->oldval = *(u_int *)p->valp;
1.1       provos    289:                if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
1.20      ratchov   290:                    sscanf(q, "%x", (unsigned int *)p->valp) != 1) {
1.1       provos    291:                        warnx("Bad number %s", q);
1.20      ratchov   292:                        return;
                    293:                }
1.1       provos    294:                break;
                    295:        case ENC:
1.20      ratchov   296:                p->oldval = *(u_int *)p->valp;
1.15      sobrado   297:                for (i = 0; encs[i].ename; i++)
1.1       provos    298:                        if (strcmp(encs[i].ename, q) == 0)
                    299:                                break;
                    300:                if (encs[i].ename)
                    301:                        *(u_int*)p->valp = encs[i].eno;
1.20      ratchov   302:                else {
1.1       provos    303:                        warnx("Unknown encoding: %s", q);
1.20      ratchov   304:                        return;
                    305:                }
1.1       provos    306:                break;
                    307:        default:
                    308:                errx(1, "Invalid read format.");
                    309:        }
                    310:        p->flags |= SET;
                    311: }
                    312:
                    313: void
1.7       pvalchev  314: getinfo(int fd)
1.1       provos    315: {
1.7       pvalchev  316:        int pos = 0, i = 0;
1.1       provos    317:
                    318:        if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
                    319:                err(1, "AUDIO_GETDEV");
1.15      sobrado   320:        for (;;) {
1.9       deraadt   321:                audio_encoding_t enc;
                    322:                enc.index = i++;
                    323:                if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
                    324:                        break;
                    325:                if (pos)
                    326:                        encbuf[pos++] = ',';
1.21      jakemsr   327:                snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d:%d:%d%s",
                    328:                    enc.name, enc.precision, enc.bps, enc.msb,
1.9       deraadt   329:                    enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
                    330:                pos += strlen(encbuf+pos);
1.1       provos    331:        }
                    332:        if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
                    333:                err(1, "AUDIO_GETFD");
                    334:        if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
                    335:                err(1, "AUDIO_GETPROPS");
1.18      jakemsr   336:        if (ioctl(fd, AUDIO_PERROR, &perrors) < 0)
                    337:                err(1, "AUDIO_PERROR");
                    338:        if (ioctl(fd, AUDIO_RERROR, &rerrors) < 0)
1.1       provos    339:                err(1, "AUDIO_RERROR");
                    340:        if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
                    341:                err(1, "AUDIO_GETINFO");
                    342: }
                    343:
                    344: void
1.7       pvalchev  345: usage(void)
1.1       provos    346: {
1.7       pvalchev  347:        extern char *__progname;                /* from crt0.o */
                    348:
                    349:        fprintf(stderr,
1.16      deraadt   350:            "usage: %s [-an] [-f file]\n"
1.15      sobrado   351:            "       %s [-n] [-f file] name ...\n"
                    352:            "       %s [-n] [-f file] name=value ...\n",
                    353:            __progname, __progname, __progname);
1.7       pvalchev  354:
1.1       provos    355:        exit(1);
                    356: }
                    357:
                    358: int
1.7       pvalchev  359: main(int argc, char **argv)
1.1       provos    360: {
                    361:        int fd, i, ch;
1.11      vincent   362:        int aflag = 0, canwrite, writeinfo = 0;
1.1       provos    363:        struct stat dstat, ostat;
1.11      vincent   364:        struct field *p;
1.12      moritz    365:        const char *file;
                    366:        const char *sep = "=";
1.1       provos    367:
1.7       pvalchev  368:        if ((file = getenv("AUDIOCTLDEVICE")) == 0 || *file == '\0')
1.3       provos    369:                file = "/dev/audioctl";
1.1       provos    370:
                    371:        while ((ch = getopt(argc, argv, "af:nw")) != -1) {
1.15      sobrado   372:                switch (ch) {
1.1       provos    373:                case 'a':
                    374:                        aflag++;
                    375:                        break;
                    376:                case 'w':
1.11      vincent   377:                        /* backward compatibility */
1.1       provos    378:                        break;
                    379:                case 'n':
                    380:                        sep = 0;
                    381:                        break;
                    382:                case 'f':
                    383:                        file = optarg;
                    384:                        break;
                    385:                default:
                    386:                        usage();
                    387:                }
                    388:        }
                    389:        argc -= optind;
                    390:        argv += optind;
1.16      deraadt   391:
                    392:        if (argc == 0)
                    393:                aflag++;
1.11      vincent   394:
                    395:        if ((fd = open(file, O_RDWR)) < 0) {
                    396:                if ((fd = open(file, O_RDONLY)) < 0)
                    397:                        err(1, "%s", file);
                    398:                canwrite = 0;
                    399:        } else
                    400:                canwrite = 1;
1.1       provos    401:
                    402:        /* Check if stdout is the same device as the audio device. */
                    403:        if (fstat(fd, &dstat) < 0)
                    404:                err(1, "fstat au");
                    405:        if (fstat(STDOUT_FILENO, &ostat) < 0)
                    406:                err(1, "fstat stdout");
                    407:        if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
                    408:            major(dstat.st_dev) == major(ostat.st_dev) &&
                    409:            minor(dstat.st_dev) == minor(ostat.st_dev))
                    410:                /* We can't write to stdout so use stderr */
                    411:                out = stderr;
                    412:
1.11      vincent   413:        if (!argc && !aflag)
                    414:                usage();
                    415:
                    416:        getinfo(fd);
1.1       provos    417:
1.11      vincent   418:        if (aflag) {
                    419:                for (i = 0; fields[i].name; i++) {
1.1       provos    420:                        if (!(fields[i].flags & ALIAS)) {
                    421:                                prfield(&fields[i], sep);
                    422:                        }
                    423:                }
1.11      vincent   424:        } else {
                    425:                while (argc--) {
                    426:                        char *q;
1.1       provos    427:
1.11      vincent   428:                        if ((q = strchr(*argv, '=')) != NULL) {
                    429:                                *q++ = 0;
                    430:                                p = findfield(*argv);
                    431:                                if (p == 0)
                    432:                                        warnx("field `%s' does not exist", *argv);
                    433:                                else {
                    434:                                        if (!canwrite)
                    435:                                                errx(1, "%s: permission denied",
                    436:                                                    *argv);
                    437:                                        if (p->flags & READONLY)
                    438:                                                warnx("`%s' is read only", *argv);
1.1       provos    439:                                        else {
1.11      vincent   440:                                                rdfield(p, q);
                    441:                                                if (p->valp == &fullduplex)
                    442:                                                        if (ioctl(fd, AUDIO_SETFD,
                    443:                                                            &fullduplex) < 0)
                    444:                                                                err(1, "set failed");
1.1       provos    445:                                        }
1.11      vincent   446:                                        writeinfo = 1;
1.1       provos    447:                                }
1.11      vincent   448:                        } else {
1.1       provos    449:                                p = findfield(*argv);
1.11      vincent   450:                                if (p == 0)
                    451:                                        warnx("field %s does not exist", *argv);
                    452:                                else {
1.1       provos    453:                                        prfield(p, sep);
                    454:                                }
1.11      vincent   455:                        }
                    456:                        argv++;
                    457:                }
                    458:                if (writeinfo && ioctl(fd, AUDIO_SETINFO, &info) < 0)
                    459:                        err(1, "set failed");
1.20      ratchov   460:                getinfo(fd);
                    461:                for (i = 0; fields[i].name; i++) {
                    462:                        if (fields[i].flags & SET) {
                    463:                                prfield(&fields[i], sep);
1.1       provos    464:                        }
                    465:                }
1.11      vincent   466:        }
1.1       provos    467:        exit(0);
                    468: }