Blob


1 #include <u.h>
2 #include <sys/select.h>
3 #include <libc.h>
4 #include <draw.h>
5 #include <cursor.h>
6 #include <event.h>
7 #include <mux.h>
8 #include <drawfcall.h>
10 typedef struct Slave Slave;
11 typedef struct Ebuf Ebuf;
13 struct Slave
14 {
15 int inuse;
16 Ebuf *head; /* queue of messages for this descriptor */
17 Ebuf *tail;
18 int (*fn)(int, Event*, uchar*, int);
19 Muxrpc *rpc;
20 vlong nexttick;
21 int fd;
22 int n;
23 };
25 struct Ebuf
26 {
27 Ebuf *next;
28 int n; /* number of bytes in buf */
29 union {
30 uchar buf[EMAXMSG];
31 Rune rune;
32 Mouse mouse;
33 } u;
34 };
36 static Slave eslave[MAXSLAVE];
37 static int Skeyboard = -1;
38 static int Smouse = -1;
39 static int Stimer = -1;
41 static int nslave;
42 static int newkey(ulong);
43 static int extract(int canblock);
45 static
46 Ebuf*
47 ebread(Slave *s)
48 {
49 Ebuf *eb;
51 while(!s->head)
52 extract(1);
53 eb = s->head;
54 s->head = s->head->next;
55 if(s->head == 0)
56 s->tail = 0;
57 return eb;
58 }
60 ulong
61 event(Event *e)
62 {
63 return eread(~0UL, e);
64 }
66 ulong
67 eread(ulong keys, Event *e)
68 {
69 Ebuf *eb;
70 int i, id;
72 if(keys == 0)
73 return 0;
74 for(;;){
75 for(i=0; i<nslave; i++)
76 if((keys & (1<<i)) && eslave[i].head){
77 id = 1<<i;
78 if(i == Smouse)
79 e->mouse = emouse();
80 else if(i == Skeyboard)
81 e->kbdc = ekbd();
82 else if(i == Stimer)
83 eslave[i].head = 0;
84 else{
85 eb = ebread(&eslave[i]);
86 e->n = eb->n;
87 if(eslave[i].fn)
88 id = (*eslave[i].fn)(id, e, eb->u.buf, eb->n);
89 else
90 memmove(e->data, eb->u.buf, eb->n);
91 free(eb);
92 }
93 return id;
94 }
95 extract(0);
96 }
97 return 0;
98 }
100 int
101 ecanmouse(void)
103 if(Smouse < 0)
104 drawerror(display, "events: mouse not initialized");
105 return ecanread(Emouse);
108 int
109 ecankbd(void)
111 if(Skeyboard < 0)
112 drawerror(display, "events: keyboard not initialzed");
113 return ecanread(Ekeyboard);
116 int
117 ecanread(ulong keys)
119 int i;
121 for(;;){
122 for(i=0; i<nslave; i++)
123 if((keys & (1<<i)) && eslave[i].head)
124 return 1;
125 if(!extract(0))
126 return 0;
128 return -1;
131 ulong
132 estartfn(ulong key, int fd, int n, int (*fn)(int, Event*, uchar*, int))
134 int i;
136 if(fd < 0)
137 drawerror(display, "events: bad file descriptor");
138 if(n <= 0 || n > EMAXMSG)
139 n = EMAXMSG;
140 i = newkey(key);
141 eslave[i].fn = fn;
142 eslave[i].fd = fd;
143 eslave[i].n = n;
144 return 1<<i;
147 ulong
148 estart(ulong key, int fd, int n)
150 return estartfn(key, fd, n, nil);
153 ulong
154 etimer(ulong key, int n)
156 if(Stimer != -1)
157 drawerror(display, "events: timer started twice");
158 Stimer = newkey(key);
159 if(n <= 0)
160 n = 1000;
161 eslave[Stimer].n = n;
162 eslave[Stimer].nexttick = nsec()+n*1000LL;
163 return 1<<Stimer;
166 void
167 einit(ulong keys)
169 if(keys&Ekeyboard){
170 for(Skeyboard=0; Ekeyboard & ~(1<<Skeyboard); Skeyboard++)
172 eslave[Skeyboard].inuse = 1;
173 if(nslave <= Skeyboard)
174 nslave = Skeyboard+1;
176 if(keys&Emouse){
177 for(Smouse=0; Emouse & ~(1<<Smouse); Smouse++)
179 eslave[Smouse].inuse = 1;
180 if(nslave <= Smouse)
181 nslave = Smouse+1;
185 static Ebuf*
186 newebuf(Slave *s, int n)
188 Ebuf *eb;
190 eb = malloc(sizeof(*eb) - sizeof(eb->u.buf) + n);
191 if(eb == nil)
192 drawerror(display, "events: out of memory");
193 eb->n = n;
194 eb->next = 0;
195 if(s->head)
196 s->tail = s->tail->next = eb;
197 else
198 s->head = s->tail = eb;
199 return eb;
202 static Muxrpc*
203 startrpc(int type)
205 uchar buf[100];
206 Wsysmsg w;
208 w.type = type;
209 convW2M(&w, buf, sizeof buf);
210 return muxrpcstart(display->mux, buf);
213 static int
214 finishrpc(Muxrpc *r, Wsysmsg *w)
216 uchar *p;
217 int n;
219 if((p = muxrpccanfinish(r)) == nil)
220 return 0;
221 GET(p, n);
222 convM2W(p, n, w);
223 free(p);
224 return 1;
227 static int
228 extract(int canblock)
230 Ebuf *eb;
231 int i, n, max;
232 fd_set rset, wset, xset;
233 struct timeval tv, *timeout;
234 Wsysmsg w;
235 vlong t0;
237 /*
238 * Flush draw buffer before waiting for responses.
239 * Avoid doing so if buffer is empty.
240 * Also make sure that we don't interfere with app-specific locking.
241 */
242 if(display->locking){
243 /*
244 * if locking is being done by program,
245 * this means it can't depend on automatic
246 * flush in emouse() etc.
247 */
248 if(canqlock(&display->qlock)){
249 if(display->bufp > display->buf)
250 flushimage(display, 1);
251 unlockdisplay(display);
253 }else
254 if(display->bufp > display->buf)
255 flushimage(display, 1);
257 /*
258 * Set up for select.
259 */
260 FD_ZERO(&rset);
261 FD_ZERO(&wset);
262 FD_ZERO(&xset);
263 max = -1;
264 timeout = nil;
265 for(i=0; i<nslave; i++){
266 if(!eslave[i].inuse)
267 continue;
268 if(i == Smouse){
269 if(eslave[i].rpc == nil)
270 eslave[i].rpc = startrpc(Trdmouse);
271 if(eslave[i].rpc){
272 FD_SET(display->srvfd, &rset);
273 FD_SET(display->srvfd, &xset);
274 if(display->srvfd > max)
275 max = display->srvfd;
277 }else if(i == Skeyboard){
278 if(eslave[i].rpc == nil)
279 eslave[i].rpc = startrpc(Trdkbd);
280 if(eslave[i].rpc){
281 FD_SET(display->srvfd, &rset);
282 FD_SET(display->srvfd, &xset);
283 if(display->srvfd > max)
284 max = display->srvfd;
286 }else if(i == Stimer){
287 t0 = nsec();
288 if(t0-eslave[i].nexttick <= 0){
289 tv.tv_sec = 0;
290 tv.tv_usec = 0;
291 }else{
292 tv.tv_sec = (t0-eslave[i].nexttick)/1000000000;
293 tv.tv_usec = (t0-eslave[i].nexttick)%1000000000 / 1000;
295 timeout = &tv;
296 }else{
297 FD_SET(eslave[i].fd, &rset);
298 FD_SET(eslave[i].fd, &xset);
299 if(eslave[i].fd > max)
300 max = eslave[i].fd;
304 if(!canblock){
305 tv.tv_sec = 0;
306 tv.tv_usec = 0;
307 timeout = &tv;
310 if(select(max+1, &rset, &wset, &xset, timeout) < 0)
311 drawerror(display, "select failure");
313 /*
314 * Look to see what can proceed.
315 */
316 n = 0;
317 for(i=0; i<nslave; i++){
318 if(!eslave[i].inuse)
319 continue;
320 if(i == Smouse){
321 if(finishrpc(eslave[i].rpc, &w)){
322 eslave[i].rpc = nil;
323 eb = newebuf(&eslave[i], sizeof(Mouse));
324 eb->u.mouse = w.mouse;
325 if(w.resized)
326 eresized(1);
327 n++;
329 }else if(i == Skeyboard){
330 if(finishrpc(eslave[i].rpc, &w)){
331 eslave[i].rpc = nil;
332 eb = newebuf(&eslave[i], sizeof(Rune)+2); /* +8: alignment */
333 eb->u.rune = w.rune;
334 n++;
336 }else if(i == Stimer){
337 t0 = nsec();
338 while(t0-eslave[i].nexttick > 0){
339 eslave[i].nexttick += eslave[i].n*1000LL;
340 eslave[i].head = (Ebuf*)1;
341 n++;
343 }else{
344 if(FD_ISSET(eslave[i].fd, &rset)){
345 eb = newebuf(&eslave[i], eslave[i].n);
346 eb->n = read(eslave[i].fd, eb->u.buf, eslave[i].n);
347 n++;
351 return n;
354 static int
355 newkey(ulong key)
357 int i;
359 for(i=0; i<MAXSLAVE; i++)
360 if((key & ~(1<<i)) == 0 && eslave[i].inuse == 0){
361 if(nslave <= i)
362 nslave = i + 1;
363 eslave[i].inuse = 1;
364 return i;
366 drawerror(display, "events: bad slave assignment");
367 return 0;
370 Mouse
371 emouse(void)
373 Mouse m;
374 Ebuf *eb;
376 if(Smouse < 0)
377 drawerror(display, "events: mouse not initialized");
378 eb = ebread(&eslave[Smouse]);
379 m = eb->u.mouse;
380 free(eb);
381 return m;
384 int
385 ekbd(void)
387 Ebuf *eb;
388 int c;
390 if(Skeyboard < 0)
391 drawerror(display, "events: keyboard not initialzed");
392 eb = ebread(&eslave[Skeyboard]);
393 c = eb->u.rune;
394 free(eb);
395 return c;
398 void
399 emoveto(Point pt)
401 _displaymoveto(display, pt);
404 void
405 esetcursor(Cursor *c)
407 _displaycursor(display, c);
410 int
411 ereadmouse(Mouse *m)
413 int resized;
415 resized = 0;
416 if(_displayrdmouse(display, m, &resized) < 0)
417 return -1;
418 if(resized)
419 eresized(1);
420 return 1;