-
-
Save ACamposPT/6794aa02a6e5e341f123d447b3645b93 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 | |
# Auto rotate screen based on device orientation | |
# Receives input from monitor-sensor (part of iio-sensor-proxy package) | |
# Screen orientation is set based upon accelerometer position | |
# This script should be added to startup applications for the user | |
# 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 | |
# | |
# Based on: | |
# https://linuxappfinder.com/blog/auto_screen_rotation_in_ubuntu | |
# https://gist.github.com/mildmojo/48e9025070a2ba40795c | |
# Requirements: | |
# sudo apt update | |
# sudo apt remove -y xserver-xorg-input-libinput | |
# sudo apt install -y xserver-xorg-input-evdev iio-sensor-proxy inotify-tools evtest xserver-xorg-input-synaptics | |
# copy input-device-info.sh and auto_screen_rotation.sh to the same folder | |
# run ./auto_screen_rotation.sh | |
# Check if this script is running | |
result=`ps aux | grep -i "auto_screen_rotation.sh" | grep -v "grep" | wc -l` | |
if [ $result -gt 2 ] | |
then | |
echo "Script is already running" | |
exit 0 | |
else | |
echo "Starting Script" | |
fi | |
# Configure these to match your hardware (names taken from `xinput` output). | |
TOUCHPAD='SynPS/2 Synaptics TouchPad' | |
TOUCHSCREEN='HID TOUCH HID Touch Panel Touchscreen' | |
function do_rotate | |
{ | |
# Detecting multi sreens | |
screensCount=$(xrandr | grep -sw "connected" | wc -l) | |
if [ $screensCount -gt 1 ]; then | |
echo "Multi Screens Detected. Ignoring" | |
#echo "Multi Screens Detected. Ignoring" >> /tmp/acceldata.log 2>&1 & | |
return | |
fi | |
#echo "Rotate : $1" >> /tmp/acceldata.log 2>&1 & | |
xrandr --current --verbose -o $1 | |
TRANSFORM='Coordinate Transformation Matrix' | |
case "$1" 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 | |
} | |
# Clear sensor.log and acceldata.log so it doesn't get too long over time | |
> /tmp/sensor.log | |
#> /tmp/acceldata.log | |
# Launch monitor-sensor and store the output in a variable that can be parsed by the rest of the script | |
monitor-sensor >> /tmp/sensor.log 2>&1 & | |
# Get accelerometer input device info | |
#source input-device-info.sh | |
#MY_DEVICE_PATH=$(inputDeviceEventHandlerPath "cmpc_accel_v4") | |
#echo 'MY_DEVICE_PATH:' ${MY_DEVICE_PATH} | |
# Read accelerometer data stream | |
#cat ${MY_DEVICE_PATH} >> /tmp/acceldata.log 2>&1 & | |
# Parse output or monitor sensor to get the new orientation whenever the log file is updated | |
# Possibles are: normal, bottom-up, right-up, left-up | |
# Light data will be ignored | |
while inotifywait -e modify /tmp/sensor.log; do | |
# Read the last line that was added to the file and get the orientation | |
ORIENTATION=$(tail -n 1 /tmp/sensor.log | grep 'orientation' | grep -oE '[^ ]+$') | |
# Set the actions to be taken for each possible orientation | |
case "$ORIENTATION" in | |
normal) | |
do_rotate normal ;; | |
bottom-up) | |
do_rotate inverted ;; | |
right-up) | |
do_rotate right ;; | |
left-up) | |
do_rotate left ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should referer this project in your project readme.