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

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

1.23    ! deraadt     1: /*     $OpenBSD: mixerctl.c,v 1.22 2005/10/01 17:07:26 deraadt Exp $   */
1.3       provos      2: /*     $NetBSD: mixerctl.c,v 1.11 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, with some code and ideas from Chuck Cranor.
                      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:  */
1.9       pvalchev   38:
                     39: /*
                     40:  * mixerctl(1) - a program to control audio mixing.
                     41:  */
                     42:
1.19      otto       43: #include <sys/types.h>
                     44: #include <sys/ioctl.h>
                     45: #include <sys/audioio.h>
                     46:
                     47: #include <err.h>
1.20      millert    48: #include <errno.h>
1.19      otto       49: #include <fcntl.h>
1.20      millert    50: #include <limits.h>
1.1       provos     51: #include <stdio.h>
                     52: #include <stdlib.h>
1.19      otto       53: #include <string.h>
1.1       provos     54: #include <unistd.h>
                     55:
1.20      millert    56: struct field *findfield(char *);
                     57: void adjlevel(char **, u_char *, int);
1.19      otto       58: void catstr(char *, char *, char *);
                     59: void prfield(struct field *, char *, int);
1.21      millert    60: void rdfield(int, struct field *, char *, int);
1.19      otto       61: __dead void usage(void);
1.1       provos     62:
1.19      otto       63: #define FIELD_NAME_MAX 64
1.1       provos     64:
                     65: struct field {
1.19      otto       66:        char name[FIELD_NAME_MAX];
1.1       provos     67:        mixer_ctrl_t *valp;
                     68:        mixer_devinfo_t *infp;
                     69: } *fields, *rfields;
                     70:
                     71: mixer_ctrl_t *values;
                     72: mixer_devinfo_t *infos;
                     73:
1.19      otto       74: void
                     75: catstr(char *p, char *q, char *out)
1.1       provos     76: {
1.19      otto       77:        char tmp[FIELD_NAME_MAX];
1.8       deraadt    78:
1.19      otto       79:        snprintf(tmp, FIELD_NAME_MAX, "%s.%s", p, q);
                     80:        strlcpy(out, tmp, FIELD_NAME_MAX);
1.1       provos     81: }
                     82:
                     83: struct field *
1.9       pvalchev   84: findfield(char *name)
1.1       provos     85: {
                     86:        int i;
1.19      otto       87:        for (i = 0; fields[i].name[0] != '\0'; i++)
1.1       provos     88:                if (strcmp(fields[i].name, name) == 0)
                     89:                        return &fields[i];
1.9       pvalchev   90:        return (0);
1.1       provos     91: }
                     92:
1.19      otto       93: #define e_member_name  un.e.member[i].label.name
                     94: #define s_member_name  un.s.member[i].label.name
                     95:
1.1       provos     96: void
1.9       pvalchev   97: prfield(struct field *p, char *sep, int prvalset)
1.1       provos     98: {
                     99:        mixer_ctrl_t *m;
                    100:        int i, n;
                    101:
                    102:        if (sep)
1.19      otto      103:                printf("%s%s", p->name, sep);
1.1       provos    104:        m = p->valp;
1.19      otto      105:        switch (m->type) {
1.1       provos    106:        case AUDIO_MIXER_ENUM:
1.18      millert   107:                for (i = 0; i < p->infp->un.e.num_mem; i++)
1.1       provos    108:                        if (p->infp->un.e.member[i].ord == m->un.ord)
1.19      otto      109:                                printf("%s",
                    110:                                        p->infp->e_member_name);
1.1       provos    111:                if (prvalset) {
1.19      otto      112:                        printf("  [ ");
1.18      millert   113:                        for (i = 0; i < p->infp->un.e.num_mem; i++)
1.19      otto      114:                                printf("%s ", p->infp->e_member_name);
                    115:                        printf("]");
1.1       provos    116:                }
                    117:                break;
                    118:        case AUDIO_MIXER_SET:
1.18      millert   119:                for (n = i = 0; i < p->infp->un.s.num_mem; i++)
1.1       provos    120:                        if (m->un.mask & p->infp->un.s.member[i].mask)
1.19      otto      121:                                printf("%s%s", n++ ? "," : "",
                    122:                                                p->infp->s_member_name);
1.1       provos    123:                if (prvalset) {
1.19      otto      124:                        printf("  { ");
1.18      millert   125:                        for (i = 0; i < p->infp->un.s.num_mem; i++)
1.19      otto      126:                                printf("%s ", p->infp->s_member_name);
                    127:                        printf("}");
1.1       provos    128:                }
                    129:                break;
                    130:        case AUDIO_MIXER_VALUE:
                    131:                if (m->un.value.num_channels == 1)
1.19      otto      132:                        printf("%d", m->un.value.level[0]);
1.1       provos    133:                else
1.19      otto      134:                        printf("%d,%d", m->un.value.level[0],
1.22      deraadt   135:                            m->un.value.level[1]);
1.1       provos    136:                if (prvalset)
1.19      otto      137:                        printf(" %s", p->infp->un.v.units.name);
1.1       provos    138:                break;
                    139:        default:
                    140:                errx(1, "Invalid format.");
                    141:        }
                    142: }
                    143:
