Skip to content

Instantly share code, notes, and snippets.

@timup
Forked from mozbugbox/xscreensaver-suspend
Created March 6, 2023 16:29

Revisions

  1. @mozbugbox mozbugbox created this gist Aug 10, 2014.
    62 changes: 62 additions & 0 deletions xscreensaver-suspend
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #!/bin/bash
    # suspend/sleep linux system on xscreensaver activation
    #
    # run the bash script in your .xsession,
    # or something like .config/lxsession/LXDE/autostart for lxde

    timeout=60 # After xscreensaver activated, wait timeout to suspend
    read_timeout=5 # timeout for read from `xscreensaver-command -watch`

    SUSPEND_BIN=/usr/sbin/pm-suspend
    XSCREEN_CMD=/usr/bin/xscreensaver-command


    timeout=$(($timeout/$read_timeout))
    blanked=0
    timer=0
    #echo $timeout

    # count down before suspend
    do_suspend() {
    timer=$(($timer + 1))
    if [ ${timer} -ge ${timeout} ]; then
    timer=0
    sudo ${SUSPEND_BIN}
    #echo "do suspend"
    fi
    #echo "timer: ${timer}"
    }

    process_watch() {
    # suspend after screen blank
    while :
    do
    # read xscreensaver -watch output
    read -t ${read_timeout} input
    if [[ $? -gt 0 && $? -lt 128 ]]; then
    break
    fi

    case "$input" in
    UNBLANK*)
    blanked=0
    timer=0
    ;;
    LOCK* | BLANK*)
    if [ ${blanked} = 0 ]; then
    blanked=1
    timer=0 # restart timer
    fi
    ;;
    esac
    #echo "$input $blanked"

    # we are blanked, check do_suspend
    if [ ${blanked} -gt 0 ]; then
    do_suspend
    fi
    done
    }

    ${XSCREEN_CMD} -watch | process_watch