-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwacom
executable file
·165 lines (148 loc) · 4.79 KB
/
wacom
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
#!/usr/bin/env bash
# Dependencies: fzf gawk xorg-xrandr xf86-input-wacom
usage() {
echo -n "
This is a (relatively) simple script that will automatically configure your connected wacom devices. It will map the chosen monitor to your wacom device.
-h, --help:
Display this help message
-m, --monitor [biggest|smallest|primary]:
Selects specific monitor
-f, --fzf:
Use fzf instead of dmenu
"
}
# ______ _ _
# | ___| | | (_)
# | |_ _ _ _ __ ___| |_ _ ___ _ __ ___
# | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
# | | | |_| | | | | (__| |_| | (_) | | | \__ \
# \_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
getdeviceid() {
# you can hardcode it here if you want
DEVICE_ID="$(xsetwacom list devices | awk -F: '/stylus/ {print $2}' | awk '{print $1}')"
[ -z "$DEVICE_ID" ] && {
echo "Could not find any connected devices. Please edit the script or try again."
exit 1
}
}
query_monitors() {
# Gathering data to query user
IFS=$'\n'
data=()
for monitor in $(xrandr -q | grep ' connected'); do
data+=("${monitor%% *}") # analogous to cut -d ' ' -f1
res="$(grep -o -E '[0-9]+x[0-9]+\+[0-9]+\+[0-9]+' <<< "$monitor")" # monitor resolution + offset
case $monitor in
*primary*)
data+=("primary $res")
;;
*)
data+=("$res")
;;
esac
done
if [ "${#data[@]}" -le 2 ]; then
# if there's only one monitor, there would be two variables in data
reply="${data[1]}" # in this case, there's only one
else
reply="$(printf '%s -- %s\n' "${data[@]}" | eval "$MENU")"}
[ -z "$reply" ] && {
echo "Please select an appropriate monitor and try again."; exit 1
}
fi
# regrabs the resolution + offset
resolution=`grep -o -E '[0-9]+x[0-9]+\+[0-9]+\+[0-9]+' <<< "$reply"`
# for some reason, the grep doesn't work. Leaving it here in case someone is smart enough to debug
#grep -o -E '[0-9]+x[0-9]+' <<< "$resolution" | cut -d 'x' -f1,2 --output-delimiter=$'\n' | read -r width height
width="$(grep -o -E '[0-9]+x' <<< "$resolution" | cut -d 'x' -f1)"
height="$(grep -o -E 'x[0-9]+' <<< "$resolution" | cut -d 'x' -f2)"
}
fixpenlocation() {
# last part is monitor resolution + offset
xsetwacom set "${1:-$DEVICE_ID}" MapToOutput "$2"
}
# slightly reduce drawing area to match aspect ratio of monitor
fixaspectratio() {
TAB_WIDTH="$(xsetwacom get "${1:-$DEVICE_ID}" Area | cut -d ' ' -f3)"
#TAB_HEIGHT="$(xsetwacom get "${1:-$DEVICE_ID}" Area | cut -d ' ' -f4)"
NEW_HEIGHT=`bc -l <<< "$TAB_WIDTH * ($2/$3)"`
# String subtitution truncates bc -l's decimal precision
xsetwacom set "${1:-$DEVICE_ID}" Area 0 0 "$TAB_WIDTH" "${NEW_HEIGHT%.*}"
}
# ___ ___ _
# | \/ | (_)
# | . . | __ _ _ _ __
# | |\/| |/ _` | | '_ \
# | | | | (_| | | | | |
# \_| |_/\__,_|_|_| |_|
wacommenu() {
getdeviceid
query_monitors
fixpenlocation "${DEVICE_ID}" "$resolution"
fixaspectratio "${DEVICE_ID}" "$height" "$width"
}
wacom_specific_monitor() {
monitors=$(xrandr -q | grep ' connected')
case $1 in
b|biggest)
# uses biggest single monitor (based on physical size)
max_size=0
for monitor in "${monitors[@]}"; do
grep -o '[0-9]*' <<< "$monitor" | tail -2 | read -r width height
size=`bc -l <<< "$width * $height"`
(( size > max_size )) && max_size=$size
done
# main funcs
getdeviceid
fixpenlocation "$DEVICE_ID" "$reply"
fixaspectratio "$DEVICE_ID" "$height" "$width"
;;
s|smallest)
# uses smallest single monitor (based on physical size)
min_size=20000 # if you have a monitor this size, then screw you
for monitor in "${monitors[@]}"; do
grep -o '[0-9]*' <<< "$monitor" | tail -2 | read -r width height
size="$(bc -l <<< "$width * $height")"
(( size < min_size )) && min_size=$size
done
# main funcs
getdeviceid
fixpenlocation "$DEVICE_ID" "$reply"
fixaspectratio "$DEVICE_ID" "$height" "$width"
;;
p|primary)
# uses primary monitor
monitor="$(xrandr -q | grep ' primary')"
grep -o '[0-9]*' <<< "$monitor" | tail -2 | read -r width height
# main funcs
getdeviceid
fixpenlocation "$DEVICE_ID" "$reply"
fixaspectratio "$DEVICE_ID" "$height" "$width"
;;
*)
usage
;;
esac
}
MENU="dmenu -i -l 3 -p 'Monitor?'"
[ -z "$1" ] && { wacommenu; exit; }
opts=$(getopt -o hmf -l help,monitor,fzf -n "$0" -- "$@") || usage
eval set -- "$opts"
while [[ -n "$1" ]]; do
case $1 in
-h|--help)
usage; exit
;;
-m|--monitor)
[ -z "$2" ] && usage; exit 1
wacom_specific_monitor "$2"; exit
;;
-f|--fzf)
MENU="fzf --prompt='Monitor?> '"
wacommenu; exit
;;
*)
wacommenu; exit
;;
esac
done