[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.20

1.20    ! ratchov     1: /*     $OpenBSD: audioctl.c,v 1.19 2008/06/26 05:42:20 ray 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;
1.20    ! ratchov    68:        u_int oldval;
1.1       provos     69: #define STRING 1
                     70: #define INT 2
                     71: #define UINT 3
                     72: #define P_R 4
                     73: #define UCHAR 6
                     74: #define ENC 7
                     75: #define PROPS 8
                     76: #define XINT 9
                     77:        char flags;
                     78: #define READONLY 1
                     79: #define ALIAS 2
                     80: #define SET 4
                     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 },
                     99:        { "play.encoding",      &info.play.encoding,    ENC,    0 },
                    100:        { "play.gain",          &info.play.gain,        UINT,   0 },
                    101:        { "play.balance",       &info.play.balance,     UCHAR,  0 },
                    102:        { "play.port",          &info.play.port,        XINT,   0 },
                    103:        { "play.avail_ports",   &info.play.avail_ports, XINT,   0 },
1.8       naddy     104:        { "play.seek",          &info.play.seek,        UINT,   READONLY },
1.1       provos    105:        { "play.samples",       &info.play.samples,     UINT,   READONLY },
                    106:        { "play.eof",           &info.play.eof,         UINT,   READONLY },
                    107:        { "play.pause",         &info.play.pause,       UCHAR,  0 },
                    108:        { "play.error",         &info.play.error,       UCHAR,  READONLY },
                    109:        { "play.waiting",       &info.play.waiting,     UCHAR,  READONLY },
                    110:        { "play.open",          &info.play.open,        UCHAR,  READONLY },
                    111:        { "play.active",        &info.play.active,      UCHAR,  READONLY },
                    112:        { "play.buffer_size",   &info.play.buffer_size, UINT,   0 },
1.17      ratchov   113:        { "play.block_size",    &info.play.block_size,  UINT,   0 },
1.18      jakemsr   114:        { "play.errors",        &perrors,               INT,    READONLY },
1.1       provos    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 },
1.8       naddy     124:        { "record.seek",        &info.record.seek,      UINT,   READONLY },
1.1       provos    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 },
1.17      ratchov   133:        { "record.block_size",  &info.record.block_size,UINT,   0 },
1.18      jakemsr   134:        { "record.errors",      &rerrors,               INT,    READONLY },
1.1       provos    135:        { 0 }
                    136: };
                    137:
                    138: struct {
1.12      moritz    139:        const char *ename;
                    140:        u_int eno;
1.1       provos    141: } encs[] = {
                    142:        { AudioEmulaw,          AUDIO_ENCODING_ULAW },
                    143:        { "ulaw",               AUDIO_ENCODING_ULAW },
                    144:        { AudioEalaw,           AUDIO_ENCODING_ALAW },
                    145:        { AudioEslinear,        AUDIO_ENCODING_SLINEAR },
                    146:        { "linear",             AUDIO_ENCODING_SLINEAR },
                    147:        { AudioEulinear,        AUDIO_ENCODING_ULINEAR },
                    148:        { AudioEadpcm,          AUDIO_ENCODING_ADPCM },
                    149:        { "ADPCM",              AUDIO_ENCODING_ADPCM },
                    150:        { AudioEslinear_le,     AUDIO_ENCODING_SLINEAR_LE },
                    151:        { "linear_le",          AUDIO_ENCODING_SLINEAR_LE },
                    152:        { AudioEulinear_le,     AUDIO_ENCODING_ULINEAR_LE },
                    153:        { AudioEslinear_be,     AUDIO_ENCODING_SLINEAR_BE },
                    154:        { "linear_be",          AUDIO_ENCODING_SLINEAR_BE },
                    155:        { AudioEulinear_be,     AUDIO_ENCODING_ULINEAR_BE },
                    156:        { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
                    157:        { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
                    158:        { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
                    159:        { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
                    160:        { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
                    161:        { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
                    162:        { 0 }
                    163: };
                    164:
                    165: static struct {
1.12      moritz    166:        const char *name;
1.1       provos    167:        u_int prop;
                    168: } props[] = {
                    169:        { "full_duplex",        AUDIO_PROP_FULLDUPLEX },
                    170:        { "mmap",               AUDIO_PROP_MMAP },
                    171:        { "independent",        AUDIO_PROP_INDEPENDENT },
                    172:        { 0 }
                    173: };
                    174:
                    175: struct field *
1.7       pvalchev  176: findfield(char *name)
1.1       provos    177: {
                    178:        int i;
1.15      sobrado   179:        for (i = 0; fields[i].name; i++)
1.1       provos    180:                if (strcmp(fields[i].name, name) == 0)
                    181:                        return &fields[i];
1.7       pvalchev  182:        return (0);
1.1       provos    183: }
                    184:
                    185: void
1.20    ! ratchov   186: prval(u_int format, void *valp)
1.1       provos    187: {
                    188:        u_int v;
1.12      moritz    189:        const char *cm;
1.1       provos    190:        int i;
                    191:
1.20    ! ratchov   192:        switch (format) {
1.1       provos    193:        case STRING:
1.20    ! ratchov   194:                fprintf(out, "%s", (char *)valp);
1.1       provos    195:                break;
                    196:        case INT:
1.20    ! ratchov   197:                fprintf(out, "%d", *(int *)valp);
1.1       provos    198:                break;
                    199:        case UINT:
1.20    ! ratchov   200:                fprintf(out, "%u", *(u_int *)valp);
1.1       provos    201:                break;
                    202:        case XINT:
1.20    ! ratchov   203:                fprintf(out, "0x%x", *(u_int *)valp);
1.1       provos    204:                break;
                    205:        case UCHAR:
1.20    ! ratchov   206:                fprintf(out, "%u", *(u_char *)valp);
1.1       provos    207:                break;
                    208:        case P_R:
1.20    ! ratchov   209:                v = *(u_int *)valp;
1.1       provos    210:                cm = "";
                    211:                if (v & AUMODE_PLAY) {
                    212:                        if (v & AUMODE_PLAY_ALL)
                    213:                                fprintf(out, "play");
                    214:                        else
                    215:                                fprintf(out, "playsync");
                    216:                        cm = ",";
                    217:                }
                    218:                if (v & AUMODE_RECORD)
                    219:                        fprintf(out, "%srecord", cm);
                    220:                break;
                    221:        case ENC:
1.20    ! ratchov   222:                v = *(u_int *)valp;
1.15      sobrado   223:                for (i = 0; encs[i].ename; i++)
1.1       provos    224:                        if (encs[i].eno == v)
                    225:                                break;
                    226:                if (encs[i].ename)
                    227:                        fprintf(out, "%s", encs[i].ename);
                    228:                else
                    229:                        fprintf(out, "%u", v);
                    230:                break;
                    231:        case PROPS:
1.20    ! ratchov   232:                v = *(u_int *)valp;
1.1       provos    233:                for (cm = "", i = 0; props[i].name; i++) {
                    234:                        if (v & props[i].prop) {
                    235:                                fprintf(out, "%s%s", cm, props[i].name);
                    236:                                cm = ",";
                    237:                        }
                    238:                }
                    239:                break;
                    240:        default:
                    241:                errx(1, "Invalid print format.");
                    242:        }
                    243: }
                    244:
                    245: void
1.20    ! ratchov   246: prfield(struct field *p, const char *sep)
        !           247: {
        !           248:        if (sep) {
        !           249:                fprintf(out, "%s", p->name);
        !           250:                if (p->flags & SET) {
        !           251:                        fprintf(out, "%s", ": ");
        !           252:                        prval(p->format, &p->oldval);
        !           253:                        fprintf(out, " -> ");
        !           254:                } else
        !           255:                        fprintf(out, "%s", sep);
        !           256:        }
        !           257:        prval(p->format, p->valp);
        !           258:        fprintf(out, "\n");
        !           259: }
        !           260:
        !           261: void
1.7       pvalchev  262: rdfield(struct field *p, char *q)
1.1       provos    263: {
                    264:        int i;
                    265:        u_int u;
                    266:
1.15      sobrado   267:        switch (p->format) {
1.1       provos    268:        case UINT:
1.20    ! ratchov   269:                p->oldval = *(u_int *)p->valp;
        !           270:                if (sscanf(q, "%u", (unsigned int *)p->valp) != 1) {
1.1       provos    271:                        warnx("Bad number %s", q);
1.20    ! ratchov   272:                        return;
        !           273:                }
1.1       provos    274:                break;
                    275:        case UCHAR:
1.20    ! ratchov   276:                *(char *)&p->oldval = *(u_char *)p->valp;
        !           277:                if (sscanf(q, "%u", &u) != 1) {
1.1       provos    278:                        warnx("Bad number %s", q);
1.20    ! ratchov   279:                        return;
        !           280:                }
        !           281:                *(u_char *)p->valp = u;
1.1       provos    282:                break;
                    283:        case XINT:
1.20    ! ratchov   284:                p->oldval = *(u_int *)p->valp;
1.1       provos    285:                if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
1.20    ! ratchov   286:                    sscanf(q, "%x", (unsigned int *)p->valp) != 1) {
1.1       provos    287:                        warnx("Bad number %s", q);
1.20    ! ratchov   288:                        return;
        !           289:                }
1.1       provos    290:                break;
                    291:        case ENC:
1.20    ! ratchov   292:                p->oldval = *(u_int *)p->valp;
1.15      sobrado   293:                for (i = 0; encs[i].ename; i++)
1.1       provos    294:                        if (strcmp(encs[i].ename, q) == 0)
                    295:                                break;
                    296:                if (encs[i].ename)
                    297:                        *(u_int*)p->valp = encs[i].eno;
1.20    ! ratchov   298:                else {
1.1       provos    299:                        warnx("Unknown encoding: %s", q);
1.20    ! ratchov   300:                        return;
        !           301:                }
1.1       provos    302:                break;
                    303:        default:
                    304:                errx(1, "Invalid read format.");
                    305:        }
                    306:        p->flags |= SET;
                    307: }
                    308:
                    309: void
1.7       pvalchev  310: getinfo(int fd)
1.1       provos    311: {
1.7       pvalchev  312:        int pos = 0, i = 0;
1.1       provos    313:
                    314:        if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
                    315:                err(1, "AUDIO_GETDEV");
1.15      sobrado   316:        for (;;) {
1.9       deraadt   317:                audio_encoding_t enc;
                    318:                enc.index = i++;
                    319:                if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
                    320:                        break;
                    321:                if (pos)
                    322:                        encbuf[pos++] = ',';
                    323:                snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
                    324:                    enc.name, enc.precision,
                    325:                    enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
                    326:                pos += strlen(encbuf+pos);
1.1       provos    327:        }
                    328:        if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
                    329:                err(1, "AUDIO_GETFD");
                    330:        if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
                    331:                err(1, "AUDIO_GETPROPS");
1.18      jakemsr   332:        if (ioctl(fd, AUDIO_PERROR, &perrors) < 0)
                    333:                err(1, "AUDIO_PERROR");
                    334:        if (ioctl(fd, AUDIO_RERROR, &rerrors) < 0)
1.1       provos    335:                err(1, "AUDIO_RERROR");
                    336:        if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
                    337:                err(1, "AUDIO_GETINFO");
                    338: }
                    339:
                    340: void
1.7       pvalchev  341: usage(void)
1.1       provos    342: {
1.7       pvalchev  343:        extern char *__progname;                /* from crt0.o */
                    344:
                    345:        fprintf(stderr,
1.16      deraadt   346:            "usage: %s [-an] [-f file]\n"
1.15      sobrado   347:            "       %s [-n] [-f file] name ...\n"
                    348:            "       %s [-n] [-f file] name=value ...\n",
                    349:            __progname, __progname, __progname);
1.7       pvalchev  350:
1.1       provos    351:        exit(1);
                    352: }
                    353:
                    354: int
