Skip to content

Commit

Permalink
Use autolock to lock screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianpb committed Jul 5, 2019
1 parent 6394fa1 commit c6778c3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 39 deletions.
112 changes: 112 additions & 0 deletions autolock.sh
@@ -0,0 +1,112 @@
#!/bin/sh

set -eu

# This script is intended to be run as the xautolock locker and notifier.
# It requires i3lock, and dunst is optional.

# Copy or link this script as /usr/bin/slock to let xfce4-session run it.
if [ "$(basename "$0")" = "slock" ]; then
cmd=lock
else
cmd=${1:-lock}
fi

# Is the screen already locked?
locked() { pkill -0 --euid "$(id -u)" --exact i3lock; }

# Return 0 if suspend is acceptable.
suspend_ok() {
[ -n "$(2>/dev/null mpc current)" ] && return 1
return 0
}

# Print the given message with a timestamp.
info() { printf '%s\t%s\n' "$(date)" "$*"; }

log() {
if [ -n "${LOCK_LOG:-}" ]; then
info >>"$LOCK_LOG" "$@"
else
info "$@"
fi
}

# Control the dunst daemon, if it is running.
dunst() {
pkill -0 --exact dunst || return 0

case ${1:-} in
stop)
log "Stopping notifications and locking screen."
pkill -USR1 --euid "$(id -u)" --exact dunst
;;
resume)
log "...Resuming notifications."
pkill -USR2 --euid "$(id -u)" --exact dunst
;;
*)
echo "dunst argument required: stop or resume"
return 1
;;
esac
}

blur() {
IMAGE=/tmp/i3lock.png
SCREENSHOT="scrot $IMAGE" # 0.46s

# Get the screenshot, add the blur and lock the screen with it
$SCREENSHOT
convert $IMAGE -scale 10% -scale 1000% $IMAGE
i3lock -i $IMAGE --nofork -f
rm $IMAGE
watson stop
}

case "$cmd" in
lock)
dunst stop

# Fork both i3lock and its monitor to avoid blocking xautolock.
blur &

pid="$!"
log "Waiting for PID $pid to end..."
while 2>/dev/null kill -0 "$pid"; do
sleep 1
done

dunst resume
;;

notify)
# Notification should not be issued while locked even if dunst is paused.
locked && exit

log "Sending notification."
# grep finds either Xautolock.notify or Xautolock*notify
secs="$(xrdb -query | grep -m1 '^Xautolock.notify' | cut -f2)"
test -n "$secs" && secs="Locking in $secs seconds"

notify-send --urgency="normal" --app-name="xautolock" \
--icon='/usr/share/icons/Adwaita/48x48/emblems/emblem-readonly.png' \
-- "Screen Lock" "$secs"
;;

suspend)
if suspend_ok; then
log "Suspending system."
systemctl suspend
else
log "Deferring suspend."
fi
;;

debug)
log "$@"
;;

*)
log "Unrecognized option: $1"
esac
35 changes: 0 additions & 35 deletions blur.sh

This file was deleted.

2 changes: 1 addition & 1 deletion i3/.config/i3/config
Expand Up @@ -175,7 +175,7 @@ exec_always --no-startup-id $HOME/.config/polybar/launch.sh
new_window 1pixel

# Lock screen. --dpms option will turn off all displays right after locking.
bindsym Mod4+Control+l exec $HOME/.dotfiles/blur.sh
bindsym Mod4+Control+l exec $HOME/.dotfiles/autolock.sh lock

# Pulse Audio controls
bindsym XF86AudioRaiseVolume exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +5% #increase sound volume
Expand Down
6 changes: 3 additions & 3 deletions polybar/.config/polybar/config
Expand Up @@ -361,7 +361,7 @@ interval = 3.0

format-connected = <ramp-signal> <label-connected>
format-connected-underline = ${colors.deeppurple}
label-connected = %essid%
label-connected = %essid% %local_ip%
label-connected-foreground=${colors.white}

format-disconnected =
Expand Down Expand Up @@ -396,7 +396,7 @@ type = internal/network
interface = wifi0
interval = 3.0

label-connected = "%upspeed:7%"
label-connected = %upspeed:7%
format-connected = <label-connected>
format-connected-prefix = 
format-connected-foreground=${colors.teal}
Expand Down Expand Up @@ -586,7 +586,7 @@ label-separator = |
label-separator-foreground = ${colors.foreground-alt}

menu-0-0 = lock
menu-0-0-exec = i3lock -i ~/Images/Wallpaper/wallpaper.png --tiling --dpms --color 000000
menu-0-0-exec = ~/.dotfiles/autolock.sh lock

[settings]
screenchange-reload = true
Expand Down

0 comments on commit c6778c3

Please sign in to comment.