跳转至

屏幕旋转

linux 系统中 xrandr 可以配置屏幕方向

屏幕显示方向配置

  • 查看当前屏幕信息

Tip

根据打印信息可知,当前系统为单屏幕显示,并且显示设备名称为 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  
  • 旋转屏幕显示方向

Note

normal:将显示设备正常的显示。
left:将显示设备逆时针旋转 90 度。
inverted:将显示设备旋转 180 度。
right:将显示设备顺时针旋转 90 度。

设置指定显示设备的旋转方向

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

操作示例:配置 HDMI-1 显示逆时针旋转 90 度。

$ xrandr --output HDMI-1 --rotate left
  • 开机自启动旋转服务参考

Ubuntu 系统

配置 LVDS-1 显示逆时针旋转 90 度

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 系统

配置 DSI-1 显示逆时针旋转 90 度

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

脚本配置

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 "$@" 

服务配置

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


触摸显示方向配置

1. 安装工具

apt update
apt install xinput
apt install xinput-calibrator

2. 查看设备和 ID,根据打印信息可知,当前触摸设备为 "goodix-ts",id 为 10

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

3. 配置输入设备的校准矩阵

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

操作示例:设置 goodix-ts 设备的校准矩阵

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. 配置输入设备坐标转换矩阵

// 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

操作示例: 根据旋转情况使用对应命令,设置 goodix-ts 设备的坐标转换矩阵为 left

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

5. 校准触摸

$ xinput_calibrator -v --device $id

操作示例: 校准 goodix-ts 设备

$ xinput_calibrator -v --device 10
  • 永久修改触摸方向

xinput 修改成功后,修改旋转触摸。

Tip

Option "TransformationMatrix" "0 -1 1 1 0 0 0 0 1" 为添加的内容

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