ref: 2e2bacc37ba01af14d632a7f698c1899f631ad5c
dir: /event.c/
#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) { int c, i; Rune rune; char buf[UTFmax]; c = getc(fd); if(c < Runeself) return c; buf[0] = c; for(i = 1; i < UTFmax;) { c = getc(fd); if(c < 0) return c; buf[i++] = c; if(fullrune(buf, i)) { chartorune(&rune, buf); return rune; } } return -1; } static int getdata(int fd, AEvent *e) { int i, n, o; Rune r; o = 0; n = getnum(fd); for(i = 0; i < n; i++) { if((r = getrune(fd)) == -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->eventfd); e->type = getc(w->eventfd); e->q0 = getnum(w->eventfd); e->q1 = getnum(w->eventfd); e->flags = 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; } 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); }