~charles/dotfiles

ref: b8a2505022f932e959c810a5fcff310a4e3d0e27 dotfiles/overlay/bin/dpmsman -rwxr-xr-x 2.5 KiB View raw
b8a25050Charles Daniels update mimassoc for focal a month ago
                                                                                
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
95
96
97
98
99
#!/usr/bin/env bash

# Sets DPMS settings with apply-dpms when interrupted with USR1.

# The DPMS profile is specified in ~/.config/dpms/current which should contain
# a single integer, which is the number of minutes before the display should
# time out.  If this file is not present, then it is automatically populated
# with either ~/.config/dpms/docked or ~/.config/dpms/undocked according to if
# the laptop is docked or not.

# As a fallback, the timeout will default to 15 minutes.

# Note that a setting of 0 will disable DPMS timeout.

# When the DPMS timeout is changed, `idle` will also be killed and restarted
# with the same timeout, plus one minute. If DPMS timeout is disabled, then
# xidle will be as well.

CONFIG_DIR="$HOME/.config/dpms"
CONFIG_CUR="$CONFIG_DIR/current"
CONFIG_DOCKED="$CONFIG_DIR/docked"
CONFIG_UNDOCKED="$CONFIG_DIR/undocked"
CONFIG_DEFAULT=15

trigger_reconfigure () {
	echo "($(date)) reconfiguring DPMS... "

	desired_profile=$CONFIG_DEFAULT

	DOCK_STATE=UNDOCKED
	if lsusb | grep -q "17ef:100a" ; then
		DOCK_STATE=DOCKED
	fi

	echo "DOCK_STATE=$DOCK_STATE"

	if [ -e "$CONFIG_CUR" ] ; then
		desired_profile="$(cat "$CONFIG_CUR")"

	elif [ "$DOCK_STATE" = "DOCKED" ] ; then
		if [ -f "$CONFIG_DOCKED" ] ; then
			desired_profile="$(cat "$CONFIG_DOCKED")"
		fi
	elif [ "$DOCK_STATE" = "UNDOCKED" ] ; then
		if [ -f "$CONFIG_UNDOCKED" ] ; then
			desired_profile="$(cat "$CONFIG_UNDOCKED")"
		fi
	fi

	echo "desired_profile=$desired_profile"

	timeout=$(expr $desired_profile '*' 60)

	current_profile=$(expr $(xset q dpms | grep Standby | cut -d: -f2 | cut -d' ' -f2) / 60)

	echo "current_profile=$current_profile"

	if [ "$current_profile" = "$desired_profile" ] ; then
		echo "current and desired profiles match"
		if ! pgrep xidle > /dev/null ; then
			if [ $desired_profile -gt 0 ] ; then
				# xidle must have been killed
				echo "starting xidle... "
				xidle -timeout "$(expr $timeout + 60)" -program "$(which xlock)"  < /dev/null > /dev/null 2>&1 &
				disown
			fi
		fi
		return
	fi

	# never sleep
	if [ $desired_profile -eq 0 ] ; then
		pkill xidle
		xset dpms 0 0 0
		xset s noblank
		xset s off
		echo "will never sleep"
	else
		xset dpms $timeout $timeout $timeout
		pkill xidle
		xidle -timeout $(expr $timeout + 60) -program "$(which xlock)" < /dev/null > /dev/null 2>&1 &
		disown
		echo "sleeping after $timeout s"
	fi
}

if pgrep dpmsman | grep -qv $$ ; then
	echo "dpmsman is already running" 1>&2
	pkill -USR1 dpmsman
	exit 1
fi

trap trigger_reconfigure USR1

trigger_reconfigure

while true ; do
	sleep 1
done