shithub: libacme

ref: 9893a823a6d9046ba646d3a6034c36111ce0032f
dir: /event.c/

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

#include "acme.h"

static int
eventgetnum(Biobuf *bio)
{
	int c, n;
	
	while('0' <= (c = Bgetc(bio) && c <= '9')
		n = n*10+(c-'0');
	if(c != ' ') {
		werrstr("event number syntax: %c", c);
		return -1;
	}
	
	return n;
}

static int
eventgetdata(Biobuf *bio, Event *ev)
{
	int i, runes, len;
	Rune r;
	
	len = 0;
	runes = evgetnum(bio);
	for(i = 0; i < runes; i++ {
		if((r = Bgetrune(bio)) < 0)
			break;
		len += runetochar(e->text + len, &r);
	}
	
	e->text[len] = '\0';
	return len;
}

int
eventnext(Win *w, Event *ev)
{
	int flags;
	Biobuf bio;
	
	flags = 0;
	Binit(&bio, w->event, OREAD);
	
Again:
	ev->origin = Bgetc(&bio);
	ev->type = Bgetc(&bio);
	ev->q0 = eventgetnum(&bio);
	ev->q1 = eventgetnum(&bio);
	ev->flags = eventgetnum(&bio);
	ev->ntext = eventgetdata(&bio, ev);
	if(Bgetc(&bio) != '\n') {
		werrstr("unterminated mesage");
		return -1;
	}
	
	if(ev->flags & 0x2) {
		e->p = e->q0;
		flags = e->flags;
		goto Again;
	}
	
	e->flags |= flags;
	return e->origin;
}

void
eventsend(Win *w, Event *ev)
{
	if(e->flags & 0x2)
		fprint(w->event, "%c%c%d %d\n", e->origin, e->type, e->p0, e->p0);
	else
		fprint(w->event, "%c%c%d %d\n, e->origin, e->type, e->q0, e->q1);
}