Skip to content

Instantly share code, notes, and snippets.

@xenithorb
Last active October 18, 2017 15:36
Show Gist options
  • Save xenithorb/a7542d7458fbca00bfa5c5891cd3fb39 to your computer and use it in GitHub Desktop.
Save xenithorb/a7542d7458fbca00bfa5c5891cd3fb39 to your computer and use it in GitHub Desktop.
Add the missing autolock functionality back to keepass on Linux.
#!/bin/bash
AUTOLOCK_MIN=10 # Minutes to wait before locking keepass
LOCK_CMD='keepass --lock-all'
declare -A IDLE_TIME_COMMANDS
IDLE_TIME_COMMANDS=(
["KDE"]="qdbus org.kde.screensaver /ScreenSaver GetSessionIdleTime"
)
xautolock_cmd() {
if command -v xautolock > /dev/null; then
xautolock -time "$AUTOLOCK_MIN" -secure -locker "$LOCK_CMD" \
-noclose -resetsaver -detectsleep
else
echo 'Please install `xautolock`'
fi
}
check_idle_loop() {
local is_idle time_command idle_time
eval time_command="\${IDLE_TIME_COMMANDS[$1]}"
until pkill -0 -f "KeePass.exe"; do
sleep 30
done
while :; do
idle_time="$($time_command)"
if (( idle_time >= AUTOLOCK_MIN*60000 && is_idle == 0 )); then
${LOCK_CMD} &&
is_idle=1
elif (( idle_time < AUTOLOCK_MIN*60000 && is_idle == 1 )); then
is_idle=0
fi
sleep 15
done
}
case "${XDG_CURRENT_DESKTOP^^}" in
KDE*) check_idle_loop KDE;;
*) xautolock_cmd;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment