How to disable touchpad when external mouse is connected

I unconsciously keep my thumbs on built-in touchpad, so I decided to use external mouse and automatically disable touchpad for the time mouse is connected.

First step

The first step is to identify touchpad device and learn how to turn it off.

Use xinput command to list input devices.

$ xinput 
⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ DLL0665:01 06CB:76AD UNKNOWN            	id=10	[slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad              	id=12	[slave  pointer  (2)]
⎜   ↳ Bluetooth Laser Travel Mouse            	id=14	[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)]
    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
    ↳ Power Button                            	id=8	[slave  keyboard (3)]
    ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard            	id=11	[slave  keyboard (3)]
    ↳ Dell WMI hotkeys                        	id=13	[slave  keyboard (3)]

Inspect state of each suspected Virtual core pointer device (id equal to 10 and 12 in this case) to find the right one as the individual values will change when you use it.

$ xinput --query-state 10
2 classes :
ButtonClass
	button[1]=up
	button[2]=up
	button[3]=up
	button[4]=up
	button[5]=up
	button[6]=up
	button[7]=up
	button[8]=up
	button[9]=up
	button[10]=up
	button[11]=up
	button[12]=up
ValuatorClass Mode=Relative Proximity=In
	valuator[0]=269
	valuator[1]=371
	valuator[2]=-589
	valuator[3]=-1265

Verify device by disabling it for a while.

$ xinput --disable 10

Enable it afterwards.

$ xinput --enable 10
In my case (Dell XPS 13) the touchpad device is DLL0665:01 06CB:76AD UNKNOWN with id equal to 10.

Second step

The second step is to gather information required to clearly identify external mouse device.

Execute the following command to start monitoring udev kernel events and connect external mouse to identify the device.

$ udevadm monitor -k -s input
monitor will print the received events for:
KERNEL - the kernel uevent

KERNEL[49387.579818] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:046D:B008.0003/input/input16 (input)
KERNEL[49387.580519] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:046D:B008.0003/input/input16/mouse2 (input)
KERNEL[49387.580556] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:046D:B008.0003/input/input16/event13 (input)

Use device found in previous step to print its properties, so it could be easily identified later.

$ udevadm info -a -p //devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:046D:B008.0003/input/input16

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:046D:B008.0003/input/input16':
    KERNEL=="input16"
    SUBSYSTEM=="input"
    DRIVER==""
    ATTR{name}=="Bluetooth Laser Travel Mouse"
    ATTR{phys}=="5c:e0:c5:9d:63:fd"
    ATTR{uniq}=="00:07:61:ec:be:5c"
    ATTR{properties}=="0"

[...]

Third step

The third step is to create udev rules using preceding information.

Identify external mouse device (second step) and execute command to disable/enable touchpad (first step) as user specific user.

Create /etc/udev/rules.d/99-touchpad-control.rules udev rules file.

$ cat << EOF | sudo tee /etc/udev/rules.d/99-touchpad-control.rules
SUBSYSTEM=="input", ACTION=="add",    ATTR{name}=="Bluetooth Laser Travel Mouse", RUN+="/bin/su milosz -c 'DISPLAY=:0 xinput --disable 10'"
SUBSYSTEM=="input", ACTION=="remove", ATTR{name}=="Bluetooth Laser Travel Mouse", RUN+="/bin/su milosz -c 'DISPLAY=:0 xinput --enable  10'"
EOF
Replace milosz with your own user name.

Signal udevd to reload the rules.

$ sudo udevadm control -R

Connect external mouse and test touchpad behavior.

Update for Ubuntu 15.10 Wily Werewolf

This solution works on Ubuntu 15.04 Vivid Vervet, but not on Ubuntu 15.10 Wily Werewolf.

Inspect /var/log/Xorg.0.log file if you are interested (look for (--) synaptics: DLL0665:01 06CB:76AD UNKNOWN: touchpad found line and output of xinput watch-props 10 command).

The solution on Ubuntu 15.10 Wily Werewolf is to use synclient command to disable or re-enable touchpad.

$ cat << EOF | sudo tee /etc/udev/rules.d/99-touchpad-control.rules
SUBSYSTEM=="input", ACTION=="add",    ATTR{name}=="Bluetooth Laser Travel Mouse", RUN+="/bin/su milosz -c 'DISPLAY=:0 /usr/bin/synclient TouchpadOff=1'"
SUBSYSTEM=="input", ACTION=="remove", ATTR{name}=="Bluetooth Laser Travel Mouse", RUN+="/bin/su milosz -c 'DISPLAY=:0 /usr/bin/synclient TouchpadOff=0'"
EOF

Additional notes

Inspect loginctl command to automate this task further (for example loginctl --no-legend list-sessions | awk '$4 == "seat0" {print $3}' to get required username).

You might be interested in these blog posts:

How to automatically set up external monitor

How to enforce read-only mode on every connected USB storage device

References

I strongly suggest to read to read the following handbook to grasp the udev concepts.

Writing udev rules by Daniel Drake

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu