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>
7 #include <memdraw.h>
8 #include "x11-memdraw.h"
10 /*
11 * Should change this to support etimer and estartfn again.
12 * The main eread() would prepare a select mask from the keys
13 * and then call select() to wait for input. Then it would read an
14 * event from the readied fd and return it. Use XCheckWindowEvent
15 * to poll the X connection.
16 */
18 ulong
19 event(Event *e)
20 {
21 return eread(~0UL, e);
22 }
24 static void
25 eflush(void)
26 {
27 /* avoid generating a message if there's nothing to show. */
28 /* this test isn't perfect, though; could do flushimage(display, 0) then call extract */
29 /* also: make sure we don't interfere if we're multiprocessing the display */
30 if(display->locking){
31 /* if locking is being done by program, this means it can't depend on automatic flush in emouse() etc. */
32 if(canqlock(&display->qlock)){
33 if(display->bufp > display->buf)
34 flushimage(display, 1);
35 unlockdisplay(display);
36 }
37 }else
38 if(display->bufp > display->buf)
39 flushimage(display, 1);
40 }
42 ulong
43 eread(ulong keys, Event *e)
44 {
45 int r;
46 ulong xmask;
47 XEvent xevent;
49 xmask = ExposureMask;
51 eflush();
53 if(keys&Emouse)
54 xmask |= MouseMask|StructureNotifyMask;
55 if(keys&Ekeyboard){
56 xmask |= KeyPressMask;
57 if((r = _xtoplan9kbd(nil)) >= 0){
58 e->kbdc = r;
59 return Ekeyboard;
60 }
61 }
63 xmask |= EnterWindowMask|LeaveWindowMask;
65 XSelectInput(_x.display, _x.drawable, xmask);
66 again:
67 XWindowEvent(_x.display, _x.drawable, xmask, &xevent);
69 switch(xevent.type){
70 case Expose:
71 _xexpose(&xevent, _x.display);
72 goto again;
73 case DestroyNotify:
74 if(_xdestroy(&xevent, _x.display))
75 postnote(PNGROUP, getpgrp(), "hangup");
76 goto again;
77 case ConfigureNotify:
78 if(_xconfigure(&xevent, _x.display))
79 eresized(1);
80 goto again;
81 case ButtonPress:
82 case ButtonRelease:
83 case MotionNotify:
84 if(_xtoplan9mouse(_x.display, &xevent, &e->mouse) < 0)
85 goto again;
86 return Emouse;
87 case KeyPress:
88 e->kbdc = _xtoplan9kbd(&xevent);
89 if(e->kbdc == -1)
90 goto again;
91 return Ekeyboard;
92 default:
93 goto again;
94 }
95 }
97 void
98 einit(ulong keys)
99 {
100 keys &= ~(Emouse|Ekeyboard);
101 if(keys){
102 fprint(2, "unknown keys in einit\n");
103 abort();
107 int
108 ekbd(void)
110 Event e;
112 eread(Ekeyboard, &e);
113 return e.kbdc;
116 Mouse
117 emouse(void)
119 Event e;
121 eread(Emouse, &e);
122 return e.mouse;
125 int
126 ecanread(ulong keys)
128 int can;
130 can = 0;
131 if(keys&Emouse)
132 can |= ecanmouse();
133 if(keys&Ekeyboard)
134 can |= ecankbd();
135 return can;
138 int
139 ecanmouse(void)
141 XEvent xe;
142 Mouse m;
144 eflush();
145 again:
146 if(XCheckWindowEvent(_x.display, _x.drawable, MouseMask, &xe)){
147 if(_xtoplan9mouse(_x.display, &xe, &m) < 0)
148 goto again;
149 XPutBackEvent(_x.display, &xe);
150 return 1;
152 return 0;
155 int
156 ecankbd(void)
158 XEvent xe;
159 int r;
161 eflush();
162 if((r = _xtoplan9kbd(nil)) >= 0){
163 _xtoplan9kbd((XEvent*)-1);
164 return 1;
166 again:
167 if(XCheckWindowEvent(_x.display, _x.drawable, KeyPressMask, &xe)){
168 if(_xtoplan9kbd(&xe) == -1)
169 goto again;
170 XPutBackEvent(_x.display, &xe);
171 return 1;
173 return 0;
176 void
177 emoveto(Point p)
179 _xmoveto(p);
182 void
183 esetcursor(Cursor *c)
185 _xsetcursor(c);