~foura/libacme

ref: 2e2bacc37ba01af14d632a7f698c1899f631ad5c libacme/event.c -rw-r--r-- 1.6 KiB
2e2bacc3 — james palmer fix getrune, getdata, getc so aeventnext works 1 year, 18 days 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
#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);
}