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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#!/bin/sh
# SPDX-FileCopyrightText: 2023-2024 The Remph <lhr@disroot.org>
# SPDX-License-Identifier: GPL-3.0-or-later
set ${BASH_VERSION:+-o pipefail} -efu
wobfifo=$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY.swob
wobini= SWOB_AUDIO=
scan_confdirs() {
for dir in ${XDG_CONFIG_HOME:+"$XDG_CONFIG_HOME"} ~/.config /etc "${0%/*}"/../etc; do
test -d "$dir"/swob || continue
if test -z "$wobini" -a -r "$dir"/swob/wob.ini; then
wobini=$dir/swob/wob.ini
fi
if test -z "$SWOB_AUDIO" -a -r "$dir"/swob/audio; then
SWOB_AUDIO=`cat "$dir"/swob/audio`
fi
if test -n "$wobini" -a -n "$SWOB_AUDIO"; then
return
fi
done
}
new_wobini() {
# fine, I will make my own wob.ini(5)
wobini=${XDG_CONFIG_HOME:-~/.config}/swob/wob.ini
echo >&2 "$0: no swob/wob.ini found; writing default to $wobini"
mkdir -p "${wobini%/*}"
cat >$wobini <<EOF
[style.volume]
background_color = 000000
[style.mute]
background_color = af0000
[style.brightness]
background_color = a89800
EOF
}
start_wob() {
if test -e "$wobfifo"; then
return # Already started
fi
# temporary fifo (call mkfifo(1) asap to minimise possibility of races)
# TODO: should this be in C, so we can call mkfifo(2) and check for
# EEXIST, rather than using test(1)? Alternatively, there is flock(1),
# or any other IPC or SHM system
mkfifo -m600 "$wobfifo"
test -n "$wobini" || new_wobini
# spawn wob process with temporary file(s)
{
trap 'rm "$wobfifo"' 0
# Don't `exec' wob here, else the trap won't work
wob -c "$wobini" <$wobfifo
} &
}
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/
t match
d
: match
# Get original line back
x
# Test if audio is muted
/\[off\]/ {
g
s/$/ mute/
b
}
# else
g
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
}
## MAIN ##
scan_confdirs
start_wob
{
do_cmd_get_percent "$@"
sleep 3 # Needs to be long enough that there aren't too many wob(1)
# processes spawned and respawned consecutively on repeated
# taps, but short enough that there aren't too many /bin/sh
# processes hanging around simultaneously running sleep(1)
# from this script! 3 seconds is an uneducated guess.
} >$wobfifo
# Don't let wob die if it's still receiving input from another process; wait
# until it says it's finished
# To solve the above mentioned problem of too many /bin/sh processes hanging
# around, we could setsid(1) wob so the script can exit without waiting as
# soon as it's done sleeping (the existing situation is that as long as one
# script sleeps, the shell that spawned the wob process will wait until that
# sleep is done)
test -z ${!-} || wait $! # surprisingly, $! could be unset (not just zero-length)
|