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

static void tp_event(struct touch_dev *dev, const struct input_event *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) {
		for (int 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)
{
	int last = -1;
	frame->ntouch = 0;
	frame->slot = -1;
	for (int 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++;
		}
	}
}

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)
		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)
		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)
		update_frame(&engine->frame, slot, 0, 0, 0);
}

static void tp_sync(struct touch_dev *dev, touch_time_t time)
{
	struct touch_engine *engine = dev->priv;
	if (engine) {
		finalize_frame(&engine->frame);
		if (engine->sync)
			engine->sync(engine, time);
	}
}

void touch_engine_init(struct touch_engine *engine,
		       void (*sync)(struct touch_engine *engine,
				    touch_time_t time),
		       void *priv)
{
	memset(engine, 0, sizeof(*engine));
	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;
}