Remove the 'int *error' out-parameter for xcb_poll_for_event.
[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         if(genrep.response_type == XCB_ERROR)
112             c->in.request_completed = c->in.request_read;
113
114         while(c->in.pending_replies && 
115               XCB_SEQUENCE_COMPARE (c->in.pending_replies->request, <=, c->in.request_completed))
116         {
117             pending_reply *oldpend = c->in.pending_replies;
118             c->in.pending_replies = oldpend->next;
119             if(!oldpend->next)
120                 c->in.pending_replies_tail = &c->in.pending_replies;
121             free(oldpend);
122         }
123     }
124
125     if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
126     {
127         pend = c->in.pending_replies;
128         if(pend && pend->request != c->in.request_read)
129             pend = 0;
130     }
131
132     /* For reply packets, check that the entire packet is available. */
133     if(genrep.response_type == XCB_REPLY)
134     {
135         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
136         {
137             uint32_t *p = (uint32_t *) c->in.queue;
138             genrep.length = p[2] * p[3] * 2;
139         }
140         length += genrep.length * 4;
141     }
142
143     buf = malloc(length + (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
144     if(!buf)
145     {
146         _xcb_conn_shutdown(c);
147         return 0;
148     }
149     if(_xcb_in_read_block(c, buf, length) <= 0)
150     {
151         free(buf);
152         return 0;
153     }
154     if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
155     {
156         free(buf);
157         return 1;
158     }
159
160     if(genrep.response_type != XCB_REPLY)
161         ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
162
163     /* reply, or checked error */
164     if( genrep.response_type == XCB_REPLY ||
165        (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
166     {
167         reader_list *reader;
168         struct reply_list *cur = malloc(sizeof(struct reply_list));
169         if(!cur)
170         {
171             _xcb_conn_shutdown(c);
172             return 0;
173         }
174         cur->reply = buf;
175         cur->next = 0;
176         *c->in.current_reply_tail = cur;
177         c->in.current_reply_tail = &cur->next;
178         for(reader = c->in.readers; 
179             reader && 
180             XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read);
181             reader = reader->next)
182         {
183             if(reader->request == c->in.request_read)
184             {
185                 pthread_cond_signal(reader->data);
186                 break;
187             }
188         }
189         return 1;
190     }
191
192     /* event, or unchecked error */
193     event = malloc(sizeof(struct event_list));
194     if(!event)
195     {
196         _xcb_conn_shutdown(c);
197         free(buf);
198         return 0;
199     }
200     event->event = buf;
201     event->next = 0;
202     *c->in.events_tail = event;
203     c->in.events_tail = &event->next;
204     pthread_cond_signal(&c->in.event_cond);
205     return 1; /* I have something for you... */
206 }
207
208 static xcb_generic_event_t *get_event(xcb_connection_t *c)
209 {
210     struct event_list *cur = c->in.events;
211     xcb_generic_event_t *ret;
212     if(!c->in.events)
213         return 0;
214     ret = cur->event;
215     c->in.events = cur->next;
216     if(!cur->next)
217         c->in.events_tail = &c->in.events;
218     free(cur);
219     return ret;
220 }
221
222 static void free_reply_list(struct reply_list *head)
223 {
224     while(head)
225     {
226         struct reply_list *cur = head;
227         head = cur->next;
228         free(cur->reply);
229         free(cur);
230     }
231 }
232
233 static int read_block(const int fd, void *buf, const size_t len)
234 {
235     int done = 0;
236     while(done < len)
237     {
238         int ret = read(fd, ((char *) buf) + done, len - done);
239         if(ret > 0)
240             done += ret;
241         if(ret < 0 && errno == EAGAIN)
242         {
243             fd_set fds;
244             FD_ZERO(&fds);
245             FD_SET(fd, &fds);
246             do {
247                 ret = select(fd + 1, &fds, 0, 0, 0);
248             } while (ret == -1 && errno == EINTR);
249         }
250         if(ret <= 0)
251             return ret;
252     }
253     return len;
254 }
255
256 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
257 {
258     struct reply_list *head;
259
260     /* If an error occurred when issuing the request, fail immediately. */
261     if(!request)
262         head = 0;
263     /* We've read requests past the one we want, so if it has replies we have
264      * them all and they're in the replies map. */
265     else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
266     {
267         head = _xcb_map_remove(c->in.replies, request);
268         if(head && head->next)
269             _xcb_map_put(c->in.replies, request, head->next);
270     }
271     /* We're currently processing the responses to the request we want, and we
272      * have a reply ready to return. So just return it without blocking. */
273     else if(request == c->in.request_read && c->in.current_reply)
274     {
275         head = c->in.current_reply;
276         c->in.current_reply = head->next;
277         if(!head->next)
278             c->in.current_reply_tail = &c->in.current_reply;
279     }
280     /* We know this request can't have any more replies, and we've already
281      * established it doesn't have a reply now. Don't bother blocking. */
282     else if(request == c->in.request_completed)
283         head = 0;
284     /* We may have more replies on the way for this request: block until we're
285      * sure. */
286     else
287         return 0;
288
289     if(error)
290         *error = 0;
291     *reply = 0;
292
293     if(head)
294     {
295         if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
296         {
297             if(error)
298                 *error = head->reply;
299             else
300                 free(head->reply);
301         }
302         else
303             *reply = head->reply;
304
305         free(head);
306     }
307
308     return 1;
309 }
310
311 /* Public interface */
312
313 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
314 {
315     void *ret = 0;
316     if(e)
317         *e = 0;
318     if(c->has_error)
319         return 0;
320
321     _xcb_lock_io(c);
322
323     /* If this request has not been written yet, write it. */
324     if(_xcb_out_flush_to(c, request))
325     {
326         pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
327         reader_list reader;
328         reader_list **prev_reader;
329
330         for(prev_reader = &c->in.readers; 
331             *prev_reader && 
332             XCB_SEQUENCE_COMPARE ((*prev_reader)->request, <=, request);
333             prev_reader = &(*prev_reader)->next)
334         {
335             /* empty */;
336         }
337         reader.request = request;
338         reader.data = &cond;
339         reader.next = *prev_reader;
340         *prev_reader = &reader;
341
342         while(!poll_for_reply(c, request, &ret, e))
343             if(!_xcb_conn_wait(c, &cond, 0, 0))
344                 break;
345
346         for(prev_reader = &c->in.readers;
347             *prev_reader && 
348             XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
349             prev_reader = &(*prev_reader)->next)
350         {
351             if(*prev_reader == &reader)
352             {
353                 *prev_reader = (*prev_reader)->next;
354                 break;
355             }
356         }
357         pthread_cond_destroy(&cond);
358     }
359
360     wake_up_next_reader(c);
361     _xcb_unlock_io(c);
362     return ret;
363 }
364
365 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
366 {
367     int ret;
368     if(c->has_error)
369     {
370         *reply = 0;
371         if(error)
372             *error = 0;
373         return 1; /* would not block */
374     }
375     assert(reply != 0);
376     _xcb_lock_io(c);
377     ret = poll_for_reply(c, request, reply, error);
378     _xcb_unlock_io(c);
379     return ret;
380 }
381
382 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
383 {
384     xcb_generic_event_t *ret;
385     if(c->has_error)
386         return 0;
387     _xcb_lock_io(c);
388     /* get_event returns 0 on empty list. */
389     while(!(ret = get_event(c)))
390         if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
391             break;
392
393     wake_up_next_reader(c);
394     _xcb_unlock_io(c);
395     return ret;
396 }
397
398 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
399 {
400     xcb_generic_event_t *ret = 0;
401     if(!c->has_error)
402     {
403         _xcb_lock_io(c);
404         /* FIXME: follow X meets Z architecture changes. */
405         if(_xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
406             ret = get_event(c);
407         _xcb_unlock_io(c);
408     }
409     return ret;
410 }
411
412 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
413 {
414     /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
415      * that would require factoring the locking out of xcb_get_input_focus,
416      * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
417     xcb_generic_error_t *ret;
418     void *reply;
419     if(c->has_error)
420         return 0;
421     if(XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_expected)
422        && XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_completed))
423     {
424         free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
425         assert(!ret);
426     }
427     reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
428     assert(!reply);
429     return ret;
430 }
431
432 /* Private interface */
433
434 int _xcb_in_init(_xcb_in *in)
435 {
436     if(pthread_cond_init(&in->event_cond, 0))
437         return 0;
438     in->reading = 0;
439
440     in->queue_len = 0;
441
442     in->request_read = 0;
443     in->request_completed = 0;
444
445     in->replies = _xcb_map_new();
446     if(!in->replies)
447         return 0;
448
449     in->current_reply_tail = &in->current_reply;
450     in->events_tail = &in->events;
451     in->pending_replies_tail = &in->pending_replies;
452
453     return 1;
454 }
455
456 void _xcb_in_destroy(_xcb_in *in)
457 {
458     pthread_cond_destroy(&in->event_cond);
459     free_reply_list(in->current_reply);
460     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
461     while(in->events)
462     {
463         struct event_list *e = in->events;
464         in->events = e->next;
465         free(e->event);
466         free(e);
467     }
468     while(in->pending_replies)
469     {
470         pending_reply *pend = in->pending_replies;
471         in->pending_replies = pend->next;
472         free(pend);
473     }
474 }
475
476 int _xcb_in_expect_reply(xcb_connection_t *c, unsigned int request, enum workarounds workaround, int flags)
477 {
478     pending_reply *pend = malloc(sizeof(pending_reply));
479     assert(workaround != WORKAROUND_NONE || flags != 0);
480     if(!pend)
481     {
482         _xcb_conn_shutdown(c);
483         return 0;
484     }
485     pend->request = request;
486     pend->workaround = workaround;
487     pend->flags = flags;
488     pend->next = 0;
489     *c->in.pending_replies_tail = pend;
490     c->in.pending_replies_tail = &pend->next;
491     return 1;
492 }
493
494 int _xcb_in_read(xcb_connection_t *c)
495 {
496     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
497     if(n > 0)
498         c->in.queue_len += n;
499     while(read_packet(c))
500         /* empty */;
501     if((n > 0) || (n < 0 && errno == EAGAIN))
502         return 1;
503     _xcb_conn_shutdown(c);
504     return 0;
505 }
506
507 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
508 {
509     int done = c->in.queue_len;
510     if(len < done)
511         done = len;
512
513     memcpy(buf, c->in.queue, done);
514     c->in.queue_len -= done;
515     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
516
517     if(len > done)
518     {
519         int ret = read_block(c->fd, (char *) buf + done, len - done);
520         if(ret <= 0)
521         {
522             _xcb_conn_shutdown(c);
523             return ret;
524         }
525     }
526
527     return len;
528 }