Configure your gaming mouse on Linux

Or: Configure XInput properties for USB devices on Linux dynamically
Using gamer mice with Linux occasionally turns out to be problematic, because their high resolution combined with the default X server settings makes the mouse pointer incredibly fast to a point where it is unusable. I am using a Razer Taipan mouse at 4000 DPI, a good speed for gaming, but not for work.

USB devices on Linux are of course configured dynamically. Their X windows properties like mouse pointer acceleration are managed through the XInput tool. The basic desktop settings tools allow configuring the mouse pointer speed, but not much more. This is insufficient when using multiple devices (in this case, the built-in laptop trackpoint and an external mouse) that require different settings. Also, there is no way to disable mouse pointer acceleration, the most useless default setting ever invented :-) One solution which is described below is to script configuring the settings using XInput and Python. There is an open question at the end in case anybody is able to help.

The basic task is to examine the input devices known to the X server (for example by using xinput --list on the command line), determine the device ids from the listing, and then applying specific properties to known devices. While xinput supports device names as selectors when setting properties, this approach turned out not to work reliably, probably because the devices are listed more than ones and names occur multiple times because of that. What did work was setting the device properties using the device ids. So the script first determines the device id for a device name, and then applies properties to it:

#!env /usr/bin/python
import sh, re

def configure_device(device_name, properties):
    for line in sh.xinput("list"):
        match = re.match('.*{0}.*id=(\w+).*'.format(device_name), line)
        if match:
            device_id = match.group(1)
            for property in properties:
                sh.xinput("set-prop", device_id, property, properties[property])
            print("{0} (device id {1}) configured.".format(device_name, device_id))
            return
    print("Device named {0} not found!".format(device_name))

This works reliably as long as the device names are detected in xinput --list. If a device cannot be found, the script continues. This is not an error, it happens for example in case a USB mouse is not connected.

Note

There are other ways to configure mouse pointer settings, as for example described in the ArchLinux Wiki. These approaches failed in the described case because they assume that the device id is known or does not change, or have to be configured by the super user.

The next step is to determine the specific properties to set. This requires some experimentation to match personal preferences. The trick to get rid of mouse pointer acceleration is to set Device Accel Velocity Scaling to 1. The mouse pointer speed can be influenced with Device Accel Constant Deceleration. The following snippet configures separate speeds for the trackpoint and the Razer mouse, and disables acceleration:

configure_device('IBM TrackPoint',
                 { 'Device Accel Velocity Scaling': 1,
                   'Device Accel Constant Deceleration': 1 } )
configure_device('Razer Taipan',
                 { 'Device Accel Velocity Scaling': 1,
                   'Device Accel Constant Deceleration': 0.5 } )

The rest is up to some trial-and-error fiddling to find the right settings. The script uses the Python sh module. A common way to make that available is to install it using pip. Assuming sudo accepts pip invocations without asking for a password, wrapping the configuration script in this shell batch takes care of it:

#!/bin/sh
#
sudo pip -q install sh && \
    python $HOME/bin/Setup-Mouse-Pointer-Acceleration.py

The remaining question is how to automatically invoke the configuration script whenever a X input device is connected. If anybody knows how to get this done, please leave a comment.

One response to “Configure your gaming mouse on Linux

  1. This was real helpful! Was struggling for hours getting my gaming mouse to work. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s