shithub: libacme

ref: 07844fca03a7c083a3ba6ab818e4df06510c57cd
dir: /event.c/

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

#include "acme.h"

static int
getc(AWin *w)
{
	if(w->nbuf == 0){
		w->nbuf = read(w->eventfd, w->buf, sizeof(w->buf));
		if(w->nbuf <= 0) {
			werrstr("event read failed: %r");
			return -1;
		}
		w->bufp = w->buf;
	}

	w->nbuf--;
	return *w->bufp++;
}

static int
getnum(AWin *w)
{
	int c, n;

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

static long
getrune(AWin *w)
{
	Rune r;
	char buf[UTFmax];
	int n;

	n = 0;
	r = getc(w);
	buf[n++] = r;

	if(r >= Runeself) {
		while(!fullrune(buf, n))
			buf[n++] = getc(w);
		chartorune(&r, buf);
	}

	return r;
}

static int
getdata(AWin *w, AEvent *e)
{
	int i, n, o;
	Rune r;
	
	o = 0;
	n = getnum(w);

	for(i = 0; i < n; i++) {
		if((r = getrune(w)) == -1)
			break;
		o += runetochar(e->text + o, &r);
	}
	
	e->text[o] = 0;
	return o;
}

int
aeventnext(AWin *w, AEvent *e)
{
	int flags;
	
	flags = 0;
loop:
	e->origin = getc(w);
	e->type = getc(w);
	e->q0 = getnum(w);
	e->q1 = getnum(w);
	e->flags = getnum(w);
	e->ntext = getdata(w, e);
	if(getc(w) != '\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;
}

int
aeventsend(AWin *w, AEvent *e)
{
	if(e->flags & 0x2)
		return fprint(w->eventfd, "%c%c%d %d\n", e->origin, e->type, e->p, e->p);
	else
		return fprint(w->eventfd, "%c%c%d %d\n", e->origin, e->type, e->q0, e->q1);
}