forked from dnschneid/crouton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
croutonwheel
executable file
·136 lines (127 loc) · 3.67 KB
/
croutonwheel
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
#!/bin/sh -e
# Copyright (c) 2016 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
. "`dirname "$0"`/../installer/functions"
# Default button numbers
u=4
d=5
l=6
r=7
# Do horizontal scrolling by default
x=1
# Set the default scaling and constant values
xs='0.2'
xc='0.01'
ys='0.2'
yc='0.05'
#
USAGE="${0##*/} [-x] [-r] [-a #.#] [-b #.#] [-c #.#] [-d #.#]
Runs the mousewheel daemon.
Options:
-r Reverse the direction of scrolling.
-x Disable horizontal scrolling.
-a Sets the X scrolling scaling factor (default: $xs).
-b Sets the X scrolling constant additive (default: $xc).
-c Sets the Y scrolling scaling factor (default: $ys).
-d Sets the Y scrolling constant additive (default: $yc)."
# Process options
while getopts 'a:b:c:d:rx' opt; do
case "$opt" in
a) xs="$OPTARG";;
b) xc="$OPTARG";;
c) ys="$OPTARG";;
d) yc="$OPTARG";;
r) z=$u; u=$d; d=$z; z=$l; l=$r; r=$z;;
x) x='0';;
\?) echo "$USAGE" 1>&2; exit 1;;
esac
done
if [ $# -ge "$OPTIND" ]; then
echo "$USAGE" 1>&2
exit 1;
fi
# Operate on the Chromium OS X11 server
eval "`host-x11`"
# Only let one instance *really* run at a time
mkdir -m 775 -p "$CROUTONLOCKDIR"
exec 3>"$CROUTONLOCKDIR/wheel"
flock 3
# Monitor xinput2 events, reacting only to scroll events (axes 2 and 3).
# Accumulate the x and y scrolls, but reduce the acceleration so it doesn't go
# crazy. After a threshold, simulate the wheel presses using xte.
# Use xev to detect window changes and disable mouse wheel events when aura is
# focused.
# The goal of this system is to avoid having to launch any processes on a
# per-event basis, both to improve latency and performance.
{
croutonxi2event &
xi="$!"
aura="`croutonwmtools list ni | awk '/aura_root/ {print $NF}'`"
xev -id "$aura" &
trap "kill $! $xi 2>/dev/null || true" INT TERM HUP 0
wait
} | unbuffered_awk '
# Test if x is a non-zero real number (i.e. not nan, inf, "", or 0)
function isnonzero(x) {
return (x != "") && (x != 0) && (x != "nan") && (x != "inf")
}
# Disable when aura is visible and enable when it is not
/VisibilityUnobscured$/ {
w = 1
x = 0
y = 0
}
/VisibilityFullyObscured$/ {
w = 0
}
!w && /^EVENT type 17/ {
# Valuators start at $10; >=10 valuators implies a trackpad
if ($19 != "") {
dx = $14
dy = $15
tp = 1
} else {
dx = $12
dy = $13
tp = 0
}
if ('"$x"' && isnonzero(dx)) {
if (dx > 0) {
if (x < 0) x = 0
x += tp ? log(dx) * '"$xs"' + '"$xc"' : 1
} else {
if (x > 0) x = 0
x -= tp ? log(-dx) * '"$xs"' + '"$xc"' : 1
}
while (x >= 1) {
output("mouseclick '"$r"'")
x -= 1
}
while (x <= -1) {
output("mouseclick '"$l"'")
x += 1
}
}
if (isnonzero(dy)) {
if (dy > 0) {
if (y < 0) y = 0
y += tp ? log(dy) * '"$ys"' + '"$yc"' : 1
} else {
if (y > 0) y = 0
y -= tp ? log(-dy) * '"$ys"' + '"$yc"' : 1
}
while (y >= 1) {
output("mouseclick '"$u"'")
y -= 1
}
while (y <= -1) {
output("mouseclick '"$d"'")
y += 1
}
}
}
' | xte &
trap "pkill -P $$ 2>/dev/null || true" INT TERM HUP 0
wait
exit 0