summaryrefslogtreecommitdiff
path: root/src/touch-engine.c
blob: 28b68da27d862ed0cf00fbd902913f72d33e7890 (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
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
/*****************************************************************************
 *
 * 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-touch.h>
#include <string.h>

static void tp_event(struct touch_dev *dev, const struct input_event *ev)
{
	struct touch_engine *engine = dev->priv;
	if (engine && engine->event)
		engine->event(engine, ev);
}

static void update_frame(struct touch_frame *frame,
			 int slot, int active,
			 const touch_prop_mask_t *mask,
			 const touch_prop_t *prop)
{
	struct touch *touch = &frame->touch[slot];
	touch->active = active;
	if (mask && prop) {
		int i;
		for (i = 0; i < DIM_TOUCH_PROP; i++)
			if (has_prop(mask, i))
				touch->prop[i] = prop[i];
	}
}

static void finalize_frame(struct touch_frame *frame, touch_time_t time)
{
	int i, last = -1;
	frame->ntouch = 0;
	frame->slot = -1;
	for (i = 0; i < DIM_TOUCH_PROP; i++) {
		frame->touch[i].next = -1;
		if (frame->touch[i].active) {
			if (last < 0)
				frame->slot = i;
			else
				frame->touch[last].next = i;
			last = i;
			frame->ntouch++;
		}
	}
	frame->time = time;
}

static void tp_create(struct touch_dev *dev, int slot,
		      const touch_prop_mask_t *mask, const touch_prop_t *prop)
{
	struct touch_engine *engine = dev->priv;
	if (engine) {
		engine->frame.ncreate++;
		update_frame(&engine->frame, slot, 1, mask, prop);
	}
}

static void tp_modify(struct touch_dev *dev, int slot,
		      const touch_prop_mask_t *mask, const touch_prop_t *prop)
{
	struct touch_engine *engine = dev->priv;
	if (engine) {
		engine->frame.nmodify++;
		update_frame(&engine->frame, slot, 1, mask, prop);
	}
}

static void tp_destroy(struct touch_dev *dev, int slot)
{
	struct touch_engine *engine = dev->priv;
	if (engine) {
		engine->frame.ndestroy++;
		update_frame(&engine->frame, slot, 0, 0, 0);
	}
}

static void tp_sync(struct touch_dev *dev, const struct input_event *syn)
{
	static const touch_time_t ms = 1000;
	struct touch_engine *engine = dev->priv;
	touch_time_t time = syn->time.tv_usec / ms + syn->time.tv_sec * ms;
	if (engine) {
		finalize_frame(&engine->frame, time);
		if (engine->sync)
			engine->sync(engine, syn);
		engine->frame.ncreate = 0;
		engine->frame.nmodify = 0;
		engine->frame.ndestroy = 0;
	}
}

void touch_engine_init(struct touch_engine *engine,
		       void (*event)(struct touch_engine *engine,
				     const struct input_event *ev),
		       void (*sync)(struct touch_engine *engine,
				    const struct input_event *ev),
		       void *priv)
{
	memset(engine, 0, sizeof(*engine));
	engine->event = event;
	engine->sync = sync;
	engine->priv = priv;
}

void touch_engine_attach(struct touch_engine *engine, struct touch_dev *dev)
{
	dev->priv = engine;
	dev->event = tp_event;
	dev->create = tp_create;
	dev->modify = tp_modify;
	dev->destroy = tp_destroy;
	dev->sync = tp_sync;
}

void touch_engine_detach(struct touch_engine *engine, struct touch_dev *dev)
{
	dev->sync = 0;
	dev->destroy = 0;
	dev->modify = 0;
	dev->create = 0;
	dev->event = 0;
	dev->priv = 0;
}