improve eww
This commit is contained in:
@@ -1,45 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
# Default sink / source volume for the popup. Refreshes on PipeWire events
|
||||
# (pactl subscribe on the pipewire-pulse socket) and every 30 s.
|
||||
#
|
||||
# volume status loop (deflisten)
|
||||
# volume setvol SINK|SOURCE PERCENT
|
||||
# volume mute SINK|SOURCE
|
||||
|
||||
volicons=("" "" "")
|
||||
icons=("" "" "")
|
||||
|
||||
vol() { wpctl get-volume @DEFAULT_AUDIO_"$1"@ | awk '{print int($2*100)}'; }
|
||||
ismuted() { wpctl get-volume @DEFAULT_AUDIO_"$1"@ | rg -qi muted; echo -n $?; }
|
||||
setvol() { wpctl set-volume @DEFAULT_AUDIO_"$1"@ "$(awk -v n="$2" 'BEGIN{print n/100}')"; }
|
||||
setmute() { wpctl set-mute @DEFAULT_AUDIO_"$1"@ toggle; }
|
||||
|
||||
gen_output() {
|
||||
percent=$(vol "SINK")
|
||||
lvl=$(awk -v n="$percent" 'BEGIN{print int(n/34)}')
|
||||
sink_muted=$(ismuted "SINK")
|
||||
source_muted=$(ismuted "SOURCE")
|
||||
sink=$(wpctl inspect @DEFAULT_AUDIO_SINK@ 2>/dev/null | grep -m1 'node\.nick' | sed 's/.*= "\(.*\)"/\1/')
|
||||
|
||||
[ "$sink_muted" = 0 ] && icon="" || icon="${volicons[$lvl]}"
|
||||
[ "$source_muted" = 0 ] && mic_icon="" || mic_icon=""
|
||||
|
||||
printf '{"icon":"%s","percent":%s,"sink_muted":%s,"mic_icon":"%s","microphone":%s,"source_muted":%s,"sink":"%s"}\n' \
|
||||
"$icon" "$percent" "$([ "$sink_muted" = 0 ] && echo true || echo false)" \
|
||||
"$mic_icon" "$(vol SOURCE)" "$([ "$source_muted" = 0 ] && echo true || echo false)" "$sink"
|
||||
level() { # prints "<percent> <muted>"
|
||||
wpctl get-volume "@DEFAULT_AUDIO_$1@" 2>/dev/null \
|
||||
| awk '{ printf "%d %s\n", $2 * 100 + 0.5, ($0 ~ /MUTED/) ? "true" : "false" }'
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
mute) setmute "$2" ;;
|
||||
setvol) setvol "$2" "$3" ;;
|
||||
sink_name() {
|
||||
wpctl inspect @DEFAULT_AUDIO_SINK@ 2>/dev/null \
|
||||
| awk -F'"' '/node\.nick/ { n = $2 } /node\.description/ { d = $2 } END { print (n != "" ? n : d) }'
|
||||
}
|
||||
|
||||
state() {
|
||||
local vol muted mic mic_muted icon mic_icon lvl
|
||||
read -r vol muted < <(level SINK)
|
||||
read -r mic mic_muted < <(level SOURCE)
|
||||
vol=${vol:-0} muted=${muted:-false} mic=${mic:-0} mic_muted=${mic_muted:-false}
|
||||
lvl=$((vol >= 67 ? 2 : vol >= 34 ? 1 : 0))
|
||||
if [[ $muted == true ]]; then icon=""; else icon=${icons[$lvl]}; fi
|
||||
if [[ $mic_muted == true ]]; then mic_icon=""; else mic_icon=""; fi
|
||||
printf '{"icon":"%s","percent":%d,"sink_muted":%s,"mic_icon":"%s","microphone":%d,"source_muted":%s,"sink":"%s"}' \
|
||||
"$icon" "$vol" "$muted" "$mic_icon" "$mic" "$mic_muted" "$(jstr "$(sink_name)")"
|
||||
}
|
||||
|
||||
refresh() { emit "$(state)"; }
|
||||
_pw_events() { pactl subscribe 2>/dev/null | grep --line-buffered -E "on (sink|source|server) "; }
|
||||
|
||||
case ${1:-} in
|
||||
mute) wpctl set-mute "@DEFAULT_AUDIO_$2@" toggle ;;
|
||||
setvol) wpctl set-volume -l 1.0 "@DEFAULT_AUDIO_$2@" "${3%.*}%" ;;
|
||||
*)
|
||||
gen_output
|
||||
|
||||
tmp=$(mktemp -d)
|
||||
pipe="$tmp/vol-events"
|
||||
mkfifo "$pipe"
|
||||
trap 'rm -rf "$tmp"; kill 0 2>/dev/null' EXIT INT TERM
|
||||
|
||||
# 2s polling fallback
|
||||
(while true; do sleep 2; echo poll; done) > "$pipe" &
|
||||
# PipeWire property-change events (fires on mute/volume change)
|
||||
(pw-cli -m 2>/dev/null | grep --line-buffered "changed") > "$pipe" &
|
||||
|
||||
while IFS= read -r _ < "$pipe"; do
|
||||
gen_output
|
||||
done
|
||||
refresh
|
||||
mux_init
|
||||
mux_add _pw_events
|
||||
mux_tick 30
|
||||
mux_loop refresh
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user