#!/usr/bin/env bash
set -e
set -u
CFG_FILE="$HOME/.config/dpmsconf"
DEFAULT=20
# Configure DPMS settings to one of several profiles
# if no profile specified, then read CFG_FILE, which should be TSV with
# either AC or BAT as column 0, and a profile as column 1
# A profile may be either the string 'never', which means to never sleep the
# monitor, or a number of minutes before blanking
# if "printcfg" is used as the profile, then CFG_FILE is printed to standard
# out and the DPMS settings are unchanged
if [ $# -eq 0 ] ; then
if [ -e "$CFG_FILE" ] ; then
if acpi -a | grep "on-line" > /dev/null 2>&1 ; then
# on AC
config="$(cat "$CFG_FILE" | grep AC | cut -f2)"
else
# on battery
config="$(cat "$CFG_FILE" | grep BAT | cut -f2)"
fi
echo "selected profile $config from config" 1>&2
else
echo "no profile specified, and no $CFG_FILE" 1>&2
exit 1
fi
else
config="$1"
fi
# not sure this does much on modern systems, but in theory it turns of the
# bell, whatever that is.
xset b off
set +e
current_config=$(expr $(xset q dpms | grep Standby | cut -d: -f2 | cut -d' ' -f2) / 60)
set -e
case "$config" in
never)
echo "disabling sleep" 1>&2
set +e
killall xautolock
set -e
xset dpms 0 0 0
;;
[0-9]*)
# if the profile matches the current settings, do nothing
# to avoid disturbing any timers
if [ $config -eq $current_config ] ; then
echo "profile is unchanged." 1>&2
# xautolock should still be running, make sure it has
# not been killed
if [ $(ps aux | awk '$11 ~ /xautolock/' | wc -l) -le 0 ] ; then
echo "xautolock not running, starting it" 1>&2
xautolock -time $config -locker "xset dpms force off && xtrlock -b" &
disown
fi
exit 0
fi
# settings need to be changed
echo "set timeout to $config minutes" 1>&2
# restart xautolock
set +e
killall xautolock
set -e
xautolock -time $config -locker "xset dpms force off && xtrlock -b" &
disown
# apply dpms settings
timeout=$(expr $config '*' 60)
xset dpms $timeout $timeout $timeout
;;
printcfg)
echo "$CFG_FILE"
;;
*)
echo "Invalid DPMS configuration '$config'" 1>&2
exit 1
;;
esac
echo "applied DPMS settings successfully" 1>&2