You are not logged in.

#1 2018-12-29 11:19:05

Registered: 2006-11-04
Posts: 125

Quick autorotate bash script using iio-sensor-proxy

Here's a fairly simple bash script to autorotate screen and touch/pen inputs automatically on devices with an accelerometer.  It depends on the iio-sensor-proxy package, for the monitor-sensor tool.

There are various others floating around, but I had problems getting any I found to work correctly.

It might be useful to some others - just insert your screen and input devices in the variables at the top.

(I dropped auto-detecting the input devices, because I found many scripts assumed any device with 'Wacom' in the name used the wacom driver, but on my system the touchscreen should use xinput but still has 'wacom' in its name, so was getting mis-identified. Could find a better match by name, but its simpler just to insert the full device names...).


XINPUT=("Wacom Pen and multitouch sensor Finger")
WACOM=("Wacom Pen and multitouch sensor Pen stylus"
        "Wacom Pen and multitouch sensor Pen eraser")

#uses the values output by monitor-sensor to call the rotate function
function rotate_ms {
    case $1 in
            rotate 0
            rotate 1
            rotate 2
            rotate 3

function rotate {

    O_NAMES=("normal" "right" "inverted" "left")
    COORDS=("1, 0, 0, 0, 1, 0, 0, 0, 1" "0, 1, 0, -1, 0, 1, 0, 0, 1" "-1, 0, 1, 0, -1, 1, 0, 0, 1" "0, -1, 1, 1, 0, 0, 0, 0, 1")
    W_NAMES=("none" "cw" "half" "ccw")



    echo "Rotating to" $TARGET_ORIENTATION

    #Rotate the screen
    xrandr --output $SCREEN --rotate $TARGET_ORIENTATION

    #Rotate libinput driver input devices
    for i in "${XINPUT[@]}" 
        xinput --set-prop "$i" "Coordinate Transformation Matrix" ${COORDS[$O_NO]}

    #Rotate Wacom driver input devices
    for i in "${WACOM[@]}"
        xsetwacom set "$i" rotate ${W_NAMES[$O_NO]}


while IFS='$\n' read -r line; do
    rotation="$(echo $line | sed -En "s/^.*orientation changed: (.*)/\1/p")"
    [[ !  -z  $rotation  ]] && rotate_ms $rotation
done < <(stdbuf -oL monitor-sensor)


Last edited by fishonadish (2018-12-29 11:40:14)


Board footer

Powered by FluxBB