-
Notifications
You must be signed in to change notification settings - Fork 10
/
mouse_movement_printer.sh
executable file
·158 lines (139 loc) · 3.29 KB
/
mouse_movement_printer.sh
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
#!/bin/bash
#
# This script requires: bash, sed, awk
#
# Using sed because not all awk versions support replacing by matched groups from the regex.
# But then it requires a sed version with support for "-u":
# * GNU sed
# * OpenBSD sed
# * FreeBSD sed
# * does NOT work with busybox sed
#
# Works with the following awk implementations:
# * gawk
# * mawk
# * nawk
# * original-awk
# * BusyBoxy awk
SCRIPTNAME=`basename "$0"`
print_help() {
cat << EOF
Usage: $SCRIPTNAME [-root] [-geometry geom] [-display display]
Uses "xev" to display how many pixels the mouse pointer has moved after each
motion event. Useful to fine-tune your pointer speed/acceleration settings.
Also useful to measure the quality or resolution of your mouse.
See also:
* xinput and xinput-gui
* mouse-dpi-tool
* man 4 libinput
* https://wayland.freedesktop.org/libinput/doc/latest/configuration.html#pointer-acceleration
* https://wayland.freedesktop.org/libinput/doc/latest/pointer-acceleration.html#pointer-acceleration
* https://gitlab.freedesktop.org/libevdev/libevdev/-/blob/master/tools/mouse-dpi-tool.c
EOF
}
# parse_parameters:
while [[ "$1" == -* ]] ; do
case "$1" in
-h|-help|--help)
print_help
exit
;;
# For simplicity, any additional parameters are passed directly to xev
# *)
# echo "Invalid parameter: '$1'"
# exit 1
# ;;
esac
done
# check dependencies
if ! type xev &>/dev/null ; then
echo "You are missing xev. Please install it."
exit 1
fi
AWK_BIN="awk"
AWK_PARAMS=( )
# Workaround for mawk:
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504
AWK_VERSION="$(echo | "${AWK_BIN}" -W version 2>&1)"
if [[ "${AWK_VERSION}" == mawk* ]] ; then
AWK_PARAMS+=(-W interactive)
fi
# xev: only mouse events, one event per line, on the root window.
# sed: unbuffered, only print matching lines.
xev -event mouse -1 "$@" \
| sed -u -n 's/MotionNotify.* (\([0-9]*\),\([0-9]*\)), root:(\([0-9]*\),\([0-9]*\)).*/\1 \2 \3 \4/p' \
| "${AWK_BIN}" "${AWK_PARAMS[@]}" '
function abs(n) {
if (n < 0) {
return -n
}
return n
}
function sign(n) {
if (n < 0) {
return -1
} else if (n > 0) {
return 1
}
return 0
}
# i is a local variable.
function blockbar(n, i) {
s = ""
for ( i=0 ; i < 8 && n > 0 ; i++ ) {
r = n - 1
if (r >= BLOCK_LEN) {
r = BLOCK_LEN - 1
}
n -= BLOCK_LEN
s = s BLOCK[r]
}
return s
}
BEGIN {
ARROW[0] = "↖"
ARROW[1] = "↑"
ARROW[2] = "↗"
ARROW[3] = "←"
ARROW[4] = "·"
ARROW[5] = "→"
ARROW[6] = "↙"
ARROW[7] = "↓"
ARROW[8] = "↘"
BLOCK[0] = "⠂"
BLOCK[1] = "⠒"
BLOCK_LEN = 2
}
{
if (has_prev) {
# Local, window-based delta.
dx = $1 - prev_x
dy = $2 - prev_y
# Global, root-window-based delta.
rx = $3 - root_x
ry = $4 - root_y
# Euclidean distance.
dist = sqrt(dx * dx + dy * dy)
# Manhattan distance.
xy = abs(dx) + abs(dy)
printf("delta:%+6d,%+6d", dx, dy)
printf(" pos:%5d,%5d", $3, $4)
if (dx != rx || dy != ry) {
# Somehow, the distance between the window-based pointer position
# and the root window pointer position diverged.
printf(" (%+6d,%+6d)", rx, ry)
}
printf(" dist:%7.1f", dist)
printf(" |x|+|y|:%5d", xy)
printf(" %s %s", ARROW[1 + sign(dx) + 3 * (1 + sign(dy))], blockbar(xy))
printf("\n")
}
prev_x = $1
prev_y = $2
root_x = $3
root_y = $4
has_prev = 1
# Flush after every line for better UX.
fflush()
}
'