Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
8 void
9 moveto(Mousectl *mc, Point pt)
10 {
11 _displaymoveto(mc->display, pt);
12 mc->m.xy = pt;
13 }
15 void
16 closemouse(Mousectl *mc)
17 {
18 if(mc == nil)
19 return;
21 /* postnote(PNPROC, mc->pid, "kill"); */
23 do; while(nbrecv(mc->c, &mc->m) > 0);
24 chanfree(mc->c);
25 chanfree(mc->resizec);
26 free(mc);
27 }
29 int
30 readmouse(Mousectl *mc)
31 {
32 if(mc->display)
33 flushimage(mc->display, 1);
34 if(recv(mc->c, &mc->m) < 0){
35 fprint(2, "readmouse: %r\n");
36 return -1;
37 }
38 return 0;
39 }
41 static
42 void
43 _ioproc(void *arg)
44 {
45 int one, resized;
46 Mouse m;
47 Mousectl *mc;
49 mc = arg;
50 threadsetname("mouseproc");
51 memset(&m, 0, sizeof m);
52 one = 1;
53 resized = 0;
54 for(;;){
55 if(_displayrdmouse(mc->display, &m, &resized) < 0)
56 threadexits("read error");
57 if(resized)
58 send(mc->resizec, &one);
59 send(mc->c, &m);
60 /*
61 * mc->m is updated after send so it doesn't have wrong value if we block during send.
62 * This means that programs should receive into mc->Mouse (see readmouse() above) if
63 * they want full synchrony.
64 */
65 mc->m = m;
66 }
67 }
69 Mousectl*
70 initmouse(char *file, Image *i)
71 {
72 Mousectl *mc;
74 mc = mallocz(sizeof(Mousectl), 1);
75 if(i)
76 mc->display = i->display;
77 mc->c = chancreate(sizeof(Mouse), 0);
78 chansetname(mc->c, "mousec");
79 mc->resizec = chancreate(sizeof(int), 2);
80 chansetname(mc->resizec, "resizec");
81 proccreate(_ioproc, mc, 32*1024);
82 return mc;
83 }
85 void
86 setcursor(Mousectl *mc, Cursor *c)
87 {
88 _displaycursor(mc->display, c);
89 }