1.19      otto      144: void
1.20      millert   145: adjlevel(char **p, u_char *olevel, int more)
                    146: {
                    147:        char *ep, *cp = *p;
                    148:        long inc;
                    149:        u_char level;
                    150:
                    151:        if (*cp != '+' && *cp != '-')
                    152:                *olevel = 0;            /* absolute setting */
                    153:
                    154:        errno = 0;
                    155:        inc = strtol(cp, &ep, 10);
                    156:        if (*cp == '\0' || (*ep != '\0' && *ep != ',') ||
                    157:            (errno == ERANGE && (inc == LONG_MAX || inc == LONG_MIN)))
                    158:                errx(1, "Bad number %s", cp);
                    159:        if (*ep == ',' && !more)
                    160:                errx(1, "Too many values");
                    161:        *p = ep;
                    162:
                    163:        if (inc < AUDIO_MIN_GAIN - *olevel)
                    164:                level = AUDIO_MIN_GAIN;
                    165:        else if (inc > AUDIO_MAX_GAIN - *olevel)
                    166:                level = AUDIO_MAX_GAIN;
                    167:        else
                    168:                level = *olevel + inc;
                    169:        *olevel = level;
                    170: }
                    171:
                    172: void
1.21      millert   173: rdfield(int fd, struct field *p, char *q, int quiet)
1.1       provos    174: {
1.19      otto      175:        mixer_ctrl_t *m, oldval;
1.20      millert   176:        int i, mask;
1.1       provos    177:        char *s;
                    178:
1.19      otto      179:        oldval = *p->valp;
1.1       provos    180:        m = p->valp;
1.19      otto      181:
                    182:        switch (m->type) {
1.1       provos    183:        case AUDIO_MIXER_ENUM:
1.22      deraadt   184:                if (strcmp(q, "toggle") == 0) {
                    185:                        for (i = 0; i < p->infp->un.e.num_mem; i++) {
                    186:                                if (m->un.ord == p->infp->un.e.member[i].ord)
                    187:                                        break;
                    188:                        }
                    189:                        if (i < p->infp->un.e.num_mem)
                    190:                                i++;
                    191:                        else
                    192:                                i = 0;
                    193:                        m->un.ord = p->infp->un.e.member[i].ord;
                    194:                        break;
                    195:                }
1.18      millert   196:                for (i = 0; i < p->infp->un.e.num_mem; i++)
1.19      otto      197:                        if (strcmp(p->infp->e_member_name, q) == 0)
1.1       provos    198:                                break;
                    199:                if (i < p->infp->un.e.num_mem)
                    200:                        m->un.ord = p->infp->un.e.member[i].ord;
1.9       pvalchev  201:                else
1.22      deraadt   202:                        errx(1, "Bad enum value %s", q);
1.1       provos    203:                break;
                    204:        case AUDIO_MIXER_SET:
                    205:                mask = 0;
1.20      millert   206:                for (; q && *q; q = s) {
1.19      otto      207:                        if ((s = strchr(q, ',')) != NULL)
1.1       provos    208:                                *s++ = 0;
1.4       millert   209:                        for (i = 0; i < p->infp->un.s.num_mem; i++)
1.19      otto      210:                                if (strcmp(p->infp->s_member_name, q) == 0)
1.1       provos    211:                                        break;
1.9       pvalchev  212:                        if (i < p->infp->un.s.num_mem)
1.1       provos    213:                                mask |= p->infp->un.s.member[i].mask;
1.9       pvalchev  214:                        else
1.22      deraadt   215:                                errx(1, "Bad set value %s", q);
1.1       provos    216:                }
                    217:                m->un.mask = mask;
                    218:                break;
                    219:        case AUDIO_MIXER_VALUE:
                    220:                if (m->un.value.num_channels == 1) {
1.20      millert   221:                        adjlevel(&q, &m->un.value.level[0], 0);
1.1       provos    222:                } else {
1.20      millert   223:                        adjlevel(&q, &m->un.value.level[0], 1);
                    224:                        if (*q++ == ',')
                    225:                                adjlevel(&q, &m->un.value.level[1], 0);
                    226:                        else
                    227:                                m->un.value.level[1] = m->un.value.level[0];
1.1       provos    228:                }
                    229:                break;
                    230:        default:
                    231:                errx(1, "Invalid format.");
                    232:        }
1.19      otto      233:
                    234:        if (ioctl(fd, AUDIO_MIXER_WRITE, p->valp) < 0) {
                    235:                warn("AUDIO_MIXER_WRITE");
1.21      millert   236:        } else if (!quiet) {
1.19      otto      237:                *p->valp = oldval;
                    238:                prfield(p, ": ", 0);
                    239:                if (ioctl(fd, AUDIO_MIXER_READ, p->valp) < 0) {
                    240:                        warn("AUDIO_MIXER_READ");
                    241:                } else {
                    242:                        printf(" -> ");
                    243:                        prfield(p, NULL, 0);
                    244:                        printf("\n");
                    245:                }
                    246:        }
1.1       provos    247: }
                    248:
                    249: int
