Skip to content

Instantly share code, notes, and snippets.

@leyyce
Last active January 22, 2021 01:53
Show Gist options
  • Save leyyce/694dff8b80518962cadc163277cca661 to your computer and use it in GitHub Desktop.
Save leyyce/694dff8b80518962cadc163277cca661 to your computer and use it in GitHub Desktop.
Simple blur lock screen that works with i3lock, xsslock, scrot and ImageMagick
#!/bin/bash
## CONFIGURATION ##############################################################
# Options to pass to i3lock
i3lock_options="-d"
TMPBG=/tmp/screen.png
MUTED=""
# Run before starting the locker
pre_lock() {
# Uncomment when you automatically want to pause cmus playback on lock
# cmus-remote -U
MUTED=$(pactl list sinks | grep '^[[:space:]]Mute:' | sed "s/^[[:space:]]Mute: //")
# Uncomment to pause the Media player daemon on lock
# mpc pause
# Mute the audio sink on lock, you might need to change the id of your sink.
# For now this only works with one audio output activated.
# With more it will fail to auto unmute on unlock
# pactl set-sink-mute 0 1
# Place your lock icon here or change path accordingly.
ICON=$HOME/.icons/lock.png
if [ -f "$TMPBG" ]; then
rm $TMPBG
fi
scrot /tmp/screen.png
convert $TMPBG -scale 10% -scale 1000% $TMPBG
# If you have multiple monitors, you might have to play around with the geometry option (-geometry (+/-)x_off(+/-)y_off) a bit
# to center the icon on your main screen.
convert $TMPBG $ICON -gravity center -geometry 0+0 -composite -matte $TMPBG
i3lock_options+=" -i $TMPBG"
return
}
# Run after the locker exits
post_lock() {
## If the sink was unmuted before locking, umute it on unlock
# if [ "$MUTED" == "no" ]; then
# pactl set-sink-mute 0 0
# fi
rm $TMPBG
return
}
###############################################################################
pre_lock
# We set a trap to kill the locker if we get killed, then start the locker and
# wait for it to exit. The waiting is not that straightforward when the locker
# forks, so we use this polling only if we have a sleep lock to deal with.
if [[ -e /dev/fd/${XSS_SLEEP_LOCK_FD:--1} ]]; then
kill_i3lock() {
pkill -xu $EUID "$@" i3lock
}
trap kill_i3lock TERM INT
# we have to make sure the locker does not inherit a copy of the lock fd
i3lock $i3lock_options {XSS_SLEEP_LOCK_FD}<&-
# now close our fd (only remaining copy) to indicate we're ready to sleep
exec {XSS_SLEEP_LOCK_FD}<&-
while kill_i3lock -0; do
sleep 0.5
done
else
trap 'kill %%' TERM INT
i3lock -n $i3lock_options &
wait
fi
post_lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment