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

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