Skip to content

Instantly share code, notes, and snippets.

@onjin
Created November 14, 2016 15:14
Show Gist options
  • Save onjin/7bedbe504870ba1dd4de7d56271faa26 to your computer and use it in GitHub Desktop.
Save onjin/7bedbe504870ba1dd4de7d56271faa26 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script rotates the screen and touchscreen input 90 degrees each time it is called,
# by Ruben Barkow: https://gist.github.com/rubo77/daa262e0229f6e398766
#### configuration
# find your Touchscreen device with `xinput`
TouchscreenDevice='ELAN Touchscreen'
screenMatrix=$(xinput --list-props "$TouchscreenDevice" | awk '/Coordinate Transformation Matrix/{print $5$6$7$8$9$10$11$12$NF}')
# Matrix for rotation
# ⎡ 1 0 0 ⎤
# ⎜ 0 1 0 ⎥
# ⎣ 0 0 1 ⎦
normal='1 0 0 0 1 0 0 0 1'
normal_float='1.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,1.000000'
#⎡ -1 0 1 ⎤
#⎜ 0 -1 1 ⎥
#⎣ 0 0 1 ⎦
inverted='-1 0 1 0 -1 1 0 0 1'
inverted_float='-1.000000,0.000000,1.000000,0.000000,-1.000000,1.000000,0.000000,0.000000,1.000000'
# 90° to the left
# ⎡ 0 -1 1 ⎤
# ⎜ 1 0 0 ⎥
# ⎣ 0 0 1 ⎦
left='0 -1 1 1 0 0 0 0 1'
left_float='0.000000,-1.000000,1.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000'
# 90° to the right
#⎡ 0 1 0 ⎤
#⎜ -1 0 1 ⎥
#⎣ 0 0 1 ⎦
right='0 1 0 -1 0 1 0 0 1'
right_float='0.000000,1.000000,0.000000,-1.000000,0.000000,1.000000,0.000000,0.000000,1.000000'
function set_normal() {
xrandr -o normal
xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $normal
killall notify-osd
notify-send "Screen normal"
}
function set_inversed() {
xrandr -o inverted
xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $inverted
killall notify-osd
notify-send "Screen inverted"
}
function set_left() {
xrandr -o left
xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $left
killall notify-osd
notify-send "Screen left"
}
function set_right() {
xrandr -o right
xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $right
killall notify-osd
notify-send "Screen right"
}
function do_twist() {
if [ $screenMatrix == $normal_float ]
then
set_left
fi
if [ $screenMatrix == $left_float ]
then
set_inversed
fi
if [ $screenMatrix == $inverted_float ]
then
set_right
fi
if [ $screenMatrix == $right_float ]
then
set_normal
fi
}
function do_upside_down_toggle() {
if [ $screenMatrix != $inverted_float ]
then
set_inversed
else
set_normal
fi
}
function do_horizontal_vertical_toggle() {
if [ $screenMatrix != $normal_float ]
then
set_normal
else
set_left
fi
}
function usage() {
echo 'Usage: rotate-screen.sh [OPTION]'
echo
echo 'This script rotates the screen and touchscreen input 90 degrees each time it is called,'
echo
echo Usage:
echo ' -h --help display this help'
echo ' -n | normal - always rotates the screen back to normal'
echo ' -i | inv | inverse - inverse horizontal'
echo ' -r | right - rotate screen right'
echo ' -l | left - rotate screen left'
}
case "$1" in
-i|inv|inverse)
do_upside_down_toggle
;;
-n|normal)
set_normal
;;
-l|left)
set_left
;;
-r|right)
set_right
;;
-v|vert|vertical)
do_horizontal_vertical_toggle
;;
-t|twist)
do_twist
;;
-h|--help|*)
usage
;;
esac
exit 0
if [ $screenMatrix == $normal_float ] && [ "$1" != "-n" ]
then
echo "Upside down"
elif [ $screenMatrix == $inverted_float ] && [ "$1" != "-j" ] && [ "$1" != "-n" ]
then
echo "90° to the left"
elif [ $screenMatrix == $left_float ] && [ "$1" != "-j" ] && [ "$1" != "-n" ]
then
echo "90° to the right"
else
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment