The Great XCB Renaming
[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     pthread_mutex_lock(&c->iolock);
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     pthread_mutex_unlock(&c->iolock);
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     pthread_mutex_lock(&c->iolock);
377     ret = poll_for_reply(c, request, reply, error);
378     pthread_mutex_unlock(&c->iolock);
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     pthread_mutex_lock(&c->iolock);
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     pthread_mutex_unlock(&c->iolock);
395     return ret;
396 }
397
398 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c, int *error)
399 {
400     if(!c->has_error)
401     {
402         xcb_generic_event_t *ret = 0;
403         int success;
404         pthread_mutex_lock(&c->iolock);
405         /* FIXME: follow X meets Z architecture changes. */
406         success = _xcb_in_read(c);
407         if(success)
408             ret = get_event(c);
409         pthread_mutex_unlock(&c->iolock);
410         if(success)
411         {
412             if(error)
413                 *error = 0;
414             return ret;
415         }
416     }
417     if(error)
418         *error = -1;
419     else
420     {
421         fprintf(stderr, "xcb_poll_for_event_t: I/O error occured, but no handler provided.\n");
422         abort();
423     }
424     return 0;
425 }
426
427 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
428 {
429     /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
430      * that would require factoring the locking out of xcb_get_input_focus_t,
431      * xcb_get_input_focus_reply_t, and xcb_wait_for_reply_t. */
432     xcb_generic_error_t *ret;
433     void *reply;
434     if(c->has_error)
435         return 0;
436     if(XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_expected)
437        && XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_completed))
438     {
439         free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
440         assert(!ret);
441     }
442     reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
443     assert(!reply);
444     return ret;
445 }
446
447 /* Private interface */
448
449 int _xcb_in_init(_xcb_in *in)
450 {
451     if(pthread_cond_init(&in->event_cond, 0))
452         return 0;
453     in->reading = 0;
454
455     in->queue_len = 0;
456
457     in->request_read = 0;
458     in->request_completed = 0;
459
460     in->replies = _xcb_map_new();
461     if(!in->replies)
462         return 0;
463
464     in->current_reply_tail = &in->current_reply;
465     in->events_tail = &in->events;
466     in->pending_replies_tail = &in->pending_replies;
467
468     return 1;
469 }
470
471 void _xcb_in_destroy(_xcb_in *in)
472 {
473     pthread_cond_destroy(&in->event_cond);
474     free_reply_list(in->current_reply);
475     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
476     while(in->events)
477     {
478         struct event_list *e = in->events;
479         in->events = e->next;
480         free(e->event);
481         free(e);
482     }
483     while(in->pending_replies)
484     {
485         pending_reply *pend = in->pending_replies;
486         in->pending_replies = pend->next;
487         free(pend);
488     }
489 }
490
491 int _xcb_in_expect_reply(xcb_connection_t *c, unsigned int request, enum workarounds workaround, int flags)
492 {
493     pending_reply *pend = malloc(sizeof(pending_reply));
494     assert(workaround != WORKAROUND_NONE || flags != 0);
495     if(!pend)
496     {
497         _xcb_conn_shutdown(c);
498         return 0;
499     }
500     pend->request = request;
501     pend->workaround = workaround;
502     pend->flags = flags;
503     pend->next = 0;
504     *c->in.pending_replies_tail = pend;
505     c->in.pending_replies_tail = &pend->next;
506     return 1;
507 }
508
509 int _xcb_in_read(xcb_connection_t *c)
510 {
511     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
512     if(n > 0)
513         c->in.queue_len += n;
514     while(read_packet(c))
515         /* empty */;
516     if((n > 0) || (n < 0 && errno == EAGAIN))
517         return 1;
518     _xcb_conn_shutdown(c);
519     return 0;
520 }
521
522 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
523 {
524     int done = c->in.queue_len;
525     if(len < done)
526         done = len;
527
528     memcpy(buf, c->in.queue, done);
529     c->in.queue_len -= done;
530     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
531
532     if(len > done)
533     {
534         int ret = read_block(c->fd, (char *) buf + done, len - done);
535         if(ret <= 0)
536         {
537             _xcb_conn_shutdown(c);
538             return ret;
539         }
540     }
541
542     return len;
543 }