summaryrefslogtreecommitdiff
path: root/src/grail-api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/grail-api.c')
-rw-r--r--src/grail-api.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/grail-api.c b/src/grail-api.c
new file mode 100644
index 0000000..9ceb1e7
--- /dev/null
+++ b/src/grail-api.c
@@ -0,0 +1,115 @@
1#include "grail-inserter.h"
2#include "grail-recognizer.h"
3#include <grail.h>
4#include <string.h>
5#include <stdio.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <malloc.h>
9#include <errno.h>
10#include "evbuf.h"
11
12struct grail_impl {
13 struct touch_dev dev;
14 struct touch_engine engine;
15 struct evbuf evbuf;
16};
17
18static void tp_event(struct touch_engine *engine,
19 const struct input_event *ev)
20{
21 struct grail *ge = engine->priv;
22 struct grail_impl *x = ge->impl;
23 evbuf_put(&x->evbuf, ev);
24}
25
26static void tp_sync(struct touch_engine *engine,
27 const struct input_event *syn)
28{
29 struct input_event ev;
30 struct grail *ge = engine->priv;
31 struct grail_impl *x = ge->impl;
32 struct touch_frame *frame = &engine->frame;
33 grail_mask_t filtered[DIM_EV_TYPE_BYTES];
34 int nevent = 0;
35 gin_frame_begin(ge, frame);
36 gru_recognize(ge, frame);
37 gin_frame_end(ge, filtered, sizeof(filtered), frame);
38 if (!ge->event) {
39 evbuf_clear(&x->evbuf);
40 return;
41 }
42 while (!evbuf_empty(&x->evbuf)) {
43 evbuf_get(&x->evbuf, &ev);
44 if (grail_mask_get(filtered, ev.type))
45 continue;
46 ge->event(ge, &ev);
47 nevent++;
48 }
49 if (nevent)
50 ge->event(ge, syn);
51}
52
53int grail_open(struct grail *ge, int fd)
54{
55 struct grail_impl *x;
56 int ret;
57 x = calloc(1, sizeof(*x));
58 if (!x)
59 return -ENOMEM;
60 ge->impl = x;
61 ret = gin_init(ge);
62 if (ret)
63 goto freemem;
64 ret = touch_dev_open(&x->dev, fd);
65 if (ret)
66 goto freegin;
67 ret = gru_init(ge);
68 if (ret)
69 goto freedev;
70 touch_engine_init(&x->engine, tp_event, tp_sync, ge);
71 touch_engine_attach(&x->engine, &x->dev);
72 return 0;
73 freedev:
74 touch_dev_close(&x->dev, fd);
75 freegin:
76 gin_destroy(ge);
77 freemem:
78 free(x);
79 ge->impl = 0;
80 return ret;
81}
82
83void grail_close(struct grail *ge, int fd)
84{
85 struct grail_impl *x = ge->impl;
86 void *status;
87 touch_engine_detach(&x->engine, &x->dev);
88 gru_destroy(ge);
89 touch_dev_close(&x->dev, fd);
90 gin_destroy(ge);
91 free(ge->impl);
92 ge->impl = 0;
93}
94
95int grail_idle(struct grail *ge, int fd, int ms)
96{
97 struct grail_impl *x = ge->impl;
98 return touch_dev_idle(&x->dev, fd, ms);
99}
100
101int grail_pull(struct grail *ge, int fd)
102{
103 struct grail_impl *x = ge->impl;
104 return touch_dev_pull(&x->dev, fd);
105}
106
107void grail_get_units(struct grail_coord *min, struct grail_coord *max,
108 const struct grail *ge)
109{
110 const struct touch_dev_caps *caps = &ge->impl->dev.caps;
111 min->x = caps->info[TP_POS_X].minimum;
112 min->y = caps->info[TP_POS_Y].minimum;
113 max->x = caps->info[TP_POS_X].maximum;
114 max->y = caps->info[TP_POS_Y].maximum;
115}