Auto-rotation

From postmarketOS

Some user interfaces like Phosh or Plasma Mobile have auto-rotation implemented. Other user interfaces like Xfce4 or MATE don't have native auto-rotation.

Scripts for X11

For X11, there are many scripts around that provide auto-rotation functionality. The following list is a selection, there are many more.

In MATE there is an issue with updating the screen when rotating by xrandr, the wallpaper area doesn't update entirely. It doesn't affect apps, though. In Xfce4 the workspace swticher plugin doesn't scale its size correctly.

Manual horizontal/vertical screen toggle

This is a simple shell script for manual toggle of the screen orientation. Due to its simplicity, it can be easily implemented. Additionally, as it is triggered manually, it doesn't need to have a functional accelerometer sensor.

  • Copy-paste the content of the script into a new text file. Safe the file somewhere, e.g. as ~/rotate_screen.sh. If you don't have a text editor available, install "mousepad" or open a terminal and use "vi".
  • Make the file executable by chmod +x ~/rotate_screen.sh (you may need to adapt path and filename).
  • Install packages "xrandr" and "xinput" by sudo apk add xrandr xinput.
  • Check the name of your touchscreen device by xinput --list and change it in the script ~/rotate_screen.sh after the "set-prop" (twice).
  • Test your script in a terminal by ~/rotate_screen.sh (may adapt path and name). When trying this via ssh, you have to add DISPLAY=:0 in front: DISPLAY=:0 ~/rotate_screen.sh.
  • Add a new command launcher to the panel, click the icon next to the "command" field, navigate to the script and choose it.
  • Done. If you now hit the launcher in the panel, the screen should rotate. Also the touchscreen should work correctly in the new orientation. If it doesn't, something went wrong. In that case, you can test the "xrandr" and "xinput" lines of the script one by one in a terminal.
  • Alternatively to a launcher, a hardware button or Android touch button could be used. This can be set up in the keyboard shortcuts settings.
#!/bin/sh

if [ "$(xrandr --verbose | grep primary | cut -d ' ' -f6)" = "normal" ]; then
  xrandr -o left
  xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
else
  xrandr -o normal
  xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
fi

Depending on the hardware, a delay of e.g. "sleep 0.3" might be helpful between the lines "xrandr" and "xinput".

Auto-rotation shell script

The next shell script uses "iio-sensor-proxy" to access the accelerometer for auto-rotation. It is based on the following website.

  • Copy-paste the content of the script into a new text file, e.g. ~/auto-rotate_screen.sh.
  • Make the file executable by chmod +x ~/auto-rotate_screen.sh (may adapt path and filename).
  • Install packages "xrandr", "xinput", "inotify-tools" and "iio-sensor-proxy" (sudo apk add xrandr xinput inotify-tools iio-sensor-proxy).
  • Enable the service "iio-sensor-proxy" by sudo rc-update add iio-sensor-proxy.
  • Reboot
  • Open a terminal and check if the command "monitor-sensor" works. Rotate the device and check if the accelerometer output works. Stop the command by Ctrl+C.
  • Check the name of your touchscreen device by xinput --list and change it in the script ~/auto-rotate_screen.sh after the "set-prop" (four times).
  • Test your script in a terminal by ~/auto-rotate_screen.sh. Stop it by Ctrl+C. Btw. testing via ssh doesn't work (because of command "monitor-sensor").
  • Add the script to the startup applications and reboot. At Xfce4, that's in Settings -> Session and Startup -> Tab "Application Autostart" -> button "Add" -> click the icon next to the "command" field, navigate to your script and select it.
  • Reboot again. The auto-rotation should now be enabled.
  • To disable/enable auto-rotation, create a launcher with the command for Xfce4 /bin/sh -c 'if [ $(ps -A | grep auto-rotate | wc -l) -ge 1 ]; then killall auto-rotate_screen.sh; else /home/user/auto-rotate_screen.sh; fi'. Name and path may need to be adapted. At MATE in pmOS, which uses the busybox "ps" command, replace the "-ge 1" by "-ge 4" because it counts also the 3x "auto-rotate" that the command itself contains. As an icon, you can choose e.g. "/usr/share/icons/Faenza/emblems/32/emblem-ubuntuone-updating.png" or "/usr/share/icons/Adwaita/symbolic/emblems/emblem-synchronizing-symbolic.svg".
#!/bin/sh

killall monitor-sensor
monitor-sensor > /dev/shm/sensor.log 2>&1 &

while inotifywait -e modify /dev/shm/sensor.log; do

  ORIENTATION=$(tail /dev/shm/sensor.log | grep 'orientation' | tail -1 | grep -oE '[^ ]+$')

  case "$ORIENTATION" in

    normal)
      xrandr -o normal
      xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
      ;;
    left-up)
      xrandr -o left
      xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
      ;;
    bottom-up)
      xrandr -o inverted
      xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
      ;;
    right-up)
      xrandr -o right
      xinput set-prop "Goodix Capacitive TouchScreen" "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
      ;;

  esac
done

Depending on the hardware, a delay of e.g. "sleep 0.3" might be helpful between the lines "xrandr" and "xinput".

surface autorotate2

Here is an implementation in python3: https://github.com/andrewrembrandt/surface-autorotate2. Packages "xrandr" and "xinput" need to be installed. Additionally, in the top area of the script, the parameters need to be adapted. "sensorname" can be found at cat /sys/bus/iio/devices/iio:deviceX/name where you have to replace deviceX by the right number for your accelerometer. "displayname" can be found in xrandr, the name before "connected primary". "inputnames" is the name of the touchscreen in "xinput --list". The "pennames" array can be emptied.

ScreenRotator

Another possibility is ScreenRotator: https://github.com/GuLinux/ScreenRotator. It needs to be built, as described in the README. It is based on Qt. For GTK-based Xfce4 and MATE that means that Qt libraries need to be installed.

Note: Compiling ScreenRotator on 32-bit systems might require the patch mentioned in ScreenRotator#29