From b487984ecc61c6229cf92550030745c192fd3d0b Mon Sep 17 00:00:00 2001 From: Arnold Sommerfeld Date: Wed, 17 May 2023 18:44:48 +0100 Subject: first commit --- home/.local/bin/automute | 123 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 home/.local/bin/automute (limited to 'home/.local/bin/automute') diff --git a/home/.local/bin/automute b/home/.local/bin/automute new file mode 100755 index 0000000..20b04f4 --- /dev/null +++ b/home/.local/bin/automute @@ -0,0 +1,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 -- cgit v1.2.3-70-g09d2