summaryrefslogtreecommitdiff
path: root/src/touch-engine.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/touch-engine.c')
-rw-r--r--src/touch-engine.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/touch-engine.c b/src/touch-engine.c
new file mode 100644
index 0000000..3e6870e
--- /dev/null
+++ b/src/touch-engine.c
@@ -0,0 +1,137 @@
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-touch.h>
26#include <string.h>
27
28static void tp_event(struct touch_dev *dev, const struct input_event *ev)
29{
30 struct touch_engine *engine = dev->priv;
31 if (engine && engine->event)
32 engine->event(engine, ev);
33}
34
35static void update_frame(struct touch_frame *frame,
36 int slot, int active,
37 const grail_mask_t *mask,
38 const touch_prop_t *prop)
39{
40 struct touch *t = &frame->touch[slot];
41 int i;
42 t->active = active;
43 grail_mask_modify(frame->touches, slot, active);
44 if (mask && prop)
45 grail_mask_foreach(i, mask, DIM_TOUCH_PROP_BYTES)
46 t->prop[i] = prop[i];
47}
48
49static void finalize_frame(struct touch_frame *frame, touch_time_t time)
50{
51 int i, nslot = 0;
52 grail_mask_foreach(i, frame->touches, DIM_TOUCH_BYTES)
53 frame->active[nslot++] = &frame->touch[i];
54 frame->nactive = nslot;
55 frame->time = time;
56}
57
58static void tp_create(struct touch_dev *dev, int slot,
59 const grail_mask_t *mask, const touch_prop_t *prop)
60{
61 struct touch_engine *engine = dev->priv;
62 if (engine) {
63 engine->frame.ncreate++;
64 update_frame(&engine->frame, slot, 1, mask, prop);
65 }
66}
67
68static void tp_modify(struct touch_dev *dev, int slot,
69 const grail_mask_t *mask, const touch_prop_t *prop)
70{
71 struct touch_engine *engine = dev->priv;
72 if (engine) {
73 engine->frame.nmodify++;
74 update_frame(&engine->frame, slot, 1, mask, prop);
75 }
76}
77
78static void tp_destroy(struct touch_dev *dev, int slot)
79{
80 struct touch_engine *engine = dev->priv;
81 if (engine) {
82 engine->frame.ndestroy++;
83 update_frame(&engine->frame, slot, 0, 0, 0);
84 }
85}
86
87static void tp_sync(struct touch_dev *dev, const struct input_event *syn)
88{
89 static const touch_time_t ms = 1000;
90 struct touch_engine *engine = dev->priv;
91 touch_time_t time = syn->time.tv_usec / ms + syn->time.tv_sec * ms;
92 if (engine) {
93 finalize_frame(&engine->frame, time);
94 if (engine->sync)
95 engine->sync(engine, syn);
96 engine->frame.ncreate = 0;
97 engine->frame.nmodify = 0;
98 engine->frame.ndestroy = 0;
99 }
100}
101
102void touch_engine_init(struct touch_engine *engine,
103 void (*event)(struct touch_engine *engine,
104 const struct input_event *ev),
105 void (*sync)(struct touch_engine *engine,
106 const struct input_event *ev),
107 void *priv)
108{
109 int i;
110 memset(engine, 0, sizeof(*engine));
111 for (i = 0; i < DIM_TOUCH; i++)
112 engine->frame.touch[i].slot = i;
113 engine->event = event;
114 engine->sync = sync;
115 engine->priv = priv;
116}
117
118void touch_engine_attach(struct touch_engine *engine, struct touch_dev *dev)
119{
120 dev->priv = engine;
121 dev->event = tp_event;
122 dev->create = tp_create;
123 dev->modify = tp_modify;
124 dev->destroy = tp_destroy;
125 dev->sync = tp_sync;
126}
127
128void touch_engine_detach(struct touch_engine *engine, struct touch_dev *dev)
129{
130 dev->sync = 0;
131 dev->destroy = 0;
132 dev->modify = 0;
133 dev->create = 0;
134 dev->event = 0;
135 dev->priv = 0;
136}
137