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