From 8dc7da2815eda45349868830fa31a1eec2fa6729 Mon Sep 17 00:00:00 2001 From: Alexander Oakman Date: Sun, 25 Aug 2024 05:05:33 +0300 Subject: [PATCH] Fix the idle-inhibitor script It turns out that the idle-inhibitor provided by sway only works with views (application windows), i.e. inhibition cannot be run on empty workspaces because they do not contain views. Also, when switching between different workspaces, the views of the previous workspace may have idle inhibitors, causing inhibition to be active even if no sound is played until the old views are updated. To update the old views, it is necessary to switch to the appropriate workspace and wait for the script to execute, which will remove the inhibitors that is incorrect and inconvenient. This patch introduces a new inhibit method based on systemd-inhibit, which has none of the above problems. --- usr/share/sway/scripts/idle-inhibitor.sh | 37 +++++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/usr/share/sway/scripts/idle-inhibitor.sh b/usr/share/sway/scripts/idle-inhibitor.sh index d38b64b..d0113a5 100755 --- a/usr/share/sway/scripts/idle-inhibitor.sh +++ b/usr/share/sway/scripts/idle-inhibitor.sh @@ -1,11 +1,32 @@ #!/bin/bash -set -e +check_status() { + grep -s -l -v "closed" \ + /proc/asound/card*/pcm*/sub*/status \ + &>/dev/null +} -if grep -s -l -v "closed" /proc/asound/card*/pcm*/sub*/status; then - swaymsg inhibit_idle open - printf "Idle inhibition is ON." -else - swaymsg inhibit_idle none - printf "Idle inhibition is OFF." -fi +out() { + printf "Idle inhibition is OFF.\n" + exit 0 +} + +watch_status() { + printf "Idle inhibition is ON.\n" + while check_status; do + sleep 10 + done + out +} + +main() { + check_status || out + test "$1" = "-w" && watch_status + systemd-inhibit \ + --what=idle \ + --who="${0##*/}" \ + --why="Inhibit idle due to audio is now playing" \ + --mode=block \ + "$0" -w +} +main "$@"