Skip to content

Instantly share code, notes, and snippets.

@coffeewasmyidea
Created June 17, 2022 08:33
Show Gist options
  • Save coffeewasmyidea/65673793135a5e875b0a6bb31ebc30fa to your computer and use it in GitHub Desktop.
Save coffeewasmyidea/65673793135a5e875b0a6bb31ebc30fa to your computer and use it in GitHub Desktop.
Fedora Linux 36, Cinnamon 5.2.7 (auto-rotate touch screen based on device orientation)
#!/bin/bash
#
# Auto-rotate touch screen based on device orientation.
#
# Fedora 36
# 5.17.14-300.fc36.x86_64
#
# Cinnamon 5.2.7
#
# You just need to add this script to the startup of applications for the user.
#
# Kill any existing monitor-sensor instance
killall monitor-sensor
# Launch monitor-sensor and store the output in a RAM based file that can be
# checked by the rest of the script. We use the RAM based file system to save
# wear where an SSD is being used.
monitor-sensor > /dev/shm/sensor.log 2>&1 &
# Initialise display orientation to 'normal'
# Without this, the display often starts in 'inverted' (or 'bottom-up') mode!
xrandr --output eDP-1 --rotate normal
xinput set-prop "SYNA8008:00 06CB:CE58 Mouse" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "SYNA8008:00 06CB:CE58 Touchpad" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Pen stylus" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Finger touch" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Pen eraser" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
# Parse output of 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 /dev/shm/sensor.log; do
# Read the last few lines that were added to the file and get the last orientation line.
ORIENTATION=$(tail /dev/shm/sensor.log | grep 'orientation' | tail -1 | grep -oE '[^ ]+$')
# Set the actions to be taken for each possible orientation
case "$ORIENTATION" in
bottom-up)
xrandr --output eDP-1 --rotate inverted
xinput set-prop "SYNA8008:00 06CB:CE58 Mouse" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
xinput set-prop "SYNA8008:00 06CB:CE58 Touchpad" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
xinput set-prop "Wacom HID 527E Pen stylus" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
xinput set-prop "Wacom HID 527E Finger touch" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
xinput set-prop "Wacom HID 527E Pen eraser" --type=float "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1;;
normal)
xrandr --output eDP-1 --rotate normal
xinput set-prop "SYNA8008:00 06CB:CE58 Mouse" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "SYNA8008:00 06CB:CE58 Touchpad" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Pen stylus" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Finger touch" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
xinput set-prop "Wacom HID 527E Pen eraser" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1;;
right-up)
xrandr --output eDP-1 --rotate right
xinput set-prop "SYNA8008:00 06CB:CE58 Mouse" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
xinput set-prop "SYNA8008:00 06CB:CE58 Touchpad" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
xinput set-prop "Wacom HID 527E Pen stylus" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
xinput set-prop "Wacom HID 527E Finger touch" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
xinput set-prop "Wacom HID 527E Pen eraser" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1;;
left-up)
xrandr --output eDP-1 --rotate left
xinput set-prop "SYNA8008:00 06CB:CE58 Mouse" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
xinput set-prop "SYNA8008:00 06CB:CE58 Touchpad" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
xinput set-prop "Wacom HID 527E Pen stylus" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
xinput set-prop "Wacom HID 527E Finger touch" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
xinput set-prop "Wacom HID 527E Pen eraser" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment