aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/xblank
blob: dc675d9274ac63b3deae762a6b7e60f1152327f9 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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 "$@"