scripts/togglehdmiaudio.sh
2019-12-15 17:31:59 +02:00

41 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Toggle between HDMI and Analog
# Find out current RUNNING sink
currentSink=$(pactl list short sinks | grep 'RUNNING' | awk '{print$2}')
sink=NULL # Selected sink
# Prefer the sink name from command-line
preferSink=$1
# Toggle between sinks
if [[ $currentSink != *"hdmi"* ]]; then
sink=$(pactl list short sinks | awk '{print$2}' | grep 'hdmi' | sed -n '1p')
else
availableSinks=$(pactl list short sinks | awk '{print$2}' | grep 'analog')
for sink in $availableSinks
do
# If preferred sink name is given the first one maching will be selected
# Empty preferred sink argument will select the first one because
# empty matches everything
if [[ $sink == *$preferSink* ]]; then
break
fi
done
fi
# Exit if there is no sink :(
if [ -z $sink ]; then
echo "Error! Could not find sink to toggle to!"
exit -1
fi
# Set outputs
echo "setting default sink to $sink"
pactl set-default-sink "$sink"
pactl list short sink-inputs|while read stream; do
streamId=$(echo $stream|cut '-d ' -f1)
echo "moving stream $streamId"
pactl move-sink-input "$streamId" "$sink"
done