Release stale Caffeine inhibitors

This commit is contained in:
Gabriel Brown
2026-08-18 07:43:15 -04:00
parent ca64d4b9b6
commit 5e6dc9e533
2 changed files with 36 additions and 0 deletions
@@ -10,6 +10,7 @@ action="${1:-}"
helper_dir="${PANAMA_ACTION_HELPER_DIR:-$script_dir}"
confirm_attempts="${PANAMA_ACTION_CONFIRM_ATTEMPTS:-20}"
confirm_delay="${PANAMA_ACTION_CONFIRM_DELAY:-0.1}"
kill_command="${PANAMA_ACTION_KILL_COMMAND:-/usr/bin/kill}"
osd_command=("$helper_dir/panama-osd")
gallery_command=("$helper_dir/panama-prism-gallery")
@@ -52,6 +53,17 @@ state_is_active() {
esac
}
release_caffeine_inhibitors() {
local inhibitors pid uid
uid="$(id -u)"
inhibitors="$(systemd-inhibit --list --no-pager --no-legend 2>/dev/null \
| awk -v uid="$uid" '$1 == "Panama" && $2 == uid && $(NF - 1) == "Caffeine" && $NF == "block" { print $4 }')"
while IFS= read -r pid; do
[[ $pid =~ ^[0-9]+$ ]] || continue
"$kill_command" "$pid" 2>/dev/null || true
done <<<"$inhibitors"
}
wait_for_confirmed_state() {
local target="$1" expected="$2" attempt
for ((attempt = 0; attempt < confirm_attempts; attempt++)); do
@@ -71,6 +83,9 @@ toggle_state() {
local target="$1" on_icon="$2" off_icon="$3" label="$4" state
state="$(qs ipc call "$target" toggle)"
case "$state" in true|false) ;; *) show_state "$state" "$on_icon" "$off_icon" "$label"; return ;; esac
if [[ $target == caffeine && $state == false ]]; then
release_caffeine_inhibitors
fi
wait_for_confirmed_state "$target" "$state"
show_state "$state" "$on_icon" "$off_icon" "$label"
}
@@ -80,11 +80,27 @@ EOF
make_recorder systemd-inhibit <<'EOF'
#!/usr/bin/env bash
if [[ -n ${PANAMA_ACTION_TEST_INHIBITOR_FILE:-} && -s $PANAMA_ACTION_TEST_INHIBITOR_FILE ]]; then
while IFS= read -r pid; do
printf 'Panama 1000 gib %s systemd-inhibit sleep:idle Caffeine block\n' "$pid"
done <"$PANAMA_ACTION_TEST_INHIBITOR_FILE"
exit 0
fi
if [[ ${PANAMA_ACTION_TEST_CONFIRMED_STATE:-${PANAMA_ACTION_TEST_STATE:-true}} == true ]]; then
printf 'Panama 1000 gib 42 systemd-inhibit sleep:idle Caffeine block\n'
fi
EOF
make_recorder kill <<'EOF'
#!/usr/bin/env bash
printf 'kill' >>"$PANAMA_ACTION_TEST_LOG"
printf ' <%s>' "$@" >>"$PANAMA_ACTION_TEST_LOG"
printf '\n' >>"$PANAMA_ACTION_TEST_LOG"
if [[ -n ${PANAMA_ACTION_TEST_INHIBITOR_FILE:-} ]]; then
: >"$PANAMA_ACTION_TEST_INHIBITOR_FILE"
fi
EOF
make_recorder pgrep <<'EOF'
#!/usr/bin/env bash
[[ ${PANAMA_ACTION_TEST_CONFIRMED_STATE:-${PANAMA_ACTION_TEST_STATE:-true}} == true ]]
@@ -99,6 +115,7 @@ run_action() {
: >"$command_log"
HOME="$work/home" PATH="$fake_bin:$PATH" \
PANAMA_ACTION_HELPER_DIR="$fake_bin" \
PANAMA_ACTION_KILL_COMMAND="$fake_bin/kill" \
PANAMA_ACTION_CONFIRM_ATTEMPTS=1 \
PANAMA_ACTION_CONFIRM_DELAY=0 \
PANAMA_ACTION_TEST_LOG="$command_log" \
@@ -137,6 +154,10 @@ assert_commands $'qs <ipc> <call> <notifications> <dnd>\npanama-osd <message> <n
PANAMA_ACTION_TEST_STATE=false run_action caffeine
assert_commands $'qs <ipc> <call> <caffeine> <toggle>\npanama-osd <message> <weather-clear-night-symbolic> <Caffeine Off>'
printf '4242\n' >"$work/inhibitors"
PANAMA_ACTION_TEST_STATE=false PANAMA_ACTION_TEST_INHIBITOR_FILE="$work/inhibitors" run_action caffeine
assert_commands $'qs <ipc> <call> <caffeine> <toggle>\nkill <4242>\npanama-osd <message> <weather-clear-night-symbolic> <Caffeine Off>'
run_action night-light
assert_commands $'qs <ipc> <call> <night-light> <toggle>\npanama-osd <message> <night-light-symbolic> <Night Light On>'