Created
July 11, 2022 21:00
-
-
Save auroracramer/6c94112237d8325b5b6280257c045370 to your computer and use it in GitHub Desktop.
Companion cron job for https://gist.github.com/auroracramer/64fa76aa82da717a520a28ec25304a66 to re-enable keyboard when screen is locked so you don't have to restart your computer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Add to crontab via: | |
# (crontab -u $USER -l; echo "*/1 * * * * $HOME/.local/bin/unstuck-keyboard" ) | crontab -u $USER - | |
# Set up env vars so that gnome-screensaver-command works with cron. If these | |
# are not set, the screen device will not be able to be detected | |
# Courtesy of: https://www.programmersought.com/article/27592171963/ | |
user=$(whoami) | |
env_reference_process=$( (pgrep -u "$user" xfce4-session || pgrep -u "$user" ciannamon-session || pgrep -u "$user" gnome-session || pgrep -u "$user" gnome-shell || pgrep -u "$user" kdeinit ) | head -1 ) | |
export DBUS_SESSION_BUS_ADDRESS=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DBUS_SESSION_BUS_ADDRESS= | sed 's/DBUS_SESSION_BUS_ADDRESS=//') | |
export DISPLAY=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DISPLAY= | sed 's/DISPLAY=//') | |
echo $DBUS_SESSION_BUS_ADDRESS | |
echo $DISPLAY | |
# If the screensaver is on, exit since we're not at the computer presumably | |
if [[ -z $(grep "inactive" <(gnome-screensaver-command -q)) ]]; then | |
KEYBOARD_INFO=$(xinput list | sed -e 's/ */ /g' | grep "AT Translated Set 2 keyboard") | |
EXTRAKEY_INFO=$(xinput list | sed -e 's/ */ /g' | grep "ThinkPad Extra Buttons") | |
KEYBOARD_ID=$(echo $KEYBOARD_INFO | grep -o -P 'id=\K\d*') | |
EXTRAKEY_ID=$(echo $EXTRAKEY_INFO | grep -o -P 'id=\K\d*') | |
MASTER_ID=$(xinput list | sed -e 's/ */ /g' | grep -o -P 'slave keyboard \(\K\d*' | head -n 1) | |
if [ ! -z "$(echo $KEYBOARD_INFO | grep -o -P 'floating slave')" ]; then | |
xinput reattach $KEYBOARD_ID $MASTER_ID | |
xinput reattach $EXTRAKEY_ID $MASTER_ID | |
echo "Re-enabled keyboard" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment