Skip to content

Screen Rotation

In Linux systems, xrandr can be used to configure the screen orientation.

Display Orientation Configuration

  • Check current screen information

Tip

From the output, we can see that the current system has a single display, and the display device name is HDMI-1.

$ xrandr
Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
HDMI-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 0mm x 0mm
   1920x1080     60.00*+  60.00    50.00    59.94    30.00    24.00    29.97    23.98
   1920x1080i    60.00    50.00    50.00    59.94
   1280x1024     60.02
   1440x900      59.90
   1360x768      60.02
   1280x720      60.00    50.00    50.00    59.94
   1024x768      60.00
   800x600       60.32
   720x576       50.00    50.00
   720x576i      50.00
   720x480       60.00    60.00    59.94    59.94
   720x480i      60.00    59.94
   640x480       60.00    59.94    59.94
  • Rotate the screen orientation

Note

normal: Normal display orientation.
left: Rotate the display 90 degrees counterclockwise.
inverted: Rotate the display 180 degrees.
right: Rotate the display 90 degrees clockwise.

Set the rotation for a specific display device:

$ xrandr --output (dev) --rotate [normal|left|inverted|right]

Example: Rotate HDMI-1 90 degrees counterclockwise:

$ xrandr --output HDMI-1 --rotate left
  • Auto-start rotation service (reference)

Ubuntu System

Configure LVDS-1 to rotate 90 degrees counterclockwise at boot:

root@ubuntu2004:~# vim /etc/systemd/system/xrandr-startup.service
[Unit]
Description=Start xrandr script on boot
After=graphical.target

[Service]
Type=oneshot
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/kickpi/.Xauthority"
ExecStart=/usr/bin/xrandr --output LVDS-1 --rotate left
User=kickpi

[Install]
WantedBy=graphical.target
sudo systemctl enable xrandr-startup
sudo systemctl start xrandr-startup

Debian System

Configure DSI-1 to rotate 90 degrees counterclockwise at boot:

vim /etc/systemd/system/xrandr-startup.service
[Unit]
Description=Start xrandr script on boot
After=graphical.target

[Service]
Type=oneshot
TimeoutStartSec=10
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/linaro/.Xauthority"
ExecStart=/usr/local/bin/wait-for-x11.sh /usr/bin/xrandr --output DSI-1 --rotate left
User=linaro

[Install]
WantedBy=graphical.target

Script configuration:

root@linaro-alip:/# cat /usr/local/bin/wait-for-x11.sh
#!/bin/bash

while ! xrandr --query > /dev/null 2>&1; do
        sleep 1
        echo "Waiting for X server to be ready..."
done
exec "$@"

Service configuration:

sudo systemctl enable xrandr-startup
sudo systemctl start xrandr-startup

Touchscreen Orientation Configuration

1. Install the required tools:

apt update
apt install xinput
apt install xinput-calibrator

2. List devices and their IDs. From the output, the touchscreen device is goodix-ts with ID 10:

$ xinput_calibrator --list
    Device "goodix-ts" id=10

3. Configure the calibration matrix for the input device:

xinput set-prop $id --type=float "libinput Calibration Matrix" 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0

Example: Set the calibration matrix for the goodix-ts device:

xinput set-prop 10 --type=float "libinput Calibration Matrix" 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0

4. Configure the coordinate transformation matrix for the input device:

# normal
$ xinput set-prop $id 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
# left
$ xinput set-prop $id 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1
# right
$ xinput set-prop $id 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1
# inverted
$ xinput set-prop $id 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1

Example: Apply the left coordinate transformation matrix to the goodix-ts device (matching the screen rotation):

xinput set-prop 10 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1

5. Calibrate the touchscreen:

$ xinput_calibrator -v --device $id

Example: Calibrate the goodix-ts device:

$ xinput_calibrator -v --device 10
  • Make the touch orientation change permanent

After successfully modifying the touch configuration with xinput, make it persistent.

Tip

The line Option "TransformationMatrix" "0 -1 1 1 0 0 0 0 1" is the configuration to be added.

vim /usr/share/X11/xorg.conf.d/40-libinput.conf

Section "InputClass"
        Identifier "libinput touchscreen catchall"
        MatchIsTouchscreen "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
        Option "TransformationMatrix" "0 -1 1 1 0 0 0 0 1"
EndSection