Commit: b870f8fde71de0bd410d083f768ec3d9ac183282
Parent: 831675c848e504b5fced2722d503fe76b5f37266
Author: Randy Palamar
Date:   Wed,  7 Jul 2021 11:30:38 -0600
fix volume block for devices without volume control
Diffstat:
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/blocks/volume.c b/blocks/volume.c
@@ -20,7 +20,7 @@ getvol(struct Block *b)
 	const char *str = "muted";
 
 	int notmuted;
-	long vol, min, max;
+	long vol = 0, min, max;
 
 	snd_mixer_open(&handle, 0);
 	snd_mixer_attach(handle, alsacard);
@@ -32,21 +32,23 @@ getvol(struct Block *b)
 	snd_mixer_selem_id_set_name(sid, b->u.s);
 	elem = snd_mixer_find_selem(handle, sid);
 
-	snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &vol);
-	snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
 	snd_mixer_selem_get_playback_switch(elem, 0, ¬muted);
 
-	/* covert from raw value to percent */
-	vol = (double)(vol - min) / (double)(max - min) * 100;
+	if (snd_mixer_selem_has_playback_volume(elem)) {
+		snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &vol);
+		snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
+
+		/* covert from raw value to percent */
+		vol = (double)(vol - min) / (double)(max - min) * 100;
+	}
 
 	/* don't change to snd_mixer_elem_free() it leaks memory */
 	snd_mixer_close(handle);
 	snd_mixer_selem_id_free(sid);
 
 	if (notmuted) {
-		/* HACK: digital out is always 100% so just say on */
-		if (abs(vol) < 100) {
-			snprintf(buf, sizeof(buf), "%d%%", (int)vol);
+		if (vol) {
+			snprintf(buf, sizeof(buf), "%ld%%", vol);
 			str = buf;
 		} else
 			str = "on";