blob: 20b04f4705dd582a9335c5e13eb1630eb60ff9d1 (
plain) (
blame)
| 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
 | #!/usr/bin/env sh
pid_file="/tmp/automutepid"
pid_exists() {
    test -r "$pid_file"
}
is_running() {
    if pid_exists; then
        ps "$(cat "$pid_file")" >/dev/null 2>&1 || return 1
    else
        return 1
    fi
}
mute() {
    pw-cli set-param "$1" Props '{mute: true}'
}
unmute() {
    pw-cli set-param "$1" Props '{mute: false}'
}
pid2index() {
    pw-dump | jq --argjson targetid "$1" 'map(select(.info.props."application.process.id" == $targetid and .info.params.PropInfo != null)) | .[].id'
}
list_sink_input_ids() {
    pw-dump | jq 'map(select(.info.params.PropInfo != null and .info.params.PropInfo[].id=="mute"  and .info.props."application.process.id" != null)) | .[].id'
}
unmute_all() {
    for i in $(list_sink_input_ids); do
        unmute "$i"
    done
    exit
}
trap unmute_all INT TERM
mute_all() {
    for i in $(list_sink_input_ids); do
        mute "$i"
    done
}
do_automute() {
    while :; do
        focus_pid="$(xdotool getwindowfocus getwindowpid)"
        if [ "$focus_pid" ] && [ "$unfocus_pid" != "$focus_pid" ]; then
            focus_index=$(pid2index "$focus_pid")
            if [ "$unfocus_pid" ]; then
                unfocus_index=$(pid2index "$unfocus_pid")
            fi
            if [ "$focus_index" ]; then
                if [ "$unfocus_index" ]; then
                    mute "$unfocus_index"
                else
                    mute_all
                fi
                unmute "$focus_index"
                unfocus_pid="$focus_pid"
            fi
        fi
        sleep 0.25
    done
}
start() {
    do_automute &
    echo "$!" >"$pid_file"
    notify-send "Automute started!"
}
stop() {
    pid_exists || exit 1
    pid="$(cat "$pid_file")"
    # kill with SIGTERM, allowing finishing touches.
    kill "$pid"
    # even after SIGTERM, ffmpeg may still run, so SIGKILL it.
    sleep 3
    is_running && kill -9 "$pid"
    rm -f "$pid_file"
    notify-send "Automute stopped!"
}
toggle() {
    if is_running; then
        echo "Stopping automute"
        stop
    else
        echo "Starting automute"
        start
    fi
    echo
    status
}
status() {
    if is_running; then
        echo "Automuting is running with PID $(cat "$pid_file")"
    else
        echo "Automute inactive"
    fi
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    toggle)
        toggle
        ;;
    status)
        status
        ;;
    *)
        toggle
        ;;
esac
 |