Retry i3lock when a keyboard grab blocks it

i3lock exits if another client holds a grab, leaving the screen blanked but
unlocked. Close gcr-prompter (ssh passphrase dialog) first, retry every 2s up
to 30 times and log failures to the journal (tag screen-lock).
This commit is contained in:
Daniele Fucini committed 2026-11-16 20:57:18 +01:00
1 parent e2fd3e4aa9
commit 478e4d391e
1 file changed
+28 -3
+28 -3
View File
@@ -2,13 +2,23 @@
# i3lock-color with a background image and the keyboard layout shown.
image=/usr/share/backgrounds/arch_1920x1080.png
err=${XDG_RUNTIME_DIR:-/tmp}/screen-lock.err
log() { printf '%s\n' "$*" | systemd-cat -t screen-lock -p warning; }
# gcr-prompter (ssh key passphrase dialog) grabs the keyboard, so i3lock can't lock: cancel it.
pkill -x gcr-prompter && log "stopped gcr-prompter before locking"
# Hold notifications while locked; dunst shows them on unlock. A signal to this
# script is passed on to i3lock, as the old exec did.
was_paused=$(dunstctl is-paused 2>/dev/null)
dunstctl set-paused true 2>/dev/null
trap 'kill "$pid" 2>/dev/null' INT TERM HUP
stopped=
trap 'stopped=1; kill "$pid" 2>/dev/null' INT TERM HUP
# i3lock exits 1 if another client holds a grab (dmenu, rofi, a prompt): retry for a minute.
tries=0
while :; do
# -n: stay in the foreground, as xss-lock needs to see when the screen is unlocked.
# --indicator keeps the ring (and the layout text) visible before typing.
# The layout's default spot (dx:dy+30) falls inside the ring: put it below.
@@ -17,10 +27,25 @@ i3lock -n -i "$image" -F -c 000000ff \
--layout-pos='ix:iy+r+40' --layout-color=ffffffff \
--layout-size=24 \
--time-color=ffffffff --date-color=ffffffff --date-str='%A, %d %B' \
--ring-color=1793d1ff --line-uses-ring "$@" &
--ring-color=1793d1ff --line-uses-ring "$@" 2>"$err" &
pid=$!
# A trapped signal interrupts the first wait; the second waits for i3lock to exit.
wait "$pid"
status=$?
# A trapped signal interrupts the first wait; this one waits for i3lock to exit.
if [ -n "$stopped" ]; then
wait "$pid" 2>/dev/null
break
fi
[ "$status" -eq 0 ] && break
tries=$((tries + 1))
log "i3lock failed (exit $status, try $tries): $(cat "$err")"
if [ "$tries" -ge 30 ]; then
log "giving up: screen not locked"
break
fi
sleep 2
[ -n "$stopped" ] && break
done
rm -f "$err"
[ "$was_paused" = true ] || dunstctl set-paused false 2>/dev/null