random/hdmi_sound_toggle.sh

51 lines
1.9 KiB
Bash
Raw Permalink Normal View History

2022-01-14 23:26:19 -06:00
#!/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
2022-10-13 19:49:01 -05:00
# Add this to /etc/acpi/handler.sh (make sure acpid is installed and enabled) to pause all media automatically switch to HDMI (if available)
# jack/headphone)
# case "$3" in
# plug)
# logger "Headphones plugged"
# ;;
# unplug)
# logger "Headphones unplugged"
# sudo -u <username> env -i bash -c "DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus' playerctl --all-players pause"
# <location-of-this-script>
# ;;
# *)
# logger "ACPI action undefined: $3"
# ;;
# esac
# ;;
2022-01-14 23:26:19 -06:00
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