libinput: Touchpad pressure-based touch detection

Touchpad pressure-based touch detection

libinput uses the touchpad pressure values to detect wether a finger has been placed on the touchpad.

This is Information provided by the kernel and combines with a libinput-specific hardware database to adjust the thresholds on a per-device basis. libinput uses pressure thresholds primarily to filter out accidental light touches but pressure information is also used for some Palm detection.

Pressure thresholds are not directly configurable by the user, rather it is planned that libinput provides custom pressure thresholds for each device where necessary. See Debugging touchpad pressure ranges for instructions for your local device.

# Information provided by the kernel

The kernel sends multiple values to inform userspace about a finger touching the touchpad. The most basic is the `EV_KEY/BTN_TOUCH` boolean event that simply announces physical contact with the touchpad. The decision when this event is sent is usually made by the kernel driver and may depend on device-specific thresholds. These thresholds are transparent to userspace and cannot be modified.

Many contemporary touchpad devices provide an absolute pressure axis in addition to `BTN_TOUCH`. This pressure generally increases as the pressure increases, however few touchpads are capable of detection pressure. The pressure value is usually related to the covered area - as the pressure increases a finger flattens and thus covers a larger area. The range provided by the kernel is not mapped to a specific physical range and often requires adjustment. Pressure is sent by the `ABS_PRESSURE` axis for single-touch touchpads or `ABS_MT_PRESSURE` on multi-touch capable touchpads.

Some devices provide additional touch size information through the `ABS_MT_TOUCH_MAJOR/ABS_MT_TOUCH_MINOR` axes and/or the `ABS_MT_WIDTH_MAJOR/ABS_MT_WIDTH_MINOR` axes. While the kernel documentation specifies how these axes are supposed to be mapped, few devices forward reliable information.

# Debugging touchpad pressure ranges

This section describes how to determine the touchpad pressure ranges required for a touchpad device and how to add the required hwdb entry locally. Note that the hwdb entry is not public API and may change at any time. Users are advised to report a bug with the updated pressure ranges when testing has completed.

Use the `libinput measure touchpad-pressure` tool provided by libinput. This tool will search for your touchpad device and print some pressure statistics, including whether a touch is/was considered logically down. Example output of the tool is below:

    $ sudo libinput measure touchpad-pressure
    Ready for recording data.
    Pressure range used: 8:10
    Palm pressure range used: 65535
    Place a single finger on the touchpad to measure pressure values.
    Ctrl+C to exit

    Sequence 1190 pressure: min:  39 max:  48 avg:  43 median:  44 tags: down
    Sequence 1191 pressure: min:  49 max:  65 avg:  62 median:  64 tags: down
    Sequence 1192 pressure: min:  40 max:  78 avg:  64 median:  66 tags: down
    Sequence 1193 pressure: min:  36 max:  83 avg:  70 median:  73 tags: down
    Sequence 1194 pressure: min:  43 max:  76 avg:  72 median:  74 tags: down
    Touchpad pressure:  47 min:  47 max:  86 tags: down

The example output shows five completed touch sequences and one ongoing one. For each, the respective minimum and maximum pressure values are printed as well as some statistics. The `tags` show that sequence was considered logically down at some point. This is an interactive tool and its output may change frequently. Refer to the libinput-measure-touchpad-pressure(1) man page for more details.

By default, this tool uses the udev hwdb entries for the pressure range. To narrow down on the best values for your device, specify the 'logically down' and 'logically up' pressure thresholds with the ``--touch-thresholds` argument:

    $ sudo libinput measure touchpad-pressure --touch-thresholds=10:8

Interact with the touchpad and check if the output of this tool matches your expectations.

Once the thresholds are decided on (e.g. 10 and 8), they can be enabled with the following hwdb file:

    $> cat /etc/udev/hwdb.d/99-touchpad-pressure.hwdb
    libinput:name:*SynPS/2 Synaptics TouchPad:dmi:*svnHewlett-Packard:*pnHPCompaq6910p*
     LIBINPUT_ATTR_PRESSURE_RANGE=10:8

The first line is the match line and should be adjusted for the device name (see evemu-record's output) and for the local system, based on the information in `/sys/class/dmi/id/modalias`. The modalias should be shortened to the specific system's information, usually system vendor (svn) and product name (pn).

Once in place, you need to run the following to commands, adjusted for your device's event node (see How to apply hwdb changes):

        sudo udevadm hwdb --update
        sudo udevadm test /sys/class/input/eventX

If the pressure range property shows up correctly, restart X or the Wayland compositor and libinput should now use the correct pressure thresholds. The Helper tools can be used to verify the correct functionality first without the need for a restart.

Once the pressure ranges are deemed correct, report a bug to get the pressure ranges into the repository.