Fixed poll_for_reply, added comments, and refactored XCBWaitForReply to call poll_for...
[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 <errno.h>
34
35 #include "xcb.h"
36 #include "xcbext.h"
37 #include "xcbint.h"
38
39 #define XCBError 0
40 #define XCBReply 1
41
42 struct event_list {
43     XCBGenericEvent *event;
44     struct event_list *next;
45 };
46
47 struct reply_list {
48     void *reply;
49     struct reply_list *next;
50 };
51
52 typedef struct pending_reply {
53     unsigned int request;
54     enum workarounds workaround;
55     int flags;
56     struct pending_reply *next;
57 } pending_reply;
58
59 typedef struct reader_list {
60     unsigned int request;
61     pthread_cond_t *data;
62     struct reader_list *next;
63 } reader_list;
64
65 static void wake_up_next_reader(XCBConnection *c)
66 {
67     int pthreadret;
68     if(c->in.readers)
69         pthreadret = pthread_cond_signal(c->in.readers->data);
70     else
71         pthreadret = pthread_cond_signal(&c->in.event_cond);
72     assert(pthreadret == 0);
73 }
74
75 static int read_packet(XCBConnection *c)
76 {
77     XCBGenericRep genrep;
78     int length = 32;
79     void *buf;
80     pending_reply *pend = 0;
81     struct event_list *event;
82
83     /* Wait for there to be enough data for us to read a whole packet */
84     if(c->in.queue_len < length)
85         return 0;
86
87     /* Get the response type, length, and sequence number. */
88     memcpy(&genrep, c->in.queue, sizeof(genrep));
89
90     /* Compute 32-bit sequence number of this packet. */
91     if((genrep.response_type & 0x7f) != XCBKeymapNotify)
92     {
93         int lastread = c->in.request_read;
94         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
95         if(c->in.request_read < lastread)
96             c->in.request_read += 0x10000;
97         if(c->in.request_read > c->in.request_expected)
98             c->in.request_expected = c->in.request_read;
99
100         if(c->in.request_read != lastread)
101         {
102             if(c->in.current_reply)
103             {
104                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
105                 c->in.current_reply = 0;
106                 c->in.current_reply_tail = &c->in.current_reply;
107             }
108             c->in.request_completed = c->in.request_read - 1;
109         }
110         if(genrep.response_type != XCBReply) /* error or event */
111             c->in.request_completed = c->in.request_read; /* XXX: does event/error imply no more replies? */
112
113         while(c->in.pending_replies && 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
123     if(genrep.response_type == XCBError || genrep.response_type == XCBReply)
124     {
125         pend = c->in.pending_replies;
126         if(pend && pend->request != c->in.request_read)
127             pend = 0;
128     }
129
130     /* For reply packets, check that the entire packet is available. */
131     if(genrep.response_type == XCBReply)
132     {
133         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
134         {
135             CARD32 *p = (CARD32 *) c->in.queue;
136             genrep.length = p[2] * p[3] * 2;
137         }
138         length += genrep.length * 4;
139     }
140
141     buf = malloc(length + (genrep.response_type == XCBReply ? 0 : sizeof(CARD32)));
142     if(!buf)
143         return 0;
144     if(_xcb_in_read_block(c, buf, length) <= 0)
145     {
146         free(buf);
147         return 0;
148     }
149     if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
150     {
151         free(buf);
152         return 1;
153     }
154
155     if(genrep.response_type != XCBReply)
156         ((XCBGenericEvent *) buf)->full_sequence = c->in.request_read;
157
158     /* reply, or checked error */
159     if( genrep.response_type == XCBReply ||
160        (genrep.response_type == XCBError && pend && (pend->flags & XCB_REQUEST_CHECKED)))
161     {
162         reader_list *reader;
163         struct reply_list *cur = malloc(sizeof(struct reply_list));
164         if(!cur)
165             return 0;
166         cur->reply = buf;
167         cur->next = 0;
168         *c->in.current_reply_tail = cur;
169         c->in.current_reply_tail = &cur->next;
170         for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
171             if(reader->request == c->in.request_read)
172             {
173                 pthread_cond_signal(reader->data);
174                 break;
175             }
176         return 1;
177     }
178
179     /* event, or unchecked error */
180     event = malloc(sizeof(struct event_list));
181     if(!event)
182     {
183         free(buf);
184         return 0;
185     }
186     event->event = buf;
187     event->next = 0;
188     *c->in.events_tail = event;
189     c->in.events_tail = &event->next;
190     pthread_cond_signal(&c->in.event_cond);
191     return 1; /* I have something for you... */
192 }
193
194 static XCBGenericEvent *get_event(XCBConnection *c)
195 {
196     struct event_list *cur = c->in.events;
197     XCBGenericEvent *ret;
198     if(!c->in.events)
199         return 0;
200     ret = cur->event;
201     c->in.events = cur->next;
202     if(!cur->next)
203         c->in.events_tail = &c->in.events;
204     free(cur);
205     return ret;
206 }
207
208 static void free_reply_list(struct reply_list *head)
209 {
210     while(head)
211     {
212         struct reply_list *cur = head;
213         head = cur->next;
214         free(cur->reply);
215         free(cur);
216     }
217 }
218
219 static int read_block(const int fd, void *buf, const size_t len)
220 {
221     int done = 0;
222     while(done < len)
223     {
224         int ret = read(fd, ((char *) buf) + done, len - done);
225         if(ret > 0)
226             done += ret;
227         if(ret < 0 && errno == EAGAIN)
228         {
229             fd_set fds;
230             FD_ZERO(&fds);
231             FD_SET(fd, &fds);
232             do {
233                 ret = select(fd + 1, &fds, 0, 0, 0);
234             } while (ret == -1 && errno == EINTR);
235         }
236         if(ret <= 0)
237             return ret;
238     }
239     return len;
240 }
241
242 static int poll_for_reply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
243 {
244     struct reply_list *head;
245
246     /* If an error occurred when issuing the request, fail immediately. */
247     if(!request)
248         head = 0;
249     /* We've read requests past the one we want, so if it has replies we have
250      * them all and they're in the replies map. */
251     else if(request < c->in.request_read)
252     {
253         head = _xcb_map_remove(c->in.replies, request);
254         if(head && head->next)
255             _xcb_map_put(c->in.replies, request, head->next);
256     }
257     /* We're currently processing the responses to the request we want, and we
258      * have a reply ready to return. So just return it without blocking. */
259     else if(request == c->in.request_read && c->in.current_reply)
260     {
261         head = c->in.current_reply;
262         c->in.current_reply = head->next;
263         if(!head->next)
264             c->in.current_reply_tail = &c->in.current_reply;
265     }
266     /* We know this request can't have any more replies, and we've already
267      * established it doesn't have a reply now. Don't bother blocking. */
268     else if(request == c->in.request_completed)
269         head = 0;
270     /* We may have more replies on the way for this request: block until we're
271      * sure. */
272     else
273         return 0;
274
275     if(error)
276         *error = 0;
277     *reply = 0;
278
279     if(head)
280     {
281         if(((XCBGenericRep *) head->reply)->response_type == XCBError)
282         {
283             if(error)
284                 *error = head->reply;
285             else
286                 free(head->reply);
287         }
288         else
289             *reply = head->reply;
290
291         free(head);
292     }
293
294     return 1;
295 }
296
297 /* Public interface */
298
299 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
300 {
301     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
302     reader_list reader;
303     reader_list **prev_reader;
304     void *ret = 0;
305     if(e)
306         *e = 0;
307
308     pthread_mutex_lock(&c->iolock);
309
310     /* If this request has not been written yet, write it. */
311     if(!_xcb_out_flush_to(c, request))
312         goto done; /* error */
313
314     for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
315         if((*prev_reader)->request == request)
316             goto done; /* error */
317
318     reader.request = request;
319     reader.data = &cond;
320     reader.next = *prev_reader;
321     *prev_reader = &reader;
322
323     /* If this request has not completed yet and has no reply waiting,
324      * wait for one. */
325     while(!poll_for_reply(c, request, &ret, e))
326         if(!_xcb_conn_wait(c, &cond, 0, 0))
327             goto done;
328
329 done:
330     for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
331         if(*prev_reader == &reader)
332         {
333             *prev_reader = (*prev_reader)->next;
334             break;
335         }
336     pthread_cond_destroy(&cond);
337
338     wake_up_next_reader(c);
339     pthread_mutex_unlock(&c->iolock);
340     return ret;
341 }
342
343 int XCBPollForReply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
344 {
345     int ret;
346     assert(reply != 0);
347     pthread_mutex_lock(&c->iolock);
348     ret = poll_for_reply(c, request, reply, error);
349     pthread_mutex_unlock(&c->iolock);
350     return ret;
351 }
352
353 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
354 {
355     return XCBWaitForEvent(c);
356 }
357
358 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
359 {
360     XCBGenericEvent *ret;
361     pthread_mutex_lock(&c->iolock);
362     /* get_event returns 0 on empty list. */
363     while(!(ret = get_event(c)))
364         if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
365             break;
366
367     wake_up_next_reader(c);
368     pthread_mutex_unlock(&c->iolock);
369     return ret;
370 }
371
372 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
373 {
374     XCBGenericEvent *ret = 0;
375     pthread_mutex_lock(&c->iolock);
376     if(error)
377         *error = 0;
378     /* FIXME: follow X meets Z architecture changes. */
379     if(_xcb_in_read(c))
380         ret = get_event(c);
381     else if(error)
382         *error = -1;
383     else
384     {
385         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
386         abort();
387     }
388     pthread_mutex_unlock(&c->iolock);
389     return ret;
390 }
391
392 unsigned int XCBGetRequestRead(XCBConnection *c)
393 {
394     unsigned int ret;
395     pthread_mutex_lock(&c->iolock);
396     /* FIXME: follow X meets Z architecture changes. */
397     _xcb_in_read(c);
398     ret = c->in.request_read;
399     pthread_mutex_unlock(&c->iolock);
400     return ret;
401 }
402
403 /* Private interface */
404
405 int _xcb_in_init(_xcb_in *in)
406 {
407     if(pthread_cond_init(&in->event_cond, 0))
408         return 0;
409     in->reading = 0;
410
411     in->queue_len = 0;
412
413     in->request_read = 0;
414     in->request_completed = 0;
415
416     in->replies = _xcb_map_new();
417     if(!in->replies)
418         return 0;
419
420     in->current_reply_tail = &in->current_reply;
421     in->events_tail = &in->events;
422     in->pending_replies_tail = &in->pending_replies;
423
424     return 1;
425 }
426
427 void _xcb_in_destroy(_xcb_in *in)
428 {
429     pthread_cond_destroy(&in->event_cond);
430     free_reply_list(in->current_reply);
431     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
432     while(in->events)
433     {
434         struct event_list *e = in->events;
435         in->events = e->next;
436         free(e->event);
437         free(e);
438     }
439     while(in->pending_replies)
440     {
441         pending_reply *pend = in->pending_replies;
442         in->pending_replies = pend->next;
443         free(pend);
444     }
445 }
446
447 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
448 {
449     if(workaround != WORKAROUND_NONE || flags != 0)
450     {
451         pending_reply *pend = malloc(sizeof(pending_reply));
452         if(!pend)
453             return 0;
454         pend->request = request;
455         pend->workaround = workaround;
456         pend->flags = flags;
457         pend->next = 0;
458         *c->in.pending_replies_tail = pend;
459         c->in.pending_replies_tail = &pend->next;
460     }
461     return 1;
462 }
463
464 int _xcb_in_read(XCBConnection *c)
465 {
466     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
467     if(n > 0)
468         c->in.queue_len += n;
469     while(read_packet(c))
470         /* empty */;
471     return (n > 0) || (n < 0 && errno == EAGAIN);
472 }
473
474 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
475 {
476     int done = c->in.queue_len;
477     if(len < done)
478         done = len;
479
480     memcpy(buf, c->in.queue, done);
481     c->in.queue_len -= done;
482     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
483
484     if(len > done)
485     {
486         int ret = read_block(c->fd, (char *) buf + done, len - done);
487         if(ret <= 0)
488             return ret;
489     }
490
491     return len;
492 }