2

I have bluetooth mouse xinput setting script to run whenever i have connect the mouse. Currently I'm simply alias the short key as 'bm' for that bash file execution but I want to know if there is automatic way to execute the bash file or alias command whenever it detects the mouse connection.

Thank in advance!

ubuntu 16.10

2 Answers2

1

What you want is to use polling approach, to continuously read output of xinput --list --name-only and figure out if the name of your mouse is there or not, and if it is - run the script. This would be something like this:

while true; do
    if xinput --list --name-only | grep -q -i 'Mouse Name' ; 
    then 
        echo "yes" # this is where you run script
        break # exit the loop after running the script.   
    fi 
done

In this case we exit loop as soon as the mouse is there. However you probably want this to be continuous so that you can connect and disconnect the mouse. Instead of break, I'd use another while loop there, that does the opposite - waits for mouse name to disappear. Body of that while loop can be just this - true or :.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
0

I would use a .rules file.

First, find out the ID_VENDOR_ID and the ID_MODEL_ID of your mouse. Disconnect the mouse, run this command and connect the mouse (the |grep ID part is only to filter information that you don't need).

udevadm monitor --property|grep ID

Lets say that you get these values:

ID_VENDOR_ID=0a12
ID_MODEL_ID=0001

Now create a file in the rules folder (96 is the priority of the rule):

sudo gedit /etc/udev/rules.d/96-myusb.rules

Add these two lines using your values for the ID_VENDOR_ID and ID_MODEL_ID. If you don't want to do anything when you remove it, don't include the second line.

ACTION=="add", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12", ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-add.sh"

ACTION=="remove", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12",ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-remove.sh"

You can test that it works creating the two scripts:

$ sudo gedit /usr/local/bin/myusb-add.sh

Add something like this (change add to remove in the other one):

#!/bin/bash
echo "added" >> /tmp/myusb.log

Finally, tail the file with tail -f /tmp/myusb.log and connect/disconnect your mouse. You should see that the text gets added to the file.

Katu
  • 3,593
  • 26
  • 42