Add native OpenBSD support for mute/volume

Based on functionality in dstat by Joerg Jung.
This commit is contained in:
Ingo Feinerer 2019-02-08 15:37:17 +01:00 committed by Aaron Marcher
parent e724907cc3
commit c1dc896c80
4 changed files with 94 additions and 29 deletions

View file

@ -18,6 +18,8 @@ Copyright 2018 Tobias Tschinkowitz <tobias@he4d.net>
Copyright 2018 David Demelier <markand@malikania.fr> Copyright 2018 David Demelier <markand@malikania.fr>
Copyright 2018-2019 Michael Buch <michaelbuch12@gmail.com> Copyright 2018-2019 Michael Buch <michaelbuch12@gmail.com>
Copyright 2018 Ian Remmler <ian@remmler.org> Copyright 2018 Ian Remmler <ian@remmler.org>
Copyright 2016-2019 Joerg Jung <jung@openbsd.org>
Copyright 2019 Ingo Feinerer <feinerer@logic.at>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above

2
README
View file

@ -38,7 +38,7 @@ In order to build slstatus you need the Xlib header files.
Installation Installation
------------ ------------
Edit config.mk to match your local setup (slstatus is installed into the Edit config.mk to match your local setup (slstatus is installed into the
/usr/local namespace by default). Uncomment OSSLIBS on OpenBSD. /usr/local namespace by default).
Afterwards enter the following command to build and install slstatus (if Afterwards enter the following command to build and install slstatus (if
necessary as root): necessary as root):

View file

@ -2,44 +2,108 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#if defined(__OpenBSD__)
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include "../util.h" #include "../util.h"
const char * #if defined(__OpenBSD__)
vol_perc(const char *card) #include <sys/audioio.h>
{
size_t i;
int v, afd, devmask;
char *vnames[] = SOUND_DEVICE_NAMES;
if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) { const char *
warn("open '%s':", card); vol_perc(const char *card)
return NULL; {
} static int cls = -1;
mixer_devinfo_t mdi;
mixer_ctrl_t mc;
int afd = -1, m = -1, v = -1;
if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { if ((afd = open(card, O_RDONLY)) < 0) {
warn("ioctl 'SOUND_MIXER_READ_DEVMASK':"); warn("open '%s':", card);
close(afd); return NULL;
return NULL; }
}
for (i = 0; i < LEN(vnames); i++) { for (mdi.index = 0; cls == -1; mdi.index++) {
if (devmask & (1 << i) && !strcmp("vol", vnames[i])) { if (ioctl(afd, AUDIO_MIXER_DEVINFO, &mdi) < 0) {
if (ioctl(afd, MIXER_READ(i), &v) < 0) { warn("ioctl 'AUDIO_MIXER_DEVINFO':");
warn("ioctl 'MIXER_READ(%ld)':", i);
close(afd); close(afd);
return NULL; return NULL;
} }
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;
}
} }
close(afd);
return bprintf("%d", m ? 0 : v * 100 / 255);
} }
#else
#include <sys/soundcard.h>
close(afd); const char *
vol_perc(const char *card)
{
size_t i;
int v, afd, devmask;
char *vnames[] = SOUND_DEVICE_NAMES;
return bprintf("%d", v & 0xff); if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
} warn("open '%s':", card);
return NULL;
}
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

View file

@ -14,7 +14,6 @@ X11LIB = /usr/X11R6/lib
CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE
CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os
LDFLAGS = -L$(X11LIB) -s LDFLAGS = -L$(X11LIB) -s
# OpenBSD: add -lossaudio
LDLIBS = -lX11 LDLIBS = -lX11
# compiler and linker # compiler and linker