]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/xlockscreen
herbstluftwm/autostart: also create tag 0
[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         printf 'xscreensaver.timeout: %d
54                 xscreensaver.lock: True
55                 xscreensaver.lockTimeout: 0
56                ' "$lock_time_minutes" | xrdb -merge
57         xscreensaver >/dev/null &
58         echo xscreensaver
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
64
65             xautolock -secure -time "$lock_time_minutes" \
66                       -locker "$lock_binary" \
67                       >/dev/null &
68             echo xautolock
69         else
70             echo "Locker '$lock_binary' not installed. Auto lock won't work!"
71             exit 1
72         fi
73     else
74         echo "No locker found. Auto lock won't work!"
75         exit 1
76     fi
77
78 elif test x"$1" = xlock; then
79     if test $# -ne 1; then
80         usage
81     fi
82
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 &
87
88         # It can take a while for xscreensaver to start, wait until it's
89         # ready. a || b && c behaves like (a || b) && c.
90         while :; do
91             xscreensaver-command -lock || test $? -ne 255 && break
92         done
93     elif installed xtrlock; then
94         # Sleep is necessary to allow xtrlock to grab the keyboard input.
95         sleep 1
96         exec xtrlock
97     elif installed xlock; then
98         exec xlock
99     else
100         echo 'No screen locker found!'
101         exit 1
102     fi
103
104 else
105     usage
106 fi