1 /*****************************************************************************
2  *
3  * mtdev - Multitouch Protocol Translation Library (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 #include "iobuf.h"
30 #include "state.h"
31 #include <unistd.h>
32 #include <poll.h>
33 
mtdev_idle(struct mtdev * dev,int fd,int ms)34 int mtdev_idle(struct mtdev *dev, int fd, int ms)
35 {
36 	struct mtdev_iobuf *buf = &dev->state->iobuf;
37         struct pollfd fds = { fd, POLLIN, 0 };
38 	return	buf->head == buf->tail && poll(&fds, 1, ms) <= 0;
39 }
40 
mtdev_fetch_event(struct mtdev * dev,int fd,struct input_event * ev)41 int mtdev_fetch_event(struct mtdev *dev, int fd, struct input_event *ev)
42 {
43 	struct mtdev_iobuf *buf = &dev->state->iobuf;
44 	int n = buf->head - buf->tail;
45 	if (n < EVENT_SIZE) {
46 		if (buf->tail && n > 0)
47 			memmove(buf->data, buf->data + buf->tail, n);
48 		buf->head = n;
49 		buf->tail = 0;
50 		SYSCALL(n = read(fd, buf->data + buf->head,
51 				 DIM_BUFFER - buf->head));
52 		if (n <= 0)
53 			return n;
54 		buf->head += n;
55 	}
56 	if (buf->head - buf->tail < EVENT_SIZE)
57 		return 0;
58 	memcpy(ev, buf->data + buf->tail, EVENT_SIZE);
59 	buf->tail += EVENT_SIZE;
60 	return 1;
61 }
62 
mtdev_empty(struct mtdev * dev)63 int mtdev_empty(struct mtdev *dev)
64 {
65 	return evbuf_empty(&dev->state->outbuf);
66 }
67 
mtdev_get_event(struct mtdev * dev,struct input_event * ev)68 void mtdev_get_event(struct mtdev *dev, struct input_event* ev)
69 {
70 	evbuf_get(&dev->state->outbuf, ev);
71 }
72 
mtdev_get(struct mtdev * dev,int fd,struct input_event * ev,int ev_max)73 int mtdev_get(struct mtdev *dev, int fd, struct input_event* ev, int ev_max)
74 {
75 	struct input_event kev;
76 	int ret, count = 0;
77 	while (count < ev_max) {
78 		while (mtdev_empty(dev)) {
79 			ret = mtdev_fetch_event(dev, fd, &kev);
80 			if (ret <= 0)
81 				return count > 0 ? count : ret;
82 			mtdev_put_event(dev, &kev);
83 		}
84 		mtdev_get_event(dev, &ev[count++]);
85 	}
86 	return count;
87 }
88