From c2808b6d99f30e30b059906e7801cc8a2b76189f Mon Sep 17 00:00:00 2001 From: parazyd <parazyd@dyne.org> Date: Wed, 28 Dec 2016 00:41:51 +0100 Subject: refactor vol_perc to not depend on alsa libraries --- slstatus.c | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 2b63be4..d580ac0 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include <alsa/asoundlib.h> #include <err.h> #include <fcntl.h> #include <ifaddrs.h> @@ -18,6 +17,7 @@ #include <sys/stat.h> #include <sys/statvfs.h> #include <sys/socket.h> +#include <sys/soundcard.h> #include <sys/sysinfo.h> #include <sys/types.h> #include <sys/utsname.h> @@ -617,41 +617,26 @@ uid(void) static char * vol_perc(const char *card) { - int mute; - long int vol, max, min; - snd_mixer_t *handle; - snd_mixer_elem_t *elem; - snd_mixer_selem_id_t *s_elem; + unsigned int i; + int v, afd, devmask; + char *vnames[] = SOUND_DEVICE_NAMES; - snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, card); - snd_mixer_selem_register(handle, NULL, NULL); - snd_mixer_load(handle); - snd_mixer_selem_id_malloc(&s_elem); - snd_mixer_selem_id_set_name(s_elem, "Master"); - elem = snd_mixer_find_selem(handle, s_elem); - - if (elem == NULL) { - snd_mixer_selem_id_free(s_elem); - snd_mixer_close(handle); - warn("Failed to get volume percentage for %s", card); + afd = open(card, O_RDONLY); + if (afd < 0) { + warn("Cannot open %s", card); return smprintf(UNKNOWN_STR); } - snd_mixer_handle_events(handle); - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - snd_mixer_selem_get_playback_volume(elem, 0, &vol); - snd_mixer_selem_get_playback_switch(elem, 0, &mute); - - snd_mixer_selem_id_free(s_elem); - snd_mixer_close(handle); + ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask); + for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) + if (devmask & (1 << i)) + if (!strcmp("vol", vnames[i])) + ioctl(afd, MIXER_READ(i), &v); - if (!mute) + close(afd); + if (v == 0) return smprintf("mute"); - else if (max == 0) - return smprintf("0%%"); - else - return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max)); + return smprintf("%d%%", v & 0xff); } static char * -- cgit v1.2.3 From 62f40164309cd6f22be9fae89c071221944618e4 Mon Sep 17 00:00:00 2001 From: parazyd <parazyd@dyne.org> Date: Fri, 30 Dec 2016 12:16:07 +0100 Subject: add vol_perc notes, add braces to singleline statements --- README.md | 10 ++++++++++ config.def.h | 2 +- slstatus.c | 12 ++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) (limited to 'slstatus.c') diff --git a/README.md b/README.md index 27a4a10..93b7b12 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,16 @@ If you use any other status bar or window manager you will have to figure it out slstatus -o | other_status_bar & +### Specific function quirks + +- Volume percentage + +If there is no `/dev/mixer` on your system and you use ALSA, it means you have to load the OSS compatibility module by issuing: + +``` +# modprobe snd-pcm-oss +``` + ## Contributing Hunt FIXME's in the code or do WTF you want! If it is useful, I will merge. diff --git a/config.def.h b/config.def.h index 4ce5958..caddd7f 100644 --- a/config.def.h +++ b/config.def.h @@ -34,7 +34,7 @@ - uid (uid of current user) [argument: NULL] - uptime (uptime) [argument: NULL] - username (username of current user) [argument: NULL] -- vol_perc (oss/alsa volume and mute status in percent) [argument: /dev/mixer] +- vol_perc (oss/alsa volume status (see README)) [argument: /dev/mixer] - wifi_perc (wifi signal in percent) [argument: wifi card interface name] - wifi_essid (wifi essid) [argument: wifi card interface name] */ static const struct arg args[] = { diff --git a/slstatus.c b/slstatus.c index d580ac0..ff81e0c 100644 --- a/slstatus.c +++ b/slstatus.c @@ -628,14 +628,18 @@ vol_perc(const char *card) } ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask); - for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) - if (devmask & (1 << i)) - if (!strcmp("vol", vnames[i])) + for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) { + if (devmask & (1 << i)) { + if (!strcmp("vol", vnames[i])) { ioctl(afd, MIXER_READ(i), &v); + } + } + } close(afd); - if (v == 0) + if (v == 0) { return smprintf("mute"); + } return smprintf("%d%%", v & 0xff); } -- cgit v1.2.3