2 * Copyright (c) 2021 Christos Margiolis <christos@FreeBSD.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 static void usage(void) __dead2;
40 static void initctls(struct mixer *);
41 static void printall(struct mixer *, int);
42 static void printminfo(struct mixer *, int);
43 static void printdev(struct mixer *, int);
44 static void printrecsrc(struct mixer *, int); /* XXX: change name */
45 static int set_dunit(struct mixer *, int);
46 /* Control handlers */
47 static int mod_volume(struct mix_dev *, void *);
48 static int mod_mute(struct mix_dev *, void *);
49 static int mod_recsrc(struct mix_dev *, void *);
50 static int print_volume(struct mix_dev *, void *);
51 static int print_mute(struct mix_dev *, void *);
52 static int print_recsrc(struct mix_dev *, void *);
55 main(int argc, char *argv[])
59 char *name = NULL, buf[NAME_MAX];
60 char *p, *q, *devstr, *ctlstr, *valstr = NULL;
61 int dunit, i, n, pall = 1, shorthand;
62 int aflag = 0, dflag = 0, oflag = 0, sflag = 0;
65 while ((ch = getopt(argc, argv, "ad:f:hos")) != -1) {
71 dunit = strtol(optarg, NULL, 10);
72 if (errno == EINVAL || errno == ERANGE)
85 case 'h': /* FALLTHROUGH */
94 /* Print all mixers and exit. */
96 if ((n = mixer_get_nmixers()) < 0)
97 err(1, "mixer_get_nmixers");
98 for (i = 0; i < n; i++) {
99 (void)snprintf(buf, sizeof(buf), "/dev/mixer%d", i);
100 if ((m = mixer_open(buf)) == NULL)
101 err(1, "mixer_open: %s", buf);
104 printrecsrc(m, oflag);
110 (void)mixer_close(m);
115 if ((m = mixer_open(name)) == NULL)
116 err(1, "mixer_open: %s", name);
120 if (dflag && set_dunit(m, dunit) < 0)
123 printrecsrc(m, oflag);
124 (void)mixer_close(m);
130 if ((p = strdup(*argv)) == NULL)
131 err(1, "strdup(%s)", *argv);
133 /* Check if we're using the shorthand syntax for volume setting. */
135 for (q = p; *q != '\0'; q++) {
138 shorthand = ((*q >= '0' && *q <= '9') ||
139 *q == '+' || *q == '-' || *q == '.');
141 } else if (*q == '.')
145 /* Split the string into device, control and value. */
146 devstr = strsep(&p, ".=");
147 if ((m->dev = mixer_get_dev_byname(m, devstr)) == NULL) {
148 warnx("%s: no such device", devstr);
156 } else if (shorthand) {
158 * Input: `dev=N` -> shorthand for `dev.volume=N`.
160 * We don't care what the rest of the string contains as
161 * long as we're sure the very beginning is right,
162 * mod_volume() will take care of parsing it properly.
164 cp = mixer_get_ctl(m->dev, C_VOL);
165 cp->mod(cp->parent_dev, p);
168 ctlstr = strsep(&p, "=");
169 if ((cp = mixer_get_ctl_byname(m->dev, ctlstr)) == NULL) {
170 warnx("%s.%s: no such control", devstr, ctlstr);
173 /* Input: `dev.control`. */
175 (void)cp->print(cp->parent_dev, cp->name);
180 /* Input: `dev.control=val`. */
181 cp->mod(cp->parent_dev, valstr);
190 (void)mixer_close(m);
198 fprintf(stderr, "usage: %1$s [-f device] [-d unit] [-os] [dev[.control[=value]]] ...\n"
199 " %1$s [-d unit] [-os] -a\n"
200 " %1$s -h\n", getprogname());
205 initctls(struct mixer *m)
210 TAILQ_FOREACH(dp, &m->devs, devs) {
211 rc += mixer_add_ctl(dp, C_VOL, "volume", mod_volume, print_volume);
212 rc += mixer_add_ctl(dp, C_MUT, "mute", mod_mute, print_mute);
213 rc += mixer_add_ctl(dp, C_SRC, "recsrc", mod_recsrc, print_recsrc);
216 (void)mixer_close(m);
217 err(1, "cannot make controls");
222 printall(struct mixer *m, int oflag)
226 printminfo(m, oflag);
227 TAILQ_FOREACH(dp, &m->devs, devs) {
234 printminfo(struct mixer *m, int oflag)
236 int playrec = MIX_MODE_PLAY | MIX_MODE_REC;
240 printf("%s:", m->mi.name);
241 if (*m->ci.longname != '\0')
242 printf(" <%s>", m->ci.longname);
243 if (*m->ci.hw_info != '\0')
244 printf(" %s", m->ci.hw_info);
248 if (m->mode & MIX_MODE_PLAY)
250 if ((m->mode & playrec) == playrec)
252 if (m->mode & MIX_MODE_REC)
258 printf(" (default)");
263 printdev(struct mixer *m, int oflag)
265 struct mix_dev *d = m->dev;
269 printf(" %-10s= %.2f:%.2f ",
270 d->name, d->vol.left, d->vol.right);
271 if (!MIX_ISREC(m, d->devno))
273 if (MIX_ISREC(m, d->devno))
275 if (MIX_ISRECSRC(m, d->devno))
277 if (MIX_ISMUTE(m, d->devno))
281 TAILQ_FOREACH(cp, &d->ctls, ctls) {
282 (void)cp->print(cp->parent_dev, cp->name);
288 printrecsrc(struct mixer *m, int oflag)
296 printf("%s: ", m->mi.name);
297 TAILQ_FOREACH(dp, &m->devs, devs) {
298 if (MIX_ISRECSRC(m, dp->devno)) {
301 printf("%s", dp->name);
304 mixer_get_ctl(dp, C_SRC)->name, n ? " " : "");
311 set_dunit(struct mixer *m, int dunit)
315 if ((n = mixer_get_dunit()) < 0) {
316 warn("cannot get default unit");
319 if (mixer_set_dunit(m, dunit) < 0) {
320 warn("cannot set default unit to: %d", dunit);
323 printf("default_unit: %d -> %d\n", n, dunit);
329 mod_volume(struct mix_dev *d, void *p)
335 char *endp, lstr[8], rstr[8];
336 float lprev, rprev, lrel, rrel;
340 cp = mixer_get_ctl(m->dev, C_VOL);
342 n = sscanf(val, "%7[^:]:%7s", lstr, rstr);
344 warnx("invalid volume value: %s", val);
349 if (*lstr == '+' || *lstr == '-')
351 v.left = strtof(lstr, &endp);
352 if (*endp != '\0' && (*endp != '%' || *(endp + 1) != '\0')) {
353 warnx("invalid volume value: %s", lstr);
361 if (*rstr == '+' || *rstr == '-')
363 v.right = strtof(rstr, &endp);
364 if (*endp != '\0' && (*endp != '%' || *(endp + 1) != '\0')) {
365 warnx("invalid volume value: %s", rstr);
374 v.right = v.left; /* FALLTHROUGH */
378 v.left += m->dev->vol.left;
380 v.right += m->dev->vol.right;
382 if (v.left < MIX_VOLMIN)
384 else if (v.left > MIX_VOLMAX)
386 if (v.right < MIX_VOLMIN)
387 v.right = MIX_VOLMIN;
388 else if (v.right > MIX_VOLMAX)
389 v.right = MIX_VOLMAX;
391 lprev = m->dev->vol.left;
392 rprev = m->dev->vol.right;
393 if (mixer_set_vol(m, v) < 0)
394 warn("%s.%s=%.2f:%.2f",
395 m->dev->name, cp->name, v.left, v.right);
397 printf("%s.%s: %.2f:%.2f -> %.2f:%.2f\n",
398 m->dev->name, cp->name, lprev, rprev, v.left, v.right);
405 mod_mute(struct mix_dev *d, void *p)
413 cp = mixer_get_ctl(m->dev, C_MUT);
423 opt = MIX_TOGGLEMUTE;
426 warnx("%c: no such modifier", *val);
429 n = MIX_ISMUTE(m, m->dev->devno);
430 if (mixer_set_mute(m, opt) < 0)
431 warn("%s.%s=%c", m->dev->name, cp->name, *val);
433 printf("%s.%s: %d -> %d\n",
434 m->dev->name, cp->name, n, MIX_ISMUTE(m, m->dev->devno));
440 mod_recsrc(struct mix_dev *d, void *p)
448 cp = mixer_get_ctl(m->dev, C_SRC);
455 opt = MIX_REMOVERECSRC;
461 opt = MIX_TOGGLERECSRC;
464 warnx("%c: no such modifier", *val);
467 n = MIX_ISRECSRC(m, m->dev->devno);
468 if (mixer_mod_recsrc(m, opt) < 0)
469 warn("%s.%s=%c", m->dev->name, cp->name, *val);
471 printf("%s.%s: %d -> %d\n",
472 m->dev->name, cp->name, n, MIX_ISRECSRC(m, m->dev->devno));
478 print_volume(struct mix_dev *d, void *p)
480 struct mixer *m = d->parent_mixer;
481 const char *ctl_name = p;
483 printf("%s.%s=%.2f:%.2f\n",
484 m->dev->name, ctl_name, m->dev->vol.left, m->dev->vol.right);
490 print_mute(struct mix_dev *d, void *p)
492 struct mixer *m = d->parent_mixer;
493 const char *ctl_name = p;
495 printf("%s.%s=%d\n", m->dev->name, ctl_name, MIX_ISMUTE(m, m->dev->devno));
501 print_recsrc(struct mix_dev *d, void *p)
503 struct mixer *m = d->parent_mixer;
504 const char *ctl_name = p;
506 if (!MIX_ISRECSRC(m, m->dev->devno))
508 printf("%s.%s=+\n", m->dev->name, ctl_name);