-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3cat-audio-device-helper.sh
executable file
·59 lines (46 loc) · 1.29 KB
/
i3cat-audio-device-helper.sh
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
#!/usr/bin/env bash
set -o pipefail # propagate errors
set -u # exit on undefined
set -e # exit on non-zero return value
#set -f # disable globbing/filename expansion
shopt -s failglob # error on unexpaned globs
device="$1"
get_volume() {
local device
device="$1"
awk -F '[][]' '/%/ { printf "%s", $2 ; exit }' <(amixer sget "$device")
}
# muted = off
# unmuted = on
get_device_on_off () {
if awk -F"[][]" '/%/ { print $0; exit }' <(amixer sget "$device") | grep -- '\[off\]' >/dev/null 2>&1; then
echo off
else
echo on
fi
}
run () {
if [ "$(get_device_on_off "$device")" = off ]; then
"$HOME/go/bin/i3cat" encode --color '#f91bac' "♪: M($(get_volume "$device"))"
else
"$HOME/go/bin/i3cat" encode "♪: $(get_volume "$device")"
fi
sleep infinity &
# DO NOT PUT ANYTHING INBETWEEN THESE LINES.
# NOT EVEN SAVING PID AND RUNNING ANOTHER PROCESS
# like echo to print the PID.
#
# OTHERWISE YOU WILL BREAK THIS SCRIPTS SIGNAL
# HANDLING CAPABILITIES.
#
# Tests can be done via
#
# pactl set-sink-volume @DEFAULT_SINK@ +1% && ps -ef | grep -E 'i3cat-audio-device-helper.sh [A-Z]+' | grep -v grep | awk '{ print $2 }' | xargs kill -SIGUSR1 # very broad (will kill vim if this file is open)
#
wait "$!"
}
sigusr1 () {
run
}
trap sigusr1 USR1
run