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

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