aboutsummaryrefslogtreecommitdiffstats
path: root/src/iobuffer.c
blob: 8bfcc07fac0800d19b2894eda925d5d030d7c9fe (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
#include "iobuffer.h"

////////////////////////////////////////////////////////

void init_iobuf(struct IOBuffer *buf)
{
	memset(buf, 0, sizeof(struct IOBuffer));
	buf->at = buf->begin;
	buf->top = buf->at;
	buf->end = buf->begin + DIM_BUFFER;
}

////////////////////////////////////////////////////////

const struct input_event* get_iobuf_event(struct IOBuffer *buf, int fd)
{
	const struct input_event *ev;
	int n = buf->top - buf->at;
	if (n < EVENT_SIZE) {
		/* partial event is available: save it */
		if (buf->at != buf->begin && n > 0)
			memmove(buf->begin, buf->at, n);
		/* start from the beginning */
		buf->at = buf->begin;
		buf->top = buf->at + n;
		/* read more data */
		SYSCALL(n = read(fd, buf->top, buf->end - buf->top));
		if (n <= 0)
			return NULL;
		buf->top += n;
	}
	if (buf->top - buf->at < EVENT_SIZE)
		return NULL;
	ev = (const struct input_event *)buf->at;
	buf->at += EVENT_SIZE;
	return ev;
}

////////////////////////////////////////////////////////