Files
DotFiles/scripts/toggle_speaker.sh
T
2026-07-15 17:24:10 +02:00

57 lines
1.3 KiB
Bash
Executable File

#/bin/sh
list_of_sinks_to_toggle=("alsa_output.usb-HP__Inc_HyperX_Cloud_Alpha_Wireless_00000001-00.analog-stereo"
"bluez_output.90_F2_60_64_1E_12.1")
get_current_sink() {
pactl get-default-sink
}
is_bluetooth_speaker_connected() {
pactl list sinks | grep "Name:" | awk -F ' ' '{print $2}' | grep --color=never "bluez_output.90_F2_60_64_1E_12.1"
}
pre_change_command() {
if [ $1 == "bluez_output.90_F2_60_64_1E_12.1" ]
then
if [ -z $(is_bluetooth_speaker_connected) ]
then
notify-send "Speaker Toggler" "Connecting bluetooth speakers"
bluetoothctl connect 90:F2:60:64:1E:12 > /dev/null 2>&1
status=$?
if [ $? -ne 0 ]
then
notify-send "Speaker Toggler" "Could not connect bluetooth speaker\nMaybe it is not turned on?"
exit 1
fi
while [ -z $(is_bluetooth_speaker_connected) ]
do
sleep 0.05
done
fi
fi
}
current_sink=$(get_current_sink)
index=-1;
for i in "${!list_of_sinks_to_toggle[@]}"
do
if [ "${list_of_sinks_to_toggle[$i]}" == "$current_sink" ]
then
index=$i
fi
done
if [ $index == -1 ]
then
echo "err: current sink not found in cycle"
fi
next_index=$(((index + 1) % ${#list_of_sinks_to_toggle[@]} ))
next_sink=${list_of_sinks_to_toggle[$next_index]}
pre_change_command $next_sink
pactl set-default-sink $next_sink