-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenlocker
executable file
·231 lines (213 loc) · 6.44 KB
/
screenlocker
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
#!/bin/sh
#####################
# --- 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 0 2 8 10 15)
# 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=""
#####################
# --- 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
}
# return type: void
# description:
# kill dim-screen.sh process
# that may be dangling from previous instances
kill_dangling_dimmers () {
if pid_tree_search "$mypid" "dim-screen.sh" > /dev/null; then
dangling_dim_screen_pid=$(pid_tree_search "$mypid" dim-screen.sh)
dimmer_comm=$(ps -p "$dangling_dim_screen_pid" -o command=)
kill "$dangling_dim_screen_pid"
printf '[%s] %s: dangling "%s" PID: %s killed.\n' \
"$(date +"%F %T")" "${myname}" \
"${dimmer_comm##*/}" \
"$dangling_dim_screen_pid"
fi
}
# return type: void
# description:
# start xsecurelock and capture it's pid
start_xsecurelock () {
XSECURELOCK_FONT="Noto Sans CJK JP" \
XSECURELOCK_PASSWORD_PROMPT="disco" \
XSECURELOCK_SAVER=~/.local/bin/saver.sh \
XSECURELOCK_SAVER_RESET_ON_AUTH_CLOSE=1 \
XSECURELOCK_SHOW_HOSTNAME=0 \
XSECURELOCK_SHOW_USERNAME=1 \
XSECURELOCK_DATETIME_FORMAT='%A %T' \
XSECURELOCK_COMPOSITE_OBSCURER=0 \
XSECURELOCK_NO_COMPOSITE=1 \
XSECURELOCK_AUTH_TIMEOUT=120 \
XSECURELOCK_BLANK_TIMEOUT=-1 \
XSECURELOCK_SAVER_STOP_ON_BLANK=1 \
XSECURELOCK_BURNIN_MITIGATION=50 \
XSECURELOCK_BURNIN_MITIGATION_DYNAMIC=1 \
XSECURELOCK_AUTH_BACKGROUND_COLOR=$AuthBgColor \
XSECURELOCK_AUTH_FOREGROUND_COLOR=$AuthFgColor \
XSECURELOCK_BLANK_DPMS_STATE=off \
xsecurelock 2>/dev/null &
xsecurelock_pid=$!
sleep 1
if ps -p "$xsecurelock_pid" -o cmd | grep -q "xsecurelock" ; then
printf '[%s] %s: xsecurelock %s launched.\n' \
"$(date +"%F %T")" \
"${myname}" \
"$xsecurelock_pid" >> "${HOME}/.cache/xsecurelock"
printf '[%s] %s: launched %s PID: %s \n' \
"$(date +"%F %T")" \
"${myname}" \
"$(ps -p $xsecurelock_pid -o command=)" "$xsecurelock_pid"
else
printf '[%s] %s: xsecurelock start failed, trying again.\n' \
"$(date +"%F %T")" "${myname}"
start_xsecurelock
fi
}
if pidof xsecurelock >/dev/null ; then
instance_pid=$(pidof xsecurelock)
printf '[%s] %s: %s %s %s\n' \
"$(date +"%F %T")" "${myname}" \
"xsecurelock instance" \
"$instance_pid" \
"already running, no new instance will be launched."
printf '[%s] %s: xsecurelock %s already running.\n' \
"$(date +"%F %T")" \
"${myname}" \
"$instance_pid" >> "${HOME}/.cache/xsecurelock"
exit
else
printf '[%s] %s: no xsecurelock instance running.\n' \
"$(date +"%F %T")" "${myname}"
printf '[%s] %s: starting screenlocking.\n' \
"$(date +"%F %T")" "${myname}"
. "${HOME}/.cache/wal/colors.sh"
export SAVER_OPT="$COLO_OPT"
case "${COLO_OPT}" in
0) export AuthBgColor="$color0" ;;
2) export AuthBgColor="$color2" ;;
8) export AuthBgColor="$color8" ;;
10) export AuthBgColor="$color10" ;;
15) export AuthBgColor="$color15" ;;
esac
case "${COLO_OPT}" in
0) export AuthFgColor="$color12" ;;
2) export AuthFgColor="$color15" ;;
8) export AuthFgColor="$color15" ;;
10) export AuthFgColor="$color0" ;;
15) export AuthFgColor="$color8" ;;
esac
start_xsecurelock
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 && \
printf '[%s] %s: "%s" PID: %s killed.\n' \
"$(date +"%F %T")" \
"${myname}" \
"${dimmer_comm##*/}" \
"$dimmer_pid"
fi
else
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
sleep 0.1
done
kill_dangling_dimmers
fi
printf '[%s] %s: screen unlocked.\n' \
"$(date +"%F %T")" "${myname}"
printf '[%s] %s: screen unlocked.\n' \
"$(date +"%F %T")" "${myname}" >> "${HOME}/.cache/xsecurelock"