aboutsummaryrefslogtreecommitdiff
path: root/swob.sh
diff options
context:
space:
mode:
Diffstat (limited to 'swob.sh')
-rwxr-xr-xswob.sh98
1 files changed, 85 insertions, 13 deletions
diff --git a/swob.sh b/swob.sh
index 8993a07..49e7675 100755
--- a/swob.sh
+++ b/swob.sh
@@ -51,10 +51,74 @@ start_wob() {
} &
}
-do_cmd_get_percent() {
- case $1 in
- volume|vol)
- amixer sset Master "$2" | sed -E '
+glob_match() {
+ # MUST be called with set +f
+ test $# -gt 1 -o -e "$1"
+}
+
+select_audio() {
+ # could guess at /run/user/`id -u`, but let's not jump the gun here
+ sound_sys=$1 rundir=${2:-$XDG_RUNTIME_DIR} tool=$3
+ test -n "$rundir" &&
+ glob_match "$rundir/$sound_sys"* &&
+ command -v "$tool" >/dev/null 2>&1 &&
+ SWOB_AUDIO=$sound_sys
+}
+
+get_audio_type() {
+ # MUST be called with set +fu
+
+ case $SWOB_AUDIO in
+ pipewire|pulse|alsa) return ;;
+ '') ;;
+ *) echo >&2 "$0: warning: unrecognised SWOB_AUDIO: $SWOB_AUDIO" ;;
+ esac
+
+ select_audio pipewire "$PIPEWIRE_RUNTIME_DIR" wpctl && return
+ select_audio pulse "$PULSE_RUNTIME_PATH" pactl && return
+ SWOB_AUDIO=alsa # default to ALSA
+}
+
+set_vol() {
+ set +fu
+ get_audio_type
+ set -fu
+
+ case $SWOB_AUDIO in
+ pipewire)
+ case $1 in
+ toggle) to_set=mute ;;
+ *) to_set='volume -l 1.0' ;;
+ esac
+ wpctl set-$to_set @DEFAULT_AUDIO_SINK@ "$1"
+ wpctl get-volume @DEFAULT_AUDIO_SINK@ | sed -E \
+ -e 's/^Volume: ([0-9]+)\.([0-9][0-9])/\1\2/' \
+ -e 's/\[MUTED\]/mute/'
+ ;;
+
+ pulse)
+ case $1 in
+ toggle) to_set=mute ;;
+ *) to_set=volume ;;
+ esac
+ pactl set-sink-$to_set @DEFAULT_SINK@ "$(echo "$1" | sed -E 's/(.*)([+-])$/\2\1/')"
+ percent=`pactl get-sink-volume @DEFAULT_SINK@ | sed -En 's/.* ([0-9]+)%.*/\1/p'`
+ mute_cmd='pactl get-sink-mute @DEFAULT_SINK@'
+ mute_out=`$mute_cmd`
+ case $mute_out in
+ 'Mute: yes') muted=1 ;;
+ 'Mute: no') muted= ;;
+ *) echo >&2 "$0: warning: bad output from $mute_cmd: $mute_out" ;;
+ esac
+ echo "$percent ${muted:+mute}"
+ ;;
+
+ *)
+ if test "$SWOB_AUDIO" != alsa; then
+ echo >&2 "$0: WARNING: internal inconsistency! SWOB_AUDIO: \`$SWOB_AUDIO'"
+ fi
+
+ amixer sset Master "$1" | sed -E '
# Extract percentage, keep original line for later
h
s/.*\[([0-9]+)%\].*/\1/
@@ -72,15 +136,23 @@ x
}
# else
g
-s/$/ volume/
-' ;;
- brightness|brt)
- brightnessctl -m set "$2" | sed -En 's/(.*[^0-9])?([0-9]+)%.*/\2 brightness/p'
- ;;
- *)
- echo >&2 "$0: error: unrecognised argument: $target"
- exit -1
- ;;
+s/$/ volume/'
+ ;;
+ esac
+}
+
+do_cmd_get_percent() {
+ case $1 in
+ volume|vol)
+ set_vol "$2"
+ ;;
+ brightness|brt)
+ brightnessctl -m set "$2" | sed -En 's/(.*[^0-9])?([0-9]+)%.*/\2 brightness/p'
+ ;;
+ *)
+ echo >&2 "$0: error: unrecognised argument: $target"
+ exit -1
+ ;;
esac
}