It's the same problem with the screen orientation on the tablet-notebook again. Or, to be precise, the touch orientation, or perhaps more accurately, the stylus orientation.
On this notebook, the screen orientation works well with a fresh installation. The touchscreen also works fine. Even after rotating the screen. The stylus worked well until I used it in the default screen position. This was fine as I was only using it in landscape mode. However, after a long time — longer than I would have expected — I discovered that when the screen is rotated, the stylus axes are inverted or flipped depending on the rotation. Interestingly, the touchscreen works well and it's just the stylus that's broken.
As I cannot have anything that works perfectly, I had to find a solution. After some unsuccessful attempts, when nothing changed or, even worse, things got worse or I even messed up the touchscreen, I finally found a solution that fixed it.
At first, I thought I would create some transformation matrices, as I did with the VisionBooks screen orientation failure. Unfortunately, it does not work in this case. The best solution seems to be to write a script that listens for changes in screen orientation and then adjusts the orientation of the pen device directly. It's not ideal, but it works really well, and I'll stick with it.
So the solution is to create a script in
/usr/local/bin/auto-rotate.sh
with following content:
!/bin/bash
your internal display name (check with: xrandr | grep " connected")
SCREEN="eDP-1"
your pen device name (check with: xinput list)
PEN="NAME_OF_THE_PEN_DEVICE"
PEN_ID=$(xinput list --id-only "$PEN")
monitor-sensor | while read -r line; do
case "$line" in
normal)
xrandr --output $SCREEN --rotate normal
xinput set-prop $PEN_ID "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
;;
bottom-up)
xrandr --output $SCREEN --rotate inverted
xinput set-prop $PEN_ID "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
;;
right-up)
xrandr --output $SCREEN --rotate right
xinput set-prop $PEN_ID "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
;;
left-up)
xrandr --output $SCREEN --rotate left
xinput set-prop $PEN_ID "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
;;
esac
done
replace the NAME_OF_THE_PEN_DEVICE by the name of your device, you can get like that:
xinput list
And make the script executable:
sudo chmod +x /usr/local/bin/auto-rotate.sh
As an optional extra, try running it to check that everything works correctly.
Then you can set it to run automatically. Create a systemd service by creating a new unit file.
sudo nano /etc/systemd/system/auto-rotate.service
and add following content:
[Unit]
Description=Auto screen & stylus rotation
[Service]
ExecStart=/usr/local/bin/auto-rotate.sh
Restart=always
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/%i/.Xauthority
User=USER_NAME
[Install]
WantedBy=graphical.target
Remember to replace 'USER_NAME' with the name of your user.
Finally, just enable the service and you're all set!
sudo systemctl enable auto-rotate.service
sudo systemctl start auto-rotate.service