1.9       pvalchev  250: main(int argc, char **argv)
1.1       provos    251: {
                    252:        int fd, i, j, ch, pos;
1.22      deraadt   253:        int aflag = 0, qflag = 0, vflag = 0, tflag = 0;
1.3       provos    254:        char *file;
1.1       provos    255:        char *sep = "=";
                    256:        mixer_devinfo_t dinfo;
1.16      tedu      257:        int ndev;
1.1       provos    258:
1.9       pvalchev  259:        if ((file = getenv("MIXERDEVICE")) == 0 || *file == '\0')
1.22      deraadt   260:                file = "/dev/mixer";
1.1       provos    261:
1.22      deraadt   262:        while ((ch = getopt(argc, argv, "af:nqtvw")) != -1) {
1.1       provos    263:                switch(ch) {
                    264:                case 'a':
                    265:                        aflag++;
                    266:                        break;
                    267:                case 'w':
1.14      tedu      268:                        /* compat */
1.1       provos    269:                        break;
                    270:                case 'v':
                    271:                        vflag++;
                    272:                        break;
                    273:                case 'n':
                    274:                        sep = 0;
                    275:                        break;
                    276:                case 'f':
                    277:                        file = optarg;
                    278:                        break;
1.10      jfb       279:                case 'q':
                    280:                        qflag = 1;
                    281:                        break;
1.22      deraadt   282:                case 't':
                    283:                        tflag = 1;
                    284:                        break;
1.1       provos    285:                case '?':
                    286:                default:
1.9       pvalchev  287:                        usage();
1.1       provos    288:                }
                    289:        }
                    290:        argc -= optind;
                    291:        argv += optind;
1.6       mickey    292:
1.14      tedu      293:        if ((fd = open(file, O_RDWR)) == -1)
                    294:                if ((fd = open(file, O_RDONLY)) == -1)
                    295:                        err(1, "%s", file);
1.1       provos    296:
1.18      millert   297:        for (ndev = 0; ; ndev++) {
1.16      tedu      298:                dinfo.index = ndev;
1.14      tedu      299:                if (ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo) < 0)
                    300:                        break;
1.1       provos    301:        }
1.5       mickey    302:
1.9       pvalchev  303:        if (!ndev)
1.5       mickey    304:                errx(1, "no mixer devices configured");
                    305:
1.9       pvalchev  306:        if ((rfields = calloc(ndev, sizeof *rfields)) == NULL ||
                    307:            (fields = calloc(ndev, sizeof *fields)) == NULL ||
                    308:            (infos = calloc(ndev, sizeof *infos)) == NULL ||
                    309:            (values = calloc(ndev, sizeof *values)) == NULL)
                    310:                err(1, "calloc()");
1.1       provos    311:
1.18      millert   312:        for (i = 0; i < ndev; i++) {
1.1       provos    313:                infos[i].index = i;
1.17      millert   314:                if (ioctl(fd, AUDIO_MIXER_DEVINFO, &infos[i]) < 0) {
1.19      otto      315:                        ndev--;
                    316:                        i--;
1.17      millert   317:                        continue;
                    318:                }
1.1       provos    319:        }
                    320:
