How to make a program that finds id's of xinput devices and sets xinput some settings

Filip S. picture Filip S. · Sep 12, 2013 · Viewed 23.8k times · Source

I have a G700 mouse connected to my computer. The problem with this mouse in Linux (Ubuntu) is that the sensitivity is very high. I also don't like mouse acceleration, so I've made a script that turns this off. The script looks like this

#!/bin/bash
# This script removes mouse acceleration, and lowers pointer speed
# Suitable for gaming mice, I use the Logitech G700.
# More info: http://www.x.org/wiki/Development/Documentation/PointerAcceleration/
xinput set-prop 11 'Device Accel Profile' -1
xinput set-prop 11 'Device Accel Constant Deceleration' 2.5
xinput set-prop 11 'Device Accel Velocity Scaling' 1.0
xinput set-prop 12 'Device Accel Profile' -1
xinput set-prop 12 'Device Accel Constant Deceleration' 2.5
xinput set-prop 12 'Device Accel Velocity Scaling' 1.0

Another problem with the G700 mouse is that it shows up as two different devices in xinput. This is most likely because the mouse has a wireless adapter, and is usually also connected via a usb cable (for charging). This is my output from xinput --list (see id 11 and 12):

$ xinput --list
⎡ Virtual core pointer                              id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                    id=4    [slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver                         id=8    [slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver                         id=9    [slave  pointer  (2)]
⎜   ↳ Logitech Unifying Device. Wireless PID:4003   id=10   [slave  pointer  (2)]
⎜   ↳ Logitech G700 Laser Mouse                     id=11   [slave  pointer  (2)]
⎜   ↳ Logitech G700 Laser Mouse                     id=12   [slave  pointer  (2)]
⎣ Virtual core keyboard                             id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard                   id=5    [slave  keyboard (3)]
    ↳ Power Button                                  id=6    [slave  keyboard (3)]
    ↳ Power Button                                  id=7    [slave  keyboard (3)]

This isn't usually a problem, since the id's are usually the same. But sometimes the id's of the mouse change, and that's where my question comes in.

What's the simplest way of writing a script/program that finds the id that belongs to the two listings named Logitech G700 Laser Mouse in the output from xinput --list, and then running the commands in the top script using those two ids?

Answer

Raphael Ahrens picture Raphael Ahrens · Sep 12, 2013

You can do something like the following.

if [ "$SEARCH" = "" ]; then 
    exit 1
fi

ids=$(xinput --list | awk -v search="$SEARCH" \
    '$0 ~ search {match($0, /id=[0-9]+/);\
                  if (RSTART) \
                    print substr($0, RSTART+3, RLENGTH-3)\
                 }'\
     )

for i in $ids
do
    xinput set-prop $i 'Device Accel Profile' -1
    xinput set-prop $i 'Device Accel Constant Deceleration' 2.5
    xinput set-prop $i 'Device Accel Velocity Scaling' 1.0
done

So with this you first find all the IDs which match the search pattern $SEARCH and store them in $ids. Then you loop over the IDs and execute the three xinput commands.

You should make sure that $SEARCH does not match to much, since this could result in undesired behavior.