summaryrefslogtreecommitdiff
path: root/src/gestures-tapping.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gestures-tapping.c')
-rw-r--r--src/gestures-tapping.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/gestures-tapping.c b/src/gestures-tapping.c
new file mode 100644
index 0000000..cc0e685
--- /dev/null
+++ b/src/gestures-tapping.c
@@ -0,0 +1,101 @@
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-recognizer.h"
26#include <math.h>
27
28static const int TAP_BEGIN = 0;
29static const int TAP_END = 1;
30static const int TAP_CANCEL = 2;
31
32static const int TAP_MIN_GAP_MS = 25;
33static const int TAP_MAX_GAP_MS = 300;
34
35int gru_tapping(struct grail *ge,
36 const struct touch_frame *frame)
37{
38 struct gesture_recognizer *gru = ge->gru;
39 struct tapping_model *state = &gru->tapping;
40 struct move_model *move = &gru->move;
41 grail_prop_t prop[DIM_GRAIL_PROP];
42 if (move->ntouch) {
43 if (state->status == TAP_CANCEL)
44 return 0;
45 state->status = TAP_BEGIN;
46 if (!state->ntouch)
47 state->start = move->time;
48 if (move->ntouch > state->ntouch) {
49 state->ntouch = move->ntouch;
50 if (state->active) {
51 gin_gid_discard(ge, state->gid);
52 state->active = 0;
53 }
54 state->x = move->x.val;
55 state->y = move->y.val;
56 } else if (state->ntouch == 1) {
57 if (fabs(move->x.val - state->x) > move->y.bar ||
58 fabs(move->y.val - state->y) > move->y.bar)
59 state->status = TAP_CANCEL;
60 }
61 if (!state->active) {
62 int type = GRAIL_TYPE_TAP1 + state->ntouch - 1;
63 state->gid = gin_gid_begin(ge, type, PRIO_TAP, frame);
64 state->active = 1;
65 }
66 } else if (state->status == TAP_BEGIN && state->ntouch) {
67 state->status = TAP_END;
68 } else {
69 state->status = TAP_BEGIN;
70 state->ntouch = 0;
71 return 0;
72 }
73 if (state->ntouch < 1 || state->ntouch > 5)
74 state->status = TAP_CANCEL;
75 if (move->time - state->start > TAP_MAX_GAP_MS)
76 state->status = TAP_CANCEL;
77 if (state->status == TAP_END &&
78 move->time - state->start < TAP_MIN_GAP_MS)
79 state->status = TAP_CANCEL;
80 if (move->x.active || move->y.active)
81 state->status = TAP_CANCEL;
82 if (state->status == TAP_CANCEL) {
83 if (state->active) {
84 gin_gid_discard(ge, state->gid);
85 state->active = 0;
86 }
87 state->ntouch = 0;
88 return 0;
89 }
90 if (state->status != TAP_END ||
91 move->time - state->start < TAP_MIN_GAP_MS)
92 return 0;
93 prop[0] = move->time - state->start;
94 gin_gid_event(ge, state->gid,
95 move->x.val, move->y.val, state->ntouch,
96 prop, 1, GRAIL_STATUS_END);
97 state->status = TAP_BEGIN;
98 state->ntouch = 0;
99 state->active = 0;
100 return 1;
101}