3 # Helper script to start/lock the screen with an available screen locker.
5 # Copyright (C) 2015 Simon Ruderich
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 printf 'usage: %s start <lock time (min)> <lock binary>\n' "$0"
25 printf ' %s lock\n' "$0"
30 type "$1" >/dev/null 2>&1
34 if test $# -eq 0; then
38 if test x"$1" = xstart; then
39 if test $# -ne 3; then
42 lock_time_minutes="$2"
45 # NOTE: We must redirect stdout of the spawned background processes to
46 # /dev/null or a potential caller which uses our output will wait forever
47 # on the inherited stdin (and we become a zombie)! Thanks to Beano on [1]
48 # for the idea, read on 2015-08-23.
50 # [1]: https://stackoverflow.com/questions/3748432/insane-crond-behavior-keeps-making-defunct-bash-processes/3750028#3750028
52 if installed xscreensaver; then
53 printf 'xscreensaver.timeout: %d
54 xscreensaver.lock: True
55 xscreensaver.lockTimeout: 0
56 ' "$lock_time_minutes" | xrdb -merge
57 xscreensaver >/dev/null &
59 elif installed xautolock; then
60 if installed "$lock_binary"; then
61 # Terminate a running xautolock because we might have to replace
62 # its timeout and other settings with our values.
63 pkill -u "$USER" xautolock || true
65 xautolock -secure -time "$lock_time_minutes" \
66 -locker "$lock_binary" \
70 echo "Locker '$lock_binary' not installed. Auto lock won't work!"
74 echo "No locker found. Auto lock won't work!"
78 elif test x"$1" = xlock; then
79 if test $# -ne 1; then
83 if installed xscreensaver; then
84 # Start xscreensaver if it's not already running. xscreensaver-command
85 # ensures xscreensaver is available for the current X session.
86 xscreensaver-command -time >/dev/null 2>&1 || xscreensaver &
88 # It can take a while for xscreensaver to start, wait until it's
89 # ready. a || b && c behaves like (a || b) && c.
91 xscreensaver-command -lock || test $? -ne 255 && break
93 elif installed xtrlock; then
94 # Sleep is necessary to allow xtrlock to grab the keyboard input.
97 elif installed xlock; then
100 echo 'No screen locker found!'