-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreenlocker
More file actions
executable file
·285 lines (259 loc) · 7.55 KB
/
screenlocker
File metadata and controls
executable file
·285 lines (259 loc) · 7.55 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/sh
#####################
# --- conf vars --- #
#####################
blank_timeout="600"
auth_font="Noto Sans CJK JP"
password_prompt="disco"
show_hostname=0
show_username=1
burnin_mitigation=50
burnin_mitigation_pixels=1
if [ -r "$CONF" ]; then
# yes, we just outright source the config file without caring...
# you can only override things written before this, so no arbitrary
# functions from config
. "$CONF"
fi
#####################
# --- variables --- #
#####################
# type: string
# description
# script base name through the idiom "${0##*/}"
myname="${0##*/}"
# type: int
# description
# current running pid of the script
mypid="$$"
# color 0
# type: hex string
color0=""
# color 2
# type: hex string
color2=""
# color 8
# type: hex string
color8=""
# color 10
# type: hex string
color10=""
# color 12
# type: hex string
color12=""
# color 15
# type: hex string
color15=""
# type: int
# description:
# randomly chosen color index
COLO_OPT=$(shuf -n 1 -e 1 2 3 4 5)
# type: hex string
# description:
# color used for auth background
AuthBgColor=""
# type: hex string
# description:
# color used for auth foreground
AuthFgColor=""
# type: int
# description:
# pid of current xsecurelock instance
xsecurelock_pid=""
# type: int
# description:
# pid of launched dimmer
dimmer_pid=""
# type: int
# description:
# pid of delayed sleep
delaysleep_pid=""
# type: string
# description: usleep path if available
has_usleep=""
#####################
# --- functions --- #
#####################
# return type: boolean
# usage: is_int "value"
# description: check if passed value is a number
is_int() {
if [ -n "$1" ]; then
printf %d "$1" >/dev/null 2>&1
else
return 1
fi
}
# thin awk wrapper that will prefer mawk over gawk
u_awk () { awk "$@"; }
if command -v mawk >/dev/null; then
u_awk () { mawk "$@"; }
fi
# usage: pid_tree_search PID NAME
# PID: the parent pid among whose ps tree we will search
# NAME: the name of the program whose pid we want
# return type: integer
# return error: standard error return value 1
pid_tree_search () {
search_pid="$1"
search_name="$2"
word_length="${#search_name}"
rval=$(
pstree -Aps "${search_pid}" \
| u_awk \
-v name="$search_name" \
-v wlen="$word_length" \
'\
BEGIN { search=name"\\([[:digit:]]*\\)" } \
match( $0, search )\
{\
print substr($0,RSTART+wlen+1,RLENGTH-wlen-2) \
}\
'
)
if is_int "$rval"; then
printf '%s\n' "$rval"
else
return 1
fi
}
# Usage: write_log "message text"
# Return: void
# Description:
# Write message to log file
write_log () {
name="$myname"
message="$*"
printf '[%s] %12s %*s: %s\n' \
"$(date +'%Y-%m-%d %H:%M:%S')" \
"$name" \
"$PIDWIDTH" "[$mypid]" \
"$message" \
>> "$LOGFILE"
}
# return type: void
# description:
# kill dim-screen.sh process
# that may be dangling from previous instances
kill_dangling_dimmers () {
if pid_tree_search "$LOCKERD_PID" "dim-screen.sh" > /dev/null; then
dangling_dim_screen_pid=$(pid_tree_search "$LOCKERD_PID" dim-screen.sh)
dimmer_comm=$(ps -p "$dangling_dim_screen_pid" -o command=)
kill "$dangling_dim_screen_pid"
write_log \
"dangling ${dimmer_comm##*/} PID: $dangling_dim_screen_pid killed."
fi
}
# return type: void
# description:
# start xsecurelock and capture it's pid
start_xsecurelock () {
# from config
export XSECURELOCK_FONT="$auth_font"
export XSECURELOCK_PASSWORD_PROMPT="$password_prompt"
export XSECURELOCK_SHOW_HOSTNAME="$show_hostname"
export XSECURELOCK_SHOW_USERNAME="$show_username"
export XSECURELOCK_BURNIN_MITIGATION="$burnin_mitigation"
export XSECURELOCK_BURNIN_MITIGATION_DYNAMIC="$burnin_mitigation_pixels"
# defined at runtime
export XSECURELOCK_AUTH_TIMEOUT="$SAVER_TIMEOUT"
export XSECURELOCK_AUTH_BACKGROUND_COLOR="$AuthBgColor"
export XSECURELOCK_AUTH_FOREGROUND_COLOR="$AuthFgColor"
# constants
export XSECURELOCK_SAVER="${HOME}/.local/bin/saver.sh"
export XSECURELOCK_SAVER_RESET_ON_AUTH_CLOSE=1
export XSECURELOCK_COMPOSITE_OBSCURER=0
export XSECURELOCK_NO_COMPOSITE=1
export XSECURELOCK_BLANK_TIMEOUT="$blank_timeout"
export XSECURELOCK_SAVER_STOP_ON_BLANK=1
export XSECURELOCK_BLANK_DPMS_STATE=off
xsecurelock 2>/dev/null &
xsecurelock_pid=$!
sleep 1
if ps -p "$xsecurelock_pid" -o cmd | grep -q "xsecurelock" ; then
write_log \
"launched xsecurelock PID: $xsecurelock_pid"
else
write_log "xsecurelock start failed, trying again."
start_xsecurelock
fi
}
# usage: msleep int
# description: sleep for milliseconds
# return type: void
msleep () {
milisecs="$1"
if [ -n "$has_usleep" ]; then
microsecs="${milisecs}000"
case "$has_usleep" in
*/usleep)
usleep "$microsecs"
;;
*/busybox)
busybox usleep "$microsecs"
;;
esac
else
secs=$(awk -v s="${milisecs}" 'BEGIN {printf "%.3f", s/1000}')
sleep "$secs"
fi
}
if pid_tree_search "$LOCKERD_PID" "xsecurelock" >/dev/null; then
instance_pid=$(pid_tree_search "$LOCKERD_PID" "xsecurelock")
write_log \
"xsecurelock instance" \
"$instance_pid" \
"already running, no new instance will be launched."
exit
else
write_log "no xsecurelock instance running."
write_log "starting screenlocking."
. "${HOME}/.cache/wal/colors.sh"
export SAVER_OPT="$COLO_OPT"
case "${COLO_OPT}" in
1) export AuthBgColor="$color0" ;;
2) export AuthBgColor="$color2" ;;
3) export AuthBgColor="$color8" ;;
4) export AuthBgColor="$color10" ;;
5) export AuthBgColor="$color15" ;;
esac
case "${COLO_OPT}" in
1) export AuthFgColor="$color12" ;;
2) export AuthFgColor="$color15" ;;
3) export AuthFgColor="$color15" ;;
4) export AuthFgColor="$color0" ;;
5) export AuthFgColor="$color8" ;;
esac
start_xsecurelock
has_usleep=$(command -v usleep)
[ -z "$has_usleep" ] && has_usleep=$(command -v busybox)
kill_dangling_dimmers
# auth watcher
while pid_tree_search "$mypid" xsecurelock >/dev/null; do
if pid_tree_search "$mypid" auth_x11 >/dev/null; then
# kill_dangling_dimmers
if kill -0 "$dimmer_pid" 2>/dev/null; then
dimmer_comm=$(ps -p "$dimmer_pid" -o command=)
kill -0 "$dimmer_pid" 2>/dev/null && \
kill "$dimmer_pid" 2>/dev/null && \
write_log "${dimmer_comm##*/} PID: $dimmer_pid killed."
fi
if kill -0 "$delaysleep_pid" 2>/dev/null; then
kill "$delaysleep_pid" 2>/dev/null && \
write_log "delaysleep PID: $delaysleep_pid killed."
fi
else
if ! pid_tree_search "$mypid" delaysleep >/dev/null; then
delaysleep -watch "$mypid" &
delaysleep_pid=$(pid_tree_search "$LOCKERD_PID" "delaysleep")
fi
if ! pid_tree_search "$mypid" dim-screen.sh >/dev/null; then
dim-screen.sh -step-time 0.2 &
dimmer_pid=$(pid_tree_search "$LOCKERD_PID" "dim-screen.sh")
fi
fi
msleep 100
done
kill_dangling_dimmers
fi
write_log "screen unlocked."