Advertisement
lilo_booter

Cocky

May 13th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.41 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Copyright (C) 2018 Charles Yates
  4. # Released under the LGPL
  5. # Faleena Hopkins can go spin
  6.  
  7. # COCKY LIBRARY
  8.  
  9. source ~/bin/split_map.sh
  10.  
  11. state() {
  12.     local name=$1 ; shift
  13.     local callbacks=_${name}_callbacks
  14.  
  15.     map "$name" || return
  16.  
  17.     # Map for callbacks
  18.     map "$callbacks" || return
  19.  
  20.     while read ; do
  21.         $callbacks.pair "$REPLY" "cocky_$REPLY"
  22.     done <<< "$( declare -F | sed -e 's/^declare -f //' | grep ^cocky_ | sed 's/^cocky_//' )"
  23.  
  24.     state::trigger() {
  25.         local name=$1 ; shift
  26.         local callbacks=_${name}_callbacks
  27.         local callback=$( $callbacks.value "$1" )
  28.         $name.pair "$1" "$2"
  29.         [[ "$callback" != "" ]] && "$callback" "$2" "$3"
  30.     }
  31.  
  32.     object.extend "$name" state
  33. }
  34.  
  35. cocky() {
  36.     # Handle arguments
  37.     js=$1
  38.  
  39.     # Create the state map
  40.     state jstate || return
  41.  
  42.     # Simple lookup map
  43.     map class 1=button 2=axis || return
  44.  
  45.     jstest --event "$js" |
  46.     while read -r ; do
  47.         if [[ "$REPLY" =~ ^Event:.type.[12]+,.*$ ]] ; then
  48.             IFS=' ,' read junk junk type junk time junk number junk value <<< "$REPLY"
  49.             jstate.trigger "$( class.value $type )$number" "$value" "$time"
  50.         fi
  51.     done
  52. }
  53.  
  54.  
  55. # SILLY TEST CASE SCRIPT
  56.  
  57. #source ~/bin/cocky.sh
  58. source ~/bin/split_imp_utils.sh
  59.  
  60. imp.festival || exit
  61.  
  62. cocky_button0() {
  63.     festival.speak "button 0 is $1"
  64. }
  65.  
  66. cocky_button1() {
  67.     festival.speak "button 1 is $1"
  68. }
  69.  
  70. cocky_button2() {
  71.     festival.speak "button 2 is $1"
  72. }
  73.  
  74. cocky "$1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement