/
xlock
executable file
·277 lines (243 loc) · 6.68 KB
/
xlock
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
#!/bin/sh -eu
###
### Defaults
###
DEFAULT_BACKGROUND="0"
DEFAULT_SCALE="3"
DEFAULT_OVERLAY_PATH=""
DEFAULT_NOTIFY_DAEMON="dunst"
DEFAULT_MUTE="0"
###
### Credits
###
MY_VERSION="v0.1"
MY_DATE="2017-07-05"
MY_URL="https://github.com/cytopia/i3-utils"
###
### System variables
###
MY_NAME="$( basename "${0}" )"
MY_LOG_DIR="${HOME}/.log"
MY_LOG_FILE="${MY_LOG_DIR}/${MY_NAME}.err"
MY_DATE="$( date '+%Y-%m-%d' )"
MY_TIME="$( date '+%H:%M:%S' )"
if [ ! -d "${MY_LOG_DIR}" ]; then
mkdir -p "${MY_LOG_DIR}"
fi
if [ ! -f "${MY_LOG_FILE}" ]; then
touch "${MY_LOG_FILE}"
fi
if [ ! -w "${MY_LOG_FILE}" ]; then
chmod u+w "${MY_LOG_FILE}"
fi
################################################################################
##
## F U N C T I O N S
##
################################################################################
# Test if argument is an integer.
#
# @param mixed
# @return integer 0: is int | 1: not an int
isint() {
echo "${1}" | grep -Eq '^([0-9]|[1-9][0-9]*)$'
}
error() {
printf "%s\n" "${1}" >&2
printf "[%s] [%s] %s\n" "${MY_DATE}" "${MY_TIME}" "${1}" >> "${MY_LOG_FILE}"
}
print_usage() {
echo "Usage: ${MY_NAME} [-b] [-m] [-s <1-100>] [-o <path>] [-n <name>]"
echo " ${MY_NAME} -h"
echo " ${MY_NAME} -v"
echo
echo "Helper script for i3lock."
echo
echo " -b (optional) Background mode."
echo " The whole script is put into background so that you can"
echo " run another script afterwards, such as system suspend/hibernat."
echo " The default is to run in foreground mode."
echo
echo " -m (optional) Mute sound before locking the screen."
echo
echo " The whole script is put into background so that you can"
echo " -s <1-100> (optional) Scale factor in percent."
echo " The default is 3."
echo
echo " -o <path> (optional) Path to an image to overlay onto lockscreen."
echo " The default is not to use an overlay image."
echo
echo " -n <name> (optional) Binary name of the notification daemon to suspend"
echo " while the screen is locked. It should be suspended so"
echo " that no notifications will be displayed while you are"
echo " away with a locked screen."
echo " The default is 'dunst'."
echo
echo " -h Show this help screen."
echo " -v Show version information."
}
print_version() {
printf "%s %s (%s) by cytopia\n" "${MY_NAME}" "${MY_VERSION}" "${MY_DATE}"
printf "%s\n" "${MY_URL}"
}
lock_screen() {
# 1-100%
_scale_factor="${1}"
# Path to overlay image
_overlay_path="${2}"
# i3lock foreground mode?
_i3lock_background="${3}"
_upscale_factor="$(( 100 * 100 / _scale_factor ))"
_screenshot="$( mktemp --suffix='.png' )"
# Ensure we do not abort here in case of ImageMagick error
if command -v maim >/dev/null 2>&1; then
echo "maim"
maim --quality 10 --delay=0.0 "${_screenshot}.jpg"
else
echo "scrot"
scrot --quality 100 --silent "${_screenshot}.jpg"
fi
mv "${_screenshot}.jpg" "${_screenshot}"
convert ${_screenshot} -scale ${_scale_factor}% -scale ${_upscale_factor}% ${_screenshot}
# Add overlay (optional)
if [ -f "${_overlay_path}" ]; then
# Ensure we do not abort here in case of ImageMagick error
convert "${_screenshot}" "${_overlay_path}" -gravity center -geometry +0+0 -composite -matte "${_screenshot}"
fi
# Lock screen
if [ "${_i3lock_background}" = "1" ]; then
i3lock --show-failed-attempts --image="${_screenshot}"
else
i3lock --nofork --show-failed-attempts --image="${_screenshot}"
fi
}
suspend_notify() {
_notify_daemon="${1}"
# Suspend notification daemon
pkill -u "${USER}" -USR1 "${_notify_daemon}"
}
resume_notify() {
_notify_daemon="${1}"
# Wait until i3lock is closed (screen not locked anymore)
while pidof i3lock >/dev/null 2>&1; do
sleep 1
done
# Resume notification daemon
pkill -u "${USER}" -USR2 "${_notify_daemon}"
}
mute_sound() {
if ! _err="$( amixer sset 'Master' mute 2>&1 >/dev/null )"; then
error "${_err}"
exit 1
fi
}
################################################################################
##
## E N T R Y P O I N T
##
################################################################################
BACKGROUND="${DEFAULT_BACKGROUND}"
SCALE="${DEFAULT_SCALE}"
OVERLAY_PATH="${DEFAULT_OVERLAY_PATH}"
NOTIFY_DAEMON="${DEFAULT_NOTIFY_DAEMON}"
MUTE="${DEFAULT_MUTE}"
while [ $# -gt 0 ]; do
case "${1}" in
-b)
BACKGROUND="1"
;;
-m)
MUTE="1"
;;
-s)
shift
if ! isint "${1}"; then
error "Argument for -s: Is not an integer: ${1}"
exit 1
fi
if [ "${1}" -lt 0 ] || [ "${1}" -gt 100 ]; then
error "Argument for -s: Must be between 1 and 100. Specified: ${1}"
exit 1
fi
SCALE="${1}"
;;
-o)
shift
if [ ! -f "${1}" ]; then
error "Argument for -o: File does not exist: ${1}"
exit 1
fi
OVERLAY_PATH="${1}"
;;
-n)
shift
NOTIFY_DAEMON="${1}"
;;
-h)
print_usage
exit 0
;;
-v)
print_version
exit 0
;;
*)
error "Invalid argument supplied: ${1}"
exit 1
;;
esac
shift
done
###
### Error Checking
###
if ! command -v maim >/dev/null 2>&1; then
if ! command -v scrot >/dev/null 2>&1; then
error "binary 'maim' or 'scrot' not found but required."
exit 1
fi
fi
if ! command -v convert >/dev/null 2>&1; then
error "binary 'convert' not found but required."
exit 1
fi
if ! command -v i3lock >/dev/null 2>&1; then
error "binary 'i3lock' not found but required."
exit 1
fi
if [ "${MUTE}" = "1" ]; then
if ! command -v amixer >/dev/null 2>&1; then
error "binary 'amixer' not found but required when using -m option."
exit 1
fi
fi
# Make sure not to start another i3lock instance,
# if it is already running.
if pidof i3lock >/dev/null 2>&1; then
error "i3lock is already running, aborting."
exit 1
fi
# Dunst shows notifications in lock screen by default.
# In order to prevent that, we need to suspend the dunst
# process prior locking and resume after the lock process
# finishes.
# Suspend notification daemon
suspend_notify "${NOTIFY_DAEMON}"
# Mute sound if desired
if [ "${MUTE}" = "1" ]; then
mute_sound
fi
# Lock screen foreground or background task
# depening on the command line argument
lock_screen "${SCALE}" "${OVERLAY_PATH}" "${BACKGROUND}"
# Resume notification service.
# In case of background service, the function will be brought
# into background itself and the script finishes.
# This way you can start other services after this script
# such as suspend to RAM/Disk and during wake-up, you will still
# have a locked screen with resuming notification during unlock
if [ "${BACKGROUND}" = "1" ]; then
resume_notify "${NOTIFY_DAEMON}" &
else
resume_notify "${NOTIFY_DAEMON}"
fi