fix(hyprlua): flatten presence-detect's check cycle to 20s
Replace the three-tier load-adaptive polling (20s/120s/600s based on CPU-or- RAM usage) with a flat 20s interval — fido2-token -L is cheap enough that backing off under load isn't worth the added complexity or the latency in noticing a key was removed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>main
parent
3b6cdb8dbd
commit
fc929d4747
|
|
@ -1,9 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# FIDO key presence detection daemon.
|
# FIDO key presence detection daemon.
|
||||||
# Checks whether a FIDO2/U2F security key is plugged in, at an adaptive
|
# Checks whether a FIDO2/U2F security key is plugged in, every 20s flat, and
|
||||||
# interval (20s under light load, 120s under moderate load, 600s under heavy
|
# shares caffeine's systemd-inhibit idle lock while a key is present, so
|
||||||
# load), and shares caffeine's systemd-inhibit idle lock while a key is
|
# hypridle never fires during an active session.
|
||||||
# present, so hypridle never fires during an active session.
|
|
||||||
#
|
#
|
||||||
# Detection: `fido2-token -L` (libfido2) enumerates connected FIDO CTAP
|
# Detection: `fido2-token -L` (libfido2) enumerates connected FIDO CTAP
|
||||||
# devices by USB HID usage page — no touch/tap required, so this is a pure
|
# 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.
|
# stays live even then — only the inhibit ownership differs.
|
||||||
PRESENCE_FLAG="/tmp/presence-detected"
|
PRESENCE_FLAG="/tmp/presence-detected"
|
||||||
|
|
||||||
INTERVAL_LIGHT=20 # seconds between checks when CPU-or-RAM usage < LOAD_LIGHT
|
INTERVAL=20 # seconds between checks, flat regardless of system load
|
||||||
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)"
|
|
||||||
|
|
||||||
# True if any FIDO2/U2F authenticator is currently enumerable over USB.
|
# 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,
|
# 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)" ]]
|
[[ -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.
|
# Returns true if the inhibitor sentinel process is still alive.
|
||||||
_inhibit_running() {
|
_inhibit_running() {
|
||||||
[[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null
|
[[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null
|
||||||
|
|
@ -122,5 +93,5 @@ while true; do
|
||||||
_stop_inhibit
|
_stop_inhibit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep "$(_next_interval)"
|
sleep "$INTERVAL"
|
||||||
done
|
done
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue