#!/bin/bash LOCK="/var/run/controller-inactivity-checker.lock" if [ -f "$LOCK" ]; then exit 1 fi trap 'rm -f "$LOCK"; exit 0' SIGTERM EXIT touch "$LOCK" STATE="active" JS_DEVICES=() TIMER="$(/usr/bin/batocera-settings-get system.controllerinactivitytimer)" if [[ -z "$TIMER" || ! "$TIMER" =~ ^[0-9]+$ || "$TIMER" -le 60 ]]; then TIMER="900" # default in seconds /usr/bin/batocera-settings-set system.controllerinactivitytimer "$TIMER" fi js_update() { JS_DEVICES=() for js_device in /dev/input/js*; do if [ -e "$js_device" ]; then JS_DEVICES+=("$js_device") fi done } do_inactivity() { STATE="inactive" # your code here } do_activity() { STATE="active" # your code here } monitor_controllers() { local JS_REFRESH_INTERVAL=10 # Number of loops before controller refresh (by default 1 loop per second) LOOP_COUNT=0 # This should always be 0 while true; do if (( LOOP_COUNT % JS_REFRESH_INTERVAL == 0 )); then js_update fi for i in "${!JS_DEVICES[@]}"; do js="${JS_DEVICES[$i]}" if [ -e "$js" ]; then if timeout 1 jstest --event "$js" | tail -n +25 | grep -q "Event"; then LOOP_COUNT=0 JS_DEVICES=("$js" "${JS_DEVICES[@]:0:$i}" "${JS_DEVICES[@]:$((i + 1))}") if [ "$STATE" = "inactive" ]; then do_activity fi break fi fi done ((LOOP_COUNT++)) if (( LOOP_COUNT >= TIMER )); then if [ "$STATE" = "active" ]; then do_inactivity fi fi done } js_update monitor_controllers exit 0