plan9port/src/libdraw/x11-mouse.c

174 lines
3.5 KiB
C
Raw Normal View History

2003-09-30 17:47:42 +00:00
#include <u.h>
2004-03-26 01:59:35 +00:00
#include "x11-inc.h"
2003-09-30 17:47:42 +00:00
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <memdraw.h>
#include "x11-memdraw.h"
2004-03-30 05:03:29 +00:00
int _windowhasfocus = 1;
int _wantfocuschanges;
2003-09-30 17:47:42 +00:00
void
moveto(Mousectl *m, Point pt)
{
_xmoveto(pt);
2003-09-30 17:47:42 +00:00
}
void
closemouse(Mousectl *mc)
{
if(mc == nil)
return;
/* postnote(PNPROC, mc->pid, "kill");
*/
do; while(nbrecv(mc->c, &mc->m) > 0);
close(mc->mfd);
close(mc->cfd);
free(mc->file);
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 fd, one;
2004-03-30 05:03:29 +00:00
Atom a;
2003-10-11 02:47:43 +00:00
ulong mask;
2003-09-30 17:47:42 +00:00
Mouse m;
Mousectl *mc;
XEvent xevent;
one = 1;
mc = arg;
threadsetname("mouseproc");
memset(&m, 0, sizeof m);
mc->pid = getpid();
2003-10-11 02:47:43 +00:00
mask = MouseMask|ExposureMask|StructureNotifyMask;
XSelectInput(_x.mousecon, _x.drawable, mask);
fd = XConnectionNumber(_x.mousecon);
2003-09-30 17:47:42 +00:00
for(;;){
while(XPending(_x.mousecon) == False)
threadfdwait(fd, 'r');
2003-10-11 02:47:43 +00:00
XNextEvent(_x.mousecon, &xevent);
2003-09-30 17:47:42 +00:00
switch(xevent.type){
case Expose:
_xexpose(&xevent, _x.mousecon);
2003-09-30 17:47:42 +00:00
continue;
case DestroyNotify:
if(_xdestroy(&xevent, _x.mousecon)){
/* drain it before sending */
/* apps that care can notice we sent a 0 */
/* otherwise we'll have getwindow send SIGHUP */
nbrecv(mc->resizec, 0);
nbrecv(mc->resizec, 0);
send(mc->resizec, 0);
}
continue;
2003-09-30 17:47:42 +00:00
case ConfigureNotify:
if(_xconfigure(&xevent, _x.mousecon))
2003-09-30 17:47:42 +00:00
nbsend(mc->resizec, &one);
continue;
2003-10-11 02:47:43 +00:00
case SelectionRequest:
_xselect(&xevent, _x.mousecon);
2003-10-11 02:47:43 +00:00
continue;
2003-09-30 17:47:42 +00:00
case ButtonPress:
case ButtonRelease:
case MotionNotify:
2004-03-31 05:15:02 +00:00
/* If the motion notifications are backing up, skip over some. */
if(xevent.type == MotionNotify){
while(XCheckWindowEvent(_x.mousecon, _x.drawable, MouseMask, &xevent)){
if(xevent.type != MotionNotify)
break;
}
}
if(_xtoplan9mouse(_x.mousecon, &xevent, &m) < 0)
2003-09-30 17:47:42 +00:00
continue;
send(mc->c, &m);
/*
* mc->Mouse 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;
break;
2004-03-30 05:03:29 +00:00
case ClientMessage:
if(xevent.xclient.message_type == _x.wmprotos){
a = xevent.xclient.data.l[0];
if(_wantfocuschanges && a == _x.takefocus){
_windowhasfocus = 1;
_x.newscreenr = _x.screenr;
nbsend(mc->resizec, &one);
}else if(_wantfocuschanges && a == _x.losefocus){
_windowhasfocus = 0;
_x.newscreenr = _x.screenr;
nbsend(mc->resizec, &one);
}
}
break;
2003-09-30 17:47:42 +00:00
}
}
}
Mousectl*
initmouse(char *file, Image *i)
{
Mousectl *mc;
threadfdwaitsetup();
2003-09-30 17:47:42 +00:00
mc = mallocz(sizeof(Mousectl), 1);
if(i)
mc->display = i->display;
mc->c = chancreate(sizeof(Mouse), 0);
mc->resizec = chancreate(sizeof(int), 2);
2004-04-21 22:46:39 +00:00
threadcreate(_ioproc, mc, 32768);
2003-09-30 17:47:42 +00:00
return mc;
}
void
setcursor(Mousectl *mc, Cursor *c)
{
_xsetcursor(c);
2003-09-30 17:47:42 +00:00
}
2004-03-30 05:03:29 +00:00
void
bouncemouse(Mouse *m)
{
XButtonEvent e;
e.type = ButtonPress;
e.window = DefaultRootWindow(_x.display);
e.state = 0;
e.button = 0;
if(m->buttons&1)
e.button = 1;
else if(m->buttons&2)
e.button = 2;
else if(m->buttons&4)
e.button = 3;
e.x = m->xy.x;
e.y = m->xy.y;
#undef time
e.time = CurrentTime;
XSendEvent(_x.display, e.window, True, ButtonPressMask, (XEvent*)&e);
XFlush(_x.display);
}