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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*****************************************************************************
*
* grail - Gesture Recognition And Instantiation Library
*
* Copyright (C) 2010 Canonical Ltd.
* Copyright (C) 2010 Henrik Rydberg <rydberg@bitmath.org>
*
* This program 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 3 of the License, or (at your
* option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
****************************************************************************/
#include "grail-inserter.h"
#include "grail-recognizer.h"
#include "grail-impl.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <errno.h>
#include <stdlib.h>
#define DIM_FRAMES 100
#define FRAME_RATE 100
/* Wait time (ms) for number of touches to stabilize. */
static const unsigned int stable_time = 50;
/* Maximum time (ms) for gesture to be recognized. */
static const unsigned int recognition_time = 200;
void grail_filter_abs_events(struct grail *ge, int usage)
{
struct grail_impl *x = ge->impl;
x->filter_abs = usage;
}
int grail_open(struct grail *ge, int fd)
{
struct grail_impl *x;
int ret;
x = calloc(1, sizeof(*x));
if (!x)
return -ENOMEM;
x->evemu = evemu_new(0);
if (!x->evemu) {
ret = -ENOMEM;
goto freemem;
}
ret = evemu_extract(x->evemu, fd);
if (ret)
goto freemem;
if (!utouch_frame_is_supported_mtdev(x->evemu)) {
ret = -ENODEV;
goto freemem;
}
x->mtdev = mtdev_new_open(fd);
if (!x->mtdev) {
ret = -ENOMEM;
goto freemem;
}
x->fh = utouch_frame_new_engine(DIM_FRAMES, DIM_TOUCH, FRAME_RATE);
if (!x->fh) {
ret = -ENOMEM;
goto freedev;
}
ret = utouch_frame_init_mtdev(x->fh, x->evemu);
if (ret)
goto freeframe;
ge->impl = x;
ret = gin_init(ge);
if (ret)
goto freeframe;
ret = gru_init(ge);
if (ret)
goto freegin;
return 0;
freegin:
gin_destroy(ge);
freeframe:
utouch_frame_delete_engine(x->fh);
freedev:
mtdev_close_delete(x->mtdev);
freemem:
evemu_delete(x->evemu);
free(x);
ge->impl = 0;
return ret;
}
void grail_close(struct grail *ge, int fd)
{
struct grail_impl *x = ge->impl;
gru_destroy(ge);
gin_destroy(ge);
utouch_frame_delete_engine(x->fh);
mtdev_close_delete(x->mtdev);
evemu_delete(x->evemu);
free(x);
ge->impl = 0;
}
int grail_idle(struct grail *ge, int fd, int ms)
{
struct grail_impl *x = ge->impl;
return mtdev_idle(x->mtdev, fd, ms);
}
void grail_get_units(const struct grail *ge,
struct grail_coord *min, struct grail_coord *max)
{
struct utouch_surface *s = utouch_frame_get_surface(ge->impl->fh);
min->x = s->min_x;
min->y = s->min_y;
max->x = s->max_x;
max->y = s->max_y;
}
/* Check for any potential gestures given the number of touch points and where
* they occur on screen. */
static int check_potential_gestures(struct grail *ge,
const struct utouch_frame *frame)
{
grail_mask_t types[DIM_GRAIL_TYPE_BYTES] = {0};
struct grail_client_info client;
struct grail_coord pos[frame->num_active];
int i;
switch (frame->num_active) {
case 1:
grail_mask_set(types, GRAIL_TYPE_DRAG1);
grail_mask_set(types, GRAIL_TYPE_PINCH1);
grail_mask_set(types, GRAIL_TYPE_ROTATE1);
grail_mask_set(types, GRAIL_TYPE_TAP1);
break;
case 2:
grail_mask_set(types, GRAIL_TYPE_DRAG2);
grail_mask_set(types, GRAIL_TYPE_PINCH2);
grail_mask_set(types, GRAIL_TYPE_ROTATE2);
grail_mask_set(types, GRAIL_TYPE_TAP2);
break;
case 3:
grail_mask_set(types, GRAIL_TYPE_DRAG3);
grail_mask_set(types, GRAIL_TYPE_PINCH3);
grail_mask_set(types, GRAIL_TYPE_ROTATE3);
grail_mask_set(types, GRAIL_TYPE_TAP3);
grail_mask_set(types, GRAIL_TYPE_EDRAG);
grail_mask_set(types, GRAIL_TYPE_EPINCH);
grail_mask_set(types, GRAIL_TYPE_EROTATE);
break;
case 4:
grail_mask_set(types, GRAIL_TYPE_DRAG4);
grail_mask_set(types, GRAIL_TYPE_PINCH4);
grail_mask_set(types, GRAIL_TYPE_ROTATE4);
grail_mask_set(types, GRAIL_TYPE_TAP4);
grail_mask_set(types, GRAIL_TYPE_MDRAG);
grail_mask_set(types, GRAIL_TYPE_MPINCH);
grail_mask_set(types, GRAIL_TYPE_MROTATE);
break;
case 5:
grail_mask_set(types, GRAIL_TYPE_DRAG5);
grail_mask_set(types, GRAIL_TYPE_PINCH5);
grail_mask_set(types, GRAIL_TYPE_ROTATE5);
grail_mask_set(types, GRAIL_TYPE_TAP5);
break;
}
for (i = 0; i < frame->num_active; i++) {
pos[i].x = gin_prop_x(ge->gin, frame->active[i]->x);
pos[i].y = gin_prop_y(ge->gin, frame->active[i]->y);
}
return ge->get_clients(ge, &client, 1, pos, frame->num_active,
types, DIM_GRAIL_TYPE_BYTES);
}
static void report_frame(struct grail *ge,
const struct utouch_frame *frame,
const struct input_event *syn)
{
struct grail_impl *impl = ge->impl;
struct gesture_recognizer *gru = ge->gru;
struct input_event iev;
struct grail_event gev;
ge->impl->frame = frame;
/* Reset timer when number of touches changes. */
if (frame->prev->revision != frame->revision && frame->num_active) {
gru->start_time = frame->time;
}
/* Once touches have stabilized, check if there are any potential
* gestures registered for the number of touches. */
if (gru->state == RECOGNIZING &&
frame->time > gru->start_time + stable_time &&
check_potential_gestures(ge, frame) == 0)
gru->state = UNRECOGNIZED;
/* Process the frame for gestures unless we're sure there are none. */
if (gru->state != UNRECOGNIZED) {
gin_frame_begin(ge, frame);
gru_recognize(ge, frame);
gin_frame_end(ge, frame);
}
if (gru->state == RECOGNIZING) {
/* If a gesture is recognized and either all touches lifted or
* we are within the recognition interval, set state to
* recognized. Note that taps do not stay in the used array, so
* we must check if any gesture events are buffered. */
if ((grail_mask_count(ge->gin->used, DIM_INSTANCE_BYTES) ||
!grailbuf_empty(&impl->gbuf)) &&
(frame->num_active == 0 ||
(frame->time > gru->start_time + stable_time &&
frame->time <= gru->start_time + recognition_time)))
gru->state = RECOGNIZED;
/* If there is no gesture and all touches lifted, set state to
* unrecognized. */
else if (frame->num_active == 0)
gru->state = UNRECOGNIZED;
}
/* Now we have the state set, process events. */
switch (gru->state) {
case RECOGNIZED:
while (!grailbuf_empty(&impl->gbuf)) {
grailbuf_get(&impl->gbuf, &gev);
if (ge->gesture)
ge->gesture(ge, &gev);
}
evbuf_clear(&impl->evbuf);
/* Once all touches are lifted and no gestures are
* active, reset recognition state. */
if (grail_mask_count(ge->gin->used, DIM_INSTANCE_BYTES) == 0 &&
frame->num_active == 0)
gru->state = RECOGNIZING;
break;
case RECOGNIZING:
/* If we're still actively recognizing, stop here. */
if (frame->time <= gru->start_time + recognition_time)
break;
gru->state = UNRECOGNIZED;
case UNRECOGNIZED:
while (!evbuf_empty(&impl->evbuf)) {
evbuf_get(&impl->evbuf, &iev);
if (ge->event)
ge->event(ge, &iev);
}
grailbuf_clear(&impl->gbuf);
/* Once all touches are lifted, reset recognition
* state. */
if (frame->num_active == 0)
gru->state = RECOGNIZING;
break;
}
}
static void grail_pump_mtdev(struct grail *ge, const struct input_event *ev)
{
struct grail_impl *impl = ge->impl;
const struct utouch_frame *frame;
evbuf_put(&impl->evbuf, ev);
if (ev->type == EV_SYN || ev->type == EV_ABS) {
frame = utouch_frame_pump_mtdev(impl->fh, ev);
if (frame)
report_frame(ge, frame, ev);
}
}
int grail_pull(struct grail *ge, int fd)
{
struct grail_impl *impl = ge->impl;
struct input_event ev;
int ret, count = 0;
while ((ret = mtdev_get(impl->mtdev, fd, &ev, 1)) > 0) {
grail_pump_mtdev(ge, &ev);
count++;
}
return count > 0 ? count : ret;
}
|