Blob


1 #include "x11-inc.h"
2 #include <u.h>
3 #include <libc.h>
4 #include <draw.h>
5 #include <thread.h>
6 #include <cursor.h>
7 #include <mouse.h>
8 #include <memdraw.h>
9 #include "x11-memdraw.h"
11 void
12 moveto(Mousectl *m, Point pt)
13 {
14 xmoveto(pt);
15 }
17 void
18 closemouse(Mousectl *mc)
19 {
20 if(mc == nil)
21 return;
23 /* postnote(PNPROC, mc->pid, "kill");
24 */
25 do; while(nbrecv(mc->c, &mc->m) > 0);
26 close(mc->mfd);
27 close(mc->cfd);
28 free(mc->file);
29 chanfree(mc->c);
30 chanfree(mc->resizec);
31 free(mc);
32 }
34 int
35 readmouse(Mousectl *mc)
36 {
37 if(mc->display)
38 flushimage(mc->display, 1);
39 if(recv(mc->c, &mc->m) < 0){
40 fprint(2, "readmouse: %r\n");
41 return -1;
42 }
43 return 0;
44 }
46 static
47 void
48 _ioproc(void *arg)
49 {
50 int one;
51 ulong mask;
52 Mouse m;
53 Mousectl *mc;
54 XEvent xevent;
56 one = 1;
57 mc = arg;
58 threadsetname("mouseproc");
59 memset(&m, 0, sizeof m);
60 mc->pid = getpid();
61 mask = MouseMask|ExposureMask|StructureNotifyMask;
62 XSelectInput(_x.mousecon, _x.drawable, mask);
63 for(;;){
64 XNextEvent(_x.mousecon, &xevent);
65 switch(xevent.type){
66 case Expose:
67 xexpose(&xevent, _x.mousecon);
68 continue;
69 case ConfigureNotify:
70 if(xconfigure(&xevent, _x.mousecon))
71 nbsend(mc->resizec, &one);
72 continue;
73 case SelectionRequest:
74 xselect(&xevent, _x.mousecon);
75 continue;
76 case ButtonPress:
77 case ButtonRelease:
78 case MotionNotify:
79 if(xtoplan9mouse(&xevent, &m) < 0)
80 continue;
81 send(mc->c, &m);
82 /*
83 * mc->Mouse is updated after send so it doesn't have wrong value if we block during send.
84 * This means that programs should receive into mc->Mouse (see readmouse() above) if
85 * they want full synchrony.
86 */
87 mc->m = m;
88 break;
89 }
90 }
91 }
93 Mousectl*
94 initmouse(char *file, Image *i)
95 {
96 Mousectl *mc;
98 mc = mallocz(sizeof(Mousectl), 1);
99 if(i)
100 mc->display = i->display;
101 mc->c = chancreate(sizeof(Mouse), 0);
102 mc->resizec = chancreate(sizeof(int), 2);
103 proccreate(_ioproc, mc, 16384);
104 return mc;
107 void
108 setcursor(Mousectl *mc, Cursor *c)
110 xsetcursor(c);