summaryrefslogtreecommitdiff
path: root/scripts/switch-audio
blob: 8b4509088635ad00f4cbe869b0baca040e05715e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash

focused_monitor="$(hyprctl monitors -j | jq -r '.[] | select(.focused == true).name')"

sinks=$(pactl -f json list sinks | jq '[.[] | select((.ports | length == 0) or ([.ports[]? | .availability != "not available"] | any))]')
sinks_count=$(echo "$sinks" | jq '. | length')

if [ "$sinks_count" -eq 0 ]; then
    notify-send -u normal "$focused_monitor" "No audio devices found"
    exit 1
fi

current_sink_name=$(pactl get-default-sink)
current_sink_index=$(echo "$sinks" | jq -r --arg name "$current_sink_name" 'map(.name) | index($name)')

if [ "$current_sink_index" != "null" ]; then
    next_sink_index=$(((current_sink_index + 1) % sinks_count))
else
    next_sink_index=0
fi

next_sink=$(echo "$sinks" | jq -r ".[$next_sink_index]")
next_sink_name=$(echo "$next_sink" | jq -r '.name')

next_sink_description=$(echo "$next_sink" | jq -r '.description')
if [ "$next_sink_description" = "(null)" ] || [ "$next_sink_description" = "null" ] || [ -z "$next_sink_description" ]; then
    sink_id=$(echo "$next_sink" | jq -r '.properties."object.id"')
    next_sink_description=$(wpctl status | grep -E "\s+\*?\s+${sink_id}\." | sed -E 's/^.*[0-9]+\.\s+//' | sed -E 's/\s+\[.*$//')
fi

if [ "$next_sink_name" != "$current_sink_name" ]; then
    pactl set-default-sink "$next_sink_name"
fi

notify-send -u low "Audio: $focused_monitor" "Switching to $next_sink_description"