commit c08ac370c650625fc389040c822dccf0afc86876
parent 295860e225d8b09452f7d4c50b943c0218764f14
Author: drkhsh <me@drkhsh.at>
Date:   Wed, 23 Nov 2022 22:17:46 +0100
[slstatus] Add alsa patch from mailing list
This patch adds ALSA volume support. slstatus on-purpose makes use of
`/dev/mixer` per default because it is a much simpler interface and
all distributions still support it.
Anyone refusing to use it may use this patch.
Credits to: Ivan Krylov <krylov.r00t@gmail.com>
Diffstat:
2 files changed, 95 insertions(+), 0 deletions(-)
diff --git a/tools.suckless.org/slstatus/patches/alsa/index.md b/tools.suckless.org/slstatus/patches/alsa/index.md
@@ -0,0 +1,21 @@
+alsa
+====
+
+Description
+-----------
+This patch adds ALSA volume support. slstatus on-purpose makes use of
+`/dev/mixer` per default because it is a much simpler interface and
+all distributions still support it. Anyone refusing to use it may use
+this patch.
+
+To use, add `-DALSA` to `CPPFLAGS`, `-lasound` to `LDFLAGS` and pass the
+control name (e.g. Master) as argument to `vol_perc` in config.h.
+
+Download
+--------
+* [slstatus-alsa-4bd78c9.patch](slstatus-alsa-4bd78c9.patch)
+
+Authors
+-------
+* Ivan Krylov <krylov.r00t@gmail.com>
+* drkhsh <me@drkhsh.at>
diff --git a/tools.suckless.org/slstatus/patches/alsa/slstatus-alsa-4bd78c9.patch b/tools.suckless.org/slstatus/patches/alsa/slstatus-alsa-4bd78c9.patch
@@ -0,0 +1,74 @@
+Author: Ivan Krylov <krylov.r00t@gmail.com>
+Date:   Sat May 4 11:59:58 2019 +0300
+
+    Add ALSA volume support
+    
+    To use, add -DALSA to CPPFLAGS, -lasound to LDFLAGS and pass the
+    control name (e.g. Master) as argument to vol_perc in config.h.
+
+diff --git a/components/volume.c b/components/volume.c
+index 6cec556..15c5a39 100644
+--- a/components/volume.c
++++ b/components/volume.c
+@@ -182,6 +182,61 @@
+ 
+ 		return bprintf("%d", value);
+ 	}
++#elif defined(ALSA)
++	#include <alsa/asoundlib.h>
++
++	static const char *devname = "default";
++	const char *
++	vol_perc(const char *mixname)
++	{
++		snd_mixer_t *mixer = NULL;
++		snd_mixer_selem_id_t *mixid = NULL;
++		snd_mixer_elem_t *elem = NULL;
++		long min = 0, max = 0, volume = -1;
++		int err;
++
++		if ((err = snd_mixer_open(&mixer, 0))) {
++			warn("snd_mixer_open: %d", err);
++			return NULL;
++		}
++		if ((err = snd_mixer_attach(mixer, devname))) {
++			warn("snd_mixer_attach(mixer, \"%s\"): %d", devname, err);
++			goto cleanup;
++		}
++		if ((err = snd_mixer_selem_register(mixer, NULL, NULL))) {
++			warn("snd_mixer_selem_register(mixer, NULL, NULL): %d", err);
++			goto cleanup;
++		}
++		if ((err = snd_mixer_load(mixer))) {
++			warn("snd_mixer_load(mixer): %d", err);
++			goto cleanup;
++		}
++
++		snd_mixer_selem_id_alloca(&mixid);
++		snd_mixer_selem_id_set_name(mixid, mixname);
++		snd_mixer_selem_id_set_index(mixid, 0);
++
++		elem = snd_mixer_find_selem(mixer, mixid);
++		if (!elem) {
++			warn("snd_mixer_find_selem(mixer, \"%s\") == NULL", mixname);
++			goto cleanup;
++		}
++
++		if ((err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max))) {
++			warn("snd_mixer_selem_get_playback_volume_range(): %d", err);
++			goto cleanup;
++		}
++		if ((err = snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &volume))) {
++			warn("snd_mixer_selem_get_playback_volume(): %d", err);
++		}
++
++	cleanup:
++		snd_mixer_free(mixer);
++		snd_mixer_detach(mixer, devname);
++		snd_mixer_close(mixer);
++
++		return volume == -1 ? NULL : bprintf("%.0f", (volume-min)*100./(max-min));
++	}
+ #else
+ 	#include <sys/soundcard.h>
+