spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

commit e7e3765e0a51f677db1df6bb6ffb11a02a2b955c
parent 98607302965b349b4ef377b31627dad69e3a1fe9
Author: sin <sin@2f30.org>
Date:   Thu,  3 Nov 2016 15:42:07 +0000

Implement mix plugin for Linux using ALSA sound lib

Diffstat:
MMakefile | 6+++++-
Mmix.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -11,14 +11,18 @@ include config.mk CPPFLAGS_OpenBSD = -I/usr/X11R6/include -I/usr/local/include LDFLAGS_OpenBSD = -L/usr/X11R6/lib -L/usr/local/lib +LDLIBS_OpenBSD = -lX11 CPPFLAGS_Linux =\ + -I/usr/local/include\ -DPATH_BAT_CAP=\"/sys/class/power_supply/BAT0/capacity\"\ -DPATH_AC_ONLINE=\"/sys/class/power_supply/AC/online\"\ -DPATH_TEMP=\"/sys/class/hwmon/hwmon0/temp1_input\"\ -DPATH_CPU_FREQ=\"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq\" +LDFLAGS_Linux = -L/usr/local/lib +LDLIBS_Linux = -lX11 -lasound CPPFLAGS = $(CPPFLAGS_$(UNAME)) LDFLAGS = $(LDFLAGS_$(UNAME)) -LDLIBS = -lX11 +LDLIBS = $(LDLIBS_$(UNAME)) # To remove extra compile time dependencies for unwanted plugins # comment out the following sections. The stub implementations diff --git a/mix.c b/mix.c @@ -63,4 +63,51 @@ out: close(fd); return ret; } +#elif __linux__ +#include <alsa/asoundlib.h> + +int +mixread(void *arg, char *buf, size_t len) +{ + snd_mixer_t *mixerhdl; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + long min, max; + long vol; + + if (snd_mixer_open(&mixerhdl, 0) < 0) { + warnx("snd_mixer_open: failed"); + return -1; + } + if (snd_mixer_attach(mixerhdl, "default") < 0) { + warnx("snd_mixer_attach: failed"); + goto err0; + } + if (snd_mixer_selem_register(mixerhdl, NULL, NULL) < 0) { + warnx("snd_mixer_selem_register: failed"); + goto err0; + } + if (snd_mixer_load(mixerhdl) < 0) { + warnx("snd_mixer_load: failed"); + goto err0; + } + snd_mixer_selem_id_alloca(&sid); + snd_mixer_selem_id_set_name(sid, "Master"); + elem = snd_mixer_find_selem(mixerhdl, sid); + if (elem == NULL) { + warnx("snd_mixer_find_selem: failed"); + goto err0; + } + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); + snd_mixer_close(mixerhdl); + if (max == 0) + snprintf(buf, len, "0%%"); + else + snprintf(buf, len, "%ld%%", vol * 100 / max); + return 0; +err0: + snd_mixer_close(mixerhdl); + return -1; +} #endif