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;
94 static int read_fds(xcb_connection_t *c, int *fds, int nfd)
96 int *ifds = &c->in.in_fd.fd[c->in.in_fd.ifd];
97 int infd = c->in.in_fd.nfd - c->in.in_fd.ifd;
101 memcpy(fds, ifds, nfd * sizeof (int));
102 c->in.in_fd.ifd += nfd;
107 static int read_packet(xcb_connection_t *c)
109 xcb_generic_reply_t genrep;
110 uint64_t length = 32;
111 uint64_t eventlength = 0; /* length after first 32 bytes for GenericEvents */
112 int nfd = 0; /* Number of file descriptors attached to the reply */
115 pending_reply *pend = 0;
116 struct event_list *event;
118 /* Wait for there to be enough data for us to read a whole packet */
119 if(c->in.queue_len < length)
122 /* Get the response type, length, and sequence number. */
123 memcpy(&genrep, c->in.queue, sizeof(genrep));
125 /* Compute 32-bit sequence number of this packet. */
126 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
128 uint64_t lastread = c->in.request_read;
129 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
130 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
131 c->in.request_read += 0x10000;
132 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
133 c->in.request_expected = c->in.request_read;
135 if(c->in.request_read != lastread)
137 if(c->in.current_reply)
139 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
140 c->in.current_reply = 0;
141 c->in.current_reply_tail = &c->in.current_reply;
143 c->in.request_completed = c->in.request_read - 1;
146 while(c->in.pending_replies &&
147 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
148 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
150 pending_reply *oldpend = c->in.pending_replies;
151 c->in.pending_replies = oldpend->next;
153 c->in.pending_replies_tail = &c->in.pending_replies;
157 if(genrep.response_type == XCB_ERROR)
158 c->in.request_completed = c->in.request_read;
160 remove_finished_readers(&c->in.readers, c->in.request_completed);
163 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
165 pend = c->in.pending_replies;
167 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
168 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
169 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
173 /* For reply packets, check that the entire packet is available. */
174 if(genrep.response_type == XCB_REPLY)
176 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
178 uint32_t *p = (uint32_t *) c->in.queue;
179 genrep.length = p[2] * p[3] * 2;
181 length += genrep.length * 4;
183 /* XXX a bit of a hack -- we "know" that all FD replys place
184 * the number of fds in the pad0 byte */
185 if (pend && pend->flags & XCB_REQUEST_REPLY_FDS)
189 /* XGE events may have sizes > 32 */
190 if ((genrep.response_type & 0x7f) == XCB_XGE_EVENT)
191 eventlength = genrep.length * 4;
193 bufsize = length + eventlength + nfd * sizeof(int) +
194 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t));
195 if (bufsize < INT32_MAX)
196 buf = malloc((size_t) bufsize);
201 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
205 if(_xcb_in_read_block(c, buf, length) <= 0)
211 /* pull in XGE event data if available, append after event struct */
214 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
224 if (!read_fds(c, (int *) &((char *) buf)[length], nfd))
232 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
238 if(genrep.response_type != XCB_REPLY)
239 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
241 /* reply, or checked error */
242 if( genrep.response_type == XCB_REPLY ||
243 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
245 struct reply_list *cur = malloc(sizeof(struct reply_list));
248 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
254 *c->in.current_reply_tail = cur;
255 c->in.current_reply_tail = &cur->next;
256 if(c->in.readers && c->in.readers->request == c->in.request_read)
257 pthread_cond_signal(c->in.readers->data);
261 /* event, or unchecked error */
262 event = malloc(sizeof(struct event_list));
265 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
271 *c->in.events_tail = event;
272 c->in.events_tail = &event->next;
273 pthread_cond_signal(&c->in.event_cond);
274 return 1; /* I have something for you... */
277 static xcb_generic_event_t *get_event(xcb_connection_t *c)
279 struct event_list *cur = c->in.events;
280 xcb_generic_event_t *ret;
284 c->in.events = cur->next;
286 c->in.events_tail = &c->in.events;
291 static void free_reply_list(struct reply_list *head)
295 struct reply_list *cur = head;
302 static int read_block(const int fd, void *buf, const ssize_t len)
307 int ret = recv(fd, ((char *) buf) + done, len - done, 0);
311 if(ret < 0 && errno == EAGAIN)
313 if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
322 ret = poll(&pfd, 1, -1);
323 } while (ret == -1 && errno == EINTR);
329 /* Initializing errno here makes sure that for Win32 this loop will execute only once */
332 ret = select(fd + 1, &fds, 0, 0, 0);
333 } while (ret == -1 && errno == EINTR);
334 #endif /* USE_POLL */
342 static int poll_for_reply(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
344 struct reply_list *head;
346 /* If an error occurred when issuing the request, fail immediately. */
349 /* We've read requests past the one we want, so if it has replies we have
350 * them all and they're in the replies map. */
351 else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
353 head = _xcb_map_remove(c->in.replies, request);
354 if(head && head->next)
355 _xcb_map_put(c->in.replies, request, head->next);
357 /* We're currently processing the responses to the request we want, and we
358 * have a reply ready to return. So just return it without blocking. */
359 else if(request == c->in.request_read && c->in.current_reply)
361 head = c->in.current_reply;
362 c->in.current_reply = head->next;
364 c->in.current_reply_tail = &c->in.current_reply;
366 /* We know this request can't have any more replies, and we've already
367 * established it doesn't have a reply now. Don't bother blocking. */
368 else if(request == c->in.request_completed)
370 /* We may have more replies on the way for this request: block until we're
381 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
384 *error = head->reply;
389 *reply = head->reply;
397 static void insert_reader(reader_list **prev_reader, reader_list *reader, uint64_t request, pthread_cond_t *cond)
399 while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request))
400 prev_reader = &(*prev_reader)->next;
401 reader->request = request;
403 reader->next = *prev_reader;
404 *prev_reader = reader;
407 static void remove_reader(reader_list **prev_reader, reader_list *reader)
409 while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, reader->request))
410 if(*prev_reader == reader)
412 *prev_reader = (*prev_reader)->next;
417 static void *wait_for_reply(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
421 /* If this request has not been written yet, write it. */
422 if(c->out.return_socket || _xcb_out_flush_to(c, request))
424 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
427 insert_reader(&c->in.readers, &reader, request, &cond);
429 while(!poll_for_reply(c, request, &ret, e))
430 if(!_xcb_conn_wait(c, &cond, 0, 0))
433 remove_reader(&c->in.readers, &reader);
434 pthread_cond_destroy(&cond);
437 _xcb_in_wake_up_next_reader(c);
441 static uint64_t widen(xcb_connection_t *c, unsigned int request)
443 uint64_t widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
444 if(widened_request > c->out.request)
445 widened_request -= UINT64_C(1) << 32;
446 return widened_request;
449 /* Public interface */
451 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
459 pthread_mutex_lock(&c->iolock);
460 ret = wait_for_reply(c, widen(c, request), e);
461 pthread_mutex_unlock(&c->iolock);
465 int *xcb_get_reply_fds(xcb_connection_t *c, void *reply, size_t reply_size)
467 return (int *) (&((char *) reply)[reply_size]);
470 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
473 pend = malloc(sizeof(*pend));
476 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
480 pend->first_request = seq;
481 pend->last_request = seq;
482 pend->workaround = 0;
483 pend->flags = XCB_REQUEST_DISCARD_REPLY;
484 pend->next = *prev_next;
488 c->in.pending_replies_tail = &pend->next;
491 static void discard_reply(xcb_connection_t *c, uint64_t request)
494 pending_reply **prev_pend;
496 /* Free any replies or errors that we've already read. Stop if
497 * xcb_wait_for_reply would block or we've run out of replies. */
498 while(poll_for_reply(c, request, &reply, 0) && reply)
501 /* If we've proven there are no more responses coming, we're done. */
502 if(XCB_SEQUENCE_COMPARE(request, <=, c->in.request_completed))
505 /* Walk the list of pending requests. Mark the first match for deletion. */
506 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
508 if(XCB_SEQUENCE_COMPARE((*prev_pend)->first_request, >, request))
511 if((*prev_pend)->first_request == request)
513 /* Pending reply found. Mark for discard: */
514 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
519 /* Pending reply not found (likely due to _unchecked request). Create one: */
520 insert_pending_discard(c, prev_pend, request);
523 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
528 /* If an error occurred when issuing the request, fail immediately. */
532 pthread_mutex_lock(&c->iolock);
533 discard_reply(c, widen(c, sequence));
534 pthread_mutex_unlock(&c->iolock);
537 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
545 return 1; /* would not block */
548 pthread_mutex_lock(&c->iolock);
549 ret = poll_for_reply(c, widen(c, request), reply, error);
550 pthread_mutex_unlock(&c->iolock);
554 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
556 xcb_generic_event_t *ret;
559 pthread_mutex_lock(&c->iolock);
560 /* get_event returns 0 on empty list. */
561 while(!(ret = get_event(c)))
562 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
565 _xcb_in_wake_up_next_reader(c);
566 pthread_mutex_unlock(&c->iolock);
570 static xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued)
572 xcb_generic_event_t *ret = 0;
575 pthread_mutex_lock(&c->iolock);
576 /* FIXME: follow X meets Z architecture changes. */
578 if(!ret && !queued && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
580 pthread_mutex_unlock(&c->iolock);
585 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
587 return poll_for_next_event(c, 0);
590 xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c)
592 return poll_for_next_event(c, 1);
595 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
598 xcb_generic_error_t *ret = 0;
602 pthread_mutex_lock(&c->iolock);
603 request = widen(c, cookie.sequence);
604 if(XCB_SEQUENCE_COMPARE(request, >=, c->in.request_expected)
605 && XCB_SEQUENCE_COMPARE(request, >, c->in.request_completed))
607 _xcb_out_send_sync(c);
608 _xcb_out_flush_to(c, c->out.request);
610 reply = wait_for_reply(c, request, &ret);
612 pthread_mutex_unlock(&c->iolock);
616 static xcb_generic_event_t *get_special_event(xcb_connection_t *c,
617 xcb_special_event_t *se)
619 xcb_generic_event_t *event = NULL;
620 struct event_list *events;
622 if ((events = se->events) != NULL) {
623 event = events->event;
624 if (!(se->events = events->next))
625 se->events_tail = &se->events;
631 xcb_generic_event_t *xcb_poll_for_special_event(xcb_connection_t *c,
632 xcb_special_event_t *se)
634 xcb_generic_event_t *event;
638 pthread_mutex_lock(&c->iolock);
639 event = get_special_event(c, se);
640 pthread_mutex_unlock(&c->iolock);
644 xcb_generic_event_t *xcb_wait_for_special_event(xcb_connection_t *c,
645 xcb_special_event_t *se)
647 xcb_generic_event_t *event;
651 pthread_mutex_lock(&c->iolock);
653 /* get_special_event returns 0 on empty list. */
654 while(!(event = get_special_event(c, se)))
655 if(!_xcb_conn_wait(c, &se->special_event_cond, 0, 0))
658 pthread_mutex_unlock(&c->iolock);
662 xcb_special_event_t *
663 xcb_register_for_special_xge(xcb_connection_t *c,
668 xcb_special_event_t *se;
672 pthread_mutex_lock(&c->iolock);
673 for (se = c->in.special_events; se; se = se->next) {
674 if (se->extension == extension &&
676 pthread_mutex_unlock(&c->iolock);
680 se = calloc(1, sizeof(xcb_special_event_t));
682 pthread_mutex_unlock(&c->iolock);
686 se->extension = extension;
690 se->events_tail = &se->events;
693 pthread_cond_init(&se->special_event_cond, 0);
695 se->next = c->in.special_events;
696 c->in.special_events = se;
698 pthread_mutex_unlock(&c->iolock);
703 xcb_unregister_for_special_event(xcb_connection_t *c,
704 xcb_special_event_t *se)
706 xcb_special_event_t *s, **prev;
707 struct event_list *events, *next;
712 pthread_mutex_lock(&c->iolock);
714 for (prev = &c->in.special_events; (s = *prev) != NULL; prev = &(s->next)) {
717 for (events = se->events; events; events = next) {
719 free (events->event);
722 pthread_cond_destroy(&se->special_event_cond);
727 pthread_mutex_unlock(&c->iolock);
730 /* Private interface */
732 int _xcb_in_init(_xcb_in *in)
734 if(pthread_cond_init(&in->event_cond, 0))
740 in->request_read = 0;
741 in->request_completed = 0;
743 in->replies = _xcb_map_new();
747 in->current_reply_tail = &in->current_reply;
748 in->events_tail = &in->events;
749 in->pending_replies_tail = &in->pending_replies;
754 void _xcb_in_destroy(_xcb_in *in)
756 pthread_cond_destroy(&in->event_cond);
757 free_reply_list(in->current_reply);
758 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
761 struct event_list *e = in->events;
762 in->events = e->next;
766 while(in->pending_replies)
768 pending_reply *pend = in->pending_replies;
769 in->pending_replies = pend->next;
774 void _xcb_in_wake_up_next_reader(xcb_connection_t *c)
778 pthreadret = pthread_cond_signal(c->in.readers->data);
780 pthreadret = pthread_cond_signal(&c->in.event_cond);
781 assert(pthreadret == 0);
784 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
786 pending_reply *pend = malloc(sizeof(pending_reply));
787 assert(workaround != WORKAROUND_NONE || flags != 0);
790 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
793 pend->first_request = pend->last_request = request;
794 pend->workaround = workaround;
797 *c->in.pending_replies_tail = pend;
798 c->in.pending_replies_tail = &pend->next;
802 void _xcb_in_replies_done(xcb_connection_t *c)
804 struct pending_reply *pend;
805 if (c->in.pending_replies_tail != &c->in.pending_replies)
807 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
808 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
810 pend->last_request = c->out.request;
811 pend->workaround = WORKAROUND_NONE;
816 int _xcb_in_read(xcb_connection_t *c)
822 .iov_base = c->in.queue + c->in.queue_len,
823 .iov_len = sizeof(c->in.queue) - c->in.queue_len,
826 struct cmsghdr cmsghdr;
827 int fd[XCB_MAX_PASS_FD];
829 struct msghdr msg = {
835 .msg_controllen = sizeof (struct cmsghdr) + sizeof(int) * (XCB_MAX_PASS_FD - c->in.in_fd.nfd),
837 n = recvmsg(c->fd, &msg, 0);
839 /* Check for truncation errors. Only MSG_CTRUNC is
840 * probably possible here, which would indicate that
841 * the sender tried to transmit more than XCB_MAX_PASS_FD
844 if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
845 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
849 n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len, 0);
853 if (msg.msg_controllen > sizeof (struct cmsghdr))
855 if (fds.cmsghdr.cmsg_level == SOL_SOCKET &&
856 fds.cmsghdr.cmsg_type == SCM_RIGHTS)
858 int nfd = (msg.msg_controllen - sizeof (struct cmsghdr)) / sizeof (int);
859 memmove(&c->in.in_fd.fd[c->in.in_fd.nfd],
862 c->in.in_fd.nfd += nfd;
866 c->in.queue_len += n;
868 while(read_packet(c))
871 if (c->in.in_fd.nfd) {
872 c->in.in_fd.nfd -= c->in.in_fd.ifd;
873 memmove(&c->in.in_fd.fd[0],
874 &c->in.in_fd.fd[c->in.in_fd.ifd],
875 c->in.in_fd.nfd * sizeof (int));
878 /* If we have any left-over file descriptors after emptying
879 * the input buffer, then the server sent some that we weren't
880 * expecting. Close them and mark the connection as broken;
882 if (c->in.queue_len == 0 && c->in.in_fd.nfd != 0) {
884 for (i = 0; i < c->in.in_fd.nfd; i++)
885 close(c->in.in_fd.fd[i]);
886 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
892 if((n > 0) || (n < 0 && errno == EAGAIN))
894 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
897 _xcb_conn_shutdown(c, XCB_CONN_ERROR);
901 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
903 int done = c->in.queue_len;
907 memcpy(buf, c->in.queue, done);
908 c->in.queue_len -= done;
909 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
913 int ret = read_block(c->fd, (char *) buf + done, len - done);
916 _xcb_conn_shutdown(c, XCB_CONN_ERROR);