foxygit / dotfiles Log in
commits tags

/bin/screensaver · 1.05 KB

raw
#!/bin/bash
# Runs random TTE (terminal text effects) animations in a loop.
# Meant to be launched inside a fullscreen terminal by screensaver-launch.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ART_FILE="${SCREENSAVER_ART:-$SCRIPT_DIR/screensaver.txt}"
WIN_CLASS="screensaver"

screensaver_in_focus() {
  local id
  id=$(xdotool getactivewindow 2>/dev/null) || return 1
  xprop -id "$id" WM_CLASS 2>/dev/null | grep -q "\"$WIN_CLASS\""
}

exit_screensaver() {
  tput cnorm 2>/dev/null
  pkill -x tte 2>/dev/null
  exit 0
}

trap exit_screensaver SIGINT SIGTERM SIGHUP SIGQUIT

printf '\033]11;rgb:00/00/00\007' # black background
tput civis 2>/dev/null            # hide text cursor

tty=$(tty 2>/dev/null)

while true; do
  tte -i "$ART_FILE" \
    --frame-rate 120 --canvas-width 0 --canvas-height 0 --reuse-canvas --anchor-canvas c --anchor-text c \
    --random-effect --no-eol --no-restore-cursor &

  while pgrep -t "${tty#/dev/}" -x tte >/dev/null; do
    if read -n1 -t 1 || ! screensaver_in_focus; then
      exit_screensaver
    fi
  done
done