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
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/sysctl.h>
36 #define BASEPATH "/dev/mixer"
38 static int _mixer_readvol(struct mix_dev *);
41 * Fetch volume from the device.
44 _mixer_readvol(struct mix_dev *dev)
48 if (ioctl(dev->parent_mixer->fd, MIXER_READ(dev->devno), &v) < 0)
50 dev->vol.left = MIX_VOLNORM(v & 0x00ff);
51 dev->vol.right = MIX_VOLNORM((v >> 8) & 0x00ff);
57 * Open a mixer device in `/dev/mixerN`, where N is the number of the mixer.
58 * Each device maps to an actual pcm audio card, so `/dev/mixer0` is the
59 * mixer for pcm0, and so on.
61 * @param name path to mixer device. NULL or "/dev/mixer" for the
62 * the default mixer (i.e `hw.snd.default_unit`).
65 mixer_open(const char *name)
67 struct mixer *m = NULL;
69 const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
72 if ((m = calloc(1, sizeof(struct mixer))) == NULL)
76 /* `name` does not start with "/dev/mixer". */
77 if (strncmp(name, BASEPATH, strlen(BASEPATH)) != 0) {
80 /* `name` is "/dev/mixer" so, we'll use the default unit. */
81 if (strncmp(name, BASEPATH, strlen(name)) == 0)
83 m->unit = strtol(name + strlen(BASEPATH), NULL, 10);
85 (void)strlcpy(m->name, name, sizeof(m->name));
88 if ((m->unit = mixer_get_dunit()) < 0)
90 (void)snprintf(m->name, sizeof(m->name), "/dev/mixer%d", m->unit);
93 if ((m->fd = open(m->name, O_RDWR)) < 0)
96 m->devmask = m->recmask = m->recsrc = 0;
97 m->f_default = m->unit == mixer_get_dunit();
98 m->mode = mixer_get_mode(m->unit);
99 /* The unit number _must_ be set before the ioctl. */
101 m->ci.card = m->unit;
102 if (ioctl(m->fd, SNDCTL_MIXERINFO, &m->mi) < 0) {
103 memset(&m->mi, 0, sizeof(m->mi));
104 strlcpy(m->mi.name, m->name, sizeof(m->mi.name));
106 if (ioctl(m->fd, SNDCTL_CARDINFO, &m->ci) < 0)
107 memset(&m->ci, 0, sizeof(m->ci));
108 if (ioctl(m->fd, SOUND_MIXER_READ_DEVMASK, &m->devmask) < 0 ||
109 ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0 ||
110 ioctl(m->fd, SOUND_MIXER_READ_RECMASK, &m->recmask) < 0 ||
111 ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
114 TAILQ_INIT(&m->devs);
115 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
116 if (!MIX_ISDEV(m, i))
118 if ((dp = calloc(1, sizeof(struct mix_dev))) == NULL)
120 dp->parent_mixer = m;
123 if (_mixer_readvol(dp) < 0)
125 (void)strlcpy(dp->name, names[i], sizeof(dp->name));
126 TAILQ_INIT(&dp->ctls);
127 TAILQ_INSERT_TAIL(&m->devs, dp, devs);
131 /* The default device is always "vol". */
132 m->dev = TAILQ_FIRST(&m->devs);
137 (void)mixer_close(m);
143 * Free resources and close the mixer.
146 mixer_close(struct mixer *m)
152 while (!TAILQ_EMPTY(&m->devs)) {
153 dp = TAILQ_FIRST(&m->devs);
154 TAILQ_REMOVE(&m->devs, dp, devs);
155 while (!TAILQ_EMPTY(&dp->ctls))
156 (void)mixer_remove_ctl(TAILQ_FIRST(&dp->ctls));
165 * Select a mixer device. The mixer structure keeps a list of all the devices
166 * the mixer has, but only one can be manipulated at a time -- this is what
167 * the `dev` in the mixer structure field is for. Each time a device is to be
168 * manipulated, `dev` has to point to it first.
170 * The caller must manually assign the return value to `m->dev`.
173 mixer_get_dev(struct mixer *m, int dev)
177 if (dev < 0 || dev >= m->ndev) {
181 TAILQ_FOREACH(dp, &m->devs, devs) {
182 if (dp->devno == dev)
191 * Select a device by name.
193 * @param name device name (e.g vol, pcm, ...)
196 mixer_get_dev_byname(struct mixer *m, const char *name)
200 TAILQ_FOREACH(dp, &m->devs, devs) {
201 if (!strncmp(dp->name, name, sizeof(dp->name)))
210 * Add a mixer control to a device.
213 mixer_add_ctl(struct mix_dev *parent_dev, int id, const char *name,
214 int (*mod)(struct mix_dev *, void *),
215 int (*print)(struct mix_dev *, void *))
220 /* XXX: should we accept NULL name? */
221 if (parent_dev == NULL) {
225 if ((ctl = calloc(1, sizeof(mix_ctl_t))) == NULL)
227 ctl->parent_dev = parent_dev;
230 (void)strlcpy(ctl->name, name, sizeof(ctl->name));
233 dp = ctl->parent_dev;
234 /* Make sure the same ID or name doesn't exist already. */
235 TAILQ_FOREACH(cp, &dp->ctls, ctls) {
236 if (!strncmp(cp->name, name, sizeof(cp->name)) || cp->id == id) {
241 TAILQ_INSERT_TAIL(&dp->ctls, ctl, ctls);
248 * Same as `mixer_add_ctl`.
251 mixer_add_ctl_s(mix_ctl_t *ctl)
256 return (mixer_add_ctl(ctl->parent_dev, ctl->id, ctl->name,
257 ctl->mod, ctl->print));
261 * Remove a mixer control from a device.
264 mixer_remove_ctl(mix_ctl_t *ctl)
273 if (!TAILQ_EMPTY(&p->ctls)) {
274 TAILQ_REMOVE(&p->ctls, ctl, ctls);
282 * Get a mixer control by id.
285 mixer_get_ctl(struct mix_dev *d, int id)
289 TAILQ_FOREACH(cp, &d->ctls, ctls) {
299 * Get a mixer control by name.
302 mixer_get_ctl_byname(struct mix_dev *d, const char *name)
306 TAILQ_FOREACH(cp, &d->ctls, ctls) {
307 if (!strncmp(cp->name, name, sizeof(cp->name)))
316 * Change the mixer's left and right volume. The allowed volume values are
317 * between MIX_VOLMIN and MIX_VOLMAX. The `ioctl` for volume change requires
318 * an integer value between 0 and 100 stored as `lvol | rvol << 8` -- for
319 * that reason, we de-normalize the 32-bit float volume value, before
320 * we pass it to the `ioctl`.
322 * Volume clumping should be done by the caller.
325 mixer_set_vol(struct mixer *m, mix_volume_t vol)
329 if (vol.left < MIX_VOLMIN || vol.left > MIX_VOLMAX ||
330 vol.right < MIX_VOLMIN || vol.right > MIX_VOLMAX) {
334 v = MIX_VOLDENORM(vol.left) | MIX_VOLDENORM(vol.right) << 8;
335 if (ioctl(m->fd, MIXER_WRITE(m->dev->devno), &v) < 0)
337 if (_mixer_readvol(m->dev) < 0)
344 * Manipulate a device's mute.
346 * @param opt MIX_MUTE mute device
347 * MIX_UNMUTE unmute device
348 * MIX_TOGGLEMUTE toggle device's mute
351 mixer_set_mute(struct mixer *m, int opt)
355 m->mutemask |= (1 << m->dev->devno);
358 m->mutemask &= ~(1 << m->dev->devno);
361 m->mutemask ^= (1 << m->dev->devno);
367 if (ioctl(m->fd, SOUND_MIXER_WRITE_MUTE, &m->mutemask) < 0)
369 if (ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0)
376 * Modify a recording device. The selected device has to be a recording device,
377 * otherwise the function will fail.
379 * @param opt MIX_ADDRECSRC add device to recording sources
380 * MIX_REMOVERECSRC remove device from recording sources
381 * MIX_SETRECSRC set device as the only recording source
382 * MIX_TOGGLERECSRC toggle device from recording sources
385 mixer_mod_recsrc(struct mixer *m, int opt)
387 if (!m->recmask || !MIX_ISREC(m, m->dev->devno)) {
393 m->recsrc |= (1 << m->dev->devno);
395 case MIX_REMOVERECSRC:
396 m->recsrc &= ~(1 << m->dev->devno);
399 m->recsrc = (1 << m->dev->devno);
401 case MIX_TOGGLERECSRC:
402 m->recsrc ^= (1 << m->dev->devno);
408 if (ioctl(m->fd, SOUND_MIXER_WRITE_RECSRC, &m->recsrc) < 0)
410 if (ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
417 * Get default audio card's number. This is used to open the default mixer
418 * and set the mixer structure's `f_default` flag.
421 mixer_get_dunit(void)
427 if (sysctlbyname("hw.snd.default_unit", &unit, &size, NULL, 0) < 0)
434 * Change the default audio card. This is normally _not_ a mixer feature, but
435 * it's useful to have, so the caller can avoid having to manually use
438 * @param unit the audio card number (e.g pcm0, pcm1, ...).
441 mixer_set_dunit(struct mixer *m, int unit)
446 if (sysctlbyname("hw.snd.default_unit", NULL, 0, &unit, size) < 0)
448 /* XXX: how will other mixers get updated? */
449 m->f_default = m->unit == unit;
455 * Get sound device mode (none, play, rec, play+rec). Userland programs can
456 * use the MIX_MODE_* flags to determine the mode of the device.
459 mixer_get_mode(int unit)
465 (void)snprintf(buf, sizeof(buf), "dev.pcm.%d.mode", unit);
466 size = sizeof(unsigned int);
467 if (sysctlbyname(buf, &mode, &size, NULL, 0) < 0)
474 * Get the total number of mixers in the system.
477 mixer_get_nmixers(void)
483 * Open a dummy mixer because we need the `fd` field for the
486 if ((m = mixer_open(NULL)) == NULL)
488 if (ioctl(m->fd, OSS_SYSINFO, &si) < 0) {
489 (void)mixer_close(m);
492 (void)mixer_close(m);
494 return (si.nummixers);