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