summaryrefslogtreecommitdiff
path: root/src/grail-recognizer.c
blob: 9ede05934beb254bdc923b373b9265c2ffd68a94 (plain)
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
/*****************************************************************************
 *
 * grail - Gesture Recognition And Instantiation Library
 *
 * Copyright (C) 2010 Canonical Ltd.
 *
 * 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/>.
 *
 * Authors:
 *      Henrik Rydberg <rydberg@bitmath.org>
 *
 ****************************************************************************/

#include "grail-inserter.h"
#include <string.h>
#include <malloc.h>
#include <errno.h>

struct gesture_recognizer {
	struct touch_frame frame;
	touch_time_t scroll_time;
	int scroll_active;
	int scroll_gslot;
	int scroll_slots[2];
};

int gru_init(struct grail *ge)
{
	ge->gru = calloc(1, sizeof(struct gesture_recognizer));
	if (!ge->gru)
		return -ENOMEM;
	return 0;
}

void gru_destroy(struct grail *ge)
{
	free(ge->gru);
	ge->gru = NULL;
}

void gru_recognize(struct grail *ge, const struct touch_frame *frame)
{
	struct gesture_inserter *gin = ge->gin;
	struct gesture_recognizer *gru = ge->gru;
	grail_prop_t prop[DIM_GRAIL_PROP];
	grail_mask_t span[16];
	int slot, nslot, x, y, dx, dy;
	if (!gru)
		return;
	if (frame->ncreate || frame->ndestroy) {
		gin_gesture_end_all(gin);
		gru->scroll_active = 0;
		return;
	}
	if (frame->ntouch != 2)
		return;
	memset(span, 0, sizeof(span));
	nslot = 0;
	x = y = dx = dy = 0;
	for (slot = frame->slot; slot >= 0; slot = next_slot(frame, slot)) {
		struct touch *ot = &gru->frame.touch[slot];
		const struct touch *t = &frame->touch[slot];
		gru->scroll_slots[nslot] = slot;
		grail_set_mask(span, slot);
		x += t->prop[TP_POS_X];
		y += t->prop[TP_POS_Y];
		dx += t->prop[TP_POS_X] - ot->prop[TP_POS_X];
		dy += t->prop[TP_POS_Y] - ot->prop[TP_POS_Y];
		nslot++;
	}
	x /= nslot;
	y /= nslot;
	dx /= nslot;
	dy /= nslot;
	if (!dy)
		return;
	if (!gru->scroll_active) {
		gru->scroll_gslot = gin_gesture_begin(ge, frame,
						      GRAIL_TYPE_VSCROLL,
						      x, y, span);
		gru->scroll_active = 1;
	}
	prop[0] = dy;
	gin_gesture_event(gin, gru->scroll_gslot, prop);
	gru->frame = *frame;
}