shithub: libacme

ref: 7609bab0c1a5ff4427d48a52cadcb69ad054e02d
dir: /event.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>

#include "acme.h"

static int
getc(int fd)
{
	char buf[1];
	
	if(read(fd, buf, sizeof(buf)) < 0)
		return buf[0];
	return -1;
}

static int
getnum(int fd)
{
	int c, n;

	n = 0;
	while('0' <= (c = getc(fd)) && c <= '9')
		n = n*10+(c-'0');
	
	if(c != ' '){
		werrstr("event number syntax: %c", c);
		return -1;
	}
	
	return n;
}

static long;
getrune(int fd)
{
	Rune rune;
	char buf[UTFmax];
	
	if(read(fd, buf, sizeof(buf)))
		return chartorune(&rune, buf);
		
	return -1;
}

static int
getdata(int fd, AEvent *e)
{
	int i, n, o;
	long r;
	
	o = 0;
	n = getnum(fd);
	for(i = 0; i < n; i++) {
		if((r = getrune(fd)) < 0)
			break;
		o += runetochar(e->text + o, (Rune*)&r);
	}
	
	e->text[o] = 0;
	return o;
}

int
aeventnext(AWin *w, AEvent *e)
{
	int flags;
	
	flags = 0;
loop:
	e->origin = getc(w->eventfd);
	e->type = getc(w->eventfd);
	e->q0 = getnum(w->eventfd);
	e->q1 = getnum(w->eventfd);
	e->ntext = getdata(w->eventfd, e);
	if(getc(w->eventfd) != '\n') {
		werrstr("unterminated message");
		return -1;
	}
	
	if(e->flags & 0x2) {
		e->p = e->q0;
		flags = e->flags;
		goto loop;
	}
	
	e->flags |= flags;
	return e->origin;
}