shithub: libacme

ref: dfe87a2482a58d297233aacea4b3b60562e40f6d
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;
	
	n = 0;
	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 = eventgetnum(bio);
	for(i = 0; i < runes; i++) {
		if((r = Bgetrune(bio)) == -1)
			break;
		len += runetochar(ev->text + len, &r);
	}
	
	ev->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) {
		ev->p = ev->q0;
		flags = ev->flags;
		goto Again;
	}
	
	ev->flags |= flags;
	return ev->origin;
}

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