-
-
Save tuxflo/5b400c86a5ebde871d94c6bff94ad6cb 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
# Forked from https://gist.github.com/mildmojo/48e9025070a2ba40795c | |
# Configured to use with a Lenovo Yoga 260 (names taken from `xinput` output). | |
# If the rotation position ($1) is ommited, the script toggles through the different states: inverted, left, right, normal | |
TOUCHPAD='ETPS/2 Elantech Touchpad' | |
TOUCHSCREEN='Wacom Co.,Ltd. Pen and multitouch sensor Finger touch' | |
TRANSFORM='Coordinate Transformation Matrix' | |
NORMAL="Coordinate Transformation Matrix (142): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000" | |
INVERTED="Coordinate Transformation Matrix (142): -1.000000, 0.000000, 1.000000, 0.000000, -1.000000, 1.000000, 0.000000, 0.000000, 1.000000" | |
LEFT="Coordinate Transformation Matrix (142): 0.000000, -1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000" | |
RIGHT="Coordinate Transformation Matrix (142): 0.000000, 1.000000, 0.000000, -1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000" | |
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 | |
} | |
if [ -z "$1" ]; then | |
echo "No orientation given, toggle mode" | |
CURRENT_MODE=`xinput --list-props "$TOUCHPAD" | grep "$TRANSFORM"` | |
XDISPLAY=`xrandr --current | grep primary | sed -e 's/ .*//g'` | |
echo "$CURRENT_MODE" | |
if [ "${CURRENT_MODE//[$' \t\n\r']/}" = "${NORMAL//[$' \t\n\r']/}" ]; then | |
echo "Current mode: normal" | |
echo "switching to: inverted" | |
do_rotate $XDISPLAY inverted | |
elif [ "${CURRENT_MODE//[$' \t\n\r']/}" == "${INVERTED//[$' \t\n\r']/}" ]; then | |
echo "Current mode: inverted" | |
echo "switching to: left" | |
do_rotate $XDISPLAY left | |
elif [ "${CURRENT_MODE//[$' \t\n\r']/}" == "${LEFT//[$' \t\n\r']/}" ]; then | |
echo "Current mode: left" | |
echo "switching to: right" | |
do_rotate $XDISPLAY right | |
elif [ "${CURRENT_MODE//[$' \t\n\r']/}" == "${RIGHT//[$' \t\n\r']/}" ]; then | |
echo "Current mode: right" | |
echo "switching to: normal" | |
do_rotate $XDISPLAY normal | |
fi | |
fi | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#!/usr/bin/bash
monitor-sensor | while read line; do
if echo $line | grep -E 'orientation changed:|Has accelerometer'; then
orientation=$(echo $line | sed -e 's/^.: //' -e 's/ .//' -e 's/[()]//g')
case $orientation in
normal)
xrandr --output eDP-1 --rotate normal
xinput set-prop "ELAN0732:00 04F3:0358" --type=float --format=32 "TransformationMatrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "ELAN0732:00 04F3:0358" "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
;;
bottom-up)
xrandr --output eDP-1 --rotate inverted
xinput set-prop "ELAN0732:00 04F3:0358" --type=float --format=32 "TransformationMatrix" -1 0 1 0 -1 1 0 0 1
xinput set-prop "ELAN0732:00 04F3:0358" "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
;;
right-up)
xrandr --output eDP-1 --rotate right
xinput set-prop "ELAN0732:00 04F3:0358" --type=float --format=32 "TransformationMatrix" 0 1 0 -1 0 1 0 0 1
xinput set-prop "ELAN0732:00 04F3:0358" "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
;;
left-up)
xrandr --output eDP-1 --rotate left
xinput set-prop "ELAN0732:00 04F3:0358" --type=float --format=32 "TransformationMatrix" 0 -1 1 1 0 0 0 0 1
xinput set-prop "ELAN0732:00 04F3:0358" "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
;;
esac
fi
done