summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rwxr-xr-xautorotate.sh66
2 files changed, 69 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8ca9562
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+This is a simple shell script that monitors the Linux IIO acceleromter for
+device rotation, then rotates the screen and touch inputs accordingly. It was
+developed and tested on a ThinkPad Yoga 12 (2nd generation).
diff --git a/autorotate.sh b/autorotate.sh
new file mode 100755
index 0000000..bf69b73
--- /dev/null
+++ b/autorotate.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+### TODO: These should be automatically identified
+touchpad='PS/2 Synaptics TouchPad'
+touchdev='SYNAPTICS Synaptics Touch Digitizer V04'
+
+# Global variables
+CURRENT='normal'
+GRAV=$(echo 7.0 / $(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_scale) | bc)
+
+rotatetouch() {
+ # For some reason, xrandr causes the touch device to disappear briefly.
+ # This loop waits for it to come back.
+ while ! xinput --list | grep -q "$touchdev"; do :; done
+ xinput set-float-prop "$touchdev" 'Coordinate Transformation Matrix' $*
+}
+
+rotatewacom() {
+ ### FIXME: the byte range (38-40) will probably break at some point
+ for dev in $(xsetwacom --list devices | cut -b 38-40); do
+ xsetwacom set "$dev" rotate $1
+ done
+}
+
+check_orientation() {
+ XRAW=$(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_x_raw)
+ YRAW=$(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_y_raw)
+
+ if [ $CURRENT != 'normal' ] && [ $YRAW -le -$GRAV ]; then
+ CURRENT='normal'
+ xrandr -o normal
+ rotatetouch 1 0 0 0 1 0 0 0 1
+ rotatewacom none
+ ### FIXME: There should be a separate script to enable/disable touchpad on folding events
+ xinput enable "$touchpad"
+ fi
+
+ if [ $CURRENT != 'inverse' ] && [ $YRAW -ge $GRAV ]; then
+ CURRENT='inverse'
+ xrandr -o inverted
+ rotatetouch -1 0 1 0 -1 1 0 0 1
+ rotatewacom half
+ xinput disable "$touchpad"
+ fi
+
+ if [ $CURRENT != 'left' ] && [ $XRAW -ge $GRAV ]; then
+ CURRENT='left'
+ xrandr -o left
+ rotatetouch 0 -1 1 1 0 0 0 0 1
+ rotatewacom ccw
+ xinput disable "$touchpad"
+ fi
+
+ if [ $CURRENT != 'right' ] && [ $XRAW -le -$GRAV ]; then
+ CURRENT='right'
+ xrandr -o right
+ rotatetouch 0 1 0 -1 0 1 0 0 1
+ rotatewacom cw
+ xinput disable "$touchpad"
+ fi
+}
+
+while true; do
+ check_orientation
+ sleep 1
+done