Blob


1 #include <u.h>
2 #include "x11-inc.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 int _windowhasfocus = 1;
12 int _wantfocuschanges;
14 void
15 moveto(Mousectl *m, Point pt)
16 {
17 _xmoveto(pt);
18 }
20 void
21 closemouse(Mousectl *mc)
22 {
23 if(mc == nil)
24 return;
26 /* postnote(PNPROC, mc->pid, "kill");
27 */
28 do; while(nbrecv(mc->c, &mc->m) > 0);
29 close(mc->mfd);
30 close(mc->cfd);
31 free(mc->file);
32 chanfree(mc->c);
33 chanfree(mc->resizec);
34 free(mc);
35 }
37 int
38 readmouse(Mousectl *mc)
39 {
40 if(mc->display)
41 flushimage(mc->display, 1);
42 if(recv(mc->c, &mc->m) < 0){
43 fprint(2, "readmouse: %r\n");
44 return -1;
45 }
46 return 0;
47 }
49 static
50 void
51 _ioproc(void *arg)
52 {
53 int fd, one;
54 Atom a;
55 ulong mask;
56 Mouse m;
57 Mousectl *mc;
58 XEvent xevent;
60 one = 1;
61 mc = arg;
62 threadsetname("mouseproc");
63 memset(&m, 0, sizeof m);
64 mc->pid = getpid();
65 mask = MouseMask|ExposureMask|StructureNotifyMask;
66 XSelectInput(_x.mousecon, _x.drawable, mask);
67 fd = XConnectionNumber(_x.mousecon);
68 for(;;){
69 while(XPending(_x.mousecon) == False)
70 threadfdwait(fd, 'r');
71 XNextEvent(_x.mousecon, &xevent);
72 switch(xevent.type){
73 case Expose:
74 _xexpose(&xevent, _x.mousecon);
75 continue;
76 case DestroyNotify:
77 if(_xdestroy(&xevent, _x.mousecon)){
78 /* drain it before sending */
79 /* apps that care can notice we sent a 0 */
80 /* otherwise we'll have getwindow send SIGHUP */
81 nbrecv(mc->resizec, 0);
82 nbrecv(mc->resizec, 0);
83 send(mc->resizec, 0);
84 }
85 continue;
86 case ConfigureNotify:
87 if(_xconfigure(&xevent, _x.mousecon))
88 nbsend(mc->resizec, &one);
89 continue;
90 case SelectionRequest:
91 _xselect(&xevent, _x.mousecon);
92 continue;
93 case ButtonPress:
94 case ButtonRelease:
95 case MotionNotify:
96 /* If the motion notifications are backing up, skip over some. */
97 if(xevent.type == MotionNotify){
98 while(XCheckWindowEvent(_x.mousecon, _x.drawable, MouseMask, &xevent)){
99 if(xevent.type != MotionNotify)
100 break;
103 if(_xtoplan9mouse(_x.mousecon, &xevent, &m) < 0)
104 continue;
105 send(mc->c, &m);
106 /*
107 * mc->Mouse is updated after send so it doesn't have wrong value if we block during send.
108 * This means that programs should receive into mc->Mouse (see readmouse() above) if
109 * they want full synchrony.
110 */
111 mc->m = m;
112 break;
113 case ClientMessage:
114 if(xevent.xclient.message_type == _x.wmprotos){
115 a = xevent.xclient.data.l[0];
116 if(_wantfocuschanges && a == _x.takefocus){
117 _windowhasfocus = 1;
118 _x.newscreenr = _x.screenr;
119 nbsend(mc->resizec, &one);
120 }else if(_wantfocuschanges && a == _x.losefocus){
121 _windowhasfocus = 0;
122 _x.newscreenr = _x.screenr;
123 nbsend(mc->resizec, &one);
126 break;
131 Mousectl*
132 initmouse(char *file, Image *i)
134 Mousectl *mc;
136 threadfdwaitsetup();
137 mc = mallocz(sizeof(Mousectl), 1);
138 if(i)
139 mc->display = i->display;
140 mc->c = chancreate(sizeof(Mouse), 0);
141 mc->resizec = chancreate(sizeof(int), 2);
142 threadcreate(_ioproc, mc, 32768);
143 return mc;
146 void
147 setcursor(Mousectl *mc, Cursor *c)
149 _xsetcursor(c);
152 /*
153 * Send the mouse event back to the window manager.
154 * So that 9term can tell rio to pop up its button3 menu.
155 * Note that we're using _x.mousecon in a few places,
156 * so we have to be sure that the mouse proc isn't using it
157 * when we call! This is all a bit wonky and should be
158 * avoided unless you know what you're doing.
159 */
160 void
161 bouncemouse(Mouse *m)
163 XButtonEvent e;
164 XWindow dw;
166 e.type = ButtonPress;
167 e.state = 0;
168 e.button = 0;
169 if(m->buttons&1)
170 e.button = 1;
171 else if(m->buttons&2)
172 e.button = 2;
173 else if(m->buttons&4)
174 e.button = 3;
175 e.same_screen = 1;
176 XTranslateCoordinates(_x.display, _x.drawable,
177 DefaultRootWindow(_x.display),
178 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
179 e.root = DefaultRootWindow(_x.mousecon);
180 e.window = e.root;
181 e.subwindow = None;
182 e.x = e.x_root;
183 e.y = e.y_root;
184 #undef time
185 e.time = CurrentTime;
186 XUngrabPointer(_x.mousecon, m->msec);
187 XSendEvent(_x.mousecon, e.root, True, ButtonPressMask, (XEvent*)&e);
188 XFlush(_x.mousecon);