1.18      millert   321:        for (i = 0; i < ndev; i++) {
1.19      otto      322:                strlcpy(rfields[i].name, infos[i].label.name, FIELD_NAME_MAX);
1.1       provos    323:                rfields[i].valp = &values[i];
                    324:                rfields[i].infp = &infos[i];
                    325:        }
                    326:
1.18      millert   327:        for (i = 0; i < ndev; i++) {
1.1       provos    328:                values[i].dev = i;
                    329:                values[i].type = infos[i].type;
                    330:                if (infos[i].type != AUDIO_MIXER_CLASS) {
                    331:                        values[i].un.value.num_channels = 2;
                    332:                        if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0) {
                    333:                                values[i].un.value.num_channels = 1;
                    334:                                if (ioctl(fd, AUDIO_MIXER_READ, &values[i]) < 0)
                    335:                                        err(1, "AUDIO_MIXER_READ");
                    336:                        }
                    337:                }
                    338:        }
                    339:
1.18      millert   340:        for (j = i = 0; i < ndev; i++) {
1.1       provos    341:                if (infos[i].type != AUDIO_MIXER_CLASS &&
                    342:                    infos[i].type != -1) {
                    343:                        fields[j++] = rfields[i];
1.18      millert   344:                        for (pos = infos[i].next; pos != AUDIO_MIXER_LAST;
1.1       provos    345:                            pos = infos[pos].next) {
                    346:                                fields[j] = rfields[pos];
1.19      otto      347:                                catstr(rfields[i].name, infos[pos].label.name,
                    348:                                    fields[j].name);
1.1       provos    349:                                infos[pos].type = -1;
                    350:                                j++;
                    351:                        }
                    352:                }
                    353:        }
                    354:
1.18      millert   355:        for (i = 0; i < j; i++) {
1.1       provos    356:                int cls = fields[i].infp->mixer_class;
                    357:                if (cls >= 0 && cls < ndev)
1.19      otto      358:                        catstr(infos[cls].label.name, fields[i].name,
                    359:                            fields[i].name);
1.1       provos    360:        }
                    361:
1.14      tedu      362:        if (!argc && aflag) {
1.19      otto      363:                for (i = 0; fields[i].name[0] != '\0'; i++) {
1.1       provos    364:                        prfield(&fields[i], sep, vflag);
1.19      otto      365:                        printf("\n");
1.1       provos    366:                }
                    367:        } else if (argc > 0 && !aflag) {
                    368:                struct field *p;
1.14      tedu      369:
1.19      otto      370:                while (argc--) {
1.14      tedu      371:                        char *q;
                    372:
1.19      otto      373:                        ch = 0;
                    374:                        if ((q = strchr(*argv, '=')) != NULL) {
                    375:                                *q++ = '\0';
                    376:                                ch = 1;
                    377:                        }
                    378:
                    379:                        if ((p = findfield(*argv)) == NULL) {
                    380:                                warnx("field %s does not exist", *argv);
1.22      deraadt   381:                        } else if (ch || tflag) {
                    382:                                if (tflag && q == NULL)
                    383:                                        q = "toggle";
1.21      millert   384:                                rdfield(fd, p, q, qflag);
1.14      tedu      385:                        } else {
1.19      otto      386:                                prfield(p, sep, vflag);
                    387:                                printf("\n");
1.1       provos    388:                        }
1.19      otto      389:
1.17      millert   390:                        argv++;
1.1       provos    391:                }
                    392:        } else
1.9       pvalchev  393:                usage();
1.1       provos    394:        exit(0);
1.9       pvalchev  395: }
                    396:
1.19      otto      397: __dead void
1.9       pvalchev  398: usage(void)
                    399: {
                    400:        extern char *__progname;        /* from crt0.o */
                    401:
                    402:        fprintf(stderr,
1.13      jmc       403:            "usage: %s [-nv] [-f file] -a\n"
                    404:            "       %s [-nv] [-f file] name [...]\n"
1.22      deraadt   405:            "       %s [-qt] [-f file] name [...]\n"
1.14      tedu      406:            "       %s [-q]  [-f file] name=value [...]\n",
1.23    ! deraadt   407:            __progname, __progname, __progname, __progname);
1.9       pvalchev  408:
                    409:        exit(1);
1.1       provos    410: }