Create a gist now

Instantly share code, notes, and snippets.

Embed
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
@mildmojo

This comment has been minimized.

Show comment
Hide comment
@mildmojo

mildmojo 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
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
@JosephLeedy

This comment has been minimized.

Show comment
Hide comment
@JosephLeedy

JosephLeedy Sep 6, 2014

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.

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

This comment has been minimized.

Show comment
Hide comment
@mnpale

mnpale May 19, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@maferv

maferv Dec 12, 2015

Beautiful script. God bless you.

maferv commented Dec 12, 2015

Beautiful script. God bless you.

@KoljaWindeler

This comment has been minimized.

Show comment
Hide comment
@KoljaWindeler

KoljaWindeler Dec 26, 2015

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

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

@yzpaul

This comment has been minimized.

Show comment
Hide comment
@yzpaul

yzpaul Jan 10, 2016

This is phenomenal, thank you so much!

yzpaul commented Jan 10, 2016

This is phenomenal, thank you so much!

@Apollia

This comment has been minimized.

Show comment
Hide comment
@Apollia

Apollia 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

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

@infinitekh

This comment has been minimized.

Show comment
Hide comment
@infinitekh

infinitekh Sep 24, 2016

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?

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?

@mildmojo

This comment has been minimized.

Show comment
Hide comment
@mildmojo

mildmojo 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.

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

This comment has been minimized.

Show comment
Hide comment
@rbnbdd

rbnbdd 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.

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.

@aiya000

This comment has been minimized.

Show comment
Hide comment
@aiya000

aiya000 Jul 23, 2017

Great !!
Thank you !

aiya000 commented Jul 23, 2017

Great !!
Thank you !

@debdeep777

This comment has been minimized.

Show comment
Hide comment
@debdeep777

debdeep777 Sep 23, 2017

Works perfectly except for the scrolling part. Both edge scrolling and two finger scrolling remain unchanged after the coordinate transform. Any idea how to fix this?
EDIT: If the stylus, touchscreen and eraser is a Wacom device, the following works instead of the coordinate transform:
xsetwacom set "$TOUCHSCREEN" Rotate ccw

The options for Rotate can be one of ccw/cw/half/none from this link.

debdeep777 commented Sep 23, 2017

Works perfectly except for the scrolling part. Both edge scrolling and two finger scrolling remain unchanged after the coordinate transform. Any idea how to fix this?
EDIT: If the stylus, touchscreen and eraser is a Wacom device, the following works instead of the coordinate transform:
xsetwacom set "$TOUCHSCREEN" Rotate ccw

The options for Rotate can be one of ccw/cw/half/none from this link.

@mildmojo

This comment has been minimized.

Show comment
Hide comment
@mildmojo

mildmojo Sep 28, 2017

@rbnbdd A GUI is farther than I want to go with this, but a GUI could easily call this script.

You could add quick launcher icons to your panel or desktop that call this script with the proper arguments for the rotations. Just add a new shortcut the way you regularly would, and tell it to run this script like e.g. /Users/<username>/rotate_desktop.sh inverted.

Owner

mildmojo commented Sep 28, 2017

@rbnbdd A GUI is farther than I want to go with this, but a GUI could easily call this script.

You could add quick launcher icons to your panel or desktop that call this script with the proper arguments for the rotations. Just add a new shortcut the way you regularly would, and tell it to run this script like e.g. /Users/<username>/rotate_desktop.sh inverted.

@tuxflo

This comment has been minimized.

Show comment
Hide comment
@tuxflo

tuxflo Dec 19, 2017

@rbnbdd It should be not too hard if you're using Zenity or something like that.

@mildmojo do you know an approach to remove the second parameter and do just a "toggle" through the different modes?

tuxflo commented Dec 19, 2017

@rbnbdd It should be not too hard if you're using Zenity or something like that.

@mildmojo do you know an approach to remove the second parameter and do just a "toggle" through the different modes?

@yuletide

This comment has been minimized.

Show comment
Hide comment
@yuletide

yuletide Jan 5, 2018

Haven't tried this exact script yet but another with similar approach here breaks the keyboard cover and touchpad (only when rotated!) on a Surface 2017 with running Ubuntu 16.04. Any ideas how to fix?

yuletide commented Jan 5, 2018

Haven't tried this exact script yet but another with similar approach here breaks the keyboard cover and touchpad (only when rotated!) on a Surface 2017 with running Ubuntu 16.04. Any ideas how to fix?

@mildmojo

This comment has been minimized.

Show comment
Hide comment
@mildmojo

mildmojo Feb 2, 2018

@tuxflo The XROT line shows how to get the current mode; you'd want to get that and then look through a list of the modes to find the next one to switch to.

@yuletide Sorry, I haven't tried this on a Surface. My test box was a Lenovo Yoga.

Owner

mildmojo commented Feb 2, 2018

@tuxflo The XROT line shows how to get the current mode; you'd want to get that and then look through a list of the modes to find the next one to switch to.

@yuletide Sorry, I haven't tried this on a Surface. My test box was a Lenovo Yoga.

@Aaahh

This comment has been minimized.

Show comment
Hide comment
@Aaahh

Aaahh Mar 2, 2018

@rbnbdd
if your own ubuntu or debian, search for shortcuts in keyboard settings

Aaahh commented Mar 2, 2018

@rbnbdd
if your own ubuntu or debian, search for shortcuts in keyboard settings

@zx6802

This comment has been minimized.

Show comment
Hide comment
@zx6802

zx6802 May 1, 2018

@ rbnbdd 18 Feb 2017
On Lubuntu LXHotkey provides a graphical interface for binding programs to keys.

zx6802 commented May 1, 2018

@ rbnbdd 18 Feb 2017
On Lubuntu LXHotkey provides a graphical interface for binding programs to keys.

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