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)
158 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
161 buf = malloc(length + eventlength +
162 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
165 _xcb_conn_shutdown(c);
169 if(_xcb_in_read_block(c, buf, length) <= 0)
175 /* pull in XGE event data if available, append after event struct */
178 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
185 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
191 if(genrep.response_type != XCB_REPLY)
192 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
194 /* reply, or checked error */
195 if( genrep.response_type == XCB_REPLY ||
196 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
199 struct reply_list *cur = malloc(sizeof(struct reply_list));
202 _xcb_conn_shutdown(c);
208 *c->in.current_reply_tail = cur;
209 c->in.current_reply_tail = &cur->next;
210 for(reader = c->in.readers;
212 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
213 reader = reader->next)
215 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
217 pthread_cond_signal(reader->data);
224 /* event, or unchecked error */
225 event = malloc(sizeof(struct event_list));
228 _xcb_conn_shutdown(c);
234 *c->in.events_tail = event;
235 c->in.events_tail = &event->next;
236 pthread_cond_signal(&c->in.event_cond);
237 return 1; /* I have something for you... */
240 static xcb_generic_event_t *get_event(xcb_connection_t *c)
242 struct event_list *cur = c->in.events;
243 xcb_generic_event_t *ret;
247 c->in.events = cur->next;
249 c->in.events_tail = &c->in.events;
254 static void free_reply_list(struct reply_list *head)
258 struct reply_list *cur = head;
265 static int read_block(const int fd, void *buf, const ssize_t len)
270 int ret = read(fd, ((char *) buf) + done, len - done);
273 if(ret < 0 && errno == EAGAIN)
281 ret = poll(&pfd, 1, -1);
282 } while (ret == -1 && errno == EINTR);
288 ret = select(fd + 1, &fds, 0, 0, 0);
289 } while (ret == -1 && errno == EINTR);
298 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
300 struct reply_list *head;
302 /* If an error occurred when issuing the request, fail immediately. */
305 /* We've read requests past the one we want, so if it has replies we have
306 * them all and they're in the replies map. */
307 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
309 head = _xcb_map_remove(c->in.replies, request);
310 if(head && head->next)
311 _xcb_map_put(c->in.replies, request, head->next);
313 /* We're currently processing the responses to the request we want, and we
314 * have a reply ready to return. So just return it without blocking. */
315 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
317 head = c->in.current_reply;
318 c->in.current_reply = head->next;
320 c->in.current_reply_tail = &c->in.current_reply;
322 /* We know this request can't have any more replies, and we've already
323 * established it doesn't have a reply now. Don't bother blocking. */
324 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
326 /* We may have more replies on the way for this request: block until we're
337 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
340 *error = head->reply;
345 *reply = head->reply;
353 /* Public interface */
355 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
357 uint64_t widened_request;
364 pthread_mutex_lock(&c->iolock);
366 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
367 if(widened_request > c->out.request)
368 widened_request -= UINT64_C(1) << 32;
370 /* If this request has not been written yet, write it. */
371 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
373 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
375 reader_list **prev_reader;
377 for(prev_reader = &c->in.readers;
379 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
380 prev_reader = &(*prev_reader)->next)
384 reader.request = request;
386 reader.next = *prev_reader;
387 *prev_reader = &reader;
389 while(!poll_for_reply(c, request, &ret, e))
390 if(!_xcb_conn_wait(c, &cond, 0, 0))
393 for(prev_reader = &c->in.readers;
395 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
396 prev_reader = &(*prev_reader)->next)
398 if(*prev_reader == &reader)
400 *prev_reader = (*prev_reader)->next;
404 pthread_cond_destroy(&cond);
407 wake_up_next_reader(c);
408 pthread_mutex_unlock(&c->iolock);
412 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
415 pend = malloc(sizeof(*pend));
418 _xcb_conn_shutdown(c);
422 pend->first_request = seq;
423 pend->last_request = seq;
424 pend->workaround = 0;
425 pend->flags = XCB_REQUEST_DISCARD_REPLY;
426 pend->next = *prev_next;
430 c->in.pending_replies_tail = &pend->next;
433 static void discard_reply(xcb_connection_t *c, unsigned int request)
435 pending_reply *pend = 0;
436 pending_reply **prev_pend;
437 uint64_t widened_request;
439 /* We've read requests past the one we want, so if it has replies we have
440 * them all and they're in the replies map. */
441 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
443 struct reply_list *head;
444 head = _xcb_map_remove(c->in.replies, request);
447 struct reply_list *next = head->next;
455 /* We're currently processing the responses to the request we want, and we
456 * have a reply ready to return. Free it, and mark the pend to free any further
458 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
460 struct reply_list *head;
461 head = c->in.current_reply;
462 c->in.current_reply = NULL;
463 c->in.current_reply_tail = &c->in.current_reply;
466 struct reply_list *next = head->next;
472 pend = c->in.pending_replies;
474 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
475 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
476 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
479 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
481 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
486 /* Walk the list of pending requests. Mark the first match for deletion. */
487 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
489 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
492 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
494 /* Pending reply found. Mark for discard: */
495 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
500 /* Pending reply not found (likely due to _unchecked request). Create one: */
501 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
502 if(widened_request > c->out.request)
503 widened_request -= UINT64_C(1) << 32;
505 insert_pending_discard(c, prev_pend, widened_request);
508 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
513 /* If an error occurred when issuing the request, fail immediately. */
517 pthread_mutex_lock(&c->iolock);
518 discard_reply(c, sequence);
519 pthread_mutex_unlock(&c->iolock);
522 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
530 return 1; /* would not block */
533 pthread_mutex_lock(&c->iolock);
534 ret = poll_for_reply(c, request, reply, error);
535 pthread_mutex_unlock(&c->iolock);
539 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
541 xcb_generic_event_t *ret;
544 pthread_mutex_lock(&c->iolock);
545 /* get_event returns 0 on empty list. */
546 while(!(ret = get_event(c)))
547 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
550 wake_up_next_reader(c);
551 pthread_mutex_unlock(&c->iolock);
555 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
557 xcb_generic_event_t *ret = 0;
560 pthread_mutex_lock(&c->iolock);
561 /* FIXME: follow X meets Z architecture changes. */
563 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
565 pthread_mutex_unlock(&c->iolock);
570 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
572 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
573 * that would require factoring the locking out of xcb_get_input_focus,
574 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
575 xcb_generic_error_t *ret;
579 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
580 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
582 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
585 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
590 /* Private interface */
592 int _xcb_in_init(_xcb_in *in)
594 if(pthread_cond_init(&in->event_cond, 0))
600 in->request_read = 0;
601 in->request_completed = 0;
603 in->replies = _xcb_map_new();
607 in->current_reply_tail = &in->current_reply;
608 in->events_tail = &in->events;
609 in->pending_replies_tail = &in->pending_replies;
614 void _xcb_in_destroy(_xcb_in *in)
616 pthread_cond_destroy(&in->event_cond);
617 free_reply_list(in->current_reply);
618 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
621 struct event_list *e = in->events;
622 in->events = e->next;
626 while(in->pending_replies)
628 pending_reply *pend = in->pending_replies;
629 in->pending_replies = pend->next;
634 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
636 pending_reply *pend = malloc(sizeof(pending_reply));
637 assert(workaround != WORKAROUND_NONE || flags != 0);
640 _xcb_conn_shutdown(c);
643 pend->first_request = pend->last_request = request;
644 pend->workaround = workaround;
647 *c->in.pending_replies_tail = pend;
648 c->in.pending_replies_tail = &pend->next;
652 void _xcb_in_replies_done(xcb_connection_t *c)
654 struct pending_reply *pend;
655 if (c->in.pending_replies_tail != &c->in.pending_replies)
657 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
658 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
660 pend->last_request = c->out.request;
661 pend->workaround = WORKAROUND_NONE;
666 int _xcb_in_read(xcb_connection_t *c)
668 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
670 c->in.queue_len += n;
671 while(read_packet(c))
673 if((n > 0) || (n < 0 && errno == EAGAIN))
675 _xcb_conn_shutdown(c);
679 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
681 int done = c->in.queue_len;
685 memcpy(buf, c->in.queue, done);
686 c->in.queue_len -= done;
687 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
691 int ret = read_block(c->fd, (char *) buf + done, len - done);
694 _xcb_conn_shutdown(c);