Re: [hackers] [slstatus][patch] Use ALSA for vol_perc on Linux
Quoting Ivan Tham (2020-05-22 18:32:58)
> If I recall correctly, it is something like:
>
> const char* buf = NULL;
>
> ...
>
> if (failure)
> goto error;
> ...
>
> buf = bprintf("%ld", (100 * (outvol - minv)) / (maxv - minv));
>
> error:
> snd_mixer_close(handle);
> return buf;
>
>
> Not sure if it is better like the other code I have seen but I think it reduces
> the code duplication.
Yeah that's far better, I only have a little duplication left while returning to avoid compilation warnings (discarded const modifier).
Here is the updated patch :
diff --git a/components/volume.c b/components/volume.c
index 61cec90..f74c068 100644
--- a/components/volume.c
+++ b/components/volume.c
_AT_@ -72,6 +72,50 @@
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) goto error;
+
+ if (snd_mixer_selem_register(handle, NULL, NULL) < 0) goto error;
+
+ if (snd_mixer_load(handle) < 0) goto error;
+
+ elem = snd_mixer_find_selem(handle, sid);
+ if (!elem) goto error;
+
+ snd_mixer_selem_get_playback_volume_range(elem, &minv, &maxv);
+
+ if(snd_mixer_selem_get_playback_volume(elem, 0, &outvol) < 0) goto error;
+
+ snd_mixer_close(handle);
+ return bprintf("%ld", (100 * (outvol - minv)) / (maxv - minv));
+
+error:
+ snd_mixer_close(handle);
+ return NULL;
+ }
#else
#include <sys/soundcard.h>
diff --git a/config.def.h b/config.def.h
index e06be66..af2197c 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -58,6 +58,7 @@ static const char unknown_str[] = "n/a";
* 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 3b32b7c..cd2a234 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 Sun May 24 2020 - 12:03:53 CEST
This archive was generated by hypermail 2.3.0
: Sun May 24 2020 - 12:12:36 CEST