status

statusbar program for dwm
git clone anongit@rnpnr.xyz:status.git
Log | Files | Refs | Feed | README | LICENSE

Commit: cad1a6904c3178f28a2c938c751897d853facdf0
Parent: 83d1fc8f7c958e1375f57235a5ffcf4b1d800a4e
Author: opask
Date:   Sat, 10 Nov 2018 17:54:48 -0700

add alsa volume support

Diffstat:
Mconfig.mk | 2+-
Mstatus.c | 38+++++++++++++++++++++++++++++++++++++-
2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/config.mk b/config.mk @@ -1,6 +1,6 @@ PREFIX = / -LIBS = -lmpdclient -lX11 +LIBS = -lmpdclient -lX11 -lasound CPPFLAGS = -D_POSIX_C_SOURCE CFLAGS = -O2 -std=c99 -Wall -pedantic $(CPPFLAGS) diff --git a/status.c b/status.c @@ -5,6 +5,8 @@ #include <string.h> #include <time.h> #include <unistd.h> +#include <alsa/asoundlib.h> +#include <alsa/mixer.h> #include <mpd/client.h> #include <X11/Xlib.h> @@ -60,6 +62,36 @@ setstatus(char *str) XSync(dpy, False); } +static long +alsavol(void) +{ + snd_mixer_t *handle; + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *elem; + + long vol, min, max; + + snd_mixer_open(&handle, 0); + snd_mixer_attach(handle, "default"); + snd_mixer_selem_register(handle, NULL, NULL); + snd_mixer_load(handle); + + snd_mixer_selem_id_malloc(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, "Speaker"); + elem = snd_mixer_find_selem(handle, sid); + + snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &vol); + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + + /* covert from raw value to percent */ + vol = (double)vol / (double)(max - min) * 100; + + snd_mixer_close(handle); + snd_mixer_selem_id_free(sid); + return vol; +} + static char * gettime(const char *fmt) { @@ -111,6 +143,7 @@ main(void) char *time; char *song; char *artist; + long vol; memset(&sa, 0, sizeof(sa)); sa.sa_handler = terminate; @@ -124,9 +157,12 @@ main(void) time = gettime("%Y年%m月%d日 ♦ %R"); song = mpd(MPD_TAG_TITLE); artist = mpd(MPD_TAG_ARTIST); + vol = alsavol(); - status = smprintf("[ %s - %s ][ %s ]", song, artist, time); + status = smprintf("[ %s - %s ][ %li%% ][ %s ]", artist, song, + vol, time); setstatus(status); + free(time); free(song); free(artist);