diff --git a/hdmi_sound_toggle.sh b/hdmi_sound_toggle.sh new file mode 100644 index 0000000..cfe9133 --- /dev/null +++ b/hdmi_sound_toggle.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +# This is a script to enable automatic switching to and from HDMI audio output +# To run this script whenever and HDMI cable is connected, copy this file to /usr/local/bin/ and run 'chmod +x /usr/local/bin/hdmi_sound_toggle.sh' +# Then create the file /etc/udev/rules.d/99-hdmi_sound.rules with the following content : +# KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/hdmi_sound_toggle.sh" +# Then run 'udevadm control --reload-rules' and reboot +# If run manually, make sure to run it as root + +export PATH=/usr/bin + +USER_NAME=$(who | awk -v vt=tty$(fgconsole) '$0 ~ vt {print $1}') +USER_ID=$(id -u "$USER_NAME") +CARD_PATH="/sys/class/drm/card0/" +AUDIO_OUTPUT="analog-stereo" +PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native" + +for OUTPUT in $(cd "$CARD_PATH" && echo card*); do + OUT_STATUS=$(<"$CARD_PATH"/"$OUTPUT"/status) + if [[ $OUT_STATUS == connected ]] + then + echo $OUTPUT connected + case "$OUTPUT" in + "card0-HDMI-A-1") + AUDIO_OUTPUT="hdmi-stereo" # Digital Stereo (HDMI 1) + ;; + "card0-HDMI-A-2") + AUDIO_OUTPUT="hdmi-stereo-extra1" # Digital Stereo (HDMI 2) + ;; + esac + fi +done +echo selecting output $AUDIO_OUTPUT +sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:$AUDIO_OUTPUT+input:analog-stereo +