1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* Stuff that reads stuff from the server. */
41 #include <sys/select.h>
46 #define XCB_XGE_EVENT 35
49 xcb_generic_event_t *event;
50 struct event_list *next;
55 struct reply_list *next;
58 typedef struct pending_reply {
59 uint64_t first_request;
60 uint64_t last_request;
61 enum workarounds workaround;
63 struct pending_reply *next;
66 typedef struct reader_list {
69 struct reader_list *next;
72 static void wake_up_next_reader(xcb_connection_t *c)
76 pthreadret = pthread_cond_signal(c->in.readers->data);
78 pthreadret = pthread_cond_signal(&c->in.event_cond);
79 assert(pthreadret == 0);
82 static int read_packet(xcb_connection_t *c)
84 xcb_generic_reply_t genrep;
86 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
88 pending_reply *pend = 0;
89 struct event_list *event;
91 /* Wait for there to be enough data for us to read a whole packet */
92 if(c->in.queue_len < length)
95 /* Get the response type, length, and sequence number. */
96 memcpy(&genrep, c->in.queue, sizeof(genrep));
98 /* Compute 32-bit sequence number of this packet. */
99 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
101 uint64_t lastread = c->in.request_read;
102 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
103 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
104 c->in.request_read += 0x10000;
105 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
106 c->in.request_expected = c->in.request_read;
108 if(c->in.request_read != lastread)
110 if(c->in.current_reply)
112 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
113 c->in.current_reply = 0;
114 c->in.current_reply_tail = &c->in.current_reply;
116 c->in.request_completed = c->in.request_read - 1;
119 while(c->in.pending_replies &&
120 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
121 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
123 pending_reply *oldpend = c->in.pending_replies;
124 c->in.pending_replies = oldpend->next;
126 c->in.pending_replies_tail = &c->in.pending_replies;
130 if(genrep.response_type == XCB_ERROR)
131 c->in.request_completed = c->in.request_read;
134 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
136 pend = c->in.pending_replies;
138 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
139 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
140 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
144 /* For reply packets, check that the entire packet is available. */
145 if(genrep.response_type == XCB_REPLY)
147 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
149 uint32_t *p = (uint32_t *) c->in.queue;
150 genrep.length = p[2] * p[3] * 2;
152 length += genrep.length * 4;
155 /* XGE events may have sizes > 32 */
156 if (genrep.response_type == XCB_XGE_EVENT)
157 eventlength = genrep.length * 4;
159 buf = malloc(length + eventlength +
160 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
163 _xcb_conn_shutdown(c);
167 if(_xcb_in_read_block(c, buf, length) <= 0)
173 /* pull in XGE event data if available, append after event struct */
176 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
183 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
189 if(genrep.response_type != XCB_REPLY)
190 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
192 /* reply, or checked error */
193 if( genrep.response_type == XCB_REPLY ||
194 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
197 struct reply_list *cur = malloc(sizeof(struct reply_list));
200 _xcb_conn_shutdown(c);
206 *c->in.current_reply_tail = cur;
207 c->in.current_reply_tail = &cur->next;
208 for(reader = c->in.readers;
210 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
211 reader = reader->next)
213 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
215 pthread_cond_signal(reader->data);
222 /* event, or unchecked error */
223 event = malloc(sizeof(struct event_list));
226 _xcb_conn_shutdown(c);
232 *c->in.events_tail = event;
233 c->in.events_tail = &event->next;
234 pthread_cond_signal(&c->in.event_cond);
235 return 1; /* I have something for you... */
238 static xcb_generic_event_t *get_event(xcb_connection_t *c)
240 struct event_list *cur = c->in.events;
241 xcb_generic_event_t *ret;
245 c->in.events = cur->next;
247 c->in.events_tail = &c->in.events;
252 static void free_reply_list(struct reply_list *head)
256 struct reply_list *cur = head;
263 static int read_block(const int fd, void *buf, const ssize_t len)
268 int ret = read(fd, ((char *) buf) + done, len - done);
271 if(ret < 0 && errno == EAGAIN)
279 ret = poll(&pfd, 1, -1);
280 } while (ret == -1 && errno == EINTR);
286 ret = select(fd + 1, &fds, 0, 0, 0);
287 } while (ret == -1 && errno == EINTR);
296 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
298 struct reply_list *head;
300 /* If an error occurred when issuing the request, fail immediately. */
303 /* We've read requests past the one we want, so if it has replies we have
304 * them all and they're in the replies map. */
305 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
307 head = _xcb_map_remove(c->in.replies, request);
308 if(head && head->next)
309 _xcb_map_put(c->in.replies, request, head->next);
311 /* We're currently processing the responses to the request we want, and we
312 * have a reply ready to return. So just return it without blocking. */
313 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
315 head = c->in.current_reply;
316 c->in.current_reply = head->next;
318 c->in.current_reply_tail = &c->in.current_reply;
320 /* We know this request can't have any more replies, and we've already
321 * established it doesn't have a reply now. Don't bother blocking. */
322 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
324 /* We may have more replies on the way for this request: block until we're
335 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
338 *error = head->reply;
343 *reply = head->reply;
351 /* Public interface */
353 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
355 uint64_t widened_request;
362 pthread_mutex_lock(&c->iolock);
364 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
365 if(widened_request > c->out.request)
366 widened_request -= UINT64_C(1) << 32;
368 /* If this request has not been written yet, write it. */
369 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
371 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
373 reader_list **prev_reader;
375 for(prev_reader = &c->in.readers;
377 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
378 prev_reader = &(*prev_reader)->next)
382 reader.request = request;
384 reader.next = *prev_reader;
385 *prev_reader = &reader;
387 while(!poll_for_reply(c, request, &ret, e))
388 if(!_xcb_conn_wait(c, &cond, 0, 0))
391 for(prev_reader = &c->in.readers;
393 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
394 prev_reader = &(*prev_reader)->next)
396 if(*prev_reader == &reader)
398 *prev_reader = (*prev_reader)->next;
402 pthread_cond_destroy(&cond);
405 wake_up_next_reader(c);
406 pthread_mutex_unlock(&c->iolock);
410 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
413 pend = malloc(sizeof(*pend));
416 _xcb_conn_shutdown(c);
420 pend->first_request = seq;
421 pend->last_request = seq;
422 pend->workaround = 0;
423 pend->flags = XCB_REQUEST_DISCARD_REPLY;
424 pend->next = *prev_next;
428 c->in.pending_replies_tail = &pend->next;
431 static void discard_reply(xcb_connection_t *c, unsigned int request)
433 pending_reply *pend = 0;
434 pending_reply **prev_pend;
435 uint64_t widened_request;
437 /* We've read requests past the one we want, so if it has replies we have
438 * them all and they're in the replies map. */
439 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
441 struct reply_list *head;
442 head = _xcb_map_remove(c->in.replies, request);
445 struct reply_list *next = head->next;
453 /* We're currently processing the responses to the request we want, and we
454 * have a reply ready to return. Free it, and mark the pend to free any further
456 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
458 struct reply_list *head;
459 head = c->in.current_reply;
460 c->in.current_reply = NULL;
461 c->in.current_reply_tail = &c->in.current_reply;
464 struct reply_list *next = head->next;
470 pend = c->in.pending_replies;
472 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
473 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
474 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
477 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
479 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
484 /* Walk the list of pending requests. Mark the first match for deletion. */
485 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
487 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
490 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
492 /* Pending reply found. Mark for discard: */
493 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
498 /* Pending reply not found (likely due to _unchecked request). Create one: */
499 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
500 if(widened_request > c->out.request)
501 widened_request -= UINT64_C(1) << 32;
503 insert_pending_discard(c, prev_pend, widened_request);
506 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
511 /* If an error occurred when issuing the request, fail immediately. */
515 pthread_mutex_lock(&c->iolock);
516 discard_reply(c, sequence);
517 pthread_mutex_unlock(&c->iolock);
520 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
528 return 1; /* would not block */
531 pthread_mutex_lock(&c->iolock);
532 ret = poll_for_reply(c, request, reply, error);
533 pthread_mutex_unlock(&c->iolock);
537 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
539 xcb_generic_event_t *ret;
542 pthread_mutex_lock(&c->iolock);
543 /* get_event returns 0 on empty list. */
544 while(!(ret = get_event(c)))
545 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
548 wake_up_next_reader(c);
549 pthread_mutex_unlock(&c->iolock);
553 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
555 xcb_generic_event_t *ret = 0;
558 pthread_mutex_lock(&c->iolock);
559 /* FIXME: follow X meets Z architecture changes. */
561 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
563 pthread_mutex_unlock(&c->iolock);
568 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
570 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
571 * that would require factoring the locking out of xcb_get_input_focus,
572 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
573 xcb_generic_error_t *ret;
577 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
578 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
580 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
583 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
588 /* Private interface */
590 int _xcb_in_init(_xcb_in *in)
592 if(pthread_cond_init(&in->event_cond, 0))
598 in->request_read = 0;
599 in->request_completed = 0;
601 in->replies = _xcb_map_new();
605 in->current_reply_tail = &in->current_reply;
606 in->events_tail = &in->events;
607 in->pending_replies_tail = &in->pending_replies;
612 void _xcb_in_destroy(_xcb_in *in)
614 pthread_cond_destroy(&in->event_cond);
615 free_reply_list(in->current_reply);
616 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
619 struct event_list *e = in->events;
620 in->events = e->next;
624 while(in->pending_replies)
626 pending_reply *pend = in->pending_replies;
627 in->pending_replies = pend->next;
632 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
634 pending_reply *pend = malloc(sizeof(pending_reply));
635 assert(workaround != WORKAROUND_NONE || flags != 0);
638 _xcb_conn_shutdown(c);
641 pend->first_request = pend->last_request = request;
642 pend->workaround = workaround;
645 *c->in.pending_replies_tail = pend;
646 c->in.pending_replies_tail = &pend->next;
650 void _xcb_in_replies_done(xcb_connection_t *c)
652 struct pending_reply *pend;
653 if (c->in.pending_replies_tail != &c->in.pending_replies)
655 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
656 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
658 pend->last_request = c->out.request;
659 pend->workaround = WORKAROUND_NONE;
664 int _xcb_in_read(xcb_connection_t *c)
666 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
668 c->in.queue_len += n;
669 while(read_packet(c))
671 if((n > 0) || (n < 0 && errno == EAGAIN))
673 _xcb_conn_shutdown(c);
677 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
679 int done = c->in.queue_len;
683 memcpy(buf, c->in.queue, done);
684 c->in.queue_len -= done;
685 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
689 int ret = read_block(c->fd, (char *) buf + done, len - done);
692 _xcb_conn_shutdown(c);