summaryrefslogtreecommitdiff
path: root/src/grail-recognizer.c
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@bitmath.org>2010-07-29 15:54:31 +0200
committerHenrik Rydberg <rydberg@euromail.se>2010-08-05 22:48:38 +0200
commite0eb1015bf30fd0913db571b000ba6a83ea22660 (patch)
treea3d62152066cfb8521c80806596a4fb0fe322014 /src/grail-recognizer.c
parentb76660d71e2ea77752025089bd71deffad64e325 (diff)
Split the code into recognizer and inserter
Refactor code to make more room for gesture recognition. Also simplify public headers. Signed-off-by: Henrik Rydberg <rydberg@bitmath.org>
Diffstat (limited to 'src/grail-recognizer.c')
-rw-r--r--src/grail-recognizer.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/grail-recognizer.c b/src/grail-recognizer.c
new file mode 100644
index 0000000..9ede059
--- /dev/null
+++ b/src/grail-recognizer.c
@@ -0,0 +1,97 @@
1/*****************************************************************************
2 *
3 * grail - Gesture Recognition And Instantiation Library
4 *
5 * Copyright (C) 2010 Canonical Ltd.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Authors:
21 * Henrik Rydberg <rydberg@bitmath.org>
22 *
23 ****************************************************************************/
24
25#include "grail-inserter.h"
26#include <string.h>
27#include <malloc.h>
28#include <errno.h>
29
30struct gesture_recognizer {
31 struct touch_frame frame;
32 touch_time_t scroll_time;
33 int scroll_active;
34 int scroll_gslot;
35 int scroll_slots[2];
36};
37
38int gru_init(struct grail *ge)
39{
40 ge->gru = calloc(1, sizeof(struct gesture_recognizer));
41 if (!ge->gru)
42 return -ENOMEM;
43 return 0;
44}
45
46void gru_destroy(struct grail *ge)
47{
48 free(ge->gru);
49 ge->gru = NULL;
50}
51
52void gru_recognize(struct grail *ge, const struct touch_frame *frame)
53{
54 struct gesture_inserter *gin = ge->gin;
55 struct gesture_recognizer *gru = ge->gru;
56 grail_prop_t prop[DIM_GRAIL_PROP];
57 grail_mask_t span[16];
58 int slot, nslot, x, y, dx, dy;
59 if (!gru)
60 return;
61 if (frame->ncreate || frame->ndestroy) {
62 gin_gesture_end_all(gin);
63 gru->scroll_active = 0;
64 return;
65 }
66 if (frame->ntouch != 2)
67 return;
68 memset(span, 0, sizeof(span));
69 nslot = 0;
70 x = y = dx = dy = 0;
71 for (slot = frame->slot; slot >= 0; slot = next_slot(frame, slot)) {
72 struct touch *ot = &gru->frame.touch[slot];
73 const struct touch *t = &frame->touch[slot];
74 gru->scroll_slots[nslot] = slot;
75 grail_set_mask(span, slot);
76 x += t->prop[TP_POS_X];
77 y += t->prop[TP_POS_Y];
78 dx += t->prop[TP_POS_X] - ot->prop[TP_POS_X];
79 dy += t->prop[TP_POS_Y] - ot->prop[TP_POS_Y];
80 nslot++;
81 }
82 x /= nslot;
83 y /= nslot;
84 dx /= nslot;
85 dy /= nslot;
86 if (!dy)
87 return;
88 if (!gru->scroll_active) {
89 gru->scroll_gslot = gin_gesture_begin(ge, frame,
90 GRAIL_TYPE_VSCROLL,
91 x, y, span);
92 gru->scroll_active = 1;
93 }
94 prop[0] = dy;
95 gin_gesture_event(gin, gru->scroll_gslot, prop);
96 gru->frame = *frame;
97}