slstatus-alsa-4bd78c9.patch (2083B)
1 Author: Ivan Krylov <krylov.r00t@gmail.com> 2 Date: Sat May 4 11:59:58 2019 +0300 3 4 Add ALSA volume support 5 6 To use, add -DALSA to CPPFLAGS, -lasound to LDFLAGS and pass the 7 control name (e.g. Master) as argument to vol_perc in config.h. 8 9 diff --git a/components/volume.c b/components/volume.c 10 index 6cec556..15c5a39 100644 11 --- a/components/volume.c 12 +++ b/components/volume.c 13 @@ -182,6 +182,61 @@ 14 15 return bprintf("%d", value); 16 } 17 +#elif defined(ALSA) 18 + #include <alsa/asoundlib.h> 19 + 20 + static const char *devname = "default"; 21 + const char * 22 + vol_perc(const char *mixname) 23 + { 24 + snd_mixer_t *mixer = NULL; 25 + snd_mixer_selem_id_t *mixid = NULL; 26 + snd_mixer_elem_t *elem = NULL; 27 + long min = 0, max = 0, volume = -1; 28 + int err; 29 + 30 + if ((err = snd_mixer_open(&mixer, 0))) { 31 + warn("snd_mixer_open: %d", err); 32 + return NULL; 33 + } 34 + if ((err = snd_mixer_attach(mixer, devname))) { 35 + warn("snd_mixer_attach(mixer, \"%s\"): %d", devname, err); 36 + goto cleanup; 37 + } 38 + if ((err = snd_mixer_selem_register(mixer, NULL, NULL))) { 39 + warn("snd_mixer_selem_register(mixer, NULL, NULL): %d", err); 40 + goto cleanup; 41 + } 42 + if ((err = snd_mixer_load(mixer))) { 43 + warn("snd_mixer_load(mixer): %d", err); 44 + goto cleanup; 45 + } 46 + 47 + snd_mixer_selem_id_alloca(&mixid); 48 + snd_mixer_selem_id_set_name(mixid, mixname); 49 + snd_mixer_selem_id_set_index(mixid, 0); 50 + 51 + elem = snd_mixer_find_selem(mixer, mixid); 52 + if (!elem) { 53 + warn("snd_mixer_find_selem(mixer, \"%s\") == NULL", mixname); 54 + goto cleanup; 55 + } 56 + 57 + if ((err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max))) { 58 + warn("snd_mixer_selem_get_playback_volume_range(): %d", err); 59 + goto cleanup; 60 + } 61 + if ((err = snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &volume))) { 62 + warn("snd_mixer_selem_get_playback_volume(): %d", err); 63 + } 64 + 65 + cleanup: 66 + snd_mixer_free(mixer); 67 + snd_mixer_detach(mixer, devname); 68 + snd_mixer_close(mixer); 69 + 70 + return volume == -1 ? NULL : bprintf("%.0f", (volume-min)*100./(max-min)); 71 + } 72 #else 73 #include <sys/soundcard.h> 74