nnscripts

no-nonsense shell script colection for Linux-based environments
git clone git://git.luxferre.top/nnscripts.git
Log | Files | Refs | README | LICENSE

nnkeys.sh (1675B)


      1 #!/bin/sh
      2 # nnkeys: no-nonsense multimedia keys manager for bare CLI systems
      3 # Linux-specific, depends on pkill and evtest
      4 # as well as AWK engine of your choice (nawk or mawk recommended)
      5 # Usage: nnkeys.sh nnkeys.conf
      6 # nnkeys.conf lines format (see example in the repo):
      7 # KEY_*   cmd
      8 # where KEY_* is the identifier from evtest
      9 # the script is daemonized automatically, just re-run it to reconfigure
     10 # created by Luxferre in 2024, released into public domain
     11 
     12 # static configuration section  
     13 
     14 DEVICE="/dev/input/event1" # check your path with evtest
     15 AWK_ENGINE=nawk # change to yours if necessary
     16 AWK_TMP="/tmp/nnkeys.awk" # temporary awk script location
     17 NNPID="/tmp/nnkeys.pid" # temporary PID location
     18 
     19 # do not change beyond this point unless you know what you're doing
     20 
     21 # config file
     22 CONF="$1"
     23 
     24 [[ ! -f "$CONF" ]] && echo "No config file!" && exit 1
     25 
     26 # guard from double-launch: kill the previous instance
     27 
     28 [[ -f "$NNPID" ]] && pkill -P $(cat $NNPID) && rm -f $NNPID $AWK_TMP
     29 
     30 strim() { # string whitespace trimmer
     31   local s2 s="$*"
     32   until s2="${s#[[:space:]]}"; [[ "$s2" = "$s" ]]; do s="$s2"; done
     33   until s2="${s%[[:space:]]}"; [[ "$s2" = "$s" ]]; do s="$s2"; done
     34   echo "$s"
     35 }
     36 
     37 # read config and shape the AWK key handler script
     38 while read -r cline; do
     39   [[ "$cline" = \#* ]] && continue # skip comments
     40   ckey="${cline%% *}"
     41   ccmd="${cline#$ckey}"
     42   ccmd="$(strim $ccmd)"
     43   [[ -n "$ccmd" ]] && echo "/${ckey}.*value\ 1/{system(\"$ccmd\")}"
     44 done < $CONF > $AWK_TMP
     45 
     46 echo "Config read, daemonizing..."
     47 
     48 nohup $SHELL -c "evtest "$DEVICE" | $AWK_ENGINE -f $AWK_TMP" > /dev/null 2>&1 &
     49 MYPID="$!"
     50 echo "$MYPID" > $NNPID
     51 
     52 echo "Listener started, pid is at $NNPID"