Skip to content

Instantly share code, notes, and snippets.

@emiguelt
Created February 25, 2019 18:30
Show Gist options
  • Save emiguelt/9d37c796d8cfc820e03b3fcaa53da309 to your computer and use it in GitHub Desktop.
Save emiguelt/9d37c796d8cfc820e03b3fcaa53da309 to your computer and use it in GitHub Desktop.
Asus T110HA display autorotation in Linux (*ubuntu)
!/bin/sh
# Auto rotate touch screen based on device orientation.
#
# Based on chadm's script at https://linuxappfinder.com/blog/auto_screen_rotation_in_ubuntu.
# Receives input from monitor-sensor (part of iio-sensor-proxy package) and sets the touchscreen
# orientation based on the accellerometer positionn. We assume that the display rotation is
# handled by Linux Mint 18.1, Cinnamon 3.2.7. If this is not the case, add the appropriate
# xrandr command into each case block.
# This script should be added to startup applications for the user.
# Kill any existing monitor-sensor instance, for example if manually invoking
# from a terminal for testing.
killall monitor-sensor
# Launch monitor-sensor and store the output in a RAM based file that can be checked by the rest of the script.
# We use the RAM based file system to save wear where an SSD is being used.
monitor-sensor > /dev/shm/sensor.log 2>&1 &
DSPID="DSI1"
TOUCHID=14
# Initialise display orientation to 'normal'
# Without this, the display often starts in 'inverted' (or 'bottom-up') mode!
xrandr --output $DSPID --brightness 0.6
xinput set-prop $TOUCHID 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1
xrandr --output $DSPID --rotate right
# Parse output of monitor sensor to get the new orientation whenever the log file is updated
# Possibles are: normal, bottom-up, right-up, left-up
# Light data will be ignored
while inotifywait -e modify /dev/shm/sensor.log; do
# Read the last few lines that were added to the file and get the last orientation line.
ORIENTATION=$(tail /dev/shm/sensor.log | grep 'orientation' | tail -1 | grep -oE '[^ ]+$')
# Set the actions to be taken for each possible orientation
case "$ORIENTATION" in
bottom-up)
xinput set-prop $TOUCHID 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1
xrandr --output $DSPID --rotate inverted
;;
normal)
xinput set-prop $TOUCHID 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
xrandr --output $DSPID --rotate normal
;;
right-up)
xinput set-prop $TOUCHID 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1
xrandr --output $DSPID --rotate right
;;
left-up)
xinput set-prop $TOUCHID 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1
xrandr --output $DSPID --rotate left
;;
esac
done
# On stopping this script, don't forget that "monitor-sensor" is still running - hence the "killall" !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment