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

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