~foura/libacme

ref: b26a8fbbb6d332c0da8bbfce89eb2fbda6e606eb libacme/event.c -rw-r--r-- 1.6 KiB
b26a8fbbjames palmer events: store source window in AEvent struct. 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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;
}

int
aeventnext(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
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);
}