summaryrefslogtreecommitdiff
path: root/src/grail-api.c
blob: e3b8125810bff11669773c29139be05ad9f5ffef (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "grail-inserter.h"
#include "grail-recognizer.h"
#include <grail.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <errno.h>
#include "evbuf.h"

static const pthread_mutex_t c_mutex =	PTHREAD_MUTEX_INITIALIZER;
static const pthread_cond_t c_wait = PTHREAD_COND_INITIALIZER;

struct grail_impl {
	struct touch_dev dev;
	struct touch_engine engine;
	struct evbuf evbuf;
	int fd;
	int pull, finished;
	pthread_mutex_t mutex;
	pthread_cond_t wait;
	pthread_t thread;
};

/*
 * While not finished, lock, wait for condition, extract the running
 * state, unlock, then process the event queue.
 *
 * The processing may in principle take seconds to complete without
 * locking up the writer side.
 */
static void *grail_thread(void *priv)
{
	struct grail_impl *x = priv;
	int pull, finished = 0;
	sigset_t block;
	sigemptyset(&block);
	sigaddset(&block, SIGIO);
	pthread_sigmask(SIG_BLOCK, &block, NULL);
	while (!finished) {
		pthread_mutex_lock(&x->mutex);
		pthread_cond_wait(&x->wait, &x->mutex);
		pull = x->pull;
		finished = x->finished;
		x->pull = 0;
		pthread_mutex_unlock(&x->mutex);
		if (!finished && pull)
			touch_dev_process(&x->dev);
	}
	pthread_exit(NULL);
}

static void tp_event(struct touch_engine *engine,
		     const struct input_event *ev)
{
	struct grail *ge = engine->priv;
	struct grail_impl *x = ge->impl;
	evbuf_put(&x->evbuf, ev);
}

static void tp_sync(struct touch_engine *engine,
		    const struct input_event *syn)
{
	struct input_event ev;
	struct grail *ge = engine->priv;
	struct grail_impl *x = ge->impl;
	struct touch_frame *frame = &engine->frame;
	grail_mask_t filtered[DIM_EV_TYPE_BYTES];
	int nevent = 0;
	gin_frame_begin(ge, frame->time);
	gru_recognize(ge, frame);
	gin_frame_end(ge, filtered);
	if (!ge->event) {
		evbuf_clear(&x->evbuf);
		return;
	}
	while (!evbuf_empty(&x->evbuf)) {
		evbuf_get(&x->evbuf, &ev);
		if (grail_get_mask(filtered, ev.type))
			continue;
		ge->event(ge, &ev);
		nevent++;
	}
	if (nevent)
		ge->event(ge, syn);
}

int grail_open(struct grail *ge, int fd)
{
	struct grail_impl *x;
	int ret;
	x = calloc(1, sizeof(*x));
	if (!x)
		return -ENOMEM;
	ret = gin_init(ge);
	if (ret)
		goto freemem;
	ret = gru_init(ge);
	if (ret)
		goto freedev;
	ret = touch_dev_open(&x->dev, fd);
	if (ret)
		goto freegru;
	x->fd = fd;
	x->mutex = c_mutex;
	x->wait = c_wait;
	ret = pthread_create(&x->thread, NULL, grail_thread, x);
	if (ret)
		goto freetouch;
	ge->impl = x;
	touch_engine_init(&x->engine, tp_event, tp_sync, ge);
	touch_engine_attach(&x->engine, &x->dev);
	return 0;
 freetouch:
	touch_dev_close(&x->dev, fd);
 freegru:
	gru_destroy(ge);
 freedev:
	gin_destroy(ge);
 freemem:
	free(x);
	return ret;
}

void grail_close(struct grail *ge, int fd)
{
	struct grail_impl *x = ge->impl;
	void *status;
	pthread_mutex_lock(&x->mutex);
	x->finished = 1;
	pthread_cond_signal (&x->wait);
	pthread_mutex_unlock(&x->mutex);
	touch_engine_detach(&x->engine, &x->dev);
	touch_dev_close(&x->dev, fd);
	gru_destroy(ge);
	gin_destroy(ge);
	free(ge->impl);
	ge->impl = 0;
}

int grail_idle(struct grail *ge, int fd, int ms)
{
	struct grail_impl *x = ge->impl;
	int ret;
	pthread_mutex_lock(&x->mutex);
	ret = touch_dev_idle(&x->dev, fd, ms);
	pthread_mutex_unlock(&x->mutex);
	return ret;
}

/*
 * First extract all input events to a lockless circular buffer, then,
 * under lock, signal the worker thread.
 *
 * This function only blocks temporarily while signalling the worker
 * thread.
 */
int grail_pull(struct grail *ge, int fd)
{
	struct grail_impl *x = ge->impl;
	int finished = touch_dev_fetch(&x->dev, fd) < 0;
	pthread_mutex_lock(&x->mutex);
	x->pull = 1;
	x->finished = finished;
	pthread_cond_signal(&x->wait);
	pthread_mutex_unlock(&x->mutex);
	return 1;
}