Added hdmi_sound_toggle.sh

This commit is contained in:
Sayantan Santra 2022-01-14 23:26:19 -06:00
parent 40475f79a7
commit 63edb97a97
1 changed files with 35 additions and 0 deletions

35
hdmi_sound_toggle.sh Normal file
View File

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