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 echo "xscreensaver.lockTimeout: $lock_time_minutes" | xrdb -merge
54 xscreensaver >/dev/null &
56 elif installed xautolock; then
57 if installed "$lock_bin"; then
58 xautolock -secure -time "$lock_time_minutes" \
59 -locker "$lock_binary" \
63 echo "Locker '$lock_bin' not installed. Auto lock won't work!"
67 echo "No locker found. Auto lock won't work!"
71 elif test x"$1" = xlock; then
72 if test $# -ne 1; then
76 if installed xscreensaver; then
77 # Start xscreensaver if it's not already running. xscreensaver-command
78 # ensures xscreensaver is available for the current X session.
79 xscreensaver-command -time >/dev/null 2>&1 || xscreensaver &
81 xscreensaver-command -lock
82 # It can take a while for xscreensaver to start, wait until it's
84 while test $? -eq 255; do
85 xscreensaver-command -lock
87 elif installed xtrlock; then
88 # Sleep is necessary to allow xtrlock to grab the keyboard input.
92 echo 'No screen locker found!'