ref: a3188c16c2d36b1e3262955452bff6c36edc3f48 dotfiles/overlay/bin/apply-dpms -rwxr-xr-x 2.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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