Blob
1 #include "x11-inc.h"3 #include <u.h>4 #include <libc.h>5 #include <draw.h>6 #include <cursor.h>7 #include <event.h>9 #include <memdraw.h>10 #include "x11-memdraw.h"12 ulong13 event(Event *e)14 {15 return eread(~0UL, e);16 }18 static void19 eflush(void)20 {21 /* avoid generating a message if there's nothing to show. */22 /* this test isn't perfect, though; could do flushimage(display, 0) then call extract */23 /* also: make sure we don't interfere if we're multiprocessing the display */24 if(display->locking){25 /* if locking is being done by program, this means it can't depend on automatic flush in emouse() etc. */26 if(canqlock(&display->qlock)){27 if(display->bufp > display->buf)28 flushimage(display, 1);29 unlockdisplay(display);30 }31 }else32 if(display->bufp > display->buf)33 flushimage(display, 1);34 }36 ulong37 eread(ulong keys, Event *e)38 {39 int r;40 ulong xmask;41 XEvent xevent;43 xmask = ExposureMask;45 eflush();47 if(keys&Emouse)48 xmask |= MouseMask|StructureNotifyMask;49 if(keys&Ekeyboard){50 xmask |= KeyPressMask;51 if((r = _xtoplan9kbd(nil)) >= 0){52 e->kbdc = r;53 return Ekeyboard;54 }55 }57 XSelectInput(_x.display, _x.drawable, xmask);58 again:59 XWindowEvent(_x.display, _x.drawable, xmask, &xevent);61 switch(xevent.type){62 case Expose:63 _xexpose(&xevent, _x.display);64 goto again;65 case DestroyNotify:66 if(_xdestroy(&xevent, _x.display))67 postnote(PNGROUP, getpgrp(), "hangup");68 goto again;69 case ConfigureNotify:70 if(_xconfigure(&xevent, _x.display))71 eresized(1);72 goto again;73 case ButtonPress:74 case ButtonRelease:75 case MotionNotify:76 if(_xtoplan9mouse(_x.display, &xevent, &e->mouse) < 0)77 goto again;78 return Emouse;79 case KeyPress:80 e->kbdc = _xtoplan9kbd(&xevent);81 if(e->kbdc == -1)82 goto again;83 return Ekeyboard;84 default:85 return 0;86 }87 }89 void90 einit(ulong keys)91 {92 keys &= ~(Emouse|Ekeyboard);93 if(keys){94 fprint(2, "unknown keys in einit\n");95 abort();96 }97 }99 int100 ekbd(void)101 {102 Event e;104 eread(Ekeyboard, &e);105 return e.kbdc;106 }108 Mouse109 emouse(void)110 {111 Event e;113 eread(Emouse, &e);114 return e.mouse;115 }117 int118 ecanread(ulong keys)119 {120 int can;122 can = 0;123 if(keys&Emouse)124 can |= ecanmouse();125 if(keys&Ekeyboard)126 can |= ecankbd();127 return can;128 }130 int131 ecanmouse(void)132 {133 XEvent xe;134 Mouse m;136 eflush();137 again:138 if(XCheckWindowEvent(_x.display, _x.drawable, MouseMask, &xe)){139 if(_xtoplan9mouse(_x.display, &xe, &m) < 0)140 goto again;141 XPutBackEvent(_x.display, &xe);142 return 1;143 }144 return 0;145 }147 int148 ecankbd(void)149 {150 XEvent xe;151 int r;153 eflush();154 if((r = _xtoplan9kbd(nil)) >= 0){155 _xtoplan9kbd((XEvent*)-1);156 return 1;157 }158 again:159 if(XCheckWindowEvent(_x.display, _x.drawable, KeyPressMask, &xe)){160 if(_xtoplan9kbd(&xe) == -1)161 goto again;162 XPutBackEvent(_x.display, &xe);163 return 1;164 }165 return 0;166 }168 void169 emoveto(Point p)170 {171 _xmoveto(p);172 }174 void175 esetcursor(Cursor *c)176 {177 _xsetcursor(c);178 }