shithub: libacme

Download patch

ref: 0f9a8ea6531e5ae44671a24c82d3b2f7e4c64be8
parent: 47cadfe7f9996d4798629eba9f877ecf711193cf
author: james palmer <foura@biobuf.link>
date: Wed Jun 2 13:19:22 EDT 2021

add event handling

--- a/acme.h
+++ b/acme.h
@@ -31,5 +31,5 @@
 	char text[Eventsz + 1];
 };
 
-void eventnext(Win *, Event *);
+int eventnext(Win *, Event *);
 void eventsend(Win *, Event *);
--- a/event.c
+++ b/event.c
@@ -1,0 +1,78 @@
+#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);
+}