From 417de1d7b7a7ce6c2b2bfef2083deaf9c377f0db Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 9 Jul 2024 11:46:49 +0200 Subject: [sample] Fix kb-lock/unlock, make more robust Revent versions of xinput change the output format of the list option for disabled devices, which broke kb-unlock.sh Fix this, and hopefully make scripts more robust, by remembering which devices where locked by kb-lock, and then unlock those same devices in kb-unlock. While at it, there's now also some basic protection against concurrent calls to those scripts. --- sample_configuration/kb-lock.sh | 22 ++++++++++++++++++++-- sample_configuration/kb-unlock.sh | 19 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/sample_configuration/kb-lock.sh b/sample_configuration/kb-lock.sh index 27db6da..64e3915 100755 --- a/sample_configuration/kb-lock.sh +++ b/sample_configuration/kb-lock.sh @@ -1,12 +1,30 @@ #!/bin/bash # use xinput to get all keyboards and pointers (mouse) -keyboards=$(xinput --list | grep -E "slave.*(keyboard|pointer)" | cut -f2 | cut -d'=' -f2) +keyboards=$( xinput list | grep -E "slave.*(keyboard|pointer)" | grep -oP '(?<=id=)\d+' ) +[ -z "$keyboards" ] && exit 0 + +# Make sure we're matching lock/unlock calls +TMP="${XDG_RUNTIME_DIR:-"/run/user/$UID"}/pvslock" +for i in 10 20 40 80 160 320 0; do + if mkdir "$TMP"; then + echo "$keyboards" > "$TMP/locked" && break + fi + usleep ${i}000 +done + +if [ "$i" = 0 ]; then + echo "locked, raced" + exit 0 +fi for id in $keyboards; do echo "disabling device #$id" - xinput --set-prop $id "Device Enabled" 0 & + xinput disable "$id" & done wait +touch "$TMP/done" + +exit 0 diff --git a/sample_configuration/kb-unlock.sh b/sample_configuration/kb-unlock.sh index c123414..5f19c62 100755 --- a/sample_configuration/kb-unlock.sh +++ b/sample_configuration/kb-unlock.sh @@ -1,12 +1,25 @@ #!/bin/bash -# use xinput to get all keyboards and pointers (mouse) -keyboards=$(xinput --list | grep -E "slave.*(keyboard|pointer)" | cut -f2 | cut -d'=' -f2) +TMP="${XDG_RUNTIME_DIR:-"/run/user/$UID"}/pvslock" +for i in 10 20 40 80 160 320 0; do + if unlink "$TMP/done"; then + keyboards=$( cat "$TMP/locked" ) + break + fi + [ -d "$TMP" ] || exit 0 + usleep ${i}000 +done +if [ "$i" = 0 ]; then + echo "locked, raced" + exit 0 +fi for id in $keyboards; do echo "enabling device #$id" - xinput --set-prop $id "Device Enabled" 1 & + xinput enable "$id" & done wait +rm -rf -- "$TMP" +exit 0 -- cgit v1.2.3-55-g7522