Bash script with mouse interaction

Thanks for reading my question. Getting this to work is probably more of a general Linux question and I might be in error posting it here instead of stack exchange, if so Moderators please let me know.

I have written a bash script for controlling several things on my Openwrt x86 box which is triggered with mouse clicks using this escape code "[?1000l".
It works as it should when I connect over ssh to the Openwrt box, but it does not work when I try to run it directly on the Openwrt box.
As best I can tell it has something to do with the environment features available when connecting over ssh vs directly.
If anyone knows how I can get this working or even a completely different approach for using the mouse directly on the Openwrt box with shell scripts I would really appreciate it.

https://asciinema.org/a/fm2egzkDfSbC7vTywKyWwWLxY

The script I wrote:

#!/usr/bin/env bash

cleanup() {
    #
    #CLEAN STUFF HERE
    #
    echo -e "\e[?1000l" #stop mouse events
    echo -e "\e[0m" #set screen back
    clear #clear screen
    echo QUIT
    date +%T
    exit
}

trap 'cleanup' INT

echo -e "\e[?1000h" #start mouse events

echo -e "\e[5;30;42m" #reen
clear #clear screen

printf '\n\tClick to begin or Ctrl-C to exit\n\t'

count=0
while read -rn12; do
    echo -e "\e[5;30;41m" #red
    clear #clear screen
    (( count++ ))
    #
    #BEGIN STUFF HERE
    #
    printf "\n\tITERATION: $count\n\tBEGIN"
    sleep 4s &
    pid=$!
    while kill -0 "$pid"; do
        if read -rn12 -t1; then
            kill "$pid"
            break
        fi
    done 2>/dev/null
    echo -e "\e[5;30;42m" #green
    clear #clear screen
    #
    #END STUFF HERE
    #
    printf "\n\tEND"
done

Just to make sure I understand... you are trying to run this script on an OpenWrt box?

Did you install bash? the default/standard shell is ash, so you would need to have actively installed bash and you'd need to be running this in that shell.

Meanwhile, are you attempting to use an actual mouse, or just the escape codes when on the OpenWrt box? I'm don't believe that OpenWrt has any mouse drivers at all, given that it does not drive a GUI on the local device (text mode only). Given this, it probably wouldn't register actual mouse clicks. I also don't know if it would understand the escape code. That said, why trigger with mouse clicks?

1 Like

Hello @psherman ! Thank you for taking the time to reply my post :pray:
Indeed I do have bash installed and yes indeed I am trying to run this directly on my Openwrt box. I made headway during the time after I made the initial post. Turns out there is package (that you have probably heard of, but is new to me) called evtest (https://openwrt.org/packages/pkgdata/evtest) which is capable of feeding the mouse events into my script such that I can do away with the escape code method. Here is my new script incase someone comes across this post and tries to do the same thing. It seems to be working exactly as I need it to, I guess whatever inbuilt USB HID mouse drivers the Openwrt Linux kernel has is good enough.

#!/usr/bin/env bash

AUTO_OFF_TIMER="4s"

DEV='/dev/input/event5'
LEFTD='*code 272 (BTN_LEFT), value 1*'
LEFTU='*code 272 (BTN_LEFT), value 0*'

BG_WORK() {
    sleep "$AUTO_OFF_TIMER"
    echo -e "\e[1;37;42m" #green
    clear #clear screen
    #
	# DO STUFF
	#
}

cleanup() {
    #
	# DO STUFF
	#
    echo -e "\e[0m" #set screen back
    clear #clear screen
    echo QUIT
    date +%T
    exit
}

trap 'cleanup' INT

echo -e "\e[1;37;42m" #green
clear #clear screen

printf '\e[1;37;44m\n\tLEFT CLICK TO START/STOP OR CTRL-C TO EXIT\t'

evtest "$DEV" | while read line; do
  case $line in
    ($LEFTD)
                if kill -0 "$pid"; then
                    kill "$pid"
                    echo -e "\e[1;37;42m" #green
                    clear #clear screen
					#
					# DO STUFF
					#
                else
                    echo -e "\e[1;37;41m" #red
                    clear #clear screen
					#
					# DO STUFF
					#
                    BG_WORK &
                    pid=$!
                fi
                ;;
    ($LEFTU)    echo "LEFT CLICK UP" > /dev/null
                ;;
  esac
done

2 Likes

Well that is cool! I didn’t know of that package, but I’ll glad that I just learned about it.

Glad you got it all working!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.