You are not logged in.

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

fishonadish
Member
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...).

https://gitlab.com/snippets/1793649

#!/bin/bash

SCREEN="eDP-1"
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
        "normal")
            rotate 0
            ;;
        "right-up")
            rotate 1
            ;;
        "bottom-up")
            rotate 2
            ;;
        "left-up")
            rotate 3
            ;;
    esac
}

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")

    O_NO=$1

    TARGET_ORIENTATION=${O_NAMES[$O_NO]}

    echo "Rotating to" $TARGET_ORIENTATION

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

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

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

}

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)

Thanks,
Fishonadish

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

Offline

Board footer

Powered by FluxBB