#!/bin/bash
# Pauses/resumes screensaverd's idle detection AND the X server's own
# blanking/DPMS timeout, so "off" really means nothing blanks the screen.
# Run once to pause (also stops the screensaver immediately if it's showing
# right now), run again to resume. State is a flag file screensaverd polls
# every second, so toggling the TTE side takes effect within ~1s regardless
# of what screensaverd is doing at the time.
set -u

export DISPLAY="${DISPLAY:-:0}"
PAUSE_FILE="${XDG_RUNTIME_DIR:-/tmp}/screensaver-paused"

# Must match what install.sh sets up (`xset s 300 300` / `xset dpms 300 300 300`).
BLANK_TIMEOUT=300

if [[ -e "$PAUSE_FILE" ]]; then
  rm -f "$PAUSE_FILE"
  xset s "$BLANK_TIMEOUT" "$BLANK_TIMEOUT"
  xset +dpms
  xset dpms "$BLANK_TIMEOUT" "$BLANK_TIMEOUT" "$BLANK_TIMEOUT"
  echo "screensaver: återupptagen"
else
  touch "$PAUSE_FILE"
  xset s off
  xset -dpms
  echo "screensaver: pausad"
fi
