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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* Copyright 2019 Aaron Sowry for Cendio AB
* Copyright 2019-2020 Pierre Ossman for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <math.h>
#define XK_MISCELLANY
#include <rfb/keysymdef.h>
#include <rfb/util.h>
#include "GestureHandler.h"
#include "BaseTouchHandler.h"
// Sensitivity threshold for gestures
static const int ZOOMSENS = 30;
static const int SCRLSENS = 50;
static const unsigned DOUBLE_TAP_TIMEOUT = 1000;
static const unsigned DOUBLE_TAP_THRESHOLD = 50;
BaseTouchHandler::BaseTouchHandler()
{
gettimeofday(&lastTapTime, NULL);
}
BaseTouchHandler::~BaseTouchHandler()
{
}
void BaseTouchHandler::handleGestureEvent(const GestureEvent& ev)
{
double magnitude;
switch (ev.type) {
case GestureBegin:
switch (ev.gesture) {
case GestureOneTap:
handleTapEvent(ev, 1);
break;
case GestureTwoTap:
handleTapEvent(ev, 3);
break;
case GestureThreeTap:
handleTapEvent(ev, 2);
break;
case GestureDrag:
fakeMotionEvent(ev);
fakeButtonEvent(true, 1, ev);
break;
case GestureLongPress:
fakeMotionEvent(ev);
fakeButtonEvent(true, 3, ev);
break;
case GestureTwoDrag:
lastMagnitudeX = ev.magnitudeX;
lastMagnitudeY = ev.magnitudeY;
fakeMotionEvent(ev);
break;
case GesturePinch:
lastMagnitudeX = sqrt(ev.magnitudeX * ev.magnitudeX +
ev.magnitudeY * ev.magnitudeY);
fakeMotionEvent(ev);
break;
}
break;
case GestureUpdate:
switch (ev.gesture) {
case GestureOneTap:
case GestureTwoTap:
case GestureThreeTap:
break;
case GestureDrag:
case GestureLongPress:
fakeMotionEvent(ev);
break;
case GestureTwoDrag:
// Always scroll in the same position.
// We don't know if the mouse was moved so we need to move it
// every update.
fakeMotionEvent(ev);
while ((ev.magnitudeY - lastMagnitudeY) > SCRLSENS) {
fakeButtonEvent(true, 4, ev);
fakeButtonEvent(false, 4, ev);
lastMagnitudeY += SCRLSENS;
}
while ((ev.magnitudeY - lastMagnitudeY) < -SCRLSENS) {
fakeButtonEvent(true, 5, ev);
fakeButtonEvent(false, 5, ev);
lastMagnitudeY -= SCRLSENS;
}
while ((ev.magnitudeX - lastMagnitudeX) > SCRLSENS) {
fakeButtonEvent(true, 6, ev);
fakeButtonEvent(false, 6, ev);
lastMagnitudeX += SCRLSENS;
}
while ((ev.magnitudeX - lastMagnitudeX) < -SCRLSENS) {
fakeButtonEvent(true, 7, ev);
fakeButtonEvent(false, 7, ev);
lastMagnitudeX -= SCRLSENS;
}
break;
case GesturePinch:
// Always scroll in the same position.
// We don't know if the mouse was moved so we need to move it
// every update.
fakeMotionEvent(ev);
magnitude = sqrt(ev.magnitudeX * ev.magnitudeX +
ev.magnitudeY * ev.magnitudeY);
if (abs(magnitude - lastMagnitudeX) > ZOOMSENS) {
fakeKeyEvent(true, XK_Control_L, ev);
while ((magnitude - lastMagnitudeX) > ZOOMSENS) {
fakeButtonEvent(true, 4, ev);
fakeButtonEvent(false, 4, ev);
lastMagnitudeX += ZOOMSENS;
}
while ((magnitude - lastMagnitudeX) < -ZOOMSENS) {
fakeButtonEvent(true, 5, ev);
fakeButtonEvent(false, 5, ev);
lastMagnitudeX -= ZOOMSENS;
}
fakeKeyEvent(false, XK_Control_L, ev);
}
}
break;
case GestureEnd:
switch (ev.gesture) {
case GestureOneTap:
case GestureTwoTap:
case GestureThreeTap:
case GesturePinch:
case GestureTwoDrag:
break;
case GestureDrag:
fakeMotionEvent(ev);
fakeButtonEvent(false, 1, ev);
break;
case GestureLongPress:
fakeMotionEvent(ev);
fakeButtonEvent(false, 3, ev);
break;
}
break;
}
}
void BaseTouchHandler::handleTapEvent(const GestureEvent& ev,
int buttonEvent)
{
GestureEvent newEv = ev;
// If the user quickly taps multiple times we assume they meant to
// hit the same spot, so slightly adjust coordinates
if ((rfb::msSince(&lastTapTime) < DOUBLE_TAP_TIMEOUT) &&
(firstDoubleTapEvent.type == ev.type)) {
double dx = firstDoubleTapEvent.eventX - ev.eventX;
double dy = firstDoubleTapEvent.eventY - ev.eventY;
double distance = sqrt((dx * dx) + (dy * dy));
if (distance < DOUBLE_TAP_THRESHOLD) {
newEv.eventX = firstDoubleTapEvent.eventX;
newEv.eventY = firstDoubleTapEvent.eventY;
} else {
firstDoubleTapEvent = ev;
}
} else {
firstDoubleTapEvent = ev;
}
gettimeofday(&lastTapTime, NULL);
fakeMotionEvent(newEv);
fakeButtonEvent(true, buttonEvent, newEv);
fakeButtonEvent(false, buttonEvent, newEv);
}
|