Linux mouse speed adjustment

Posted on 08 January 2017
2 minute read

So I’ve recently switched semi-permanently over to using Linux Mint as my OS on my Lenovo W530. Using *nix as a desktop OS isn’t new for me, back i the day I used to run FreeBSD4.4 with Window Maker on an IBM PII 350Mhz desktop box.

However, things have moved on nicely and when I have my laptop sitting in my dock, I have a mouse plugged in, namely a Razer Mamba. This is a great mouse, and one of only 2 mice I actually enjoy using (my other trusty steeds are a couple of Logitech LX8s). The issue I have is that under Linux Mint both the trackpad and mouse are waaaay too fast for my liking and the type of use I need them for, despite being adjusted to the lowest setting in the Mouse & Touchpad settings, so I use xinput to set them directly. The problem is, is that the mouse is listed twice; the first instance is for the mouse, and the second instance as a “keyboard” (I guess this is due to the programmable nature of the device, which means I need to set the properties by using the device ID instead of the name. Not too much of an issue if you do it manually, but I want to automate this at login as setting it manually each time will get very boring, very quickly, so I decided to use PHP for this purpose as I already have it installed on the laptop.

I use the following script to set both the trackpad and mouse speeds according to the values I like. YMMV and the values can easily be adjusted from the constants at the top of the script.

#!/usr/bin/php
<?php
/**
 * Mouse & trackpad speed adjustment
 */
const MOUSE_DEVICE_DECEL = 1.8;
const TRACKPAD_DEVICE_DECEL = 5;

$deviceList = `xinput --list`;
$devices = explode("\n", $deviceList);

foreach ($devices as $device) {
    if (false !== strpos($device, 'Razer Razer Mamba')) {
        // Get first instance ID (1st == mouse, 2nd == keyboard)
        preg_match('/id=([0-9]+)/', $device, $matches);

        if (2 == count($matches)) {
            $deviceId = trim($matches[1]);
            shell_exec('xinput --set-prop ' . $deviceId . ' "Device Accel Constant Deceleration" ' . MOUSE_DEVICE_DECEL);
            echo "Mouse speed adjusted.\n";
        }

        break;
    }
}

shell_exec('xinput --set-prop "SynPS/2 Synaptics TouchPad" "Device Accel Constant Deceleration" ' . TRACKPAD_DEVICE_DECEL);

echo "Trackpad speed adjusted.\n";

Once the script was coded, I saved this as mouse.php under my home dir and added it to my Startup Applications settings: Startup applications