diff options
Diffstat (limited to 'mtdev/iobuf.c')
| -rw-r--r-- | mtdev/iobuf.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/mtdev/iobuf.c b/mtdev/iobuf.c new file mode 100644 index 0000000..8e367b8 --- /dev/null +++ b/mtdev/iobuf.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | /*************************************************************************** | ||
| 2 | * | ||
| 3 | * Multitouch X driver | ||
| 4 | * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | * | ||
| 20 | **************************************************************************/ | ||
| 21 | |||
| 22 | #include <mtdev-iobuf.h> | ||
| 23 | |||
| 24 | void init_iobuf(struct IOBuffer *buf) | ||
| 25 | { | ||
| 26 | memset(buf, 0, sizeof(struct IOBuffer)); | ||
| 27 | buf->at = buf->begin; | ||
| 28 | buf->top = buf->at; | ||
| 29 | buf->end = buf->begin + DIM_BUFFER; | ||
| 30 | } | ||
| 31 | |||
| 32 | const struct input_event *get_iobuf_event(struct IOBuffer *buf, int fd) | ||
| 33 | { | ||
| 34 | const struct input_event *ev; | ||
| 35 | int n = buf->top - buf->at; | ||
| 36 | if (n < EVENT_SIZE) { | ||
| 37 | /* partial event is available: save it */ | ||
| 38 | if (buf->at != buf->begin && n > 0) | ||
| 39 | memmove(buf->begin, buf->at, n); | ||
| 40 | /* start from the beginning */ | ||
| 41 | buf->at = buf->begin; | ||
| 42 | buf->top = buf->at + n; | ||
| 43 | /* read more data */ | ||
| 44 | SYSCALL(n = read(fd, buf->top, buf->end - buf->top)); | ||
| 45 | if (n <= 0) | ||
| 46 | return NULL; | ||
| 47 | buf->top += n; | ||
| 48 | } | ||
| 49 | if (buf->top - buf->at < EVENT_SIZE) | ||
| 50 | return NULL; | ||
| 51 | ev = (const struct input_event *)buf->at; | ||
| 52 | buf->at += EVENT_SIZE; | ||
| 53 | return ev; | ||
| 54 | } | ||
