~moody/mpl

mpl/event.c -rw-r--r-- 1.1 KiB
7309e0f2 — Jacob Moody Archive and point to zuke 6 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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>

#include "dat.h"
#include "fncs.h"

Click click[128];
Click *next;
int size;

enum{
	MIN,
	CIN,
	ROLL,
};

Click*
findpoint(Point xy)
{
	int i;
	for(i=0;i<size;i++)
		if(ptinrect(xy, click[i].r))
			return click+i;
	return nil;
}

void
eventthread(void *arg)
{
	Mouse m;
	Click c;
	Channel **chans = arg;
	Channel *min = chans[0];
	Channel *cin = chans[1];
	Channel *out = chans[2];
	Channel *roll = chans[3];
	free(chans);

	Alt alts[] = {
		{min, &m, CHANRCV},
		{cin, &c, CHANRCV},
		{roll, nil, CHANRCV},
		{nil, nil, CHANEND},
	};
	for(;;)
		switch(alt(alts)){
		case CIN:
			size++;
			assert(size < 128);
			click[size] = c;
			break;
		case MIN:
			if(m.buttons != 1)
				continue;
			next = findpoint(m.xy);
			if(next != nil)
				send(out, next);
			break;
		case ROLL:
			size = 0;
			break;
		}
}

void
spawnevent(Channel *min, Channel *cin, Channel *out, Channel *rollback)
{
	Channel **chans;
	size = 0;
	chans = emalloc(sizeof(Channel*)*4);
	chans[0] = min;
	chans[1] = cin;
	chans[2] = out;
	chans[3] = rollback;
	threadcreate(eventthread, chans, 8192);
}