#!/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