[hackers] [slstatus][patch] Use ALSA for vol_perc on Linux

From: Thomas Vigouroux <tomvig38_AT_gmail.com>
Date: Thu, 21 May 2020 22:39:22 +0200

For linux only, use ALSA instead of OSS to get volume.
Requires slstatus to be compiled with -lasound.

diff --git a/components/volume.c b/components/volume.c
index 61cec90..6a5f5a1 100644
--- a/components/volume.c
+++ b/components/volume.c
_AT_@ -72,6 +72,63 @@
 
                 return bprintf("%d", m ? 0 : v * 100 / 255);
         }
+#elif defined(__linux__)
+ #include <alsa/asoundlib.h>
+ #include <alsa/mixer.h>
+
+ const char *
+ vol_perc(const char * card)
+ {
+ long minv, maxv, outvol;
+ snd_mixer_t* handle;
+ snd_mixer_elem_t* elem;
+ snd_mixer_selem_id_t* sid;
+
+ static const char* mix_name = "Master";
+ static int mix_index = 0;
+
+ snd_mixer_selem_id_alloca(&sid);
+
+ /* sets simple-mixer index and name */
+ snd_mixer_selem_id_set_index(sid, mix_index);
+ snd_mixer_selem_id_set_name(sid, mix_name);
+
+ if (snd_mixer_open(&handle, 0) < 0)
+ return NULL;
+ if (snd_mixer_attach(handle, card) < 0) {
+ snd_mixer_close(handle);
+ return NULL;
+ }
+ if (snd_mixer_selem_register(handle, NULL, NULL) < 0) {
+ snd_mixer_close(handle);
+ return NULL;
+ }
+ if (snd_mixer_load(handle) < 0) {
+ snd_mixer_close(handle);
+ return NULL;
+ }
+ elem = snd_mixer_find_selem(handle, sid);
+ if (!elem) {
+ snd_mixer_close(handle);
+ return NULL;
+ }
+
+ snd_mixer_selem_get_playback_volume_range(elem, &minv, &maxv);
+
+ if(snd_mixer_selem_get_playback_volume(elem, 0, &outvol) < 0) {
+ snd_mixer_close(handle);
+ return NULL;
+ }
+
+ /* make the value bound to 100 */
+ outvol -= minv;
+ maxv -= minv;
+ minv = 0;
+ outvol = 100 * outvol / maxv; /* make the value bound from 0 to 100 */
+
+ snd_mixer_close(handle);
+ return bprintf("%ld", outvol);
+ }
 #else
         #include <sys/soundcard.h>
 
diff --git a/config.def.h b/config.def.h
index afcd044..76254dc 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -61,6 +61,7 @@ const char * notmuch_db_path = "/home/thomas/Mail/";
  * uptime system uptime NULL
  * username username of current user NULL
  * vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer)
+ * sound card name on Linux (default)
  * wifi_perc WiFi signal in percent interface name (wlan0)
  * wifi_essid WiFi ESSID interface name (wlan0)
  */
diff --git a/config.mk b/config.mk
index 7a02893..ad4290a 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -14,7 +14,7 @@ X11LIB = /usr/X11R6/lib
 CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE
 CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os
 LDFLAGS = -L$(X11LIB) -s
-LDLIBS = -lX11
+LDLIBS = -lX11 -lasound
 
 # compiler and linker
 CC = cc
Received on Thu May 21 2020 - 22:39:22 CEST

This archive was generated by hypermail 2.3.0 : Thu May 21 2020 - 22:48:33 CEST