1.7       pvalchev  355: main(int argc, char **argv)
1.1       provos    356: {
                    357:        int fd, i, ch;
1.11      vincent   358:        int aflag = 0, canwrite, writeinfo = 0;
1.1       provos    359:        struct stat dstat, ostat;
1.11      vincent   360:        struct field *p;
1.12      moritz    361:        const char *file;
                    362:        const char *sep = "=";
1.1       provos    363:
1.7       pvalchev  364:        if ((file = getenv("AUDIOCTLDEVICE")) == 0 || *file == '\0')
1.3       provos    365:                file = "/dev/audioctl";
1.1       provos    366:
                    367:        while ((ch = getopt(argc, argv, "af:nw")) != -1) {
1.15      sobrado   368:                switch (ch) {
1.1       provos    369:                case 'a':
                    370:                        aflag++;
                    371:                        break;
                    372:                case 'w':
1.11      vincent   373:                        /* backward compatibility */
1.1       provos    374:                        break;
                    375:                case 'n':
                    376:                        sep = 0;
                    377:                        break;
                    378:                case 'f':
                    379:                        file = optarg;
                    380:                        break;
                    381:                default:
                    382:                        usage();
                    383:                }
                    384:        }
                    385:        argc -= optind;
                    386:        argv += optind;
1.16      deraadt   387:
                    388:        if (argc == 0)
                    389:                aflag++;
1.11      vincent   390:
                    391:        if ((fd = open(file, O_RDWR)) < 0) {
                    392:                if ((fd = open(file, O_RDONLY)) < 0)
                    393:                        err(1, "%s", file);
                    394:                canwrite = 0;
                    395:        } else
                    396:                canwrite = 1;
1.1       provos    397:
                    398:        /* Check if stdout is the same device as the audio device. */
                    399:        if (fstat(fd, &dstat) < 0)
                    400:                err(1, "fstat au");
                    401:        if (fstat(STDOUT_FILENO, &ostat) < 0)
                    402:                err(1, "fstat stdout");
                    403:        if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
                    404:            major(dstat.st_dev) == major(ostat.st_dev) &&
                    405:            minor(dstat.st_dev) == minor(ostat.st_dev))
                    406:                /* We can't write to stdout so use stderr */
                    407:                out = stderr;
                    408:
