summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/grail-api.c70
-rw-r--r--src/touch-dev.c35
3 files changed, 5 insertions, 102 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 932c8ae..3b001b2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,7 +23,7 @@ libgrail_la_SOURCES = \
23 grail-recognizer.h \ 23 grail-recognizer.h \
24 grail-api.c 24 grail-api.c
25 25
26AM_CFLAGS = $(CWARNFLAGS) -pthread 26AM_CFLAGS = $(CWARNFLAGS)
27 27
28INCLUDES = -I$(top_srcdir)/include/ 28INCLUDES = -I$(top_srcdir)/include/
29 29
diff --git a/src/grail-api.c b/src/grail-api.c
index 2c6c438..9ceb1e7 100644
--- a/src/grail-api.c
+++ b/src/grail-api.c
@@ -1,8 +1,6 @@
1#include "grail-inserter.h" 1#include "grail-inserter.h"
2#include "grail-recognizer.h" 2#include "grail-recognizer.h"
3#include <grail.h> 3#include <grail.h>
4#include <pthread.h>
5#include <signal.h>
6#include <string.h> 4#include <string.h>
7#include <stdio.h> 5#include <stdio.h>
8#include <unistd.h> 6#include <unistd.h>
@@ -11,48 +9,12 @@
11#include <errno.h> 9#include <errno.h>
12#include "evbuf.h" 10#include "evbuf.h"
13 11
14static const pthread_mutex_t c_mutex = PTHREAD_MUTEX_INITIALIZER;
15static const pthread_cond_t c_wait = PTHREAD_COND_INITIALIZER;
16
17struct grail_impl { 12struct grail_impl {
18 struct touch_dev dev; 13 struct touch_dev dev;
19 struct touch_engine engine; 14 struct touch_engine engine;
20 struct evbuf evbuf; 15 struct evbuf evbuf;
21 int fd;
22 int pull, finished;
23 pthread_mutex_t mutex;
24 pthread_cond_t wait;
25 pthread_t thread;
26}; 16};
27 17
28/*
29 * While not finished, lock, wait for condition, extract the running
30 * state, unlock, then process the event queue.
31 *
32 * The processing may in principle take seconds to complete without
33 * locking up the writer side.
34 */
35static void *grail_thread(void *priv)
36{
37 struct grail_impl *x = priv;
38 int pull, finished = 0;
39 sigset_t block;
40 sigemptyset(&block);
41 sigaddset(&block, SIGIO);
42 pthread_sigmask(SIG_BLOCK, &block, NULL);
43 while (!finished) {
44 pthread_mutex_lock(&x->mutex);
45 pthread_cond_wait(&x->wait, &x->mutex);
46 pull = x->pull;
47 finished = x->finished;
48 x->pull = 0;
49 if (!finished && pull)
50 touch_dev_process(&x->dev);
51 pthread_mutex_unlock(&x->mutex);
52 }
53 pthread_exit(NULL);
54}
55
56static void tp_event(struct touch_engine *engine, 18static void tp_event(struct touch_engine *engine,
57 const struct input_event *ev) 19 const struct input_event *ev)
58{ 20{
@@ -105,17 +67,9 @@ int grail_open(struct grail *ge, int fd)
105 ret = gru_init(ge); 67 ret = gru_init(ge);
106 if (ret) 68 if (ret)
107 goto freedev; 69 goto freedev;
108 x->fd = fd;
109 x->mutex = c_mutex;
110 x->wait = c_wait;
111 ret = pthread_create(&x->thread, NULL, grail_thread, x);
112 if (ret)
113 goto freegru;
114 touch_engine_init(&x->engine, tp_event, tp_sync, ge); 70 touch_engine_init(&x->engine, tp_event, tp_sync, ge);
115 touch_engine_attach(&x->engine, &x->dev); 71 touch_engine_attach(&x->engine, &x->dev);
116 return 0; 72 return 0;
117 freegru:
118 gru_destroy(ge);
119 freedev: 73 freedev:
120 touch_dev_close(&x->dev, fd); 74 touch_dev_close(&x->dev, fd);
121 freegin: 75 freegin:
@@ -130,10 +84,6 @@ void grail_close(struct grail *ge, int fd)
130{ 84{
131 struct grail_impl *x = ge->impl; 85 struct grail_impl *x = ge->impl;
132 void *status; 86 void *status;
133 pthread_mutex_lock(&x->mutex);
134 x->finished = 1;
135 pthread_cond_signal (&x->wait);
136 pthread_mutex_unlock(&x->mutex);
137 touch_engine_detach(&x->engine, &x->dev); 87 touch_engine_detach(&x->engine, &x->dev);
138 gru_destroy(ge); 88 gru_destroy(ge);
139 touch_dev_close(&x->dev, fd); 89 touch_dev_close(&x->dev, fd);
@@ -145,29 +95,13 @@ void grail_close(struct grail *ge, int fd)
145int grail_idle(struct grail *ge, int fd, int ms) 95int grail_idle(struct grail *ge, int fd, int ms)
146{ 96{
147 struct grail_impl *x = ge->impl; 97 struct grail_impl *x = ge->impl;
148 int ret; 98 return touch_dev_idle(&x->dev, fd, ms);
149 pthread_mutex_lock(&x->mutex);
150 ret = touch_dev_idle(&x->dev, fd, ms);
151 pthread_mutex_unlock(&x->mutex);
152 return ret;
153} 99}
154 100
155/*
156 * First extract all input events to a lockless circular buffer, then,
157 * under lock, signal the worker thread.
158 *
159 * This function only blocks temporarily while signalling the worker
160 * thread.
161 */
162int grail_pull(struct grail *ge, int fd) 101int grail_pull(struct grail *ge, int fd)
163{ 102{
164 struct grail_impl *x = ge->impl; 103 struct grail_impl *x = ge->impl;
165 pthread_mutex_lock(&x->mutex); 104 return touch_dev_pull(&x->dev, fd);
166 x->pull = 1;
167 x->finished = touch_dev_fetch(&x->dev, fd) < 0;
168 pthread_cond_signal(&x->wait);
169 pthread_mutex_unlock(&x->mutex);
170 return 1;
171} 105}
172 106
173void grail_get_units(struct grail_coord *min, struct grail_coord *max, 107void grail_get_units(struct grail_coord *min, struct grail_coord *max,
diff --git a/src/touch-dev.c b/src/touch-dev.c
index b327266..4cc8d55 100644
--- a/src/touch-dev.c
+++ b/src/touch-dev.c
@@ -26,7 +26,6 @@
26#include <malloc.h> 26#include <malloc.h>
27#include <string.h> 27#include <string.h>
28#include <errno.h> 28#include <errno.h>
29#include "evbuf.h"
30 29
31/* from mtdev-mapping.h */ 30/* from mtdev-mapping.h */
32#define MT_TOUCH_MAJOR 0 31#define MT_TOUCH_MAJOR 0
@@ -50,7 +49,6 @@ struct touch_dev_impl {
50 int status; 49 int status;
51 grail_mask_t mask[DIM_TOUCH_BYTES]; 50 grail_mask_t mask[DIM_TOUCH_BYTES];
52 touch_prop_t prop[DIM_TOUCH_PROP]; 51 touch_prop_t prop[DIM_TOUCH_PROP];
53 struct evbuf evbuf;
54 struct mtdev mtdev; 52 struct mtdev mtdev;
55}; 53};
56 54
@@ -82,23 +80,6 @@ static void set_caps(struct touch_dev_caps *caps,
82 set_info(caps, TP_PRESSURE, &mtcaps->abs[MT_PRESSURE]); 80 set_info(caps, TP_PRESSURE, &mtcaps->abs[MT_PRESSURE]);
83} 81}
84 82
85static int touch_get_fetched(struct touch_dev_impl *x,
86 struct input_event* ev, int ev_max)
87{
88 struct input_event kev;
89 int count = 0;
90 while (count < ev_max) {
91 while (mtdev_empty(&x->mtdev)) {
92 if (evbuf_empty(&x->evbuf))
93 return count;
94 evbuf_get(&x->evbuf, &kev);
95 mtdev_put_event(&x->mtdev, &kev);
96 }
97 mtdev_get_event(&x->mtdev, &ev[count++]);
98 }
99 return count;
100}
101
102static inline void set_prop(struct touch_dev_impl *x, int code, int value) 83static inline void set_prop(struct touch_dev_impl *x, int code, int value)
103{ 84{
104 grail_mask_set(x->mask, code); 85 grail_mask_set(x->mask, code);
@@ -207,24 +188,12 @@ int touch_dev_idle(struct touch_dev *dev, int fd, int ms)
207 return mtdev_idle(&dev->impl->mtdev, fd, ms); 188 return mtdev_idle(&dev->impl->mtdev, fd, ms);
208} 189}
209 190
210int touch_dev_fetch(struct touch_dev *dev, int fd) 191int touch_dev_pull(struct touch_dev *dev, int fd)
211{
212 struct touch_dev_impl *x = dev->impl;
213 struct input_event ev;
214 int ret, count = 0;
215 while ((ret = mtdev_fetch_event(&x->mtdev, fd, &ev)) > 0) {
216 evbuf_put(&x->evbuf, &ev);
217 count++;
218 }
219 return count > 0 ? count : ret;
220}
221
222int touch_dev_process(struct touch_dev *dev)
223{ 192{
224 struct touch_dev_impl *x = dev->impl; 193 struct touch_dev_impl *x = dev->impl;
225 struct input_event ev; 194 struct input_event ev;
226 int ret, count = 0, consumed; 195 int ret, count = 0, consumed;
227 while ((ret = touch_get_fetched(x, &ev, 1)) > 0) { 196 while ((ret = mtdev_get(&x->mtdev, fd, &ev, 1)) > 0) {
228 consumed = 0; 197 consumed = 0;
229 if (ev.type == EV_SYN) { 198 if (ev.type == EV_SYN) {
230 if (ev.code == SYN_REPORT) 199 if (ev.code == SYN_REPORT)