Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Created June 18, 2014 06:47
Show Gist options
  • Save mildmojo/48e9025070a2ba40795c to your computer and use it in GitHub Desktop.
Save mildmojo/48e9025070a2ba40795c to your computer and use it in GitHub Desktop.
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
@marcomarinho
Copy link

Check this script. Basically automatically rotates the screen based on the orientation
https://github.com/marcomarinho/auto-rotate-tablets-linux/tree/main

@sphh
Copy link

sphh commented Nov 30, 2021

@marcomarinho: It looks, like your script uses

monitor-sensor >> sensor.log 2>&1 &

while inotifywait -e modify sensor.log; do
    [...]
done

If I understand that correctly, monitor-sensor writes the output to a file on the (hard-)disk and acts, whenever this file changes. When I start monitor-sensor, it writes a line roughly ever 0.5s. If you do not restart your script, the sensor.log file gets longer and longer using up more and more hard-disk space. That would trouble me, if you only send the laptop to sleep and do not restart it.

I can see another problem: SSDs do not like these regular writing and could wear out quickly.

Wouldn't it be better to write a script, which monitors the DBus (as I believe monitor-sensor does) and act on changes of the orientation? Actually I shortly thought about adding this approach to my script, but did not do it in the end, because I personally do not like automatic screen orientation (it always flips the screen when you do not want it to do). What do you think?

@mildmojo
Copy link
Author

As an aside, the other day my 8-year-old Yoga 11s started auto-rotating the screen when I physically change the laptop's orientation. Maybe I updated a system package somewhere and it suddenly gained support for my hardware? Wild.

Now that it's finally supported, I'd kind of like to turn it off. 😝 I now prefer setting the rotation manually with global hotkeys set to call the script in this gist. I can see how it would be useful in tablet mode, though.

@mw-cyrano
Copy link

mw-cyrano commented Jan 28, 2022

Thank you so much! This works flawlessly on my XPS 13 2-in-1, even if external monitor connected, which remains unaffected. Touch events on the screen, external mouse and digitizer work on spot. Finally video conferences with a digital and streamable paper at hand.

@clavisound
Copy link

Thank you for the script.

On yoga 11s with my custom kernel-5.15.31 on Slackware-15 I changed only one line

TOUCHSCREEN='Atmel Atmel maXTouch Digitizer'
vs
TOUCHSCREEN='Atmel Atmel maXTouch Digitizer Touchscreen'

@undg
Copy link

undg commented Oct 24, 2023

I got similar problem, I write this simple utility app that reads sensors and rotate screen + digitizer.

https://github.com/undg/autorotate

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