0

Maybe somebody has some optimizations to this code or sed expressions?

Just bourne shell, sed, xrandr and libinput rotation script:

% cat rotate.sh

#!/bin/sh

if [ "$#" -lt 1 ] ; then
    echo "Usage: $0 OUTPUT_SCREEN_TO_ROTATE" >&2
    exit 1
fi

tbltrot ()
{
if [ "$#" -lt 1 ] ; then
    echo "Usage: $0 POSITION TABLET_DEVICE1 [TABLET_DEVICE2...]" >&2
    exit 3
fi

case "${1}" in
    normal)
        PENORI=0
        ;;
    left)
        PENORI=2
        ;;
    inverted)
        PENORI=3
        ;;
    right)
        PENORI=1
        ;;
    *)
        echo "Some unexpected first argument of ${0}"
        exit 2
        ;;
esac

ROTOPROP="Wacom Rotation"

shift

for i in $@
do
  xinput set-prop "`xinput list --name-only | sed -n -e '/^[[:alpha:]].*'"${i}"'$/p'`" "${ROTOPROP}" ${PENORI}
done

}

# Setting next available screen rotatation

# comments are given line by line after'-o' option of 'xrandr'command
# just queried X output screens state
# just filtered for first script argument to be a string beginning
# just removed all stuff from line beginning to number with ')' parenthesis
# just removed all stuff from first word to same word and substituted it with next word
# if that word was 'x' than it is 'right' screen orientation, so the next should be 'normal'

SCRNORI=`xrandr -q --verbose | \
       sed -n -e '/^'"${1}"'.*/p' | \
       sed -e 's/^.*[[:xdigit:]])[[:space:]]*//' \
           -e 's/^\([[:lower:]]*\)[[:space:]].*\1[[:space:]]*\([[:lower:]]*\)[[:space:]].*$/\2/' \
           -e 's/^x$/normal/'`

xrandr -o "${SCRNORI}"

if [ "$#" -gt 1 ] ; then
    shift
    tbltrot "${SCRNORI}" ${@}
fi

I bound it to rotate button with xbindkeys (rotate.sh located in one of user's PATH dirs). "stylus" "eraser" and "touch" are substrings of device names from xinput:

% cat .xbindkeysrc

"rotate.sh LVDS1 stylus eraser touch"
    m:0x0 + c:204
    XF86Eject
  • 1
    Welcome to Unix.SE. You were asked yesterday to turn this into a question, and I see you’ve tried to do that. Unfortunately requests for improvement are off-topic here; a much better approach in this case, which would make your script more useful, would be to explain what problem you solved in your question (“How can I rotate the screen on a tablet?” or something to that effect), and give your script in your answer. – Stephen Kitt Apr 12 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.