ambevar-dotfiles/.local/bin/hdmi-switch

76 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
usage () {
cat <<EOF>&2
Usage: ${0##*/}
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 $0"
To get audio working on radeon, you may need to boot with the following kernel
paremeter:
radeon.audio=1
You must be root. This script is Linux only.
EOF
}
if [ "$(uname)" != "Linux" ]; then
usage
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
usage
exit 1
fi
ALSACONF="/etc/asound.conf"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
[ -z "$HDMI_STATUS" ] && exit
APLAY_BUF="$(aplay -l | grep HDMI)"
getprop () {
echo "$APLAY_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 --timeout=2
XRANDR_BUF="$(xrandr)"
PRIMARY="$(echo "$XRANDR_BUF" | awk '/^[[:alnum:]-]+ connected/ {print $1; exit}')"
HDMI="$(echo "$XRANDR_BUF" | awk '/^HDMI-[[:digit:]]+ connected/ {print $1; exit}')"
echo "PRIMARY=[$PRIMARY], HDMI=[$HDMI]"
## Video
if [ -n "$HDMI" ]; then
# xrandr --output "$PRIMARY" --auto --primary --output "$HDMI" --auto --noprimary --right-of "$PRIMARY"
xrandr --output "$HDMI" --auto --noprimary --right-of "$PRIMARY"
# else
# xrandr --output "$HDMI" --off
fi
## 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