2017-09-17 11:45:03 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2017-09-17 10:18:17 -04:00
|
|
|
#include <fcntl.h>
|
2018-05-07 05:21:59 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-09-17 10:18:17 -04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-09-24 09:33:01 -04:00
|
|
|
#include "../util.h"
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2019-02-08 09:37:17 -05:00
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
#include <sys/audioio.h>
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2019-02-08 09:37:17 -05:00
|
|
|
const char *
|
|
|
|
vol_perc(const char *card)
|
|
|
|
{
|
|
|
|
static int cls = -1;
|
|
|
|
mixer_devinfo_t mdi;
|
|
|
|
mixer_ctrl_t mc;
|
|
|
|
int afd = -1, m = -1, v = -1;
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2019-02-08 09:37:17 -05:00
|
|
|
if ((afd = open(card, O_RDONLY)) < 0) {
|
|
|
|
warn("open '%s':", card);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mdi.index = 0; cls == -1; mdi.index++) {
|
|
|
|
if (ioctl(afd, AUDIO_MIXER_DEVINFO, &mdi) < 0) {
|
|
|
|
warn("ioctl 'AUDIO_MIXER_DEVINFO':");
|
2017-09-17 10:18:17 -04:00
|
|
|
close(afd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-02-08 09:37:17 -05:00
|
|
|
if (mdi.type == AUDIO_MIXER_CLASS &&
|
|
|
|
!strncmp(mdi.label.name,
|
|
|
|
AudioCoutputs,
|
|
|
|
MAX_AUDIO_DEV_LEN))
|
|
|
|
cls = mdi.index;
|
|
|
|
}
|
|
|
|
for (mdi.index = 0; v == -1 || m == -1; mdi.index++) {
|
|
|
|
if (ioctl(afd, AUDIO_MIXER_DEVINFO, &mdi) < 0) {
|
|
|
|
warn("ioctl 'AUDIO_MIXER_DEVINFO':");
|
|
|
|
close(afd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (mdi.mixer_class == cls &&
|
|
|
|
((mdi.type == AUDIO_MIXER_VALUE &&
|
|
|
|
!strncmp(mdi.label.name,
|
|
|
|
AudioNmaster,
|
|
|
|
MAX_AUDIO_DEV_LEN)) ||
|
|
|
|
(mdi.type == AUDIO_MIXER_ENUM &&
|
|
|
|
!strncmp(mdi.label.name,
|
|
|
|
AudioNmute,
|
|
|
|
MAX_AUDIO_DEV_LEN)))) {
|
|
|
|
mc.dev = mdi.index, mc.type = mdi.type;
|
|
|
|
if (ioctl(afd, AUDIO_MIXER_READ, &mc) < 0) {
|
|
|
|
warn("ioctl 'AUDIO_MIXER_READ':");
|
|
|
|
close(afd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (mc.type == AUDIO_MIXER_VALUE)
|
|
|
|
v = mc.un.value.num_channels == 1 ?
|
|
|
|
mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] :
|
|
|
|
(mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] >
|
|
|
|
mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] ?
|
|
|
|
mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] :
|
|
|
|
mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
|
|
|
|
else if (mc.type == AUDIO_MIXER_ENUM)
|
|
|
|
m = mc.un.ord;
|
|
|
|
}
|
2017-09-17 10:18:17 -04:00
|
|
|
}
|
2019-02-08 09:37:17 -05:00
|
|
|
|
|
|
|
close(afd);
|
|
|
|
|
|
|
|
return bprintf("%d", m ? 0 : v * 100 / 255);
|
2017-09-17 10:18:17 -04:00
|
|
|
}
|
2019-02-08 09:37:17 -05:00
|
|
|
#else
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
|
|
|
const char *
|
|
|
|
vol_perc(const char *card)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int v, afd, devmask;
|
|
|
|
char *vnames[] = SOUND_DEVICE_NAMES;
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2019-02-08 09:37:17 -05:00
|
|
|
if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
|
|
|
|
warn("open '%s':", card);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2019-02-08 09:37:17 -05:00
|
|
|
if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
|
|
|
|
warn("ioctl 'SOUND_MIXER_READ_DEVMASK':");
|
|
|
|
close(afd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < LEN(vnames); i++) {
|
|
|
|
if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
|
|
|
|
if (ioctl(afd, MIXER_READ(i), &v) < 0) {
|
|
|
|
warn("ioctl 'MIXER_READ(%ld)':", i);
|
|
|
|
close(afd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(afd);
|
|
|
|
|
|
|
|
return bprintf("%d", v & 0xff);
|
|
|
|
}
|
|
|
|
#endif
|