9 #include "x11-memdraw.h"
11 int _windowhasfocus = 1;
12 int _wantfocuschanges;
15 moveto(Mousectl *m, Point pt)
21 closemouse(Mousectl *mc)
26 /* postnote(PNPROC, mc->pid, "kill");
28 do; while(nbrecv(mc->c, &mc->m) > 0);
33 chanfree(mc->resizec);
38 readmouse(Mousectl *mc)
41 flushimage(mc->display, 1);
42 if(recv(mc->c, &mc->m) < 0){
43 fprint(2, "readmouse: %r\n");
50 * This is necessary because some X libraries (e.g., on FC3)
51 * use an inordinate amount of stack space to do _xsetcursor.
52 * Perhaps instead there should be a generic "run this X routine"
53 * stack that you send a function and argument to.
57 _cursorproc(void *arg)
63 threadsetname("cursorproc (sigh)");
65 c = recvp(mc->ccursor);
67 sendp(mc->ccursorwait, nil);
84 threadsetname("mouseproc");
85 memset(&m, 0, sizeof m);
87 mask = MouseMask|ExposureMask|StructureNotifyMask;
88 XSelectInput(_x.mousecon, _x.drawable, mask);
89 fd = XConnectionNumber(_x.mousecon);
92 XNextEvent(_x.mousecon, &xevent);
95 _xexpose(&xevent, _x.mousecon);
98 if(_xdestroy(&xevent, _x.mousecon)){
99 /* drain it before sending */
100 /* apps that care can notice we sent a 0 */
101 /* otherwise we'll have getwindow send SIGHUP */
102 nbrecv(mc->resizec, 0);
103 nbrecv(mc->resizec, 0);
104 send(mc->resizec, 0);
107 case ConfigureNotify:
108 if(_xconfigure(&xevent, _x.mousecon))
109 nbsend(mc->resizec, &one);
111 case SelectionRequest:
112 _xselect(&xevent, _x.mousecon);
117 /* If the motion notifications are backing up, skip over some. */
118 if(0 && xevent.type == MotionNotify){
119 while(XCheckWindowEvent(_x.mousecon, _x.drawable, MouseMask, &xevent)){
120 if(xevent.type != MotionNotify)
125 if(_xtoplan9mouse(_x.mousecon, &xevent, &m) < 0)
130 * mc->Mouse is updated after send so it doesn't have wrong value if we block during send.
131 * This means that programs should receive into mc->Mouse (see readmouse() above) if
132 * they want full synchrony.
137 if(xevent.xclient.message_type == _x.wmprotos){
138 a = xevent.xclient.data.l[0];
139 if(_wantfocuschanges && a == _x.takefocus){
141 _x.newscreenr = _x.screenr;
142 nbsend(mc->resizec, &one);
143 }else if(_wantfocuschanges && a == _x.losefocus){
145 _x.newscreenr = _x.screenr;
146 nbsend(mc->resizec, &one);
155 initmouse(char *file, Image *i)
159 mc = mallocz(sizeof(Mousectl), 1);
161 mc->display = i->display;
162 mc->c = chancreate(sizeof(Mouse), 0);
163 chansetname(mc->c, "mousec");
164 mc->resizec = chancreate(sizeof(int), 2);
165 chansetname(mc->resizec, "resizec");
166 mc->ccursor = chancreate(sizeof(void*), 0);
167 chansetname(mc->ccursor, "ccursor");
168 mc->ccursorwait = chancreate(sizeof(void*), 0);
169 chansetname(mc->ccursor, "ccursorwait");
170 proccreate(_ioproc, mc, 256*1024);
171 proccreate(_cursorproc, mc, 256*1024); /* sigh */
176 setcursor(Mousectl *mc, Cursor *c)
178 qlock(&mc->cursorlock);
179 sendp(mc->ccursor, c);
180 recvp(mc->ccursorwait);
181 qunlock(&mc->cursorlock);
185 * Send the mouse event back to the window manager.
186 * So that 9term can tell rio to pop up its button3 menu.
187 * Note that we're using _x.mousecon in a few places,
188 * so we have to be sure that the mouse proc isn't using it
189 * when we call! This is all a bit wonky and should be
190 * avoided unless you know what you're doing.
193 bouncemouse(Mouse *m)
198 e.type = ButtonPress;
203 else if(m->buttons&2)
205 else if(m->buttons&4)
208 XTranslateCoordinates(_x.display, _x.drawable,
209 DefaultRootWindow(_x.display),
210 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
211 e.root = DefaultRootWindow(_x.mousecon);
217 e.time = CurrentTime;
218 XUngrabPointer(_x.mousecon, m->msec);
219 XSendEvent(_x.mousecon, e.root, True, ButtonPressMask, (XEvent*)&e);