summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mtdev-plumbing.h105
-rw-r--r--include/mtdev.h99
-rw-r--r--src/common.h1
-rw-r--r--src/core.c12
-rw-r--r--src/evbuf.h5
-rw-r--r--src/iobuf.c37
-rw-r--r--test/mtdev.c9
7 files changed, 148 insertions, 120 deletions
diff --git a/include/mtdev-plumbing.h b/include/mtdev-plumbing.h
new file mode 100644
index 0000000..b610873
--- /dev/null
+++ b/include/mtdev-plumbing.h
@@ -0,0 +1,105 @@
1/*****************************************************************************
2 *
3 * mtdev - MT device event converter (MIT license)
4 *
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 ****************************************************************************/
28
29#ifndef _MTDEV_PLUMBING_H
30#define _MTDEV_PLUMBING_H
31
32#include <mtdev.h>
33
34/**
35 * mtdev_init - initialize mtdev converter
36 * @dev: the mtdev to initialize
37 *
38 * Sets up the internal data structures.
39 *
40 * Returns zero on success, negative error number otherwise.
41 */
42int mtdev_init(struct mtdev *dev);
43
44/**
45 * mtdev_configure - configure the mtdev converter
46 * @dev: the mtdev to configure
47 * @fd: file descriptor of the kernel device
48 *
49 * Reads the device properties to set up the protocol capabilities.
50 * If preferred, this can be done by hand, omitting this call.
51 *
52 * Returns zero on success, negative error number otherwise.
53 */
54int mtdev_configure(struct mtdev *dev, int fd);
55
56/**
57 * mtdev_fetch_event - fetch an event from the kernel device
58 * @dev: the mtdev in use
59 * @fd: file descriptor of the kernel device
60 * @ev: the kernel input event to fill
61 *
62 * Fetch a kernel event from the kernel device. The read operation
63 * behaves as dictated by the file descriptor; if O_NONBLOCK is not
64 * set, the read will block until an event is available.
65 *
66 * On success, returns the number of events read (0 or 1). Otherwise,
67 * a standard negative error number is returned.
68 */
69int mtdev_fetch_event(struct mtdev *dev, int fd, struct input_event *ev);
70
71/**
72 * mtdev_put_event - put an event into the converter
73 * @dev: the mtdev in use
74 * @ev: the kernel input event to put
75 *
76 * Put a kernel event into the mtdev converter. The event should
77 * come straight from the device.
78 *
79 * This call does not block; if the buffer becomes full, older events
80 * are dropped. The buffer is guaranteed to handle several complete MT
81 * packets.
82 */
83void mtdev_put_event(struct mtdev *dev, const struct input_event *ev);
84
85/**
86 * mtdev_empty - check if there are events to get
87 * @dev: the mtdev in use
88 *
89 * Returns true if the processed event queue is empty, false otherwise.
90 */
91int mtdev_empty(struct mtdev *dev);
92
93/**
94 * mtdev_get_event - get processed events from mtdev
95 * @dev: the mtdev in use
96 * @ev: the input event to fill
97 *
98 * Get a processed event from mtdev. The events appear as if they came
99 * from a type B device emitting MT slot events.
100 *
101 * The queue must be non-empty before calling this function.
102 */
103void mtdev_get_event(struct mtdev *dev, struct input_event* ev);
104
105#endif
diff --git a/include/mtdev.h b/include/mtdev.h
index 98e022a..b759803 100644
--- a/include/mtdev.h
+++ b/include/mtdev.h
@@ -109,28 +109,6 @@ struct mtdev {
109}; 109};
110 110
111/** 111/**
112 * mtdev_init - initialize mtdev converter
113 * @dev: the mtdev to initialize
114 *
115 * Sets up the internal data structures.
116 *
117 * Returns zero on success, negative error number otherwise.
118 */
119int mtdev_init(struct mtdev *dev);
120
121/**
122 * mtdev_configure - configure the mtdev converter
123 * @dev: the mtdev to configure
124 * @fd: file descriptor of the kernel device
125 *
126 * Reads the device properties to set up the protocol capabilities.
127 * If preferred, this can be done by hand, omitting this call.
128 *
129 * Returns zero on success, negative error number otherwise.
130 */
131int mtdev_configure(struct mtdev *dev, int fd);
132
133/**
134 * mtdev_open - open an mtdev converter 112 * mtdev_open - open an mtdev converter
135 * @dev: the mtdev to open 113 * @dev: the mtdev to open
136 * @fd: file descriptor of the kernel device 114 * @fd: file descriptor of the kernel device
@@ -140,8 +118,8 @@ int mtdev_configure(struct mtdev *dev, int fd);
140 * 118 *
141 * Returns zero on success, negative error number otherwise. 119 * Returns zero on success, negative error number otherwise.
142 * 120 *
143 * This call combines mtdev_init() and mtdev_configure(), which 121 * This call combines the plumbing functions mtdev_init() and
144 * may be used separately instead. 122 * mtdev_configure().
145 */ 123 */
146int mtdev_open(struct mtdev *dev, int fd); 124int mtdev_open(struct mtdev *dev, int fd);
147 125
@@ -157,73 +135,26 @@ int mtdev_open(struct mtdev *dev, int fd);
157int mtdev_idle(struct mtdev *dev, int fd, int ms); 135int mtdev_idle(struct mtdev *dev, int fd, int ms);
158 136
159/** 137/**
160 * mtdev_fetch - fetch an event from the kernel device 138 * mtdev_get - get processed events from mtdev
161 * @dev: the mtdev in use
162 * @fd: file descriptor of the kernel device
163 * @ev: the kernel input event to fill
164 *
165 * Fetch a kernel event from the kernel device. The read operation
166 * behaves as dictated by the file descriptor; if O_NONBLOCK is not
167 * set, the read will block until an event is available.
168 *
169 * On success, returns the number of events read. Otherwise, a standard
170 * negative error number is returned.
171 */
172int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev);
173
174/**
175 * mtdev_put - put an event into the converter
176 * @dev: the mtdev in use
177 * @ev: the kernel input event to put
178 *
179 * Put a kernel event into the mtdev converter. The event should
180 * come straight from the device.
181 *
182 * This call does not block; if the buffer becomes full, older events
183 * are dropped. The buffer is guaranteed to handle several complete MT
184 * packets.
185 */
186void mtdev_put(struct mtdev *dev, const struct input_event *ev);
187
188/**
189 * mtdev_pull - pull events from the kernel device
190 * @dev: the mtdev in use 139 * @dev: the mtdev in use
191 * @fd: file descriptor of the kernel device 140 * @fd: file descriptor of the kernel device
192 * @max_events: max number of events to read (zero for all) 141 * @ev: array of input events to fill
193 * 142 * @ev_max: maximum number of events to read
194 * Read a maxmimum of max_events events from the device, and put them
195 * in the converter. If max_events is zero, all available events will
196 * be read. The read operation behaves as dictated by the file
197 * descriptor; if O_NONBLOCK is not set, the read will block until
198 * max_events events are available or the buffer is full.
199 * 143 *
200 * On success, returns the number of events read. Otherwise, a standard 144 * Get a processed event from mtdev. The events appear as if they came
201 * negative error number is returned. 145 * from a type B device emitting MT slot events.
202 *
203 * This call combines mtdev_fetch() with mtdev_put(), which
204 * may be used separately instead.
205 */
206int mtdev_pull(struct mtdev *dev, int fd, int max_events);
207
208/**
209 * mtdev_empty - check if there are events to get
210 * @dev: the mtdev in use
211 * 146 *
212 * Returns true if the event queue is empty, false otherwise. 147 * The read operations involved behave as dictated by the file
213 */ 148 * descriptor; if O_NONBLOCK is not set, mtdev_get() will block until
214int mtdev_empty(struct mtdev *dev); 149 * the specified number of processed events are available.
215
216/**
217 * mtdev_get - get canonical events from mtdev
218 * @dev: the mtdev in use
219 * @ev: the input event to fill
220 * 150 *
221 * Get a canonical event from mtdev. The events appear as if they came 151 * On success, returns the number of events read. Otherwise,
222 * from a type B device emitting MT slot events. 152 * a standard negative error number is returned.
223 * 153 *
224 * The queue must be non-empty before calling this function. 154 * This call combines the plumbing functions mtdev_fetch_event(),
155 * mtdev_put_event() and mtdev_get_event().
225 */ 156 */
226void mtdev_get(struct mtdev *dev, struct input_event* ev); 157int mtdev_get(struct mtdev *dev, int fd, struct input_event* ev, int ev_max);
227 158
228/** 159/**
229 * mtdev_close - close the mtdev converter 160 * mtdev_close - close the mtdev converter
diff --git a/src/common.h b/src/common.h
index 03bc7bd..57d47a0 100644
--- a/src/common.h
+++ b/src/common.h
@@ -30,6 +30,7 @@
30#define COMMON_H 30#define COMMON_H
31 31
32#include <mtdev-mapping.h> 32#include <mtdev-mapping.h>
33#include <mtdev-plumbing.h>
33#include <malloc.h> 34#include <malloc.h>
34#include <string.h> 35#include <string.h>
35#include <errno.h> 36#include <errno.h>
diff --git a/src/core.c b/src/core.c
index 3df8257..8df2477 100644
--- a/src/core.c
+++ b/src/core.c
@@ -360,7 +360,7 @@ int mtdev_open(struct mtdev *dev, int fd)
360 return ret; 360 return ret;
361} 361}
362 362
363void mtdev_put(struct mtdev *dev, const struct input_event *ev) 363void mtdev_put_event(struct mtdev *dev, const struct input_event *ev)
364{ 364{
365 struct mtdev_state *state = dev->state; 365 struct mtdev_state *state = dev->state;
366 if (ev->type == EV_SYN && ev->code == SYN_REPORT) { 366 if (ev->type == EV_SYN && ev->code == SYN_REPORT) {
@@ -376,16 +376,6 @@ void mtdev_put(struct mtdev *dev, const struct input_event *ev)
376 } 376 }
377} 377}
378 378
379int mtdev_empty(struct mtdev *dev)
380{
381 return evbuf_empty(&dev->state->outbuf);
382}
383
384void mtdev_get(struct mtdev *dev, struct input_event* ev)
385{
386 evbuf_get(&dev->state->outbuf, ev);
387}
388
389void mtdev_close(struct mtdev *dev) 379void mtdev_close(struct mtdev *dev)
390{ 380{
391 free(dev->state); 381 free(dev->state);
diff --git a/src/evbuf.h b/src/evbuf.h
index ba61cd5..1afd9f5 100644
--- a/src/evbuf.h
+++ b/src/evbuf.h
@@ -42,11 +42,6 @@ static inline int evbuf_empty(const struct mtdev_evbuf *evbuf)
42 return evbuf->head == evbuf->tail; 42 return evbuf->head == evbuf->tail;
43} 43}
44 44
45static inline int evbuf_full(const struct mtdev_evbuf *evbuf)
46{
47 return ((evbuf->head + 1) & (DIM_EVENTS - 1)) == evbuf->tail;
48}
49
50static inline void evbuf_put(struct mtdev_evbuf *evbuf, 45static inline void evbuf_put(struct mtdev_evbuf *evbuf,
51 const struct input_event *ev) 46 const struct input_event *ev)
52{ 47{
diff --git a/src/iobuf.c b/src/iobuf.c
index 050f2ed..85a9f19 100644
--- a/src/iobuf.c
+++ b/src/iobuf.c
@@ -37,7 +37,7 @@ int mtdev_idle(struct mtdev *dev, int fd, int ms)
37 return buf->head == buf->tail && poll(&fds, 1, ms) <= 0; 37 return buf->head == buf->tail && poll(&fds, 1, ms) <= 0;
38} 38}
39 39
40int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev) 40int mtdev_fetch_event(struct mtdev *dev, int fd, struct input_event *ev)
41{ 41{
42 struct mtdev_iobuf *buf = &dev->state->iobuf; 42 struct mtdev_iobuf *buf = &dev->state->iobuf;
43 int n = buf->head - buf->tail; 43 int n = buf->head - buf->tail;
@@ -59,21 +59,30 @@ int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev)
59 return 1; 59 return 1;
60} 60}
61 61
62int mtdev_pull(struct mtdev *dev, int fd, int max_events) 62int mtdev_empty(struct mtdev *dev)
63{ 63{
64 struct mtdev_state *state = dev->state; 64 return evbuf_empty(&dev->state->outbuf);
65 struct input_event ev; 65}
66
67void mtdev_get_event(struct mtdev *dev, struct input_event* ev)
68{
69 evbuf_get(&dev->state->outbuf, ev);
70}
71
72int mtdev_get(struct mtdev *dev, int fd, struct input_event* ev, int ev_max)
73{
74 struct input_event kev;
66 int ret, count = 0; 75 int ret, count = 0;
67 if (max_events <= 0) 76 while (count < ev_max) {
68 max_events = DIM_EVENTS; 77 while (mtdev_empty(dev)) {
69 while (max_events-- && !evbuf_full(&state->inbuf)) { 78 ret = mtdev_fetch_event(dev, fd, &kev);
70 ret = mtdev_fetch(dev, fd, &ev); 79 if (ret < 0)
71 if (ret < 0) 80 return ret;
72 return ret; 81 if (ret == 0)
73 if (ret == 0) 82 return count;
74 break; 83 mtdev_put_event(dev, &kev);
75 mtdev_put(dev, &ev); 84 }
76 count++; 85 mtdev_get_event(dev, &ev[count++]);
77 } 86 }
78 return count; 87 return count;
79} 88}
diff --git a/test/mtdev.c b/test/mtdev.c
index 60fae7b..a4858c5 100644
--- a/test/mtdev.c
+++ b/test/mtdev.c
@@ -26,7 +26,7 @@
26 * 26 *
27 ****************************************************************************/ 27 ****************************************************************************/
28 28
29#include <mtdev-mapping.h> 29#include <mtdev.h>
30#include <stdio.h> 30#include <stdio.h>
31#include <fcntl.h> 31#include <fcntl.h>
32 32
@@ -64,11 +64,8 @@ static void loop_device(int fd)
64 } 64 }
65 /* while the device has not been inactive for five seconds */ 65 /* while the device has not been inactive for five seconds */
66 while (!mtdev_idle(&dev, fd, 5000)) { 66 while (!mtdev_idle(&dev, fd, 5000)) {
67 /* fetch all available kernel events */ 67 /* extract all available processed events */
68 mtdev_pull(&dev, fd, 0); 68 while (mtdev_get(&dev, fd, &ev, 1) > 0) {
69 /* extract all available canonical events */
70 while (!mtdev_empty(&dev)) {
71 mtdev_get(&dev, &ev);
72 if (use_event(&ev)) 69 if (use_event(&ev))
73 print_event(&ev); 70 print_event(&ev);
74 } 71 }