ambevar-dotfiles/.scripts/hdmi-switch

55 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
## This script is Linux only.
##
## This script will toggle HDMI Audio (and set video properly). To do this
## automatically when the cable is plugged, add the following udev rule:
##
## $ cat /etc/udev/rules.d/hdmi.rules
## SUBSYSTEM=="drm", ACTION=="change", RUN+="/bin/sh /usr/local/bin/hdmi-switch"
##
## To get audio working on radeon, you may need to boot with the following
## kernel paremeter:
##
## radeon.audio=1
if [ $(id -u) -ne 0 ]; then
echo "You must be root to run this script."
exit
fi
ALSACONF="/etc/asound.conf"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
[ -z "$HDMI_STATUS" ] && exit
BUF="$(aplay -l | grep HDMI)"
_getprop() {
echo "$BUF" | awk -v prop="$1" '{for(i=1; i<NF; i++) if ($i == prop) { print substr($(i+1), 1,1); exit}}'
}
CARD="$(_getprop card)"
DEVICE="$(_getprop device)"
udevadm settle --quiet --timeout=2
## Video
for i in $(xrandr | awk '/^[[:alnum:]-]+ connected/ {print $1}'); do
xrandr --output "$i" --auto
done
## Sound
if [ "$HDMI_STATUS" = "connected" ]; then
cat<<EOF > "$ALSACONF"
pcm.!default {
type hw
card $CARD
device $DEVICE
}
EOF
chmod 644 "${ALSACONF}"
echo "HDMI sound enabled"
else
rm -f "$ALSACONF"
echo "HDMI sound disabled"
fi