Files
bananarchy/overlay/rootfs/usr/bin/bananarchy-idle-lock.sh
T

125 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/sh
# Arm the screenlock daemon when dwm blanks the panel on idle.
#
# dwm's native idle blanking (bananarchy-idle-blank.sh off) only powers the
# panel down: it does not call sxmo_state.sh / sxmo_hook_screenoff.sh and no
# idle_locker job is started by the session any more. As a result locker.so
# never receives LOCK_BLANK on a blank event and the phone stays unlocked.
# This helper arms a trusted "sxmo_state.sh set screenoff" (which starts
# xlocker screenoff -> LOCK_BLANK) after a short dark-panel delay. Waking the
# panel before the deadline cancels the pending lock, so a quick key press
# still returns to the unlocked desktop on every target.
#
# Config: ~/.config/sxmo/idle_lock_delay (seconds, "off" disables, 0 locks
# immediately when the panel blanks). Default 30 s.
set -u
CFGDIR="${XDG_CONFIG_HOME:-${HOME:-/home/user}/.config}/sxmo"
CFG="$CFGDIR/idle_lock_delay"
PIDFILE="${BANANARCHY_IDLE_LOCK_PIDFILE:-/tmp/bananarchy_idle_lock.pid}"
DEFAULT=30
MAX_DELAY=3600
LOCK_FLAG="${BANANARCHY_LOCK_FLAG:-/tmp/bananarchy_screen_locked}"
STATE_CMD="${BANANARCHY_STATE_CMD:-/usr/bin/sxmo_state.sh}"
WM_CMD="${BANANARCHY_WM_CMD:-/usr/bin/sxmo_wm.sh}"
LID_STATE="${BANANARCHY_LID_STATE:-/run/bananarchy/lid-state}"
RING_FLAG="${BANANARCHY_RING_FLAG:-/tmp/sxmo_call_ringing}"
ACTIVE_CALL_FLAG="${BANANARCHY_ACTIVE_CALL_FLAG:-/tmp/sxmo_active_call}"
get_delay() {
v="$DEFAULT"
if [ -r "$CFG" ]; then
read -r v < "$CFG" 2>/dev/null || v="$DEFAULT"
fi
case "$v" in
off|OFF|no|NO|none|NONE) printf 'off\n'; return 0 ;;
esac
case "$v" in ''|*[!0-9]*) v="$DEFAULT" ;; esac
[ "$v" -le "$MAX_DELAY" ] || v="$MAX_DELAY"
printf '%s\n' "$v"
}
label() {
v="$(get_delay)"
case "$v" in
off) printf 'Off\n' ;;
0) printf 'Immediately\n' ;;
*) printf '%ss\n' "$v" ;;
esac
}
panel_is_dark() {
st="$("$WM_CMD" display 2>/dev/null || true)"
[ "$st" = "off" ] && return 0
if [ -r /sys/class/backlight/backlight/brightness ]; then
_bl=1
read -r _bl < /sys/class/backlight/backlight/brightness 2>/dev/null || _bl=1
[ "${_bl:-1}" -le 0 ] 2>/dev/null && return 0
fi
return 1
}
cancel() {
[ -r "$PIDFILE" ] || return 0
pid=""
read -r pid < "$PIDFILE" 2>/dev/null || pid=""
case "$pid" in
''|*[!0-9]*) ;;
*) kill "$pid" 2>/dev/null || true ;;
esac
rm -f "$PIDFILE" 2>/dev/null || true
return 0
}
lock_panel() {
[ -f "$LOCK_FLAG" ] && return 0
[ "$(cat "$LID_STATE" 2>/dev/null || true)" = "closed" ] && return 0
[ -e "$RING_FLAG" ] && return 0
[ -e "$ACTIVE_CALL_FLAG" ] && return 0
panel_is_dark || return 0
"$STATE_CMD" set screenoff >/dev/null 2>&1 || true
}
arm() {
cancel
d="$(get_delay)"
[ "$d" = "off" ] && return 0
if [ "$d" -eq 0 ]; then
lock_panel
return 0
fi
(
sleep "$d" || exit 0
lock_panel
) >/dev/null 2>&1 &
printf '%s\n' "$!" > "$PIDFILE" 2>/dev/null || true
return 0
}
case "${1:-get}" in
get) get_delay ;;
label) label ;;
set)
v="${2:-}"
case "$v" in
off|OFF|no|NO|none|NONE) v=off ;;
''|*[!0-9]*)
printf 'Usage: %s [get|label|set <seconds|off>|arm|cancel]\n' "${0##*/}" >&2
exit 1
;;
esac
case "$v" in off) ;; *)
[ "$v" -le "$MAX_DELAY" ] || v="$MAX_DELAY"
;; esac
mkdir -p "$CFGDIR" 2>/dev/null || exit 1
printf '%s\n' "$v" > "$CFG" || exit 1
printf '%s\n' "$v"
;;
arm) arm ;;
cancel) cancel ;;
*)
printf 'Usage: %s [get|label|set <seconds|off>|arm|cancel]\n' "${0##*/}" >&2
exit 1
;;
esac
exit 0