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 xautolock -secure -time "$lock_time_minutes" \
62 -locker "$lock_binary" \
66 echo "Locker '$lock_binary' not installed. Auto lock won't work!"
70 echo "No locker found. Auto lock won't work!"
74 elif test x"$1" = xlock; then
75 if test $# -ne 1; then
79 if installed xscreensaver; then
80 # Start xscreensaver if it's not already running. xscreensaver-command
81 # ensures xscreensaver is available for the current X session.
82 xscreensaver-command -time >/dev/null 2>&1 || xscreensaver &
84 # It can take a while for xscreensaver to start, wait until it's
87 xscreensaver-command -lock || test $? -ne 255 && break
89 elif installed xtrlock; then
90 # Sleep is necessary to allow xtrlock to grab the keyboard input.
94 echo 'No screen locker found!'