Fix Digital Pen Mapping on 2-in-1 Laptop

Ubuntu MATE 22.04 - Fix Digital Pen Mapping on the LENOVO FLEX 5

Ubuntu MATE provides support for touch screens by default. My Lenovo Flex 5i is a 2-in-1 (a.k.a "convertible") laptop with the ability to use the touch screen with a finger or an included stylus (Lenovo digital pen) on the screen. I did have to make some adjustments to the stylus/screen mapping when using the stylus, though. In this tutorial I describe how to fix that mapping for this model but you will likely find the concepts useful to fix a similar issue with other makes and models of 2-in-1 laptops. If you want additional details with more usability tips that made using the device easier for me when in tablet mode, I wrote an article 5 Steps To Configure The Lenovo IdeaPad Flex 5i With It's Stylus Using Ubuntu MATE.

Getting Started

After I replaced Windows with Ubunt MATE 22.04, everything appeared to work just fine before I tried rotating the screen. For purposes of testing, I used the Displays application to rotate the screen left, right, and inverted as well as to the normal orientation. After rotating the screen, using my finger on the touch screen worked just fine no matter the orientation. However, the stylus worked properly only in the normal landscape orientation. I found that the stylus appeared to be mapped incorrectly in the other orientations. For example, when orienting the screen upside down (inverted landscape), tapping the stylus in the upper left corner of the screen would make the pointer appear in the lower right corner.

How To Map the Stylus Correctly for Different Screen Orientations

In order to get the stylus to map correctly, and knowing that the finger touch was mapped correctly, I compared the mapping for finger and stylus in each orientation. Here is how I did that. (Reference:

1. Determine the names of the touch-enabled hardware device using the 'xsetwacom' command:

I typed this command into a terminal:
xsetwacom --list devices

Here is the output I received:

Wacom HID 52C8 Pen stylus id: 9 type: STYLUS
Wacom HID 52C8 Pen eraser id: 15 type: ERASER
Wacom HID 52C8 Finger touch id: 10 type: TOUCH

(xsetwacom is installed in Ubuntu MATE 22.04 by default.)

2. Determine the coordinate map for each device using 'xinput list-props'.

In my case, I discovered that the stylus used this mapping no matter the screen orientation. Here is the command I used to determine that:
xinput list-props "Wacom HID 52C8 Pen stylus"

This is the result I got for the stylus regardless of the screen orientation:

Device 'Wacom HID 52C8 Pen stylus':
Device Enabled (190): 1
Coordinate Transformation Matrix (192): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000

We don't need all of that for the mapping. Here is the key information we need from this with all the extra zeros removed:

"Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1</kbd>

Now, while the screen was in each of the 4 orientations, I ran this command to see the mapping used by the finger touch device, that was behaving correctly:
xinput list-props "Wacom HID 52C8 Finger touch"

This lists the Coordinate Transformation Matrix information we will need for each orientation:
Normal orientation: "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
Left orientation: "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
Inverted orientation: "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
Right orientation: "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1

3. Create Scripts to Rotate the Screen and Fix the Mapping

Armed with the information above, you can create one or more scripts to rotate the screen and fix the mapping when you run the scripts. I created four scripts, one for each orientation of the screen, using the Coordinate Transform matrices above. Here are the two commands that form the main components of the script. For each of the four scripts that I created, see the article mentioned above. Feel free to copy and modify the example scripts if they will help you with your touch screen and stylus configuration.

xrandr --output eDP-1 --rotate inverted
xinput set-prop "Wacom HID 52C8 Pen stylus" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1