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.c70
1 files changed, 2 insertions, 68 deletions
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,