diff options
Diffstat (limited to '.local/bin/xblank')
-rwxr-xr-x | .local/bin/xblank | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/.local/bin/xblank b/.local/bin/xblank new file mode 100755 index 0000000..dc675d9 --- /dev/null +++ b/.local/bin/xblank @@ -0,0 +1,204 @@ +#!/bin/sh + +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. + +readonly PROGRAM_NAME="$(basename "$0")" + +main() +{ + # Handle opts + while getopts ":C:S:c:ht:v" opt + do + case "$opt" in + S) rundir=$OPTARG ;; + d) command_dir=$OPTARG ;; + h) usage && exit 0 ;; + v) verbose=1 ;; + *) + printf "Invalid option passed: %s\n" "$OPTARG" >&2 + ;; + esac + done + + shift $(( OPTIND - 1 )) + + # Set default values + [ -z "$rundir" ] && rundir="$XDG_RUNTIME_DIR/$PROGRAM_NAME" + + time_old=0 + time_now="$(xprintidle)" + + # Check availability of dependencies + for i in xprintidle xset + do + command -v "$i" >/dev/null && continue + + log "Missing dependency $i" + exit 3 + done + + # Ensure $DISPLAY is set + if [ -z "$DISPLAY" ] + then + log "\$DISPLAY is unset" + exit 4 + fi + + # Initialize the rundir + log "Create rundir ($rundir)" + mkdir -p -- "$rundir" + + printf "0\n" > "$rundir/paused" + [ ! -f "$rundir/state" ] && printf "active\n" > "$rundir/state" + + # Enter the loop + while : + do + # Do nothing if the program is paused + if is_paused + then + log "$PROGRAM_NAME is paused ($rundir)" + + sleep 1 + continue + fi + + # Get the time the system has been idle in milliseconds + time_old=$time_now + time_now="$(($(xprintidle) / 1000))" + + # Run command_active upon detecting activity + if [ "$((time_now))" -lt "$((time_old))" ] + then + log "Detected activity ($time_now < $time_old)" + cmd="$(get_script active)" + + if [ "$cmd" = "" ] + then + log "No executable found for activation" + + sleep 1 + continue + fi + + log "> $cmd" + $cmd + fi + + # Run command based on inactivity duration + loop_index=$(($time_old)) + + log "Running (potential) scripts $time_old through $time_now" + + while [ "$(($loop_index))" -lt "$(($time_now))" ] + do + loop_index="$(($loop_index + 1))" + + log "Running script for $loop_index" + + cmd=$(get_script idle_$loop_index) + + if [ "$cmd" = "" ] + then + log "No executable found for idling (${loop_index}s)" + + continue + fi + + log "> $cmd" + $cmd + done + + # Sleep to not burn your CPU + sleep 1 + done +} + +get_script() +{ + if [ -n "$command_dir" ] + then + if [ -x "$command_dir/$1" ] + then + printf "%s\n" "$command_dir/$1" + return + fi + + # Don't continue if a command_dir was explicitly set + return + fi + + set -f + OIFS=$IFS + IFS=: + + dirs="$XDG_CONFIG_HOME:$XDG_CONFIG_DIRS:$HOME/.config" + + for dir in $dirs + do + [ "$dir" = "" ] && continue + + if [ -x "$dir/$PROGRAM_NAME/$1" ] + then + printf "%s\n" "$dir/$PROGRAM_NAME/$1" + return + fi + done + + IFS=$OIFS + set +f +} + +is_paused() +{ + if [ "$(head -n 1 "$rundir/paused")" = "0" ] + then + return 1 + fi + + return 0 +} + +log() +{ + [ -z "$verbose" ] && return + + printf "[%s] %s\n" "$(prettify_time "$time_now")" "$*" >&2 +} + +prettify_time() +{ + printf "%03dh %02dm %02ds\n" \ + "$(( $1 / 60 / 60 ))" \ + "$(( $1 / 60 % 60 ))" \ + "$(( $1 % 60 ))" +} + +usage() +{ + cat <<EOF +Usage: + ${0##*/} -h + ${0##*/} [-C <command>] [-R <path>] [-c <command>] [-t <timeout>] [-v] + +Force-blank the screen after a certain timeout. + +Options: + -C Set the command to run when X is becoming active. + -R Set the location of the rundir. + -c Set the command to run when X is going idle. + -h Show this help text and exit. + -t Set the timeout in seconds. Defaults to 60. + -v Add verbosity. +EOF +} + +main "$@" |