Subject: [PATCH] Auto detect the playback devices and the playback switches.

From: Raphael Geissert <[email-masked]>
Date: Thu, 29 Jan 2009 20:35:18 -0600

This finally resolves all the mess caused by different device names depending
on the version of the alsa modules and machine model.

Signed-off-by: Raphael Geissert <[email-masked]>
---
 debian/eeepc-acpi-scripts.default |   12 ++++++--
 etc/acpi/actions/volume.sh        |   51 ++++++++++++++++++++++++++++--------
 2 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/debian/eeepc-acpi-scripts.default b/debian/eeepc-acpi-scripts.default
index 17e1906..c18236d 100644
--- a/debian/eeepc-acpi-scripts.default
+++ b/debian/eeepc-acpi-scripts.default
@@ -3,9 +3,15 @@
 ENABLE_OSD='no'
 OSD_FONT='DejaVuSans 36'
 
-# lookup the following values by using amixer if they don't work for you
-VOLUME_LABEL='LineOut'
-HEADPHONE_LABEL='iSpeaker'
+# Leave these empty to attempt to autoconfigure. If they don't work for you
+# lookup the following values by using amixer
+SOUND_LABEL=
+SOUND_SWITCH=
+
+# Set to 'yes' to display detailed information when the volup and voldown
+# hotkeys are pressed but the volume percentage of all the playback devices
+# is not the same
+DETAILED_SOUND_INFO=no
 
 # These are the switches for xrandr --output VGA and --output LVDS
 # respectively when Fn-F5 is used to select VGA mode.
diff --git a/etc/acpi/actions/volume.sh b/etc/acpi/actions/volume.sh
index 904d71d..a8b012a 100755
--- a/etc/acpi/actions/volume.sh
+++ b/etc/acpi/actions/volume.sh
@@ -11,6 +11,7 @@ DEFAULT=/etc/default/$PKG
 if [ -e "$DEFAULT" ]; then . "$DEFAULT"; fi
 . $FUNC_LIB
 . /etc/acpi/lib/notify.sh
+. /etc/acpi/lib/sound.sh
 action=$1
 
 usage() {
@@ -27,32 +28,58 @@ if ! [ -x $AMIXER ]; then
     exit 1
 fi
 
+configureSound
+
 show_muteness() {
-    status=$($AMIXER get $VOLUME_LABEL | sed -n '/%/{s/.*\[\(on\|off\)\].*/\u\1/p;q}')
-    notify audio "Audio $status"
+    local label msg status all_equal=1 current
+    for label in $SOUND_SWITCH; do
+	current=$($AMIXER get $label |
+		    sed -n 's/.*\[\(on\|off\)\].*/\1/;ta;d;:a;p;q')
+	[ "$status" ] || status="$current"
+	[ "$status" = "$current" ] || all_equal=
+	msg="$msg $current ($label)"
+    done
+    if [ "$all_equal" ]; then
+	msg="$status"
+    fi
+    notify audio "Audio $msg"
 }
 
 show_volume() {
-    percent=$($AMIXER get $VOLUME_LABEL | sed -n '/%/{s/.*\[\(.*\)%\].*/\1/p;q}')
-    notify audio "Volume $percent"
+    local label msg percent all_equal=1 current
+    for label in $SOUND_LABEL; do
+	current=$($AMIXER get $label |
+		    sed -n '/%/{s/.*\[\(.*\)%\].*/\1/p;q}')
+	[ "$percent" ] || percent="$current"
+	[ "$percent" = "$current" ] || all_equal=
+	msg="$msg $current% ($label)"
+	if [ "$DETAILED_SOUND_INFO" != 'yes' ]; then
+	    break
+	fi
+    done
+    if [ "$all_equal" ]; then
+	msg="$percent%"
+    fi
+    notify audio "Volume $msg"
 }
 
 case "$action" in
     toggle)
-        # muting $VOLUME_LABEL affects the headphone jack but not the speakers
-        $AMIXER -q set $VOLUME_LABEL toggle
-        # muting $HEADPHONE_LABEL affects the speakers but not the headphone jack
-        $AMIXER -q set $HEADPHONE_LABEL toggle
+        for label in $SOUND_SWITCH; do
+            $AMIXER -q set $label toggle
+        done
         show_muteness
         ;;
     down)
-        amixer -q set $VOLUME_LABEL 2- unmute
-        amixer -q set $HEADPHONE_LABEL unmute
+        for label in $SOUND_LABEL; do
+            $AMIXER -q set $label 2%- unmute
+        done
         show_volume
         ;;
     up)
-        amixer -q set $VOLUME_LABEL 2+ unmute
-        amixer -q set $HEADPHONE_LABEL unmute
+        for label in $SOUND_LABEL; do
+            $AMIXER -q set $label 2%+ unmute
+        done
         show_volume
         ;;
     *)