Touchpad control for pi-top

From Spivey's Corner
Jump to: navigation, search

The pi-top has a decent touchpad, but for some things it's just easier to use a mouse, and when I have one plugged in, I prefer to disable the touchpad. This page contains a script and some configuration items that make this happen automatically, just as it does on a Mac!

First, the script, which goes in /usr/local/bin/pt-touchpad-ctl, with execute permission set. It works out whether a mouse is connected by looking to see if there is a mouse device other than the touchpad under /sys/class/input. Then it finds the home directory of the user who owns the X session so that it can use the appropriate .Xauthority file. Next, it finds the X device number for the trackpad by looking for a pointer device with "HAILUCK" in its name. It enables or disables this device using xinput.

#!/bin/bash

# Disable the touchpad if a mouse is plugged in, and enable it again when
# the mouse is no longer present.  This is designed to be invoked from udev
# events, and also from the X startup scripts.

# We will disable the touchpad if there is another mouse device
action=enable
if [ "$(grep -l -v HAILUCK /sys/class/input/mouse*/device/name)" != "" ]; then
    action=disable
fi

# Find the home directory for the user owning the X session 
user=$(ps h -o user -C lxsession)
home=$(gawk -v FS=: -v user=$user '$1 == user { print $6 }' /etc/passwd)

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

export DISPLAY=:0
export XAUTHORITY=$home/.Xauthority

# Find the device number for the trackpad
device=$(xinput list | sed -n '/HAILUCK.*pointer/s/.*id=\([0-9]*\).*/\1/p')

# Enable or disable the touchpad
xinput $action $device

This script will enable or disable the trackpad as appropriate, whatever its current state. All we need to arrange is that it is invoked at appropriate times.

Installing the following rules in a file /etc/udev/rules.d/99-touchpad-ctl.rules will cause the script to be invoked whenever a mouse is connected or disconnected.

SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", \
    RUN+="/usr/local/bin/pt-touchpad-ctl"
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", \
    RUN+="/usr/local/bin/pt-touchpad-ctl"

That deals with plugging in or unplugging a mouse while the machine is running, but it doesn't detect a mouse that is present when the machine starts up. For that, it's necessary to invoke the script when the X session first starts. (The design of the script means it's harmless to do this when no mouse is connected.) This can be achieved by adding a line at the end of the file /etc/xdg/lxsession/LXDE-pi/autostart, so that it looks something like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
pt-touchpad-ctl

The contents of that file may have been copied into $HOME/.config/lxsession/LXDE-pi/autostart, so it is necessary to make the same change there, preserving any other customisations that have been made (such as the installation of the pi-top battery widget). Edit: the at sign should be omitted, because we don't want to relaunch the script if it exits.

Following these changes, it's simplest to reboot the machine so that the rules are activated.

Credits

These instructions are based on information found on the following web pages:

I adapted these solutions for LXDE and the Pi-Top hardware.

Notes

  1. You need to have grep, sed, gawk and xinput installed for the script to work. I haven't checked, but I suspect all but gawk are installed as a default.
Personal tools

Variants
Actions
Navigation
Tools