diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2010-06-17 18:12:58 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2010-06-17 18:12:58 +0200 |
| commit | 66e5de9eaefc33ffa6af3617f9ec7a50f10af50d (patch) | |
| tree | 66535311e0b9c422b30341237fd596fb9ebbdd75 /src/iobuf.c | |
Initial load of mtdev project
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/iobuf.c')
| -rw-r--r-- | src/iobuf.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/iobuf.c b/src/iobuf.c new file mode 100644 index 0000000..6d238ec --- /dev/null +++ b/src/iobuf.c | |||
| @@ -0,0 +1,69 @@ | |||
| 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 | #include "iobuf.h" | ||
| 30 | #include "state.h" | ||
| 31 | |||
| 32 | int mtdev_fetch(struct mtdev *dev, struct input_event *ev, int fd) | ||
| 33 | { | ||
| 34 | struct mtdev_iobuf *buf = &dev->state->iobuf; | ||
| 35 | int n = buf->head - buf->tail; | ||
| 36 | if (n < EVENT_SIZE) { | ||
| 37 | if (buf->tail && n > 0) | ||
| 38 | memmove(buf->data, buf->data + buf->tail, n); | ||
| 39 | buf->head = n; | ||
| 40 | buf->tail = 0; | ||
| 41 | SYSCALL(n = read(fd, buf->data + buf->head, | ||
| 42 | DIM_BUFFER - buf->head)); | ||
| 43 | if (n <= 0) | ||
| 44 | return n; | ||
| 45 | buf->head += n; | ||
| 46 | } | ||
| 47 | if (buf->head - buf->tail < EVENT_SIZE) | ||
| 48 | return 0; | ||
| 49 | memcpy(ev, buf->data + buf->tail, EVENT_SIZE); | ||
| 50 | buf->tail += EVENT_SIZE; | ||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | int mtdev_pull(struct mtdev *dev, int fd, int max_events) | ||
| 55 | { | ||
| 56 | struct mtdev_state *state = dev->state; | ||
| 57 | struct input_event ev; | ||
| 58 | int ret, count = 0; | ||
| 59 | while (max_events-- && !evbuf_full(&state->inbuf)) { | ||
| 60 | ret = mtdev_fetch(dev, &ev, fd); | ||
| 61 | if (ret < 0) | ||
| 62 | return ret; | ||
| 63 | if (ret == 0) | ||
| 64 | break; | ||
| 65 | mtdev_put(dev, &ev); | ||
| 66 | count++; | ||
| 67 | } | ||
| 68 | return count; | ||
| 69 | } | ||
