dgy
/
hexagons
Archived
1
0
Fork 0
This repository has been archived on 2021-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
hexagons/bin/autolock.sh

89 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
# This script is intended to be run as the xautolock locker and notifier.
# It requires i3lock, and dunst is optional.
# Is the screen already locked?
locked() { pkill -0 --euid "$(id -u)" --exact i3lock; }
# Return 0 if suspend is acceptable.
suspend_ok() {
[ -n "$(2>/dev/null mpc current)" ] && return 1
return 0
}
# Print the given message with a timestamp.
info() { printf '%s\t%s\n' "$(date)" "$*"; }
log() {
if [ -n "${LOCK_LOG:-}" ]; then
info >>"$LOCK_LOG" "$@"
else
info "$@"
fi
}
# Control the dunst daemon, if it is running.
dunst() {
pkill -0 --exact dunst || return 0
case ${1:-} in
stop)
log "Stopping notifications and locking screen."
pkill -USR1 --euid "$(id -u)" --exact dunst
;;
resume)
log "...Resuming notifications."
pkill -USR2 --euid "$(id -u)" --exact dunst
;;
*)
echo "dunst argument required: stop or resume"
return 1
;;
esac
}
case "$cmd" in
lock)
dunst stop
# Fork both i3lock and its monitor to avoid blocking xautolock.
i3lock -k --timestr="%H:%M" --datestr="%A %d %B" --insidevercolor=49b9f3ff --insidewrongcolor=fb4245bf --insidecolor=000000ff --ringvercolor=95b9deff --ringwrongcolor=ff6c5fbf --ringcolor=a3e41ebf --textcolor=ffffffff --separatorcolor=171717bf --keyhlcolor=ffff3dff --bshlcolor=ff009eff -B --ignore-empty-password --beep --nofork &
pid="$!"
log "Waiting for PID $pid to end..."
while 2>/dev/null kill -0 "$pid"; do
sleep 1
done
dunst resume
;;
notify)
# Notification should not be issued while locked even if dunst is paused.
locked && exit
log "Sending notification."
# grep finds either Xautolock.notify or Xautolock*notify
secs="$(xrdb -query | grep -m1 '^Xautolock.notify' | cut -f2)"
test -n "$secs" && secs="Locking in $secs seconds"
;;
suspend)
if suspend_ok; then
log "Suspending system."
systemctl suspend
else
log "Deferring suspend."
fi
;;
debug)
log "$@"
;;
*)
log "Unrecognized option: $1"
esac