From: Simon Ruderich Date: Sun, 23 Aug 2015 09:32:04 +0000 (+0200) Subject: unify screen locking X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=b71680c55e6e492ed008f8ec5af3bf169b272f09;p=config%2Fdotfiles.git unify screen locking --- diff --git a/bin/xlockscreen b/bin/xlockscreen new file mode 100755 index 0000000..1bac3a1 --- /dev/null +++ b/bin/xlockscreen @@ -0,0 +1,98 @@ +#!/bin/sh + +# Helper script to start/lock the screen with an available screen locker. + +# Copyright (C) 2015 Simon Ruderich +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -eu + + +usage() { + printf 'usage: %s start \n' "$0" + printf ' %s lock\n' "$0" + exit 1 +} + +installed() { + type "$1" >/dev/null 2>&1 +} + + +if test $# -eq 0; then + usage +fi + +if test x"$1" = xstart; then + if test $# -ne 3; then + usage + fi + lock_time_minutes="$2" + lock_binary="$3" + + # NOTE: We must redirect stdout of the spawned background processes to + # /dev/null or a potential caller which uses our output will wait forever + # on the inherited stdin (and we become a zombie)! Thanks to Beano on [1] + # for the idea, read on 2015-08-23. + # + # [1]: https://stackoverflow.com/questions/3748432/insane-crond-behavior-keeps-making-defunct-bash-processes/3750028#3750028 + + if installed xscreensaver; then + echo "xscreensaver.lockTimeout: $lock_time_minutes" | xrdb -merge + xscreensaver >/dev/null & + echo xscreensaver + elif installed xautolock; then + if installed "$lock_bin"; then + xautolock -secure -time "$lock_time_minutes" \ + -locker "$lock_binary" \ + >/dev/null & + echo xautolock + else + echo "Locker '$lock_bin' not installed. Auto lock won't work!" + exit 1 + fi + else + echo "No locker found. Auto lock won't work!" + exit 1 + fi + +elif test x"$1" = xlock; then + if test $# -ne 1; then + usage + fi + + if installed xscreensaver; then + # Start xscreensaver if it's not already running. xscreensaver-command + # ensures xscreensaver is available for the current X session. + xscreensaver-command -time >/dev/null 2>&1 || xscreensaver & + + xscreensaver-command -lock + # It can take a while for xscreensaver to start, wait until it's + # ready. + while test $? -eq 255; do + xscreensaver-command -lock + done + elif installed xtrlock; then + # Sleep is necessary to allow xtrlock to grab the keyboard input. + sleep 1 + xtrlock + else + echo 'No screen locker found!' + exit 1 + fi + +else + usage +fi diff --git a/herbstluftwm/autostart b/herbstluftwm/autostart index 2b048ab..d58ff9e 100755 --- a/herbstluftwm/autostart +++ b/herbstluftwm/autostart @@ -67,28 +67,8 @@ hc keybind $mod-Shift-Return spawn urxvt hc keybind $mod-Shift-c close hc keybind $mod-q reload hc keybind $mod-Shift-q quit -# Find an existing screen locker. -for locker in xscreensaver xtrlock ''; do - if installed "$locker"; then - break - fi -done -if test -z "$locker"; then - echo 'No screen locker found!' | notify 60 -fi -# Additional options. -locker_pre= -if test x"$locker" = xxscreensaver; then - # Start xscreensaver if it's not already running. xscreensaver-command - # ensures xscreensaver is available for the current X session. - locker_pre='xscreensaver-command -time >/dev/null 2>&1 || xscreensaver &' - locker='xscreensaver-command -lock' -elif test x"$locker" = xxtrlock; then - # Sleep is necessary to allow xtrlock to grab the keyboard input. - locker_pre='sleep 1;' -fi # Lock the screen. -hc keybind $mod-z spawn sh -c "$locker_pre exec $locker" +hc keybind $mod-z spawn "$HOME/.xlockscreen" lock # Tag key bindings. Create tags 1 to 9 with bindings to switch and move # windows to them. diff --git a/setup.sh b/setup.sh index a16faf5..d68dd72 100755 --- a/setup.sh +++ b/setup.sh @@ -72,6 +72,8 @@ if installed X; then link xmodmaprc ~/.xmodmaprc link Xresources ~/.Xresources link XCompose ~/.XCompose + + link bin/xlockscreen ~/.xlockscreen fi if installed xpdf; then diff --git a/xinitrc b/xinitrc index a464b6c..ae38da6 100755 --- a/xinitrc +++ b/xinitrc @@ -54,12 +54,11 @@ background_pids= # # Default to use a background image. set_background=yes -# Default to display a warning if `xautolock` is not available. +# Default to display a warning if no screen locker is not available. screen_lock_force=yes -# Lock screen after x minutes of inactivity, requires `xautolock` to be -# installed. +# Lock screen after x minutes of inactivity. screen_lock_time=3 -# Locker program to lock the screen, used below with `xautolock`. +# Locker program to lock the screen (with xautolock). screen_locker=xtrlock # Load settings for the local system. @@ -127,18 +126,11 @@ if installed redshift; then redshift -l 49.9:10.9 -t 5500:4500 >/dev/null & fi -# Automatically lock the screen after x minutes of inactivity if -# `xscreensaver` or `xautolock` is available. Warn if no locker was found. -if installed xscreensaver; then - xscreensaver & -elif installed xautolock; then - if installed "$screen_locker"; then - xautolock -secure -time "$screen_lock_time" -locker "$screen_locker" & - else - error "Locker '$screen_locker' not installed. Auto lock won't work!" - fi -elif test -n "$screen_lock_force"; then - error "'xautolock' not found. Auto lock won't work!" + +# Start best available screen locker. +xlockscreen=`"$HOME/.xlockscreen" start "$screen_lock_time" "$screen_locker"` +if test $? -ne 0 && test -n "$screen_lock_force"; then + error "$xlockscreen" fi diff --git a/xmonad/xmonad.hs b/xmonad/xmonad.hs index 198bf7f..bdee75d 100644 --- a/xmonad/xmonad.hs +++ b/xmonad/xmonad.hs @@ -153,9 +153,8 @@ myKeys = [ -- Freenode (2010-12-18 14:45 CET). , ((myModMask, xK_b), toggleWS) - -- Lock the screen. The sleep is necessary to allow xtrlock to grab the - -- keyboard input. - , ((myModMask, xK_z), spawn "sleep 1 && exec xtrlock") + -- Lock the screen. + , ((myModMask, xK_z), spawn "$HOME/.xlockscreen lock") ] ++ -- When using multiple screens, switching to another workspace causes