Monday, October 10, 2016
Just wanted to share my solution for screen locking that proved itself over the years very practical. As I stepped away from full-blown desktop environments, here and there I had to search how to get some of the features back, but keep it very lightweight on resources. I wanted a very primitive solution with a few dependencies.
Imagine you are watching a long YouTube video and suddenly the screen is locked. Not very user friendly. So fading out notification with possibility to cancel screen locking is a must have feature.
For this recipe we will need:
Here goes the script that will start screen fading out:
#!/bin/bash SEC=10 [ $# -eq 1 ] && SEC=$1 FRAMES=100 SLEEP=`echo $SEC / $FRAMES | bc -l` trap "xcalib -clear" EXIT sleep 0.1 LAST_IDLE=`xprintidle` for (( i = 1; i <= $FRAMES; i++ )); do NEW_IDLE=`xprintidle` if [ $LAST_IDLE -gt $NEW_IDLE ]; then exit 0 fi LAST_IDLE=$NEW_IDLE xcalib -co 95 -a sleep $SLEEP done xset dpms force off
When executed it will gradually lower the contrast to pitch black. If there is any mouse movement or a key press it will cancel fading out. If you like customization, it also accept a parameter, number of seconds during which screen will be fading out (
10 by default). Save it somewhere in the home directory, for example in
Screen fader by itself is not enough, we also need a locker tool as well as the tool that will detect when there is no user activity. Add the following line to your script that runs at log in, in case of Openbox it is
$ xautolock -time 30 -locker "dm-tool lock" -notify 15 -notifier "$HOME/.bin/screen-fader.sh" &
30 is the number of minutes after which the screen will be locked if no user activity detected.
15 is the number of seconds prior to screen locking when notifier will be executed. In our case the notifier is
screen-fader.sh script from above. Replace
dm-tool lock with locker of your choice, if you don't use lightdm.
Then we need to change the setting for the default X screen saver that it doesn't interfere with ours. So add this line as well to your
$ xset s 1800 1800 dpms 1800 1800 1800 # screen blanking after 30 minutes
Finally, set a handler to lock the screen on demand via a hot key. For Openbox that would be in
<keybind key="C-A-L"> <action name="Execute"> <name>Lock Screen</name> <command>xautolock -locknow</command> </action> </keybind>
That's all. Now you are in full control of screen locking. Sweet!