1
0
Fork 0
gnu-plus-dotfiles/dot_config/sway/caffeine.sh
rain d78ba4152b sway: factor env restoration into restore-wayland-env.sh, fix caffeine notify
caffeine.sh click was working (icon toggled, swayidle restarted) but
the 'Caffeine ON/OFF' notification never appeared. Same root cause as
the prior swayidle bug: waybar's on-click context strips WAYLAND_DISPLAY
from the spawned script's env. notify-send writes to D-Bus (exit 0) but
mako can't display because it has no Wayland socket to render on. Click
appears to do nothing visually.

Fix: extract the /proc/*/environ probe into a shared helper
(restore-wayland-env.sh) and source it from both caffeine.sh AND
start-swayidle.sh. Now any future on-click script in this repo can
`. restore-wayland-env.sh` to inherit the right env.

Verified on tadbit:
- click 1 (caffeine ON): swayidle killed, flag set, 'Caffeine ON'
  notification appeared in mako
- click 2 (caffeine OFF): swayidle restarted, flag cleared, 'Caffeine
  OFF' notification appeared in mako

Both are visible in makoctl history now.
2026-06-23 22:18:01 -04:00

39 lines
1.6 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.)
#
# At top: source restore-wayland-env.sh to inherit WAYLAND_DISPLAY (and
# friends) from a running Wayland client. waybar's on-click context
# strips these, so without this:
# - notify-send writes to D-Bus (exits 0) but mako can't display
# (no Wayland socket to render on)
# - the click appears to silently do nothing
# This is the SAME bug start-swayidle.sh had for swayidle itself —
# fixed in both places via the shared helper.
# shellcheck source=restore-wayland-env.sh
source "$HOME/.config/sway/restore-wayland-env.sh"
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