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 ulong
12 event(Event *e)
13 {
14 return eread(~0UL, e);
15 }
17 static void
18 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 }else
31 if(display->bufp > display->buf)
32 flushimage(display, 1);
33 }
35 ulong
36 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 xmask |= EnterWindowMask|LeaveWindowMask;
58 XSelectInput(_x.display, _x.drawable, xmask);
59 again:
60 XWindowEvent(_x.display, _x.drawable, xmask, &xevent);
62 switch(xevent.type){
63 case Expose:
64 _xexpose(&xevent, _x.display);
65 goto again;
66 case DestroyNotify:
67 if(_xdestroy(&xevent, _x.display))
68 postnote(PNGROUP, getpgrp(), "hangup");
69 goto again;
70 case ConfigureNotify:
71 if(_xconfigure(&xevent, _x.display))
72 eresized(1);
73 goto again;
74 case ButtonPress:
75 case ButtonRelease:
76 case MotionNotify:
77 if(_xtoplan9mouse(_x.display, &xevent, &e->mouse) < 0)
78 goto again;
79 return Emouse;
80 case KeyPress:
81 e->kbdc = _xtoplan9kbd(&xevent);
82 if(e->kbdc == -1)
83 goto again;
84 return Ekeyboard;
85 default:
86 return 0;
87 }
88 }
90 void
91 einit(ulong keys)
92 {
93 keys &= ~(Emouse|Ekeyboard);
94 if(keys){
95 fprint(2, "unknown keys in einit\n");
96 abort();
97 }
98 }
100 int
101 ekbd(void)
103 Event e;
105 eread(Ekeyboard, &e);
106 return e.kbdc;
109 Mouse
110 emouse(void)
112 Event e;
114 eread(Emouse, &e);
115 return e.mouse;
118 int
119 ecanread(ulong keys)
121 int can;
123 can = 0;
124 if(keys&Emouse)
125 can |= ecanmouse();
126 if(keys&Ekeyboard)
127 can |= ecankbd();
128 return can;
131 int
132 ecanmouse(void)
134 XEvent xe;
135 Mouse m;
137 eflush();
138 again:
139 if(XCheckWindowEvent(_x.display, _x.drawable, MouseMask, &xe)){
140 if(_xtoplan9mouse(_x.display, &xe, &m) < 0)
141 goto again;
142 XPutBackEvent(_x.display, &xe);
143 return 1;
145 return 0;
148 int
149 ecankbd(void)
151 XEvent xe;
152 int r;
154 eflush();
155 if((r = _xtoplan9kbd(nil)) >= 0){
156 _xtoplan9kbd((XEvent*)-1);
157 return 1;
159 again:
160 if(XCheckWindowEvent(_x.display, _x.drawable, KeyPressMask, &xe)){
161 if(_xtoplan9kbd(&xe) == -1)
162 goto again;
163 XPutBackEvent(_x.display, &xe);
164 return 1;
166 return 0;
169 void
170 emoveto(Point p)
172 _xmoveto(p);
175 void
176 esetcursor(Cursor *c)
178 _xsetcursor(c);