nsbin/rotate-by-sensor.sh

45 lines
1.2 KiB
Bash
Raw Normal View History

#!/bin/sh
# 2022-02-22, Nico Schottelius
# Inspired by https://gist.github.com/Links2004/5976ce97a14dabf773c3ff98d03c0f61
output=$(xrandr | awk '/connected primary/ { print $1 }')
input=$(xinput --list | grep -e "Pen Pen" | sed 's/.*id=\([0-9]*\).*/\1/')
monitor-sensor | (
while true; do
read line
ORIENTATION=$(echo $line | awk -F: '/Accelerometer orientation changed:/ { print $2 } ' | sed 's/ *//')
# Check if we are matching on a correct line, if not, skip
if [ -z "$ORIENTATION" ]; then
continue
fi
echo $ORIENTATION
case "$ORIENTATION" in
normal)
rotate="normal"
matrix="1 0 0 0 1 0 0 0 1"
;;
bottom-up)
rotate="inverted"
matrix="-1 0 1 0 -1 1 0 0 1"
;;
right-up)
rotate="right"
matrix="0 1 0 -1 0 1 0 0 1"
;;
left-up)
rotate="left"
matrix="0 -1 1 1 0 0 0 0 1"
;;
esac
# set -x
xrandr --output ${output} --rotate ${rotate}
xinput set-prop ${input} 'Coordinate Transformation Matrix' ${matrix}
# set +x
done
)