1.11      vincent   409:        if (!argc && !aflag)
                    410:                usage();
                    411:
                    412:        getinfo(fd);
1.1       provos    413:
1.11      vincent   414:        if (aflag) {
                    415:                for (i = 0; fields[i].name; i++) {
1.1       provos    416:                        if (!(fields[i].flags & ALIAS)) {
                    417:                                prfield(&fields[i], sep);
                    418:                        }
                    419:                }
1.11      vincent   420:        } else {
                    421:                while (argc--) {
                    422:                        char *q;
1.1       provos    423:
1.11      vincent   424:                        if ((q = strchr(*argv, '=')) != NULL) {
                    425:                                *q++ = 0;
                    426:                                p = findfield(*argv);
                    427:                                if (p == 0)
                    428:                                        warnx("field `%s' does not exist", *argv);
                    429:                                else {
                    430:                                        if (!canwrite)
                    431:                                                errx(1, "%s: permission denied",
                    432:                                                    *argv);
                    433:                                        if (p->flags & READONLY)
                    434:                                                warnx("`%s' is read only", *argv);
1.1       provos    435:                                        else {
1.11      vincent   436:                                                rdfield(p, q);
                    437:                                                if (p->valp == &fullduplex)
                    438:                                                        if (ioctl(fd, AUDIO_SETFD,
                    439:                                                            &fullduplex) < 0)
                    440:                                                                err(1, "set failed");
1.1       provos    441:                                        }
1.11      vincent   442:                                        writeinfo = 1;
1.1       provos    443:                                }
1.11      vincent   444:                        } else {
1.1       provos    445:                                p = findfield(*argv);
1.11      vincent   446:                                if (p == 0)
                    447:                                        warnx("field %s does not exist", *argv);
                    448:                                else {
1.1       provos    449:                                        prfield(p, sep);
                    450:                                }
1.11      vincent   451:                        }
                    452:                        argv++;
                    453:                }
                    454:                if (writeinfo && ioctl(fd, AUDIO_SETINFO, &info) < 0)
                    455:                        err(1, "set failed");
1.20    ! ratchov   456:                getinfo(fd);
        !           457:                for (i = 0; fields[i].name; i++) {
        !           458:                        if (fields[i].flags & SET) {
        !           459:                                prfield(&fields[i], sep);
1.1       provos    460:                        }
                    461:                }
1.11      vincent   462:        }
1.1       provos    463:        exit(0);
                    464: }