Implement dmenuxautolock.

This commit is contained in:
dabruh 2022-06-03 22:22:51 +02:00
parent aabb025e38
commit 2db614cde3
2 changed files with 84 additions and 1 deletions

View File

@ -2,7 +2,8 @@
exec_always --no-startup-id xss-lock --transfer-sleep-lock -- i3exit lock
# lock screen after inactivity
exec --no-startup-id xautolock -time $lock_after_min -locker 'loginctl lock-session' -notify $lock_notify_sec -notifier 'notify-send --icon=gtk-info xautolock "The screen will lock soon"'
exec --no-startup-id dmenuxautolock -t $lock_after_min -n $lock_notify_sec
bindsym $mod+Ctrl+Shift+l exec --no-startup-id dmenuxautolock -t 0 -n 0
# Run i3hwlock
exec_always --no-startup-id pgrep i3hwlock || i3hwlock

82
.local/bin/dmenuxautolock Executable file
View File

@ -0,0 +1,82 @@
#!/bin/bash
SCRIPT_NAME="$(basename -- "$0")"
FILE="/tmp/dmenuxautolock"
DEFAULT_TIMEOUT=60
DEFAULT_NOTIFY=10
function usage() {
echo "Usage: $SCRIPT_NAME [OPTIONS]"
echo
echo "Options:"
echo " -h Display help."
echo " -t [MIN] Time after which to lock the screen."
echo " -n [SEC] Notify n seconds before locking."
}
while getopts ":t:n:h" arg; do
case $arg in
t) TIMEOUT=$OPTARG ;;
n) NOTIFY=$OPTARG ;;
h)
usage
exit 0
;;
:)
echo "$SCRIPT_NAME: Must supply an argument to -$OPTARG." >&2
usage
exit 1
;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
exit 2
;;
esac
done
function read_current() {
local timeout notify
timeout="$(cut -d, -f1 2>/dev/null <"$FILE")"
notify="$(cut -d, -f2 2>/dev/null <"$FILE")"
[ -n "$timeout" ] && export CURRENT_TIMEOUT="$timeout"
[ -n "$notify" ] && export CURRENT_NOTIFY="$notify"
}
read_current
if [ -z "$TIMEOUT" ]; then
[ -n "$CURRENT_TIMEOUT" ] && TIMEOUT="$CURRENT_TIMEOUT" || TIMEOUT="$DEFAULT_TIMEOUT"
fi
if [ "$TIMEOUT" -eq 0 ]; then
[ -n "$CURRENT_TIMEOUT" ] && current="$CURRENT_TIMEOUT" || current="$DEFAULT_TIMEOUT"
timeout="$(xargs -n 1 <<<"'$current min (current)' '1 min' '2 min' '5 min' '15 min' '30 min' '60 min'" | dmenu -i -l 25 -p "Lock timeout")"
TIMEOUT="$(cut -d' ' -f1 <<<"$timeout")"
[ -z "$TIMEOUT" ] && exit 1
fi
if [ -z "$NOTIFY" ]; then
[ -n "$CURRENT_NOTIFY" ] && NOTIFY="$CURRENT_NOTIFY" || NOTIFY="$DEFAULT_NOTIFY"
fi
if [ "$NOTIFY" -eq 0 ]; then
[ -n "$CURRENT_NOTIFY" ] && current="$CURRENT_NOTIFY" || current="$DEFAULT_NOTIFY"
notify="$(xargs -n 1 <<<"'$current sec (current)' '15 sec' '30 sec' '45 sec'" | dmenu -i -l 25 -p "Lock notify")"
NOTIFY="$(cut -d' ' -f1 <<<"$notify")"
[ -z "$NOTIFY" ] && exit 1
fi
echo "INFO: Setting TIMEOUT=$TIMEOUT, NOTIFY=$NOTIFY."
echo "$TIMEOUT,$NOTIFY" >"$FILE"
killall xautolock 2>/dev/null
xautolock -time "$TIMEOUT" -locker 'loginctl lock-session' -notify "$NOTIFY" -notifier "notify-send --expire-time $((NOTIFY * 1000)) --icon=gtk-info xautolock 'The screen will lock soon.'" &
xalpid=$!
sleep 0.5
if ! [ -d "/proc/${xalpid}" ]; then
i3-nagbar -m 'Error: xautolock failed to start!' &
fi