Blob
1 #include <u.h>2 #include "x11-inc.h"3 #include <libc.h>4 #include <draw.h>5 #include <cursor.h>6 #include <event.h>8 #include <memdraw.h>9 #include "x11-memdraw.h"11 ulong12 event(Event *e)13 {14 return eread(~0UL, e);15 }17 static void18 eflush(void)19 {20 /* avoid generating a message if there's nothing to show. */21 /* this test isn't perfect, though; could do flushimage(display, 0) then call extract */22 /* also: make sure we don't interfere if we're multiprocessing the display */23 if(display->locking){24 /* if locking is being done by program, this means it can't depend on automatic flush in emouse() etc. */25 if(canqlock(&display->qlock)){26 if(display->bufp > display->buf)27 flushimage(display, 1);28 unlockdisplay(display);29 }30 }else31 if(display->bufp > display->buf)32 flushimage(display, 1);33 }35 ulong36 eread(ulong keys, Event *e)37 {38 int r;39 ulong xmask;40 XEvent xevent;42 xmask = ExposureMask;44 eflush();46 if(keys&Emouse)47 xmask |= MouseMask|StructureNotifyMask;48 if(keys&Ekeyboard){49 xmask |= KeyPressMask;50 if((r = _xtoplan9kbd(nil)) >= 0){51 e->kbdc = r;52 return Ekeyboard;53 }54 }56 XSelectInput(_x.display, _x.drawable, xmask);57 again:58 XWindowEvent(_x.display, _x.drawable, xmask, &xevent);60 switch(xevent.type){61 case Expose:62 _xexpose(&xevent, _x.display);63 goto again;64 case DestroyNotify:65 if(_xdestroy(&xevent, _x.display))66 postnote(PNGROUP, getpgrp(), "hangup");67 goto again;68 case ConfigureNotify:69 if(_xconfigure(&xevent, _x.display))70 eresized(1);71 goto again;72 case ButtonPress:73 case ButtonRelease:74 case MotionNotify:75 if(_xtoplan9mouse(_x.display, &xevent, &e->mouse) < 0)76 goto again;77 return Emouse;78 case KeyPress:79 e->kbdc = _xtoplan9kbd(&xevent);80 if(e->kbdc == -1)81 goto again;82 return Ekeyboard;83 default:84 return 0;85 }86 }88 void89 einit(ulong keys)90 {91 keys &= ~(Emouse|Ekeyboard);92 if(keys){93 fprint(2, "unknown keys in einit\n");94 abort();95 }96 }98 int99 ekbd(void)100 {101 Event e;103 eread(Ekeyboard, &e);104 return e.kbdc;105 }107 Mouse108 emouse(void)109 {110 Event e;112 eread(Emouse, &e);113 return e.mouse;114 }116 int117 ecanread(ulong keys)118 {119 int can;121 can = 0;122 if(keys&Emouse)123 can |= ecanmouse();124 if(keys&Ekeyboard)125 can |= ecankbd();126 return can;127 }129 int130 ecanmouse(void)131 {132 XEvent xe;133 Mouse m;135 eflush();136 again:137 if(XCheckWindowEvent(_x.display, _x.drawable, MouseMask, &xe)){138 if(_xtoplan9mouse(_x.display, &xe, &m) < 0)139 goto again;140 XPutBackEvent(_x.display, &xe);141 return 1;142 }143 return 0;144 }146 int147 ecankbd(void)148 {149 XEvent xe;150 int r;152 eflush();153 if((r = _xtoplan9kbd(nil)) >= 0){154 _xtoplan9kbd((XEvent*)-1);155 return 1;156 }157 again:158 if(XCheckWindowEvent(_x.display, _x.drawable, KeyPressMask, &xe)){159 if(_xtoplan9kbd(&xe) == -1)160 goto again;161 XPutBackEvent(_x.display, &xe);162 return 1;163 }164 return 0;165 }167 void168 emoveto(Point p)169 {170 _xmoveto(p);171 }173 void174 esetcursor(Cursor *c)175 {176 _xsetcursor(c);177 }