如何才能等到 xscreensaver 解锁?

如何才能等到 xscreensaver 解锁?

如何才能等到 xscreensaver 解锁? xscreensaver-command -lock锁定屏幕,但在屏幕再次解锁之前返回。 xscreensaver 提供了一个xscreensaver-command -watch打印UNBLANK事件的方法,但是我如何解析它们并确保xscreensaver-command -watch之后被杀死?

答案1

该程序将在 xscreensaver 解锁后退出(或在不需要密码的激活后“解锁”):

#!/bin/bash

set -eu

FIFO=/tmp/xscreensaver-wait-for-unlock-$$.fifo
rm -f "$FIFO"
mkfifo "$FIFO"

# Kill `xscreensaver-command -watch` when we exit
trap "exit" INT TERM
trap "kill %1; rm -f $FIFO" EXIT

xscreensaver-command -watch > $FIFO &

while read line; do
    if echo -E "$line" | grep -q "^UNBLANK "; then
        # Make sure the screen is actually unlocked
        xscreensaver-command -time | grep -q " screen non-blanked since " && exit 0 || echo "saw UNBLANK but screen was not unlocked"
    fi
done < $FIFO

相关内容