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
|
/*****************************************************************************
*
* 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>
static void tp_event(struct touch_dev *dev,
const struct input_event *ev)
{
struct grail *ge = dev->priv;
struct grail_impl *x = ge->impl;
if (ev->type == EV_ABS) {
if (ev->code == ABS_X)
x->pointer_x = ev->value;
if (ev->code == ABS_Y)
x->pointer_y = ev->value;
return;
}
if (ev->type == EV_KEY) {
switch (ev->code) {
case BTN_TOUCH:
case BTN_TOOL_FINGER:
case BTN_TOOL_DOUBLETAP:
case BTN_TOOL_TRIPLETAP:
case BTN_TOOL_QUADTAP:
return;
}
}
evbuf_put(&x->evbuf, ev);
}
static int handle_abs_events(struct grail *ge,
const struct input_event *syn,
int pointer)
{
struct grail_impl *impl = ge->impl;
struct input_event ev = *syn;
int nevent = 0;
if (pointer != impl->pointer_status) {
ev.type = EV_KEY;
ev.code = BTN_TOUCH;
ev.value = pointer;
ge->event(ge, &ev);
impl->pointer_status = pointer;
nevent++;
}
if (pointer) {
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = impl->pointer_x;
ge->event(ge, &ev);
nevent++;
ev.type = EV_ABS;
ev.code = ABS_Y;
ev.value = impl->pointer_y;
ge->event(ge, &ev);
nevent++;
}
return nevent;
}
static int pointer_active(const struct grail *ge)
{
static const int fm_mask = 0x03;
const struct move_model *move = &ge->gru->move;
const struct tapping_model *tap = &ge->gru->tapping;
return move->single && ((move->active & fm_mask) || tap->active);
}
static void tp_sync(struct touch_dev *dev,
const struct input_event *syn)
{
struct input_event ev;
struct grail *ge = dev->priv;
struct grail_impl *impl = ge->impl;
struct gesture_inserter *gin = ge->gin;
struct touch_frame *frame = &dev->frame;
int pointer, used_move, nevent = 0;
gin_frame_begin(ge, frame);
gru_recognize(ge, frame);
gin_frame_end(ge, frame);
used_move = grail_mask_count(gin->types, sizeof(gin->types));
pointer = pointer_active(ge) && !used_move;
if (!ge->event) {
evbuf_clear(&impl->evbuf);
return;
}
if (!impl->filter_abs)
nevent += handle_abs_events(ge, syn, pointer);
while (!evbuf_empty(&impl->evbuf)) {
evbuf_get(&impl->evbuf, &ev);
ge->event(ge, &ev);
nevent++;
}
if (nevent)
ge->event(ge, syn);
}
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;
ge->impl = x;
ret = touch_dev_open(&x->dev, fd);
if (ret)
goto freemem;
x->dev.event = tp_event;
x->dev.sync = tp_sync;
x->dev.priv = ge;
ret = gin_init(ge);
if (ret)
goto freedev;
ret = gru_init(ge);
if (ret)
goto freegin;
return 0;
freegin:
gin_destroy(ge);
freedev:
touch_dev_close(&x->dev, fd);
freemem:
free(x);
ge->impl = 0;
return ret;
}
void grail_close(struct grail *ge, int fd)
{
struct grail_impl *x = ge->impl;
void *status;
gru_destroy(ge);
gin_destroy(ge);
touch_dev_close(&x->dev, fd);
free(ge->impl);
ge->impl = 0;
}
int grail_idle(struct grail *ge, int fd, int ms)
{
struct grail_impl *x = ge->impl;
return touch_dev_idle(&x->dev, fd, ms);
}
int grail_pull(struct grail *ge, int fd)
{
struct grail_impl *x = ge->impl;
return touch_dev_pull(&x->dev, fd);
}
void grail_get_units(const struct grail *ge,
struct grail_coord *min, struct grail_coord *max)
{
const struct touch_caps *caps = &ge->impl->dev.caps;
min->x = caps->min_x;
min->y = caps->min_y;
max->x = caps->max_x;
max->y = caps->max_y;
}
|