ref: 14684c2d7066633e502b00bbaa26b1f816f02751
dir: /event.c/
#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; } void aeventopen(AWin *w) { if(w->eventfd < 0) w->eventfd = awinfsopen(w, "event", ORDWR); } int aeventread(AWin *w, AEvent *e) { int flags; flags = 0; loop: e->win = w; 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 aeventwrite(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); } void aeventclose(AWin *w) { close(w->eventfd); w->eventfd = -1; }