xcb_poll_for_event: Return already-read events before read(2)ing again.
[free-sw/xcb/libxcb] / src / xcb_in.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* Stuff that reads stuff from the server. */
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <sys/select.h>
34 #include <errno.h>
35
36 #include "xcb.h"
37 #include "xcbext.h"
38 #include "xcbint.h"
39
40 #define XCB_ERROR 0
41 #define XCB_REPLY 1
42
43 struct event_list {
44     xcb_generic_event_t *event;
45     struct event_list *next;
46 };
47
48 struct reply_list {
49     void *reply;
50     struct reply_list *next;
51 };
52
53 typedef struct pending_reply {
54     unsigned int request;
55     enum workarounds workaround;
56     int flags;
57     struct pending_reply *next;
58 } pending_reply;
59
60 typedef struct reader_list {
61     unsigned int request;
62     pthread_cond_t *data;
63     struct reader_list *next;
64 } reader_list;
65
66 static void wake_up_next_reader(xcb_connection_t *c)
67 {
68     int pthreadret;
69     if(c->in.readers)
70         pthreadret = pthread_cond_signal(c->in.readers->data);
71     else
72         pthreadret = pthread_cond_signal(&c->in.event_cond);
73     assert(pthreadret == 0);
74 }
75
76 static int read_packet(xcb_connection_t *c)
77 {
78     xcb_generic_reply_t genrep;
79     int length = 32;
80     void *buf;
81     pending_reply *pend = 0;
82     struct event_list *event;
83
84     /* Wait for there to be enough data for us to read a whole packet */
85     if(c->in.queue_len < length)
86         return 0;
87
88     /* Get the response type, length, and sequence number. */
89     memcpy(&genrep, c->in.queue, sizeof(genrep));
90
91     /* Compute 32-bit sequence number of this packet. */
92     if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
93     {
94         unsigned int lastread = c->in.request_read;
95         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
96         if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
97             c->in.request_read += 0x10000;
98         if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
99             c->in.request_expected = c->in.request_read;
100
101         if(c->in.request_read != lastread)
102         {
103             if(c->in.current_reply)
104             {
105                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
106                 c->in.current_reply = 0;
107                 c->in.current_reply_tail = &c->in.current_reply;
108             }
109             c->in.request_completed = c->in.request_read - 1;
110         }
111
112         while(c->in.pending_replies && 
113               XCB_SEQUENCE_COMPARE (c->in.pending_replies->request, <=, c->in.request_completed))
114         {
115             pending_reply *oldpend = c->in.pending_replies;
116             c->in.pending_replies = oldpend->next;
117             if(!oldpend->next)
118                 c->in.pending_replies_tail = &c->in.pending_replies;
119             free(oldpend);
120         }
121
122         if(genrep.response_type == XCB_ERROR)
123             c->in.request_completed = c->in.request_read;
124     }
125
126     if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
127     {
128         pend = c->in.pending_replies;
129         if(pend && pend->request != c->in.request_read)
130             pend = 0;
131     }
132
133     /* For reply packets, check that the entire packet is available. */
134     if(genrep.response_type == XCB_REPLY)
135     {
136         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
137         {
138             uint32_t *p = (uint32_t *) c->in.queue;
139             genrep.length = p[2] * p[3] * 2;
140         }
141         length += genrep.length * 4;
142     }
143
144     buf = malloc(length + (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
145     if(!buf)
146     {
147         _xcb_conn_shutdown(c);
148         return 0;
149     }
150     if(_xcb_in_read_block(c, buf, length) <= 0)
151     {
152         free(buf);
153         return 0;
154     }
155     if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
156     {
157         free(buf);
158         return 1;
159     }
160
161     if(genrep.response_type != XCB_REPLY)
162         ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
163
164     /* reply, or checked error */
165     if( genrep.response_type == XCB_REPLY ||
166        (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
167     {
168         reader_list *reader;
169         struct reply_list *cur = malloc(sizeof(struct reply_list));
170         if(!cur)
171         {
172             _xcb_conn_shutdown(c);
173             return 0;
174         }
175         cur->reply = buf;
176         cur->next = 0;
177         *c->in.current_reply_tail = cur;
178         c->in.current_reply_tail = &cur->next;
179         for(reader = c->in.readers; 
180             reader && 
181             XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read);
182             reader = reader->next)
183         {
184             if(reader->request == c->in.request_read)
185             {
186                 pthread_cond_signal(reader->data);
187                 break;
188             }
189         }
190         return 1;
191     }
192
193     /* event, or unchecked error */
194     event = malloc(sizeof(struct event_list));
195     if(!event)
196     {
197         _xcb_conn_shutdown(c);
198         free(buf);
199         return 0;
200     }
201     event->event = buf;
202     event->next = 0;
203     *c->in.events_tail = event;
204     c->in.events_tail = &event->next;
205     pthread_cond_signal(&c->in.event_cond);
206     return 1; /* I have something for you... */
207 }
208
209 static xcb_generic_event_t *get_event(xcb_connection_t *c)
210 {
211     struct event_list *cur = c->in.events;
212     xcb_generic_event_t *ret;
213     if(!c->in.events)
214         return 0;
215     ret = cur->event;
216     c->in.events = cur->next;
217     if(!cur->next)
218         c->in.events_tail = &c->in.events;
219     free(cur);
220     return ret;
221 }
222
223 static void free_reply_list(struct reply_list *head)
224 {
225     while(head)
226     {
227         struct reply_list *cur = head;
228         head = cur->next;
229         free(cur->reply);
230         free(cur);
231     }
232 }
233
234 static int read_block(const int fd, void *buf, const size_t len)
235 {
236     int done = 0;
237     while(done < len)
238     {
239         int ret = read(fd, ((char *) buf) + done, len - done);
240         if(ret > 0)
241             done += ret;
242         if(ret < 0 && errno == EAGAIN)
243         {
244             fd_set fds;
245             FD_ZERO(&fds);
246             FD_SET(fd, &fds);
247             do {
248                 ret = select(fd + 1, &fds, 0, 0, 0);
249             } while (ret == -1 && errno == EINTR);
250         }
251         if(ret <= 0)
252             return ret;
253     }
254     return len;
255 }
256
257 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
258 {
259     struct reply_list *head;
260
261     /* If an error occurred when issuing the request, fail immediately. */
262     if(!request)
263         head = 0;
264     /* We've read requests past the one we want, so if it has replies we have
265      * them all and they're in the replies map. */
266     else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
267     {
268         head = _xcb_map_remove(c->in.replies, request);
269         if(head && head->next)
270             _xcb_map_put(c->in.replies, request, head->next);
271     }
272     /* We're currently processing the responses to the request we want, and we
273      * have a reply ready to return. So just return it without blocking. */
274     else if(request == c->in.request_read && c->in.current_reply)
275     {
276         head = c->in.current_reply;
277         c->in.current_reply = head->next;
278         if(!head->next)
279             c->in.current_reply_tail = &c->in.current_reply;
280     }
281     /* We know this request can't have any more replies, and we've already
282      * established it doesn't have a reply now. Don't bother blocking. */
283     else if(request == c->in.request_completed)
284         head = 0;
285     /* We may have more replies on the way for this request: block until we're
286      * sure. */
287     else
288         return 0;
289
290     if(error)
291         *error = 0;
292     *reply = 0;
293
294     if(head)
295     {
296         if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
297         {
298             if(error)
299                 *error = head->reply;
300             else
301                 free(head->reply);
302         }
303         else
304             *reply = head->reply;
305
306         free(head);
307     }
308
309     return 1;
310 }
311
312 /* Public interface */
313
314 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
315 {
316     void *ret = 0;
317     if(e)
318         *e = 0;
319     if(c->has_error)
320         return 0;
321
322     _xcb_lock_io(c);
323
324     /* If this request has not been written yet, write it. */
325     if(_xcb_out_flush_to(c, request))
326     {
327         pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
328         reader_list reader;
329         reader_list **prev_reader;
330
331         for(prev_reader = &c->in.readers; 
332             *prev_reader && 
333             XCB_SEQUENCE_COMPARE ((*prev_reader)->request, <=, request);
334             prev_reader = &(*prev_reader)->next)
335         {
336             /* empty */;
337         }
338         reader.request = request;
339         reader.data = &cond;
340         reader.next = *prev_reader;
341         *prev_reader = &reader;
342
343         while(!poll_for_reply(c, request, &ret, e))
344             if(!_xcb_conn_wait(c, &cond, 0, 0))
345                 break;
346
347         for(prev_reader = &c->in.readers;
348             *prev_reader && 
349             XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
350             prev_reader = &(*prev_reader)->next)
351         {
352             if(*prev_reader == &reader)
353             {
354                 *prev_reader = (*prev_reader)->next;
355                 break;
356             }
357         }
358         pthread_cond_destroy(&cond);
359     }
360
361     wake_up_next_reader(c);
362     _xcb_unlock_io(c);
363     return ret;
364 }
365
366 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
367 {
368     int ret;
369     if(c->has_error)
370     {
371         *reply = 0;
372         if(error)
373             *error = 0;
374         return 1; /* would not block */
375     }
376     assert(reply != 0);
377     _xcb_lock_io(c);
378     ret = poll_for_reply(c, request, reply, error);
379     _xcb_unlock_io(c);
380     return ret;
381 }
382
383 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
384 {
385     xcb_generic_event_t *ret;
386     if(c->has_error)
387         return 0;
388     _xcb_lock_io(c);
389     /* get_event returns 0 on empty list. */
390     while(!(ret = get_event(c)))
391         if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
392             break;
393
394     wake_up_next_reader(c);
395     _xcb_unlock_io(c);
396     return ret;
397 }
398
399 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
400 {
401     xcb_generic_event_t *ret = 0;
402     if(!c->has_error)
403     {
404         _xcb_lock_io(c);
405         /* FIXME: follow X meets Z architecture changes. */
406         ret = get_event(c);
407         if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
408             ret = get_event(c);
409         _xcb_unlock_io(c);
410     }
411     return ret;
412 }
413
414 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
415 {
416     /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
417      * that would require factoring the locking out of xcb_get_input_focus,
418      * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
419     xcb_generic_error_t *ret;
420     void *reply;
421     if(c->has_error)
422         return 0;
423     if(XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_expected)
424        && XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_completed))
425     {
426         free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
427         assert(!ret);
428     }
429     reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
430     assert(!reply);
431     return ret;
432 }
433
434 /* Private interface */
435
436 int _xcb_in_init(_xcb_in *in)
437 {
438     if(pthread_cond_init(&in->event_cond, 0))
439         return 0;
440     in->reading = 0;
441
442     in->queue_len = 0;
443
444     in->request_read = 0;
445     in->request_completed = 0;
446
447     in->replies = _xcb_map_new();
448     if(!in->replies)
449         return 0;
450
451     in->current_reply_tail = &in->current_reply;
452     in->events_tail = &in->events;
453     in->pending_replies_tail = &in->pending_replies;
454
455     return 1;
456 }
457
458 void _xcb_in_destroy(_xcb_in *in)
459 {
460     pthread_cond_destroy(&in->event_cond);
461     free_reply_list(in->current_reply);
462     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
463     while(in->events)
464     {
465         struct event_list *e = in->events;
466         in->events = e->next;
467         free(e->event);
468         free(e);
469     }
470     while(in->pending_replies)
471     {
472         pending_reply *pend = in->pending_replies;
473         in->pending_replies = pend->next;
474         free(pend);
475     }
476 }
477
478 int _xcb_in_expect_reply(xcb_connection_t *c, unsigned int request, enum workarounds workaround, int flags)
479 {
480     pending_reply *pend = malloc(sizeof(pending_reply));
481     assert(workaround != WORKAROUND_NONE || flags != 0);
482     if(!pend)
483     {
484         _xcb_conn_shutdown(c);
485         return 0;
486     }
487     pend->request = request;
488     pend->workaround = workaround;
489     pend->flags = flags;
490     pend->next = 0;
491     *c->in.pending_replies_tail = pend;
492     c->in.pending_replies_tail = &pend->next;
493     return 1;
494 }
495
496 int _xcb_in_read(xcb_connection_t *c)
497 {
498     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
499     if(n > 0)
500         c->in.queue_len += n;
501     while(read_packet(c))
502         /* empty */;
503     if((n > 0) || (n < 0 && errno == EAGAIN))
504         return 1;
505     _xcb_conn_shutdown(c);
506     return 0;
507 }
508
509 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
510 {
511     int done = c->in.queue_len;
512     if(len < done)
513         done = len;
514
515     memcpy(buf, c->in.queue, done);
516     c->in.queue_len -= done;
517     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
518
519     if(len > done)
520     {
521         int ret = read_block(c->fd, (char *) buf + done, len - done);
522         if(ret <= 0)
523         {
524             _xcb_conn_shutdown(c);
525             return ret;
526         }
527     }
528
529     return len;
530 }