Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Script to rotate the screen and touch devices on modern Linux desktops. Great for convertible laptops.
#!/bin/bash
#
# rotate_desktop.sh
#
# Rotates modern Linux desktop screen and input devices to match. Handy for
# convertible notebooks. Call this script from panel launchers, keyboard
# shortcuts, or touch gesture bindings (xSwipe, touchegg, etc.).
#
# Using transformation matrix bits taken from:
# https://wiki.ubuntu.com/X/InputCoordinateTransformation
#
# Configure these to match your hardware (names taken from `xinput` output).
TOUCHPAD='SynPS/2 Synaptics TouchPad'
TOUCHSCREEN='Atmel Atmel maXTouch Digitizer'
if [ -z "$1" ]; then
echo "Missing orientation."
echo "Usage: $0 [normal|inverted|left|right] [revert_seconds]"
echo
exit 1
fi
function do_rotate
{
xrandr --output $1 --rotate $2
TRANSFORM='Coordinate Transformation Matrix'
case "$2" in
normal)
[ ! -z "$TOUCHPAD" ] && xinput set-prop "$TOUCHPAD" "$TRANSFORM" 1 0 0 0 1 0 0 0 1
[ ! -z "$TOUCHSCREEN" ] && xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 1 0 0 0 1 0 0 0 1
;;
inverted)
[ ! -z "$TOUCHPAD" ] && xinput set-prop "$TOUCHPAD" "$TRANSFORM" -1 0 1 0 -1 1 0 0 1
[ ! -z "$TOUCHSCREEN" ] && xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" -1 0 1 0 -1 1 0 0 1
;;
left)
[ ! -z "$TOUCHPAD" ] && xinput set-prop "$TOUCHPAD" "$TRANSFORM" 0 -1 1 1 0 0 0 0 1
[ ! -z "$TOUCHSCREEN" ] && xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 0 -1 1 1 0 0 0 0 1
;;
right)
[ ! -z "$TOUCHPAD" ] && xinput set-prop "$TOUCHPAD" "$TRANSFORM" 0 1 0 -1 0 1 0 0 1
[ ! -z "$TOUCHSCREEN" ] && xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 0 1 0 -1 0 1 0 0 1
;;
esac
}
XDISPLAY=`xrandr --current | grep primary | sed -e 's/ .*//g'`
XROT=`xrandr --current --verbose | grep primary | egrep -o ' (normal|left|inverted|right) '`
do_rotate $XDISPLAY $1
if [ ! -z "$2" ]; then
sleep $2
do_rotate $XDISPLAY $XROT
exit 0
fi
Owner

mildmojo commented Jun 18, 2014

Tested with the following versions:

$ xinput --version
xinput version 1.6.1
XI version on server: 2.3
$ xrandr --version
xrandr program version       1.4.1
Server reports RandR version 1.4

Thanks! This worked great with my HP Envy x360! I had to calibrate it first by running the command xinput_calibrator --output-type xorg.conf.d and following its instructions to get the pointer orientation correct.

mnpale commented May 19, 2015

Thanks, this helped me flip the screen on my XPS 12 in Linux Mint. I tied this to keyboard shortcuts.

maferv commented Dec 12, 2015

Beautiful script. God bless you.

Great script! Thanks. I've extended it to toggle between left and normal by reading the current orientation.

yzpaul commented Jan 10, 2016

This is phenomenal, thank you so much!

Apollia commented Jun 10, 2016

Thank you so much!!! Works great on my Toughbook CF-C1 in Lighthouse 64 Puppy Linux 6.02 Beta 2!

My forked and slightly modified version:

https://github.com/Apollia/Rotate-Toughbook-CF-C1-Screen-in-Lighthouse-64-Puppy-Linux-6.02-Beta-2/tree/master

Thanks!! This is so good!!
I use this script for watching youtube personal cam video that looks on stars.

Hmmm. But two touch or three touch events are yet normal mode.
Is this are impossible using xinput?

Owner

mildmojo commented Feb 15, 2017

I'm not sure about multitouch gestures; I think I use touchegg to manage them, and I use them so infrequently that I never noticed if they were correct after rotation. I've been pretty disappointed with multitouch gestures and touchscreen support in Linux, TBH.

rbnbdd commented Feb 18, 2017

I'm new to Linux/Ubuntu and have only tried it on my Yoga 2 11 per USB-Stick. So my question is if it somehow it possible to get the code in a GUI where you can just press 4 buttons and it will change the rotation of the Screen.

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