This is one of those really simple things that are surprisingly tricky to do right. My goal: automatically disable the touchpad when an external mouse is plugged in. Why go through all the trouble? Because—in addition to being plain annoying—a moving cursor can alter the window focus in my current tiling window manager.
On Windows, you can either do it easily (if the driver supports it) or it’s nearly impossible. For better or worse, on Linux, you can almost always do it after expending some (possibly non-trivial) effort.
In the end, I boiled it down to:
- /bin/touchpad-ctl: executable script; don’t forget to set
- /etc/udev/rules.d/01-touchpad.rules: two udev rules
- a minor modification to
These are intended for Arch Linux. I haven’t tried on any other systems yet, but it shouldn’t be too hard to adapt them for other distributions. It might even work out of the box!
I started out by following the Arch Linux wiki, but I’ve simplified the script and made some of my own discoveries along the way.
Firstly, the udev rules: these consist of two simple rules that will call
/bin/touchpad-ctl whenever a mouse is added or removed. Nothing too fancy here.
# /etc/udev/rules.d/01-touchpad.rules SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", RUN+="/bin/touchpad-ctl" SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", RUN+="/bin/touchpad-ctl"
The udev rules would miss the opportunity to fire if the mouse is already plugged in at boot, so it’s necessary to call
.xinitrc to make sure that the touchpad is disabled in this scenario.
touchpad-ctl script is where the magic occurs. Since I’ve not come across an easy to way to figure out which device is the actual touchpad, you’ll have to set this variable manually in the script. To list all the mouse-like devices, run this command:
(If it says it can’t find udevadm, then try running it as root.)
The script does two things:
Check if any external mouse is plugged in. This is the easy part: simply loop through all the mice and check if it’s an external mouse (i.e. not the touchpad).
Enable or disable the touchpad with
synclient. This is a bit more difficult because it needs to know the
DISPLAYvariable is hardcoded, while the
XAUTHORITYis obtained from the home directories of the users. This is repeated for every user.