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 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 void
89 einit(ulong keys)
90 {
91 keys &= ~(Emouse|Ekeyboard);
92 if(keys){
93 fprint(2, "unknown keys in einit\n");
94 abort();
95 }
96 }
98 int
99 ekbd(void)
101 Event e;
103 eread(Ekeyboard, &e);
104 return e.kbdc;
107 Mouse
108 emouse(void)
110 Event e;
112 eread(Emouse, &e);
113 return e.mouse;
116 int
117 ecanread(ulong keys)
119 int can;
121 can = 0;
122 if(keys&Emouse)
123 can |= ecanmouse();
124 if(keys&Ekeyboard)
125 can |= ecankbd();
126 return can;
129 int
130 ecanmouse(void)
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;
143 return 0;
146 int
147 ecankbd(void)
149 XEvent xe;
150 int r;
152 eflush();
153 if((r = _xtoplan9kbd(nil)) >= 0){
154 _xtoplan9kbd((XEvent*)-1);
155 return 1;
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;
164 return 0;
167 void
168 emoveto(Point p)
170 _xmoveto(p);
173 void
174 esetcursor(Cursor *c)
176 _xsetcursor(c);