1
0
Fork 0
gnu-plus-dotfiles/dot_config/sway/caffeine.sh
rain 05e75bc41e sway: extract start-swayidle.sh as single source of truth (fix caffeine)
caffeine.sh click was silently breaking idle lock. Two bugs that
compounded:

1. caffeine.sh inlined its own swayidle command (with swaylock -f -i
   , not lock-fancy.sh), drifted out of sync with sway/config
   which used lock-fancy.sh. Toggle off would restart swayidle with a
   different lock command than the rest of the session.

2. WAYLAND_DISPLAY isn't set in waybar's on-click context. When
   caffeine.sh called 'swaymsg output * power off' (part of the
   restart command) and later called swaylock / grim (via lock-fancy),
   those failed silently. swayidle itself can run without
   WAYLAND_DISPLAY but it can't actually monitor input/output, so it
   exits immediately. Result: user clicks caffeine off, flag clears,
   icon goes back to 'inactive', but auto-lock is silently dead.
   The user thinks they turned caffeine off. They didn't. They're
   just unprotected.

Fix:
- new start-swayidle.sh holds the canonical swayidle command
- sway/config: exec $HOME/.config/sway/start-swayidle.sh
- caffeine.sh: killall swayidle; start-swayidle.sh &
- start-swayidle.sh probes /proc/*/environ for a Wayland client's
  env (mako always has it) and exports WAYLAND_DISPLAY /
  DBUS_SESSION_BUS_ADDRESS / XDG_RUNTIME_DIR / DISPLAY if missing
  before exec'ing swayidle

Single source of truth + env restoration = click works on every box
that has the same Wayland-capable process tree (mako, swaybar, etc.),
no matter who/what is calling the script.

Verified on tadbit: pre-fix toggle off killed swayidle permanently.
Post-fix: toggle off restarts swayidle successfully, env inherited
from mako's /proc/PID/environ.
2026-06-23 21:51:09 -04:00

32 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
# caffeine.sh — toggle caffeine mode by killing/restarting swayidle
# When ON: kill swayidle (no idle lock/screen-off)
# When OFF: restart swayidle with the same timeouts as the Sway session
#
# The "restart" path calls start-swayidle.sh, which is the SAME script
# Sway's config uses (`exec $HOME/.config/sway/start-swayidle.sh`).
# Single source of truth: if you change timeouts, change them in
# start-swayidle.sh and both the session start AND the toggle share them.
# (Earlier this script hardcoded the swayidle command itself, and it
# drifted out of sync with sway/config — toggling caffeine off would
# silently change your lock command and timeout values.)
#
# start-swayidle.sh also handles env restoration: when invoked from
# waybar's on-click, WAYLAND_DISPLAY isn't set, and swayidle silently
# exits without it (which means idle lock is silently dead — the
# worst failure mode because the bar still shows "caffeine off").
FLAG="/tmp/caffeine-inhibit"
if [ -f "$FLAG" ]; then
# Turn OFF caffeine — restart swayidle
rm -f "$FLAG"
killall swayidle 2>/dev/null
"$HOME/.config/sway/start-swayidle.sh" &
notify-send -t 2000 "☕ Caffeine OFF" "Idle sleep enabled" 2>/dev/null
else
# Turn ON caffeine — kill swayidle
touch "$FLAG"
killall swayidle 2>/dev/null
notify-send -t 2000 "☕ Caffeine ON" "Idle sleep disabled" 2>/dev/null
fi