Skip to content

Instantly share code, notes, and snippets.

@duckida
Created February 16, 2026 15:23
Show Gist options
  • Select an option

  • Save duckida/571a93b682a9a0c4281631875ce6cdca to your computer and use it in GitHub Desktop.

Select an option

Save duckida/571a93b682a9a0c4281631875ce6cdca to your computer and use it in GitHub Desktop.
How to convert xinput calibration to LibInput matrix

Touch Coordinate Calibration with libinput

1. The Mathematical Framework

Modern libinput uses an Affine Transformation Matrix. To map a raw touch coordinate $(x, y)$ to a normalized screen coordinate $(x', y')$, we use the following equation:

$$ \begin{bmatrix} x' \ y' \ 1 \end{bmatrix} = \begin{bmatrix} a & b & c \ d & e & f \ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \ y \ 1 \end{bmatrix} $$


2. Input Data Required

To perform the conversion, you need the following values from xinput_calibrator:

  • MinX, MaxX: The horizontal raw range.
  • MinY, MaxY: The vertical raw range.
  • Screen Resolution: Optional, but $65535$ is the standard normalized range for X11.

3. Step-by-Step Conversion Logic

Step A: Calculate the Spans

$$ \Delta X = MaxX - MinX $$ $$ \Delta Y = MaxY - MinY $$

Step B: Calculate Scale Factors ($a$ and $e$)

$$ a = \text{Target Range} / \Delta X $$ $$ e = \text{Target Range} / \Delta Y $$

Note: Use $1.0$ if the target is normalized, or $65535$ for raw X11.

Step C: Calculate Offsets ($c$ and $f$)

$$ c = -MinX / \Delta X $$ $$ f = -MinY / \Delta Y $$

Step D: Handle Rotations (The "Swap" Logic)

If the screen is rotated 90° or 270°, the $a$ and $e$ values move to the $b$ and $d$ positions.


4. Reference Conversion (Real World Example)

Based on the ADS7846 data:

  • MinX: 3982, MaxX: 62782
  • MinY: 62821, MaxY: 3430
Variable Calculation Result
$\Delta X$ $62782 - 3982$ 58800
$\Delta Y$ $3430 - 62821$ -59391
Scale X ($a$) $65535 / 58800$ (approx) 1.1145
Scale Y ($e$) $65535 / -59391$ (approx) -1.1034
Offset X ($c$) $-3982 / 58800$ -0.0677
Offset Y ($f$) $-62821 / -59391$ 1.0577

Final libinput string

"1.1145 0 -0.0677 0 -1.1034 1.0577 0 0 1"

5. Troubleshooting & Configuration Notes

When generating these matrices, verify the following:

  • Inversion Check: If $MinY > MaxY$, the $e$ value must be negative and the $f$ offset will usually be $> 1$.
  • Precision: Provide at least 4 decimal places for accuracy on small (3.5") screens.
  • Driver Specifics:
    • Remind the user that libinput configuration must be placed in /etc/X11/xorg.conf.d/.
    • The evdev driver must be uninstalled to prevent property overrides.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment