Skip to content

Instantly share code, notes, and snippets.

@jfredrickson
Last active February 24, 2019 04:51
Show Gist options
  • Save jfredrickson/cc0a4dd5cafb80e964b65c2c8ebdfa28 to your computer and use it in GitHub Desktop.
Save jfredrickson/cc0a4dd5cafb80e964b65c2c8ebdfa28 to your computer and use it in GitHub Desktop.
Custom i3lock and autolock on suspend

Screen locker

A custom script that triggers a screen lock, suspending dunst notifications.

Also provides a systemd unit that executes the lock script upon system suspend.

Prerequisites

  • i3lock
  • dunst

Files

systemd

custom scripts

  • $HOME/bin/lock.sh
  • $HOME/bin/notification.sh

Note that I3LOCK_BACKGROUND in lock.sh should be configured as desired.

[Unit]
Description=Lock the screen on resume from suspend
Before=sleep.target suspend.target
[Service]
User=%i
Type=exec
Environment=DISPLAY=:0
ExecStart=/home/%i/bin/lock.sh
[Install]
WantedBy=sleep.target suspend.target
#!/bin/sh
I3LOCK_BACKGROUND=/tmp/lock-bg.png
if [[ $(pgrep -u $UID -x i3lock) ]] ; then
echo "The screen is already locked. Exiting."
exit
fi
prior_state=`cat $HOME/.notifications-state`
# Pause notifications if they are currently enabled
if [ $prior_state = "ACTIVE" ] ; then
$HOME/bin/notifications.sh off
fi
xbacklight > $HOME/.xbacklight-lock
xbacklight -set 50
i3lock -n -i $I3LOCK_BACKGROUND
xbacklight -set $(cat $HOME/.xbacklight-lock)
# Resume notifications if they were enabled before locking
if [ $prior_state = "ACTIVE" ] ; then
$HOME/bin/notifications.sh on
fi
#!/bin/sh
on() {
pkill -U $UID -USR2 dunst
echo "ACTIVE" > $HOME/.notifications-state
}
off() {
pkill -U $UID -USR1 dunst
echo "INACTIVE" > $HOME/.notifications-state
}
if [ "$1" = "on" ] ; then
on
elif [ "$1" = "off" ] ; then
off
else
if [ $(cat $HOME/.notifications-state) = "ACTIVE" ] ; then
off
else
on
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment