Skip to content

Instantly share code, notes, and snippets.

@Links2004
Created February 5, 2017 10:37
Show Gist options
  • Save Links2004/5976ce97a14dabf773c3ff98d03c0f61 to your computer and use it in GitHub Desktop.
Save Links2004/5976ce97a14dabf773c3ff98d03c0f61 to your computer and use it in GitHub Desktop.
Linux auto rotate screen and adjust touch matrix based on accelerometer data
#!/bin/bash
# Auto rotate screen based on device orientation
# based on https://linuxappfinder.com/blog/auto_screen_rotation_in_ubuntu
# install
# 1. apt-get install iio-sensor-proxy inotify-tools
# 2. add script to autostart
# Receives input from monitor-sensor (part of iio-sensor-proxy package)
# Screen orientation and launcher location is set based upon accelerometer position
# Launcher will be on the left in a landscape orientation and on the bottom in a portrait orientation
# This script should be added to startup applications for the user
LOG=/run/user/$(id -u $USER)/sensor.log
export DISPLAY=:0
# put your display name here
DNAME=DSI-1
# may change grep to match your touchscreen
INDEV=$(xinput --list | grep TouchScreen | sed 's/.*id=\([0-9]*\).*/\1/')
function rotate {
#echo ---- rotete ----
ORIENTATION=$1
CUR_ROT=$(xrandr -q --verbose | grep $DNAME | cut -d" " -f6)
NEW_ROT="normal"
CTM="1 0 0 0 1 0 0 0 1"
# Set the actions to be taken for each possible orientation
case "$ORIENTATION" in
normal)
NEW_ROT="normal"
CTM="1 0 0 0 1 0 0 0 1"
gsettings set com.canonical.Unity.Launcher launcher-position Top
;;
bottom-up)
NEW_ROT="inverted"
CTM="-1 0 1 0 -1 1 0 0 1"
gsettings set com.canonical.Unity.Launcher launcher-position Top
;;
right-up)
NEW_ROT="left"
CTM="0 -1 1 1 0 0 0 0 1"
gsettings set com.canonical.Unity.Launcher launcher-position Left
;;
left-up)
NEW_ROT="right"
CTM="0 1 0 -1 0 1 0 0 1"
gsettings set com.canonical.Unity.Launcher launcher-position Left
;;
esac
#echo ORIENTATION: $ORIENTATION
#echo INDEV: $INDEV
#echo DNAME: $DNAME
#echo DISPLAY: $DISPLAY
##echo NEW_ROT: $NEW_ROT
#echo CUR_ROT: $CUR_ROT
#echo CTM: $CTM
#if [ "$NEW_ROT" != "$CUR_ROT" ] ; then
xrandr --output $DNAME --rotate $NEW_ROT
xinput set-prop $INDEV 'Coordinate Transformation Matrix' $CTM
# fi
}
# set default orientation
rotate left-up
# kill old monitor-sensor
killall monitor-sensor
# Clear sensor.log so it doesn't get too long over time
> $LOG
# Launch monitor-sensor and store the output in a variable that can be parsed by the rest of the script
monitor-sensor >> $LOG 2>&1 &
# Parse output or 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 $LOG; do
# Read the last line that was added to the file and get the orientation
ORIENTATION=$(tail -n 1 $LOG | grep 'orientation' | grep -oE '[^ ]+$')
if [ ! -z $ORIENTATION ] ; then
rotate $ORIENTATION
fi
done
@Matoran
Copy link

Matoran commented Mar 15, 2019

Hey thanks for your code, it's very useful for my x1 yoga gen 3.
For those with same laptop you need to swap left and right.

You could update the code like this:
monitor-sensor | grep --line-buffered "orientation" >> $LOG 2>&1 &
inotifywait will then only be triggered by orientation.

@Hanselljlh
Copy link

Can someone help me with how to make this work? I am brand new to
Linux Deepin. I think all I am missing is the proper display name. When I type in the command to display the monitor info I think it is saying screen 1... And I feel that is wrong. Have a Yoga 2 11.

@markus-96
Copy link

markus-96 commented Feb 27, 2021

xrandr --prop | grep connected
this will give you the name of your Display. In my case it was eDP-1 and the output looked like this:

eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
HDMI-1 disconnected (normal left inverted right x axis y axis)
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-2 disconnected (normal left inverted right x axis y axis)

Since my Touchscreen somehow appears two times in xinput list, the grep did not work properly in line 21. So I wrote it down in line 66 like this:
xinput set-prop "pointer:ELAN Touchscreen" --type=float 'Coordinate Transformation Matrix' $CTM
Works fine for me..
I also had to duplicate the line 66 because of the pen I use that acts like a second touchscreen, looks like this:
xinput set-prop "pointer:ELAN Touchscreen Pen (0)" --type=float 'Coordinate Transformation Matrix' $CTM

I also had to swap left and right, but be careful, you may not change the value NEW_ROT, you have to change the condition. Otherwise, the CTM will be wrong and the Touchscreen doesn't work properly. And I commented out the lines with the Unity.Launcher since I am using Ubuntu Mate. Like this:

	right-up)      #  change this to "left-up)"
		NEW_ROT="left"       #  do not touch this!
		CTM="0 -1 1 1 0 0 0 0 1"
		#gsettings set com.canonical.Unity.Launcher launcher-position Left    #commented out..
		;;
	left-up)      #  change this to "right-up)"
		NEW_ROT="right"       #  do not touch this!
		CTM="0 1 0 -1 0 1 0 0 1"
		#gsettings set com.canonical.Unity.Launcher launcher-position Left    #commented out..
		;;

Took like an hour for me to get it working, maybe it will help anyone...

@markus-96
Copy link

If you have multiple displays attached, the CTM has to be modified...

@NojaneKashaniSaffar
Copy link

If you don't know your display's name do this:
xrandr --listmonitors

You will get something like this:

Monitors: 1
 0: +*eDP-1 2560/366x1600/229+0+0  eDP-1

We just list all the monitors connected to our device

as you can see, my display's name is "eDP-1"

Now for people who don't know the TouchScreen name, you can do that:

sudo stdbuf -oL libinput debug-events

use your touchscreen, and you will see which event it is, and once you've got the name of this event update the code (Line 21)

Mine for example:

INDEV=$(xinput --list | grep "CUST0000:00 04F3:2A12" | sed 's/.*id=\([0-9]*\).*/\1/')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment