aboutsummaryrefslogtreecommitdiff
path: root/src/libdraw/mouse.c
blob: 382efae5216bd8b1c7caae8deaac2aec8bc458cc (plain)
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
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>

void
moveto(Mousectl *mc, Point pt)
{
	_displaymoveto(mc->display, pt);
	mc->m.xy = pt;
}

void
closemouse(Mousectl *mc)
{
	if(mc == nil)
		return;

/*	postnote(PNPROC, mc->pid, "kill"); */

	do; while(nbrecv(mc->c, &mc->m) > 0);
	chanfree(mc->c);
	chanfree(mc->resizec);
	free(mc);
}

int
readmouse(Mousectl *mc)
{
	if(mc->display)
		flushimage(mc->display, 1);
	if(recv(mc->c, &mc->m) < 0){
		fprint(2, "readmouse: %r\n");
		return -1;
	}
	return 0;
}

static
void
_ioproc(void *arg)
{
	int one, resized;
	Mouse m;
	Mousectl *mc;

	mc = arg;
	threadsetname("mouseproc");
	memset(&m, 0, sizeof m);
	one = 1;
	resized = 0;
	for(;;){
		if(_displayrdmouse(mc->display, &m, &resized) < 0) {
			if(postnote(PNPROC, getpid(), "hangup") < 0)
				fprint(2, "postnote: %r\n");
			sleep(10*1000);
			threadexitsall("mouse read error");
		}
		if(resized)
			send(mc->resizec, &one);
		send(mc->c, &m);
		/*
		 * mc->m is updated after send so it doesn't have wrong value if we block during send.
		 * This means that programs should receive into mc->Mouse (see readmouse() above) if
		 * they want full synchrony.
		 */
		mc->m = m;
	}
}

Mousectl*
initmouse(char *file, Image *i)
{
	Mousectl *mc;

	mc = mallocz(sizeof(Mousectl), 1);
	if(i)
		mc->display = i->display;
	mc->c = chancreate(sizeof(Mouse), 0);
	chansetname(mc->c, "mousec");
	mc->resizec = chancreate(sizeof(int), 2);
	chansetname(mc->resizec, "resizec");
	proccreate(_ioproc, mc, 32*1024);
	return mc;
}

void
setcursor(Mousectl *mc, Cursor *c)
{
	_displaycursor(mc->display, c, nil);
}

void
setcursor2(Mousectl *mc, Cursor *c, Cursor2 *c2)
{
	_displaycursor(mc->display, c, c2);
}