nirish

config files for niri window manager
Log | Files | Refs | README | LICENSE

switch-audio (1392B)


      1 #!/usr/bin/env bash
      2 
      3 focused_monitor="$(hyprctl monitors -j | jq -r '.[] | select(.focused == true).name')"
      4 
      5 sinks=$(pactl -f json list sinks | jq '[.[] | select((.ports | length == 0) or ([.ports[]? | .availability != "not available"] | any))]')
      6 sinks_count=$(echo "$sinks" | jq '. | length')
      7 
      8 if [ "$sinks_count" -eq 0 ]; then
      9     notify-send -u normal "$focused_monitor" "No audio devices found"
     10     exit 1
     11 fi
     12 
     13 current_sink_name=$(pactl get-default-sink)
     14 current_sink_index=$(echo "$sinks" | jq -r --arg name "$current_sink_name" 'map(.name) | index($name)')
     15 
     16 if [ "$current_sink_index" != "null" ]; then
     17     next_sink_index=$(((current_sink_index + 1) % sinks_count))
     18 else
     19     next_sink_index=0
     20 fi
     21 
     22 next_sink=$(echo "$sinks" | jq -r ".[$next_sink_index]")
     23 next_sink_name=$(echo "$next_sink" | jq -r '.name')
     24 
     25 next_sink_description=$(echo "$next_sink" | jq -r '.description')
     26 if [ "$next_sink_description" = "(null)" ] || [ "$next_sink_description" = "null" ] || [ -z "$next_sink_description" ]; then
     27     sink_id=$(echo "$next_sink" | jq -r '.properties."object.id"')
     28     next_sink_description=$(wpctl status | grep -E "\s+\*?\s+${sink_id}\." | sed -E 's/^.*[0-9]+\.\s+//' | sed -E 's/\s+\[.*$//')
     29 fi
     30 
     31 if [ "$next_sink_name" != "$current_sink_name" ]; then
     32     pactl set-default-sink "$next_sink_name"
     33 fi
     34 
     35 notify-send -u low "Audio: $focused_monitor" "Switching to $next_sink_description"