1
0
Fork 0

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.
This commit is contained in:
Rain 2026-06-23 22:18:01 -04:00
parent 05e75bc41e
commit d78ba4152b
3 changed files with 49 additions and 29 deletions

View file

@ -16,8 +16,8 @@
# - sway at session start, with the full Wayland/Sway env (correct)
# - caffeine.sh from waybar's on-click, with NO Wayland env (broken — see below)
# When run from caffeine.sh, swayidle needs WAYLAND_DISPLAY to talk to the
# compositor. We probe /proc for the env of an already-running Wayland client
# (mako, swaybar, foot, etc.) and inherit WAYLAND_DISPLAY / DISPLAY /
# compositor. The shared helper restore-wayland-env.sh probes /proc for
# an already-running Wayland client and inherits WAYLAND_DISPLAY / DISPLAY /
# DBUS_SESSION_BUS_ADDRESS / XDG_RUNTIME_DIR from it. This is the same
# trick `dbus-update-activation-environment` uses internally, just per-process.
#
@ -26,30 +26,9 @@
# caffeine.sh, the script is backgrounded (&) and this function returns.
set -e
# --- 1. Restore Wayland env if missing -----------------------------------
# The bug we're fixing: clicking the caffeine button runs this script
# from waybar's on-click context, which doesn't inherit sway's env.
# swayidle and lock-fancy.sh both call swaymsg/grim which need
# WAYLAND_DISPLAY — without it, swayidle silently exits and the user
# has NO idle lock at all (the worst possible failure mode: looks normal
# in the bar, but auto-lock is silently dead).
if [[ -z "$WAYLAND_DISPLAY" && -z "$SWAYSOCK" ]]; then
# Find any running Wayland client and steal its env. mako (notification
# daemon) is always present and always has the right env.
for pid in /proc/[0-9]*; do
[[ -r "$pid/environ" ]] || continue
candidate=$(tr '\0' '\n' < "$pid/environ" 2>/dev/null | grep -E '^(WAYLAND_DISPLAY|DBUS_SESSION_BUS_ADDRESS|XDG_RUNTIME_DIR|DISPLAY)=' || true)
if [[ -n "$candidate" ]]; then
export $candidate
break
fi
done
fi
# shellcheck source=restore-wayland-env.sh
source "$HOME/.config/sway/restore-wayland-env.sh"
# --- 2. Start swayidle ----------------------------------------------------
# If invoked from sway's `exec` line (config), swayidle replaces this shell
# and runs forever. If invoked from caffeine.sh (backgrounded with &), it
# runs as a child of the parent shell and caffeine.sh returns immediately.
exec swayidle -w \
timeout 300 "$HOME/.config/sway/lock-fancy.sh" \
timeout 600 'swaymsg "output * power off"' \