cecremote.sh (6752B)
1 #!/bin/bash 2 function keychar { 3 parin1=$1 #first param; abc1 4 parin2=$2 #second param; 0=a, 1=b, 2=c, 3=1, 4=a, ... 5 parin2=$((parin2)) #convert to numeric 6 parin1len=${#parin1} #length of parin1 7 parin2pos=$((parin2 % parin1len)) #position mod 8 char=${parin1:parin2pos:1} #char key to simulate 9 if [ "$parin2" -gt 0 ]; then #if same key pressed multiple times, delete previous char; write a, delete a write b, delete b write c, ... 10 xdotool key "BackSpace" 11 fi 12 #special cases for xdotool ( X Keysyms ) 13 if [ "$char" = " " ]; then char="space"; fi 14 if [ "$char" = "." ]; then char="period"; fi 15 if [ "$char" = "-" ]; then char="minus"; fi 16 xdotool key $char 17 } 18 datlastkey=$(date +%s%N) 19 strlastkey="" 20 intkeychar=0 21 intmsbetweenkeys=2000 #two presses of a key sooner that this makes it delete previous key and write the next one (a->b->c->1->a->...) 22 intmousestartspeed=10 #mouse starts moving at this speed (pixels per key press) 23 intmouseacc=10 #added to the mouse speed for each key press (while holding down key, more key presses are sent from the remote) 24 intmousespeed=10 25 26 while read oneline 27 do 28 keyline=$(echo $oneline | grep " key ") 29 #echo $keyline --- debugAllLines 30 if [ -n "$keyline" ]; then 31 datnow=$(date +%s%N) 32 datdiff=$((($datnow - $datlastkey) / 1000000)) #bla bla key pressed: previous channel (123) 33 strkey=$(grep -oP '(?<=sed: ).*?(?= \()' <<< "$keyline") #bla bla key pres-->sed: >>previous channel<< (<--123) 34 strstat=$(grep -oP '(?<=key ).*?(?=:)' <<< "$keyline") #bla bla -->key >>pressed<<:<-- previous channel (123) 35 strpressed=$(echo $strstat | grep "pressed") 36 strreleased=$(echo $strstat | grep "released") 37 if [ -n "$strpressed" ]; then 38 #echo $keyline --- debug 39 if [ "$strkey" = "$strlastkey" ] && [ "$datdiff" -lt "$intmsbetweenkeys" ]; then 40 intkeychar=$((intkeychar + 1)) #same key pressed for a different char 41 else 42 intkeychar=0 #different key / too far apart 43 fi 44 datlastkey=$datnow 45 strlastkey=$strkey 46 case "$strkey" in 47 "1") 48 xdotool key "BackSpace" 49 ;; 50 "2") 51 keychar "abc2" intkeychar 52 ;; 53 "3") 54 keychar "def3" intkeychar 55 ;; 56 "4") 57 keychar "ghi4" intkeychar 58 ;; 59 "5") 60 keychar "jkl5" intkeychar 61 ;; 62 "6") 63 keychar "mno6" intkeychar 64 ;; 65 "7") 66 keychar "pqrs7" intkeychar 67 ;; 68 "8") 69 keychar "tuv8" intkeychar 70 ;; 71 "9") 72 keychar "wxyz9" intkeychar 73 ;; 74 "0") 75 keychar " 0.-" intkeychar 76 ;; 77 "previous channel") 78 xdotool key "Return" #Enter 79 ;; 80 "channel up") 81 xdotool click 4 #mouse scroll up 82 ;; 83 "channel down") 84 xdotool click 5 #mouse scroll down 85 ;; 86 "channels list") 87 xdotool click 3 #right mouse button click" 88 ;; 89 "up") 90 intpixels=$((-1 * intmousespeed)) 91 xdotool mousemove_relative -- 0 $intpixels #move mouse up 92 intmousespeed=$((intmousespeed + intmouseacc)) #speed up 93 ;; 94 "down") 95 intpixels=$(( 1 * intmousespeed)) 96 xdotool mousemove_relative -- 0 $intpixels #move mouse down 97 intmousespeed=$((intmousespeed + intmouseacc)) #speed up 98 ;; 99 "left") 100 intpixels=$((-1 * intmousespeed)) 101 xdotool mousemove_relative -- $intpixels 0 #move mouse left 102 intmousespeed=$((intmousespeed + intmouseacc)) #speed up 103 ;; 104 "right") 105 intpixels=$(( 1 * intmousespeed)) 106 xdotool mousemove_relative -- $intpixels 0 #move mouse right 107 intmousespeed=$((intmousespeed + intmouseacc)) #speed up 108 ;; 109 "select") 110 xdotool click 1 #left mouse button click 111 ;; 112 "return") 113 xdotool key "Alt_L+Left" #WWW-Back 114 ;; 115 "exit") 116 echo Key Pressed: EXIT 117 ;; 118 "F2") 119 chromium-browser "https://www.youtube.com" & 120 ;; 121 "F3") 122 chromium-browser "https://www.google.com" & 123 ;; 124 "F4") 125 echo Key Pressed: YELLOW C 126 ;; 127 "F1") 128 chromium-browser --incognito "https://www.google.com" & 129 ;; 130 "rewind") 131 echo Key Pressed: REWIND 132 ;; 133 "pause") 134 echo Key Pressed: PAUSE 135 ;; 136 "Fast forward") 137 echo Key Pressed: FAST FORWARD 138 ;; 139 "play") 140 echo Key Pressed: PLAY 141 ;; 142 "stop") 143 ## with my remote I only got "STOP" as key released (auto-released), not as key pressed; see below 144 echo Key Pressed: STOP 145 ;; 146 *) 147 echo Unrecognized Key Pressed: $strkey ; CEC Line: $keyline 148 ;; 149 150 esac 151 fi 152 if [ -n "$strreleased" ]; then 153 #echo $keyline --- debug 154 case "$strkey" in 155 "stop") 156 echo Key Released: STOP 157 ;; 158 "up") 159 intmousespeed=$intmousestartspeed #reset mouse speed 160 ;; 161 "down") 162 intmousespeed=$intmousestartspeed #reset mouse speed 163 ;; 164 "left") 165 intmousespeed=$intmousestartspeed #reset mouse speed 166 ;; 167 "right") 168 intmousespeed=$intmousestartspeed #reset mouse speed 169 ;; 170 esac 171 fi 172 fi 173 done