]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/xlockscreen
unify screen locking
[config/dotfiles.git] / bin / xlockscreen
1 #!/bin/sh
2
3 # Helper script to start/lock the screen with an available screen locker.
4
5 # Copyright (C) 2015  Simon Ruderich
6 #
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.
11 #
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.
16 #
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/>.
19
20 set -eu
21
22
23 usage() {
24     printf 'usage: %s start <lock time (min)> <lock binary>\n' "$0"
25     printf '       %s lock\n' "$0"
26     exit 1
27 }
28
29 installed() {
30     type "$1" >/dev/null 2>&1
31 }
32
33
34 if test $# -eq 0; then
35     usage
36 fi
37
38 if test x"$1" = xstart; then
39     if test $# -ne 3; then
40         usage
41     fi
42     lock_time_minutes="$2"
43     lock_binary="$3"
44
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.
49     #
50     # [1]: https://stackoverflow.com/questions/3748432/insane-crond-behavior-keeps-making-defunct-bash-processes/3750028#3750028
51
52     if installed xscreensaver; then
53         echo "xscreensaver.lockTimeout: $lock_time_minutes" | xrdb -merge
54         xscreensaver >/dev/null &
55         echo xscreensaver
56     elif installed xautolock; then
57         if installed "$lock_bin"; then
58             xautolock -secure -time "$lock_time_minutes" \
59                       -locker "$lock_binary" \
60                       >/dev/null &
61             echo xautolock
62         else
63             echo "Locker '$lock_bin' not installed. Auto lock won't work!"
64             exit 1
65         fi
66     else
67         echo "No locker found. Auto lock won't work!"
68         exit 1
69     fi
70
71 elif test x"$1" = xlock; then
72     if test $# -ne 1; then
73         usage
74     fi
75
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 &
80
81         xscreensaver-command -lock
82         # It can take a while for xscreensaver to start, wait until it's
83         # ready.
84         while test $? -eq 255; do
85             xscreensaver-command -lock
86         done
87     elif installed xtrlock; then
88         # Sleep is necessary to allow xtrlock to grab the keyboard input.
89         sleep 1
90         xtrlock
91     else
92         echo 'No screen locker found!'
93         exit 1
94     fi
95
96 else
97     usage
98 fi