From 35d7d391265e53a7c7d88ade0f765ea393d7d868 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Mon, 14 May 2018 23:22:54 +0200 Subject: [PATCH] [i3/lockscreen] updated to socat & dbus thanks to @bebehei --- i3/lockscreen.sh | 74 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/i3/lockscreen.sh b/i3/lockscreen.sh index c8b979a..f83328f 100755 --- a/i3/lockscreen.sh +++ b/i3/lockscreen.sh @@ -1,22 +1,24 @@ #!/usr/bin/bash -[ -f "~/.lockvars" ] && [ -r "~/.lockvars" ] && . ~/.lockvars +# shellcheck source=.lockvars +# shellcheck disable=SC1091 +[ -f "$HOME/.lockvars" ] && [ -r "$HOME/.lockvars" ] && . ~/.lockvars LOCK_TIME=${LOCK_TIME:-3} LOCK_NOTIFY_TIME=${LOCK_NOTIFY_TIME:-15} +SOCK_PATH="/run/user/${UID}/i3/locksock-${XDG_SESSION_ID:-unknown}" + if [ -n "${WAYLAND_DISPLAY}" ]; then LOCK_CMD="swaylock" else LOCK_CMD="i3lock" fi -PROGNAME=$(basename $0) - -OUTPUT_IMAGE="/tmp/i3lock.png" +OUTPUT_IMAGE="$HOME/.lockscreen.png" error(){ - echo -e '\e[01;31m'$*'\e[0m' >&2 + echo -e '\e[01;31m"$*"\e[0m' >&2 } usage(){ @@ -29,8 +31,10 @@ usage(){ -d execute the xautolock daemon -h help - Mind, that the options are order-sensitive. -lf != -fl && -l == -lf - (-fl is probably the thing you want) + Mind, that the options are order-sensitive: + -lf != -fl && -l == -lf + + (-fl is probably the thing you want) FIN exit 1 } @@ -39,13 +43,28 @@ checkfull(){ [ 1 -eq "$force" ] || ~/.local/bin/checknofullscreen } +encrypt_the_chest(){ + if command -v systemctl &>/dev/null; then + systemctl hibernate || shutdown now + else + shutdown now + fi +} + lock(){ - scrot -z $OUTPUT_IMAGE - convert $OUTPUT_IMAGE -resize 20% -level 0%,100%,0.9 -blur 0x2 -resize 500% $OUTPUT_IMAGE + socat /dev/null "UNIX-LISTEN:${SOCK_PATH}" & + local socat_pid=$! + + scrot -z "$OUTPUT_IMAGE" + convert "$OUTPUT_IMAGE" -resize 20% -level 0%,100%,0.9 -blur 0x2 -resize 500% "$OUTPUT_IMAGE" + ${LOCK_CMD} \ -t \ - -i $OUTPUT_IMAGE - rm $OUTPUT_IMAGE + -n \ + -i "$OUTPUT_IMAGE" + + rm "$OUTPUT_IMAGE" + kill ${socat_pid} } notification(){ @@ -59,16 +78,40 @@ notification(){ "Will Lock Screen in 15s" } -daemon(){ +daemon_signallistener(){ + dbus-monitor --system --profile path=/org/freedesktop/login1 2>/dev/null \ + | while read -r _ _ _ _ _ _ _ signal _; do + case "${signal}" in + PrepareForSleep) $0 -fl & ;; + *) true;; + esac + done +} + +daemon_xautolock() { + # Do not use -secure option, as we have to use -locknow xautolock \ - -time $LOCK_TIME \ + -time "$LOCK_TIME" \ -locker "$0 -l" \ -nowlocker "$0 -fl" \ - -notify $LOCK_NOTIFY_TIME \ + -notify "$LOCK_NOTIFY_TIME" \ -notifier "$0 -n" \ -noclose } +# Listen for signals from the DBus Service as +# xautolock does not always catch system sleeps +daemon(){ + daemon_signallistener & + local pid_signal=$! + + daemon_xautolock & + local pid_xautolock=$! + + trap 'kill "${pid_xautolock}" "${pid_signal}" && wait' + wait +} + force=0 while getopts ":hdfln" opt; do @@ -80,7 +123,8 @@ while getopts ":hdfln" opt; do force=1 ;; l) - checkfull && lock + [ -S "${SOCK_PATH}" ] \ + || (checkfull && (lock )) #|| encrypt_the_chest)) ;; n) checkfull && notification