#!/bin/bash

bgfunc() {
  local last=$EPOCHSECONDS;
  local asleep=0
  local dump='/home/pi/last_key'

  while true; do read -sr -t 0.1 input >/dev/null;
    [[ ${#input} -gt 0 ]] && { [[ $(pgrep -fc 'MENU') -gt 0 ]] && echo "$input" > $dump; last=$EPOCHSECONDS; read -sr -t 1 void >/dev/null; } || { sleep 1; }
    echo "$(( $EPOCHSECONDS - $last ))" > /tmp/elapsed.fifo
  done< <(thd --dump /dev/input/event* | grep -v --line-buffered '^#' |& awk -W interactive '{$1=$1;print $NF}' | grep --line-buffered -vw '/dev/input/event1')
}

( bgfunc & )
