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. */
46 #include <sys/select.h>
47 #include <sys/socket.h>
51 #include "xcb_windefs.h"
56 #define XCB_XGE_EVENT 35
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 remove_finished_readers(reader_list **prev_reader, uint64_t completed)
84 while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, completed))
86 /* If you don't have what you're looking for now, you never
87 * will. Wake up and leave me alone. */
88 pthread_cond_signal((*prev_reader)->data);
89 *prev_reader = (*prev_reader)->next;
93 static int read_packet(xcb_connection_t *c)
95 xcb_generic_reply_t genrep;
97 uint64_t eventlength = 0; /* length after first 32 bytes for GenericEvents */
100 pending_reply *pend = 0;
101 struct event_list *event;
103 /* Wait for there to be enough data for us to read a whole packet */
104 if(c->in.queue_len < length)
107 /* Get the response type, length, and sequence number. */
108 memcpy(&genrep, c->in.queue, sizeof(genrep));
110 /* Compute 32-bit sequence number of this packet. */
111 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
113 uint64_t lastread = c->in.request_read;
114 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
115 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
116 c->in.request_read += 0x10000;
117 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
118 c->in.request_expected = c->in.request_read;
120 if(c->in.request_read != lastread)
122 if(c->in.current_reply)
124 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
125 c->in.current_reply = 0;
126 c->in.current_reply_tail = &c->in.current_reply;
128 c->in.request_completed = c->in.request_read - 1;
131 while(c->in.pending_replies &&
132 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
133 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
135 pending_reply *oldpend = c->in.pending_replies;
136 c->in.pending_replies = oldpend->next;
138 c->in.pending_replies_tail = &c->in.pending_replies;
142 if(genrep.response_type == XCB_ERROR)
143 c->in.request_completed = c->in.request_read;
145 remove_finished_readers(&c->in.readers, c->in.request_completed);
148 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
150 pend = c->in.pending_replies;
152 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
153 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
154 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
158 /* For reply packets, check that the entire packet is available. */
159 if(genrep.response_type == XCB_REPLY)
161 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
163 uint32_t *p = (uint32_t *) c->in.queue;
164 genrep.length = p[2] * p[3] * 2;
166 length += genrep.length * 4;
169 /* XGE events may have sizes > 32 */
170 if ((genrep.response_type & 0x7f) == XCB_XGE_EVENT)
171 eventlength = genrep.length * 4;
173 bufsize = length + eventlength +
174 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t));
175 if (bufsize < INT32_MAX)
176 buf = malloc((size_t) bufsize);
181 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
185 if(_xcb_in_read_block(c, buf, length) <= 0)
191 /* pull in XGE event data if available, append after event struct */
194 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
201 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
207 if(genrep.response_type != XCB_REPLY)
208 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
210 /* reply, or checked error */
211 if( genrep.response_type == XCB_REPLY ||
212 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
214 struct reply_list *cur = malloc(sizeof(struct reply_list));
217 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
223 *c->in.current_reply_tail = cur;
224 c->in.current_reply_tail = &cur->next;
225 if(c->in.readers && c->in.readers->request == c->in.request_read)
226 pthread_cond_signal(c->in.readers->data);
230 /* event, or unchecked error */
231 event = malloc(sizeof(struct event_list));
234 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
240 *c->in.events_tail = event;
241 c->in.events_tail = &event->next;
242 pthread_cond_signal(&c->in.event_cond);
243 return 1; /* I have something for you... */
246 static xcb_generic_event_t *get_event(xcb_connection_t *c)
248 struct event_list *cur = c->in.events;
249 xcb_generic_event_t *ret;
253 c->in.events = cur->next;
255 c->in.events_tail = &c->in.events;
260 static void free_reply_list(struct reply_list *head)
264 struct reply_list *cur = head;
271 static int read_block(const int fd, void *buf, const ssize_t len)
276 int ret = recv(fd, ((char *) buf) + done, len - done, 0);
280 if(ret < 0 && errno == EAGAIN)
282 if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
291 ret = poll(&pfd, 1, -1);
292 } while (ret == -1 && errno == EINTR);
298 /* Initializing errno here makes sure that for Win32 this loop will execute only once */
301 ret = select(fd + 1, &fds, 0, 0, 0);
302 } while (ret == -1 && errno == EINTR);
303 #endif /* USE_POLL */
311 static int poll_for_reply(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
313 struct reply_list *head;
315 /* If an error occurred when issuing the request, fail immediately. */
318 /* We've read requests past the one we want, so if it has replies we have
319 * them all and they're in the replies map. */
320 else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
322 head = _xcb_map_remove(c->in.replies, request);
323 if(head && head->next)
324 _xcb_map_put(c->in.replies, request, head->next);
326 /* We're currently processing the responses to the request we want, and we
327 * have a reply ready to return. So just return it without blocking. */
328 else if(request == c->in.request_read && c->in.current_reply)
330 head = c->in.current_reply;
331 c->in.current_reply = head->next;
333 c->in.current_reply_tail = &c->in.current_reply;
335 /* We know this request can't have any more replies, and we've already
336 * established it doesn't have a reply now. Don't bother blocking. */
337 else if(request == c->in.request_completed)
339 /* We may have more replies on the way for this request: block until we're
350 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
353 *error = head->reply;
358 *reply = head->reply;
366 static void insert_reader(reader_list **prev_reader, reader_list *reader, uint64_t request, pthread_cond_t *cond)
368 while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request))
369 prev_reader = &(*prev_reader)->next;
370 reader->request = request;
372 reader->next = *prev_reader;
373 *prev_reader = reader;
376 static void remove_reader(reader_list **prev_reader, reader_list *reader)
378 while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, reader->request))
379 if(*prev_reader == reader)
381 *prev_reader = (*prev_reader)->next;
386 static void *wait_for_reply(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
390 /* If this request has not been written yet, write it. */
391 if(c->out.return_socket || _xcb_out_flush_to(c, request))
393 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
396 insert_reader(&c->in.readers, &reader, request, &cond);
398 while(!poll_for_reply(c, request, &ret, e))
399 if(!_xcb_conn_wait(c, &cond, 0, 0))
402 remove_reader(&c->in.readers, &reader);
403 pthread_cond_destroy(&cond);
406 _xcb_in_wake_up_next_reader(c);
410 static uint64_t widen(xcb_connection_t *c, unsigned int request)
412 uint64_t widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
413 if(widened_request > c->out.request)
414 widened_request -= UINT64_C(1) << 32;
415 return widened_request;
418 /* Public interface */
420 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
428 pthread_mutex_lock(&c->iolock);
429 ret = wait_for_reply(c, widen(c, request), e);
430 pthread_mutex_unlock(&c->iolock);
434 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
437 pend = malloc(sizeof(*pend));
440 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
444 pend->first_request = seq;
445 pend->last_request = seq;
446 pend->workaround = 0;
447 pend->flags = XCB_REQUEST_DISCARD_REPLY;
448 pend->next = *prev_next;
452 c->in.pending_replies_tail = &pend->next;
455 static void discard_reply(xcb_connection_t *c, uint64_t request)
458 pending_reply **prev_pend;
460 /* Free any replies or errors that we've already read. Stop if
461 * xcb_wait_for_reply would block or we've run out of replies. */
462 while(poll_for_reply(c, request, &reply, 0) && reply)
465 /* If we've proven there are no more responses coming, we're done. */
466 if(XCB_SEQUENCE_COMPARE(request, <=, c->in.request_completed))
469 /* Walk the list of pending requests. Mark the first match for deletion. */
470 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
472 if(XCB_SEQUENCE_COMPARE((*prev_pend)->first_request, >, request))
475 if((*prev_pend)->first_request == request)
477 /* Pending reply found. Mark for discard: */
478 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
483 /* Pending reply not found (likely due to _unchecked request). Create one: */
484 insert_pending_discard(c, prev_pend, request);
487 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
492 /* If an error occurred when issuing the request, fail immediately. */
496 pthread_mutex_lock(&c->iolock);
497 discard_reply(c, widen(c, sequence));
498 pthread_mutex_unlock(&c->iolock);
501 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
509 return 1; /* would not block */
512 pthread_mutex_lock(&c->iolock);
513 ret = poll_for_reply(c, widen(c, request), reply, error);
514 pthread_mutex_unlock(&c->iolock);
518 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
520 xcb_generic_event_t *ret;
523 pthread_mutex_lock(&c->iolock);
524 /* get_event returns 0 on empty list. */
525 while(!(ret = get_event(c)))
526 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
529 _xcb_in_wake_up_next_reader(c);
530 pthread_mutex_unlock(&c->iolock);
534 static xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued)
536 xcb_generic_event_t *ret = 0;
539 pthread_mutex_lock(&c->iolock);
540 /* FIXME: follow X meets Z architecture changes. */
542 if(!ret && !queued && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
544 pthread_mutex_unlock(&c->iolock);
549 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
551 return poll_for_next_event(c, 0);
554 xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c)
556 return poll_for_next_event(c, 1);
559 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
562 xcb_generic_error_t *ret = 0;
566 pthread_mutex_lock(&c->iolock);
567 request = widen(c, cookie.sequence);
568 if(XCB_SEQUENCE_COMPARE(request, >=, c->in.request_expected)
569 && XCB_SEQUENCE_COMPARE(request, >, c->in.request_completed))
571 _xcb_out_send_sync(c);
572 _xcb_out_flush_to(c, c->out.request);
574 reply = wait_for_reply(c, request, &ret);
576 pthread_mutex_unlock(&c->iolock);
580 /* Private interface */
582 int _xcb_in_init(_xcb_in *in)
584 if(pthread_cond_init(&in->event_cond, 0))
590 in->request_read = 0;
591 in->request_completed = 0;
593 in->replies = _xcb_map_new();
597 in->current_reply_tail = &in->current_reply;
598 in->events_tail = &in->events;
599 in->pending_replies_tail = &in->pending_replies;
604 void _xcb_in_destroy(_xcb_in *in)
606 pthread_cond_destroy(&in->event_cond);
607 free_reply_list(in->current_reply);
608 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
611 struct event_list *e = in->events;
612 in->events = e->next;
616 while(in->pending_replies)
618 pending_reply *pend = in->pending_replies;
619 in->pending_replies = pend->next;
624 void _xcb_in_wake_up_next_reader(xcb_connection_t *c)
628 pthreadret = pthread_cond_signal(c->in.readers->data);
630 pthreadret = pthread_cond_signal(&c->in.event_cond);
631 assert(pthreadret == 0);
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, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
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 = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len, 0);
670 c->in.queue_len += n;
671 while(read_packet(c))
674 if((n > 0) || (n < 0 && errno == EAGAIN))
676 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
679 _xcb_conn_shutdown(c, XCB_CONN_ERROR);
683 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
685 int done = c->in.queue_len;
689 memcpy(buf, c->in.queue, done);
690 c->in.queue_len -= done;
691 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
695 int ret = read_block(c->fd, (char *) buf + done, len - done);
698 _xcb_conn_shutdown(c, XCB_CONN_ERROR);