summaryrefslogtreecommitdiff
path: root/dot/local/bin/thinkpad-rotate
blob: db44030dd2a8d284e2adab00c0e0e20d44686a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# This is a script that toggles rotation of the screen through xrandr,
# and also toggles rotation of the stylus, eraser and cursor through xsetwacom

query_screen()
{
    xrandr --verbose -q | while read screen conn p _ _ orientation _; do
        [ "$conn" = connected -a "$p" = primary ] || continue
        echo "$screen $orientation"
        break
    done
}

screen_properties=$(query_screen)
orientation=${screen_properties#* }
screen_name=${screen_properties% *}

xrandr_to_xsetwacom()
{
    case "$1" in
        normal)   echo none ;;
        right)    echo cw ;;
        inverted) echo half ;;
        left)     echo ccw ;;
    esac
}

set_rotation()
{
    local x="$(xrandr_to_xsetwacom "$1")"
	  xrandr --output "${screen_name}" --rotate "$1"
	  xsetwacom set 14 Rotate "$x"
	  xsetwacom set 15 Rotate "$x"
	  xsetwacom set 16 Rotate "$x"
}

flip()
{
    case "$orientation" in
        normal)   set_rotation inverted ;;
        right)    set_rotation left ;;
        inverted) set_rotation normal ;;
        left)     set_rotation right ;;
    esac
}

rotate_right()
{
    case "$orientation" in
        normal)   set_rotation right ;;
        right)    set_rotation inverted ;;
        inverted) set_rotation left ;;
        left)     set_rotation normal ;;
    esac
}

if [ $# = 0 ]; then
    flip
    exit
elif [ "$1" = rotate ]; then
    rotate_right
elif [ "$(xrandr_to_xsetwacom "$1")" ]; then
    set_rotation "$1"
else
    exec >&2
    echo "Usage: $0 [normal|right|inverted|left]"
    echo
    echo "  With arguments, set the screen orientation."
    echo "  Without arguments, flip the screen orientation 180 degrees."
fi