Skip to content

Instantly share code, notes, and snippets.

@i5513
Forked from mildmojo/rotate_desktop.sh
Last active March 19, 2018 12:15
Show Gist options
  • Save i5513/e27c3f548fba1d34955e7552d1e39d3c to your computer and use it in GitHub Desktop.
Save i5513/e27c3f548fba1d34955e7552d1e39d3c 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='Synaptics TM3066-002'
TOUCHSCREEN='ELAN21EF:00 04F3:227C'
while [ "$ORIENTATION" != "normal" \
-a "$ORIENTATION" != "left" \
-a "$ORIENTATION" != "right" \
-a "$ORIENTATION" != "inverted" ]
do
ORIENTATION="$({ timeout 0.2 monitor-sensor ;} 2> /dev/null |
awk '/orientation/ {
gsub (")","");
gsub ("-up","");
gsub("bottom","inverted");
print $NF;
}' 2> /dev/null | tail -n 1 )"
if [ $SECONDS -gt 10 ]
then
xmessage "Not found Orientation. We will exit"
exit 1
fi
done
if [ -z "$1" ]; then
echo "Missing orientation."
echo "Usage: $0 [normal|inverted|left|right|auto] [revert_seconds]"
echo
exit 1
fi
function do_rotate
{
if [ "$2" = "auto" ]
then
set -- $1 $ORIENTATION
fi
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
@i5513
Copy link
Author

i5513 commented Mar 19, 2018

Add "auto" option. We could monit monitor-sensor and then rotate. I have not any laptop now for test this change

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