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>
42 #include <sys/socket.h>
46 #include "xcb_windefs.h"
51 #define XCB_XGE_EVENT 35
53 /* required for compiling for Win32 using MinGW */
59 xcb_generic_event_t *event;
60 struct event_list *next;
65 struct reply_list *next;
68 typedef struct pending_reply {
69 uint64_t first_request;
70 uint64_t last_request;
71 enum workarounds workaround;
73 struct pending_reply *next;
76 typedef struct reader_list {
79 struct reader_list *next;
82 static void wake_up_next_reader(xcb_connection_t *c)
86 pthreadret = pthread_cond_signal(c->in.readers->data);
88 pthreadret = pthread_cond_signal(&c->in.event_cond);
89 assert(pthreadret == 0);
92 static int read_packet(xcb_connection_t *c)
94 xcb_generic_reply_t genrep;
96 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
98 pending_reply *pend = 0;
99 struct event_list *event;
101 /* Wait for there to be enough data for us to read a whole packet */
102 if(c->in.queue_len < length)
105 /* Get the response type, length, and sequence number. */
106 memcpy(&genrep, c->in.queue, sizeof(genrep));
108 /* Compute 32-bit sequence number of this packet. */
109 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
111 uint64_t lastread = c->in.request_read;
112 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
113 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
114 c->in.request_read += 0x10000;
115 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
116 c->in.request_expected = c->in.request_read;
118 if(c->in.request_read != lastread)
120 if(c->in.current_reply)
122 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
123 c->in.current_reply = 0;
124 c->in.current_reply_tail = &c->in.current_reply;
126 c->in.request_completed = c->in.request_read - 1;
129 while(c->in.pending_replies &&
130 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
131 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
133 pending_reply *oldpend = c->in.pending_replies;
134 c->in.pending_replies = oldpend->next;
136 c->in.pending_replies_tail = &c->in.pending_replies;
140 if(genrep.response_type == XCB_ERROR)
141 c->in.request_completed = c->in.request_read;
144 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
146 pend = c->in.pending_replies;
148 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
149 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
150 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
154 /* For reply packets, check that the entire packet is available. */
155 if(genrep.response_type == XCB_REPLY)
157 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
159 uint32_t *p = (uint32_t *) c->in.queue;
160 genrep.length = p[2] * p[3] * 2;
162 length += genrep.length * 4;
165 /* XGE events may have sizes > 32 */
166 if (genrep.response_type == XCB_XGE_EVENT)
168 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
171 buf = malloc(length + eventlength +
172 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
175 _xcb_conn_shutdown(c);
179 if(_xcb_in_read_block(c, buf, length) <= 0)
185 /* pull in XGE event data if available, append after event struct */
188 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
195 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
201 if(genrep.response_type != XCB_REPLY)
202 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
204 /* reply, or checked error */
205 if( genrep.response_type == XCB_REPLY ||
206 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
209 struct reply_list *cur = malloc(sizeof(struct reply_list));
212 _xcb_conn_shutdown(c);
218 *c->in.current_reply_tail = cur;
219 c->in.current_reply_tail = &cur->next;
220 for(reader = c->in.readers;
222 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
223 reader = reader->next)
225 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
227 pthread_cond_signal(reader->data);
234 /* event, or unchecked error */
235 event = malloc(sizeof(struct event_list));
238 _xcb_conn_shutdown(c);
244 *c->in.events_tail = event;
245 c->in.events_tail = &event->next;
246 pthread_cond_signal(&c->in.event_cond);
247 return 1; /* I have something for you... */
250 static xcb_generic_event_t *get_event(xcb_connection_t *c)
252 struct event_list *cur = c->in.events;
253 xcb_generic_event_t *ret;
257 c->in.events = cur->next;
259 c->in.events_tail = &c->in.events;
264 static void free_reply_list(struct reply_list *head)
268 struct reply_list *cur = head;
275 static int read_block(const int fd, void *buf, const ssize_t len)
280 int ret = recv(fd, ((char *) buf) + done, len - done,MSG_WAITALL);
284 if(ret < 0 && errno == EAGAIN)
286 if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
295 ret = poll(&pfd, 1, -1);
296 } while (ret == -1 && errno == EINTR);
302 /* Initializing errno here makes sure that for Win32 this loop will execute only once */
305 ret = select(fd + 1, &fds, 0, 0, 0);
306 } while (ret == -1 && errno == EINTR);
307 #endif /* USE_POLL */
315 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
317 struct reply_list *head;
319 /* If an error occurred when issuing the request, fail immediately. */
322 /* We've read requests past the one we want, so if it has replies we have
323 * them all and they're in the replies map. */
324 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
326 head = _xcb_map_remove(c->in.replies, request);
327 if(head && head->next)
328 _xcb_map_put(c->in.replies, request, head->next);
330 /* We're currently processing the responses to the request we want, and we
331 * have a reply ready to return. So just return it without blocking. */
332 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
334 head = c->in.current_reply;
335 c->in.current_reply = head->next;
337 c->in.current_reply_tail = &c->in.current_reply;
339 /* We know this request can't have any more replies, and we've already
340 * established it doesn't have a reply now. Don't bother blocking. */
341 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
343 /* We may have more replies on the way for this request: block until we're
354 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
357 *error = head->reply;
362 *reply = head->reply;
370 /* Public interface */
372 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
374 uint64_t widened_request;
381 pthread_mutex_lock(&c->iolock);
383 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
384 if(widened_request > c->out.request)
385 widened_request -= UINT64_C(1) << 32;
387 /* If this request has not been written yet, write it. */
388 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
390 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
392 reader_list **prev_reader;
394 for(prev_reader = &c->in.readers;
396 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
397 prev_reader = &(*prev_reader)->next)
401 reader.request = request;
403 reader.next = *prev_reader;
404 *prev_reader = &reader;
406 while(!poll_for_reply(c, request, &ret, e))
407 if(!_xcb_conn_wait(c, &cond, 0, 0))
410 for(prev_reader = &c->in.readers;
412 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
413 prev_reader = &(*prev_reader)->next)
415 if(*prev_reader == &reader)
417 *prev_reader = (*prev_reader)->next;
421 pthread_cond_destroy(&cond);
424 wake_up_next_reader(c);
425 pthread_mutex_unlock(&c->iolock);
429 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
432 pend = malloc(sizeof(*pend));
435 _xcb_conn_shutdown(c);
439 pend->first_request = seq;
440 pend->last_request = seq;
441 pend->workaround = 0;
442 pend->flags = XCB_REQUEST_DISCARD_REPLY;
443 pend->next = *prev_next;
447 c->in.pending_replies_tail = &pend->next;
450 static void discard_reply(xcb_connection_t *c, unsigned int request)
452 pending_reply *pend = 0;
453 pending_reply **prev_pend;
454 uint64_t widened_request;
456 /* We've read requests past the one we want, so if it has replies we have
457 * them all and they're in the replies map. */
458 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
460 struct reply_list *head;
461 head = _xcb_map_remove(c->in.replies, request);
464 struct reply_list *next = head->next;
472 /* We're currently processing the responses to the request we want, and we
473 * have a reply ready to return. Free it, and mark the pend to free any further
475 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
477 struct reply_list *head;
478 head = c->in.current_reply;
479 c->in.current_reply = NULL;
480 c->in.current_reply_tail = &c->in.current_reply;
483 struct reply_list *next = head->next;
489 pend = c->in.pending_replies;
491 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
492 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
493 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
496 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
498 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
503 /* Walk the list of pending requests. Mark the first match for deletion. */
504 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
506 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
509 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
511 /* Pending reply found. Mark for discard: */
512 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
517 /* Pending reply not found (likely due to _unchecked request). Create one: */
518 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
519 if(widened_request > c->out.request)
520 widened_request -= UINT64_C(1) << 32;
522 insert_pending_discard(c, prev_pend, widened_request);
525 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
530 /* If an error occurred when issuing the request, fail immediately. */
534 pthread_mutex_lock(&c->iolock);
535 discard_reply(c, sequence);
536 pthread_mutex_unlock(&c->iolock);
539 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
547 return 1; /* would not block */
550 pthread_mutex_lock(&c->iolock);
551 ret = poll_for_reply(c, request, reply, error);
552 pthread_mutex_unlock(&c->iolock);
556 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
558 xcb_generic_event_t *ret;
561 pthread_mutex_lock(&c->iolock);
562 /* get_event returns 0 on empty list. */
563 while(!(ret = get_event(c)))
564 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
567 wake_up_next_reader(c);
568 pthread_mutex_unlock(&c->iolock);
572 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
574 xcb_generic_event_t *ret = 0;
577 pthread_mutex_lock(&c->iolock);
578 /* FIXME: follow X meets Z architecture changes. */
580 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
582 pthread_mutex_unlock(&c->iolock);
587 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
589 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
590 * that would require factoring the locking out of xcb_get_input_focus,
591 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
592 xcb_generic_error_t *ret;
596 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
597 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
599 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
602 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
607 /* Private interface */
609 int _xcb_in_init(_xcb_in *in)
611 if(pthread_cond_init(&in->event_cond, 0))
617 in->request_read = 0;
618 in->request_completed = 0;
620 in->replies = _xcb_map_new();
624 in->current_reply_tail = &in->current_reply;
625 in->events_tail = &in->events;
626 in->pending_replies_tail = &in->pending_replies;
631 void _xcb_in_destroy(_xcb_in *in)
633 pthread_cond_destroy(&in->event_cond);
634 free_reply_list(in->current_reply);
635 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
638 struct event_list *e = in->events;
639 in->events = e->next;
643 while(in->pending_replies)
645 pending_reply *pend = in->pending_replies;
646 in->pending_replies = pend->next;
651 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
653 pending_reply *pend = malloc(sizeof(pending_reply));
654 assert(workaround != WORKAROUND_NONE || flags != 0);
657 _xcb_conn_shutdown(c);
660 pend->first_request = pend->last_request = request;
661 pend->workaround = workaround;
664 *c->in.pending_replies_tail = pend;
665 c->in.pending_replies_tail = &pend->next;
669 void _xcb_in_replies_done(xcb_connection_t *c)
671 struct pending_reply *pend;
672 if (c->in.pending_replies_tail != &c->in.pending_replies)
674 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
675 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
677 pend->last_request = c->out.request;
678 pend->workaround = WORKAROUND_NONE;
683 int _xcb_in_read(xcb_connection_t *c)
685 int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,MSG_WAITALL);
687 c->in.queue_len += n;
688 while(read_packet(c))
691 if((n > 0) || (n < 0 && errno == EAGAIN))
693 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
696 _xcb_conn_shutdown(c);
700 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
702 int done = c->in.queue_len;
706 memcpy(buf, c->in.queue, done);
707 c->in.queue_len -= done;
708 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
712 int ret = read_block(c->fd, (char *) buf + done, len - done);
715 _xcb_conn_shutdown(c);