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 /* Control handlers */
46 static int mod_dunit(struct mix_dev *, void *);
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 *);
54 static const mix_ctl_t ctl_dunit = {
57 .name = "default_unit",
63 main(int argc, char *argv[])
67 char *name = NULL, buf[NAME_MAX];
68 char *p, *q, *devstr, *ctlstr, *valstr = NULL;
69 int dunit, i, n, pall = 1, shorthand;
70 int aflag = 0, dflag = 0, oflag = 0, sflag = 0;
73 while ((ch = getopt(argc, argv, "ad:f:hos")) != -1) {
79 dunit = strtol(optarg, NULL, 10);
80 if (errno == EINVAL || errno == ERANGE)
93 case 'h': /* FALLTHROUGH */
102 /* Print all mixers and exit. */
104 if ((n = mixer_get_nmixers()) < 0)
105 err(1, "mixer_get_nmixers");
106 for (i = 0; i < n; i++) {
107 (void)snprintf(buf, sizeof(buf), "/dev/mixer%d", i);
108 if ((m = mixer_open(buf)) == NULL)
109 err(1, "mixer_open: %s", buf);
112 printrecsrc(m, oflag);
118 (void)mixer_close(m);
123 if ((m = mixer_open(name)) == NULL)
124 err(1, "mixer_open: %s", name);
128 if (dflag && ctl_dunit.mod(m->dev, &dunit) < 0)
131 printrecsrc(m, oflag);
132 (void)mixer_close(m);
138 if ((p = strdup(*argv)) == NULL)
139 err(1, "strdup(%s)", *argv);
141 /* Check if we're using the shorthand syntax for volume setting. */
143 for (q = p; *q != '\0'; q++) {
146 shorthand = ((*q >= '0' && *q <= '9') ||
147 *q == '+' || *q == '-' || *q == '.');
149 } else if (*q == '.')
153 /* Split the string into device, control and value. */
154 devstr = strsep(&p, ".=");
155 if ((m->dev = mixer_get_dev_byname(m, devstr)) == NULL) {
156 warnx("%s: no such device", devstr);
164 } else if (shorthand) {
166 * Input: `dev=N` -> shorthand for `dev.volume=N`.
168 * We don't care what the rest of the string contains as
169 * long as we're sure the very beginning is right,
170 * mod_volume() will take care of parsing it properly.
172 cp = mixer_get_ctl(m->dev, C_VOL);
173 cp->mod(cp->parent_dev, p);
176 ctlstr = strsep(&p, "=");
177 if ((cp = mixer_get_ctl_byname(m->dev, ctlstr)) == NULL) {
178 warnx("%s.%s: no such control", devstr, ctlstr);
181 /* Input: `dev.control`. */
183 (void)cp->print(cp->parent_dev, cp->name);
188 /* Input: `dev.control=val`. */
189 cp->mod(cp->parent_dev, valstr);
198 (void)mixer_close(m);
206 fprintf(stderr, "usage: %1$s [-f device] [-d unit] [-os] [dev[.control[=value]]] ...\n"
207 " %1$s [-d unit] [-os] -a\n"
208 " %1$s -h\n", getprogname());
213 initctls(struct mixer *m)
218 TAILQ_FOREACH(dp, &m->devs, devs) {
219 rc += mixer_add_ctl(dp, C_VOL, "volume", mod_volume, print_volume);
220 rc += mixer_add_ctl(dp, C_MUT, "mute", mod_mute, print_mute);
221 rc += mixer_add_ctl(dp, C_SRC, "recsrc", mod_recsrc, print_recsrc);
224 (void)mixer_close(m);
225 err(1, "cannot make controls");
230 printall(struct mixer *m, int oflag)
234 printminfo(m, oflag);
235 TAILQ_FOREACH(dp, &m->devs, devs) {
242 printminfo(struct mixer *m, int oflag)
244 int playrec = MIX_MODE_PLAY | MIX_MODE_REC;
248 printf("%s:", m->mi.name);
249 if (*m->ci.longname != '\0')
250 printf(" <%s>", m->ci.longname);
251 if (*m->ci.hw_info != '\0')
252 printf(" %s", m->ci.hw_info);
256 if (m->mode & MIX_MODE_PLAY)
258 if ((m->mode & playrec) == playrec)
260 if (m->mode & MIX_MODE_REC)
266 printf(" (default)");
271 printdev(struct mixer *m, int oflag)
273 struct mix_dev *d = m->dev;
277 printf(" %-10s= %.2f:%.2f ",
278 d->name, d->vol.left, d->vol.right);
279 if (!MIX_ISREC(m, d->devno))
281 if (MIX_ISREC(m, d->devno))
283 if (MIX_ISRECSRC(m, d->devno))
285 if (MIX_ISMUTE(m, d->devno))
289 TAILQ_FOREACH(cp, &d->ctls, ctls) {
290 (void)cp->print(cp->parent_dev, cp->name);
296 printrecsrc(struct mixer *m, int oflag)
304 printf("%s: ", m->mi.name);
305 TAILQ_FOREACH(dp, &m->devs, devs) {
306 if (MIX_ISRECSRC(m, dp->devno)) {
309 printf("%s", dp->name);
312 mixer_get_ctl(dp, C_SRC)->name, n ? " " : "");
319 mod_dunit(struct mix_dev *d, void *p)
321 int dunit = *((int *)p);
324 if ((n = mixer_get_dunit()) < 0) {
325 warn("cannot get default unit");
328 if (mixer_set_dunit(d->parent_mixer, dunit) < 0) {
329 warn("cannot set default unit to: %d", dunit);
332 printf("%s: %d -> %d\n", ctl_dunit.name, n, dunit);
338 mod_volume(struct mix_dev *d, void *p)
344 char lstr[8], rstr[8];
345 float lprev, rprev, lrel, rrel;
349 cp = mixer_get_ctl(m->dev, C_VOL);
351 n = sscanf(val, "%7[^:]:%7s", lstr, rstr);
353 warnx("invalid volume value: %s", val);
358 if (*lstr == '+' || *lstr == '-')
360 v.left = strtof(lstr, NULL);
362 /* be backwards compatible */
363 if (strstr(lstr, ".") == NULL)
367 if (*rstr == '+' || *rstr == '-')
369 v.right = strtof(rstr, NULL);
371 /* be backwards compatible */
372 if (strstr(rstr, ".") == NULL)
377 v.right = v.left; /* FALLTHROUGH */
380 v.left += m->dev->vol.left;
382 v.right += m->dev->vol.right;
384 if (v.left < MIX_VOLMIN)
386 else if (v.left > MIX_VOLMAX)
388 if (v.right < MIX_VOLMIN)
389 v.right = MIX_VOLMIN;
390 else if (v.right > MIX_VOLMAX)
391 v.right = MIX_VOLMAX;
393 lprev = m->dev->vol.left;
394 rprev = m->dev->vol.right;
395 if (mixer_set_vol(m, v) < 0)
396 warn("%s.%s=%.2f:%.2f",
397 m->dev->name, cp->name, v.left, v.right);
399 printf("%s.%s: %.2f:%.2f -> %.2f:%.2f\n",
400 m->dev->name, cp->name, lprev, rprev, v.left, v.right);
407 mod_mute(struct mix_dev *d, void *p)
415 cp = mixer_get_ctl(m->dev, C_MUT);
425 opt = MIX_TOGGLEMUTE;
428 warnx("%c: no such modifier", *val);
431 n = MIX_ISMUTE(m, m->dev->devno);
432 if (mixer_set_mute(m, opt) < 0)
433 warn("%s.%s=%c", m->dev->name, cp->name, *val);
435 printf("%s.%s: %d -> %d\n",
436 m->dev->name, cp->name, n, MIX_ISMUTE(m, m->dev->devno));
442 mod_recsrc(struct mix_dev *d, void *p)
450 cp = mixer_get_ctl(m->dev, C_SRC);
457 opt = MIX_REMOVERECSRC;
463 opt = MIX_TOGGLERECSRC;
466 warnx("%c: no such modifier", *val);
469 n = MIX_ISRECSRC(m, m->dev->devno);
470 if (mixer_mod_recsrc(m, opt) < 0)
471 warn("%s.%s=%c", m->dev->name, cp->name, *val);
473 printf("%s.%s: %d -> %d\n",
474 m->dev->name, cp->name, n, MIX_ISRECSRC(m, m->dev->devno));
480 print_volume(struct mix_dev *d, void *p)
482 struct mixer *m = d->parent_mixer;
483 const char *ctl_name = p;
485 printf("%s.%s=%.2f:%.2f\n",
486 m->dev->name, ctl_name, m->dev->vol.left, m->dev->vol.right);
492 print_mute(struct mix_dev *d, void *p)
494 struct mixer *m = d->parent_mixer;
495 const char *ctl_name = p;
497 printf("%s.%s=%d\n", m->dev->name, ctl_name, MIX_ISMUTE(m, m->dev->devno));
503 print_recsrc(struct mix_dev *d, void *p)
505 struct mixer *m = d->parent_mixer;
506 const char *ctl_name = p;
508 if (!MIX_ISRECSRC(m, m->dev->devno))
510 printf("%s.%s=+\n", m->dev->name, ctl_name);