diff --git a/desktopenvs/hyprlua/scripts/presence-detect.sh b/desktopenvs/hyprlua/scripts/presence-detect.sh index f061fdd..0bf3710 100755 --- a/desktopenvs/hyprlua/scripts/presence-detect.sh +++ b/desktopenvs/hyprlua/scripts/presence-detect.sh @@ -1,9 +1,8 @@ #!/bin/bash # FIDO key presence detection daemon. -# Checks whether a FIDO2/U2F security key is plugged in, at an adaptive -# interval (20s under light load, 120s under moderate load, 600s under heavy -# load), and shares caffeine's systemd-inhibit idle lock while a key is -# present, so hypridle never fires during an active session. +# Checks whether a FIDO2/U2F security key is plugged in, every 20s flat, and +# shares caffeine's systemd-inhibit idle lock while a key is present, so +# hypridle never fires during an active session. # # Detection: `fido2-token -L` (libfido2) enumerates connected FIDO CTAP # devices by USB HID usage page — no touch/tap required, so this is a pure @@ -25,12 +24,7 @@ OWNED_FLAG="/tmp/presence-inhibit-owned" # stays live even then — only the inhibit ownership differs. PRESENCE_FLAG="/tmp/presence-detected" -INTERVAL_LIGHT=20 # seconds between checks when CPU-or-RAM usage < LOAD_LIGHT -INTERVAL_MID=120 # seconds between checks when usage is between the two thresholds -INTERVAL_HEAVY=600 # seconds between checks when usage >= LOAD_HEAVY -LOAD_LIGHT=0.20 # CPU-or-RAM fraction below which we poll at INTERVAL_LIGHT -LOAD_HEAVY=0.50 # CPU-or-RAM fraction at/above which we back off to INTERVAL_HEAVY -NPROC="$(nproc)" +INTERVAL=20 # seconds between checks, flat regardless of system load # True if any FIDO2/U2F authenticator is currently enumerable over USB. # fido2-token -L lists one line per connected device and needs no PIN, touch, @@ -39,29 +33,6 @@ _fido_key_present() { [[ -n "$(fido2-token -L 2>/dev/null)" ]] } -# Prints current CPU-or-RAM usage as a fraction (0..1): the higher of the -# core-count-normalized 1-minute load average and the used-RAM fraction. Load -# average is a cheap, sampling-free proxy for "CPU busy" — no extra measurement -# delay per tick. -_resource_usage() { - local load1 cpu_frac mem_frac - load1="$(awk '{print $1}' /proc/loadavg)" - cpu_frac="$(awk -v l="$load1" -v n="$NPROC" 'BEGIN{print l/n}')" - mem_frac="$(free | awk '/^Mem:/ {print $3/$2}')" - awk -v c="$cpu_frac" -v m="$mem_frac" 'BEGIN{print (c>m)?c:m}' -} - -# Three-tier poll interval based on current resource usage: -# >= LOAD_HEAVY -> INTERVAL_HEAVY (heavy load: back off, stay out of the way) -# < LOAD_LIGHT -> INTERVAL_LIGHT (near-idle: poll fast, cheap and responsive) -# otherwise -> INTERVAL_MID (moderate load) -_next_interval() { - local u; u="$(_resource_usage)" - awk -v u="$u" -v heavy="$LOAD_HEAVY" -v light="$LOAD_LIGHT" \ - -v fh="$INTERVAL_HEAVY" -v fl="$INTERVAL_LIGHT" -v fm="$INTERVAL_MID" \ - 'BEGIN{ if (u >= heavy) print fh; else if (u < light) print fl; else print fm }' -} - # Returns true if the inhibitor sentinel process is still alive. _inhibit_running() { [[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null @@ -122,5 +93,5 @@ while true; do _stop_inhibit fi - sleep "$(_next_interval)" + sleep "$INTERVAL" done