summarylogtreecommitdiffstats
path: root/xwacomcalibrate
blob: bd51369fe3825c112dcb4255dfeec0058d09c1bb (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash

print_help () {
   printf "\nUsage: xwacomcalibrate [-h] [-d] [-f] [-r (cw | half | ccw)]\n\n"
   printf " h: prints this help\n"
   printf " d: Uses xdotool to continously run the script"
   printf " f: Uses whole X screen as window size\n"
   printf " r: Chooses the device rotation. When given, needs either \"cw\" for clockwise, \"half\" for overhead, or \"ccw\" for counterclockwise as an argument\n"
   printf "\n"
}

while getopts "hdfr:" opt
do
    case $opt in
        h )
            print_help
            ;;
        d )
            DAEMON=true
            ;;
        f ) FULLSCREEN=true
            ;;
        r ) if [[\
                 $OPTARG == "cw"\
                      || $OPTARG == "half"\
                      || $OPTARG == "ccw"\
                ]]
            then
                ROTATION=$OPTARG
            else
                echo "Invalid rotation: $arg" 1>&2
                exit 1
            fi
            ;;
        : )
            echo "Invalid Option: $OPTARG requires an argument" 1>&2
            exit 1 ;;
        * ) echo "Unknown Option"
            print_help
            exit 1
            ;;
    esac
done
shift $(( OPTIND -1 ))

get_dev () {
    DEV=$(\
          xsetwacom --list devices\
              | grep "STYLUS"\
              | cut -d$'\t' -f 2\
              | tr -cd [:digit:]
       )
    if [[ ! $DEV ]]
    then
        echo "no device found"
        exit 1
    fi
}

full_tablet_size () {
    xsetwacom --set $1 ResetArea
    TB_X=$( xsetwacom --get $1 Area | cut -d ' ' -f 1)
    TB_Y=$( xsetwacom --get $1 Area | cut -d ' ' -f 2)
    TB_WIDTH=$( xsetwacom --get $1 Area | cut -d ' ' -f 3)
    TB_HEIGHT=$( xsetwacom --get $1 Area | cut -d ' ' -f 4)
}

set_rotation () {
    if [[ $2 ]]
    then
        xsetwacom --set $1 Rotate $2
    else
        xsetwacom --set $1 Rotate none
    fi
}

set_output () {
    xsetwacom set $1 MapToOutput "${2}x${3}+${4}+${5}"
}

set_area () {
    xsetwacom set $1 area "$2" "$3"\
              $( echo "$4 + $2" | bc )\
              $( echo "$5 + $3" | bc )
}

get_x_screen_size () {
    W_X="0"
    W_Y="0"
    W_WIDTH=$( xwininfo -root | grep Width | tr -dc [:digit:])
    W_HEIGHT=$( xwininfo -root | grep Height | tr -dc [:digit:])
}

get_active_window_size () {
    W_X=$(\
          xdotool getactivewindow getwindowgeometry --shell\
              | grep X\
              | cut -d "=" -f2\
       )
    W_Y=$(\
          xdotool getactivewindow getwindowgeometry --shell\
              | grep Y\
              | cut -d "=" -f2\
       )
    W_WIDTH=$(\
              xdotool getactivewindow getwindowgeometry --shell\
                  | grep WIDTH\
                  | cut -d "=" -f2\
           )
    W_HEIGHT=$(\
               xdotool getactivewindow getwindowgeometry --shell\
                   | grep HEIGHT\
                   | cut -d "=" -f2\
            )
}

calculate_new_tablet_size () {

    if [[ $1 == "cw" || $1 == "ccw" ]]
    then
        twidth=$3
        theight=$2
    else
        twidth=$2
        theight=$3
    fi

    tar=$( echo "scale=16; $twidth / $theight" | bc )
    war=$( echo "scale=16; $4 / $5" | bc )

    if [[ $( echo "scale=16; $tar <= $war" | bc ) -eq "1" ]]
    then
        theight=$( echo "scale=0; $twidth / $war /1" | bc )
    else
        twidth=$( echo "scale=0; $theight * $war /1" | bc )
    fi

    if [[ $1 == "cw" || $1 == "ccw" ]]
    then
        tmp=$twidth
        twidth=$theight
        theight=$tmp
        unset tmp
    fi

    TB_X=$( echo "scale=0; ($TB_WIDTH - $twidth) / 2" | bc )
    TB_Y=$( echo "scale=0; ($TB_HEIGHT - $theight) / 2" | bc )

    TB_WIDTH=$twidth
    TB_HEIGHT=$theight
}

main () {
    get_dev
    set_rotation "$DEV" "$ROTATION"
    full_tablet_size "$DEV"
    if [[ $FULLSCREEN ]]
    then
        get_x_screen_size
    else
        get_active_window_size
    fi
    calculate_new_tablet_size "$ROTATION"\
                              "$TB_WIDTH" "$TB_HEIGHT"\
                              "$W_WIDTH" "$W_HEIGHT"
    set_output "$DEV" "$W_WIDTH" "$W_HEIGHT" "$W_X" "$W_Y"
    set_area "$DEV" "$TB_X" "$TB_Y" "$TB_WIDTH" "$TB_HEIGHT"
    echo "${W_WIDTH}x${W_HEIGHT}+${W_X}+${W_Y}   \
${TB_WIDTH}x${TB_HEIGHT}+${TB_X}+${TB_Y} $ROTATIONATION"
}

if [[ $DAEMON ]]
then
    main
    xdotool search . behave %@ focus exec --sync\
            $(\
              echo 'xwacomcalibrate ' $( if [[ $ROTATION ]]; then echo '-r ' $ROTATION ; fi )\
                   $( if [[ $FULLSCREEN ]]; then echo '-f'; fi ) )
else
    main
fi