1#!/usr/bin/env bash
2
3export DISPLAY="${DISPLAY:-:0}"
4
5STATE_FILE="$XDG_CACHE_HOME/caffeine_state"
6PID_FILE="$XDG_CACHE_HOME/caffeine.pid"
7
8caffeine_pid() {
9 systemd-inhibit --list | awk '
10 $1 == "Caffeine" && $5 == "systemd-inhibit" { print $4 }
11 '
12}
13
14case $1 in
15toggle)
16 PID="$(caffeine_pid)"
17 if [ -n "$PID" ]; then
18 kill "$PID" 2>/dev/null
19 rm -f "$STATE_FILE" "$PID_FILE"
20 xset +dpms s on
21 notify-send "Caffeine off" "System might go to sleep."
22 else
23 systemd-inhibit --what=idle:sleep:shutdown --mode=block \
24 --who="Caffeine" --why="User-activated session protection" \
25 sleep infinity &
26 echo $! >"$PID_FILE"
27 touch "$STATE_FILE"
28 xset -dpms s off
29 notify-send "Caffeine on" "System will stay awake."
30 fi
31 ;;
32status)
33 if xset q 2>/dev/null | grep -q "DPMS is Disabled"; then
34 echo ""
35 else
36 echo ""
37 fi
38 ;;
39esac