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

From: Ivan Tham <pickfire_AT_riseup.net>
Date: Fri, 22 May 2020 16:52:21 +0800

On Thu, May 21, 2020 at 10:39:22PM +0200, Thomas Vigouroux wrote:
>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 */

I believe you may need to convert it to double before division and round the
volume. My dwms have something similar:

                  return smprintf("%d", (int) rint((double) (val - min) /
                                                   (double) (max - min) * 100));

https://github.com/pickfire/dwms/blob/c2038a4104f4f80aa6f675821c62e87ccfe48bed/dwms.c#L213-L241

>+
>+ 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
>

-- 
Do what you like, like what you do.  -- Pickfire
Received on Fri May 22 2020 - 10:52:21 CEST

This archive was generated by hypermail 2.3.0 : Fri May 22 2020 - 11:00:37 CEST