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>
45 #include "xcb_windefs.h"
50 #define XCB_XGE_EVENT 35
53 xcb_generic_event_t *event;
54 struct event_list *next;
59 struct reply_list *next;
62 typedef struct pending_reply {
63 uint64_t first_request;
64 uint64_t last_request;
65 enum workarounds workaround;
67 struct pending_reply *next;
70 typedef struct reader_list {
73 struct reader_list *next;
76 static void wake_up_next_reader(xcb_connection_t *c)
80 pthreadret = pthread_cond_signal(c->in.readers->data);
82 pthreadret = pthread_cond_signal(&c->in.event_cond);
83 assert(pthreadret == 0);
86 static int read_packet(xcb_connection_t *c)
88 xcb_generic_reply_t genrep;
90 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
92 pending_reply *pend = 0;
93 struct event_list *event;
95 /* Wait for there to be enough data for us to read a whole packet */
96 if(c->in.queue_len < length)
99 /* Get the response type, length, and sequence number. */
100 memcpy(&genrep, c->in.queue, sizeof(genrep));
102 /* Compute 32-bit sequence number of this packet. */
103 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
105 uint64_t lastread = c->in.request_read;
106 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
107 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
108 c->in.request_read += 0x10000;
109 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
110 c->in.request_expected = c->in.request_read;
112 if(c->in.request_read != lastread)
114 if(c->in.current_reply)
116 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
117 c->in.current_reply = 0;
118 c->in.current_reply_tail = &c->in.current_reply;
120 c->in.request_completed = c->in.request_read - 1;
123 while(c->in.pending_replies &&
124 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
125 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
127 pending_reply *oldpend = c->in.pending_replies;
128 c->in.pending_replies = oldpend->next;
130 c->in.pending_replies_tail = &c->in.pending_replies;
134 if(genrep.response_type == XCB_ERROR)
135 c->in.request_completed = c->in.request_read;
138 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
140 pend = c->in.pending_replies;
142 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
143 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
144 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
148 /* For reply packets, check that the entire packet is available. */
149 if(genrep.response_type == XCB_REPLY)
151 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
153 uint32_t *p = (uint32_t *) c->in.queue;
154 genrep.length = p[2] * p[3] * 2;
156 length += genrep.length * 4;
159 /* XGE events may have sizes > 32 */
160 if (genrep.response_type == XCB_XGE_EVENT)
162 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
165 buf = malloc(length + eventlength +
166 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
169 _xcb_conn_shutdown(c);
173 if(_xcb_in_read_block(c, buf, length) <= 0)
179 /* pull in XGE event data if available, append after event struct */
182 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
189 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
195 if(genrep.response_type != XCB_REPLY)
196 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
198 /* reply, or checked error */
199 if( genrep.response_type == XCB_REPLY ||
200 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
203 struct reply_list *cur = malloc(sizeof(struct reply_list));
206 _xcb_conn_shutdown(c);
212 *c->in.current_reply_tail = cur;
213 c->in.current_reply_tail = &cur->next;
214 for(reader = c->in.readers;
216 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
217 reader = reader->next)
219 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
221 pthread_cond_signal(reader->data);
228 /* event, or unchecked error */
229 event = malloc(sizeof(struct event_list));
232 _xcb_conn_shutdown(c);
238 *c->in.events_tail = event;
239 c->in.events_tail = &event->next;
240 pthread_cond_signal(&c->in.event_cond);
241 return 1; /* I have something for you... */
244 static xcb_generic_event_t *get_event(xcb_connection_t *c)
246 struct event_list *cur = c->in.events;
247 xcb_generic_event_t *ret;
251 c->in.events = cur->next;
253 c->in.events_tail = &c->in.events;
258 static void free_reply_list(struct reply_list *head)
262 struct reply_list *cur = head;
269 static int read_block(const int fd, void *buf, const ssize_t len)
275 int ret = read(fd, ((char *) buf) + done, len - done);
277 int ret = recv(fd, ((char *) buf) + done, len - done,0);
283 if(ret < 0 && errno == EAGAIN)
285 if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
294 ret = poll(&pfd, 1, -1);
295 } while (ret == -1 && errno == EINTR);
301 /* Initializing errno here makes sure that for Win32 this loop will execute only once */
304 ret = select(fd + 1, &fds, 0, 0, 0);
305 } while (ret == -1 && errno == EINTR);
306 #endif /* USE_POLL */
314 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
316 struct reply_list *head;
318 /* If an error occurred when issuing the request, fail immediately. */
321 /* We've read requests past the one we want, so if it has replies we have
322 * them all and they're in the replies map. */
323 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
325 head = _xcb_map_remove(c->in.replies, request);
326 if(head && head->next)
327 _xcb_map_put(c->in.replies, request, head->next);
329 /* We're currently processing the responses to the request we want, and we
330 * have a reply ready to return. So just return it without blocking. */
331 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
333 head = c->in.current_reply;
334 c->in.current_reply = head->next;
336 c->in.current_reply_tail = &c->in.current_reply;
338 /* We know this request can't have any more replies, and we've already
339 * established it doesn't have a reply now. Don't bother blocking. */
340 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
342 /* We may have more replies on the way for this request: block until we're
353 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
356 *error = head->reply;
361 *reply = head->reply;
369 /* Public interface */
371 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
373 uint64_t widened_request;
380 pthread_mutex_lock(&c->iolock);
382 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
383 if(widened_request > c->out.request)
384 widened_request -= UINT64_C(1) << 32;
386 /* If this request has not been written yet, write it. */
387 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
389 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
391 reader_list **prev_reader;
393 for(prev_reader = &c->in.readers;
395 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
396 prev_reader = &(*prev_reader)->next)
400 reader.request = request;
402 reader.next = *prev_reader;
403 *prev_reader = &reader;
405 while(!poll_for_reply(c, request, &ret, e))
406 if(!_xcb_conn_wait(c, &cond, 0, 0))
409 for(prev_reader = &c->in.readers;
411 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
412 prev_reader = &(*prev_reader)->next)
414 if(*prev_reader == &reader)
416 *prev_reader = (*prev_reader)->next;
420 pthread_cond_destroy(&cond);
423 wake_up_next_reader(c);
424 pthread_mutex_unlock(&c->iolock);
428 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
431 pend = malloc(sizeof(*pend));
434 _xcb_conn_shutdown(c);
438 pend->first_request = seq;
439 pend->last_request = seq;
440 pend->workaround = 0;
441 pend->flags = XCB_REQUEST_DISCARD_REPLY;
442 pend->next = *prev_next;
446 c->in.pending_replies_tail = &pend->next;
449 static void discard_reply(xcb_connection_t *c, unsigned int request)
451 pending_reply *pend = 0;
452 pending_reply **prev_pend;
453 uint64_t widened_request;
455 /* We've read requests past the one we want, so if it has replies we have
456 * them all and they're in the replies map. */
457 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
459 struct reply_list *head;
460 head = _xcb_map_remove(c->in.replies, request);
463 struct reply_list *next = head->next;
471 /* We're currently processing the responses to the request we want, and we
472 * have a reply ready to return. Free it, and mark the pend to free any further
474 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
476 struct reply_list *head;
477 head = c->in.current_reply;
478 c->in.current_reply = NULL;
479 c->in.current_reply_tail = &c->in.current_reply;
482 struct reply_list *next = head->next;
488 pend = c->in.pending_replies;
490 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
491 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
492 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
495 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
497 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
502 /* Walk the list of pending requests. Mark the first match for deletion. */
503 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
505 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
508 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
510 /* Pending reply found. Mark for discard: */
511 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
516 /* Pending reply not found (likely due to _unchecked request). Create one: */
517 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
518 if(widened_request > c->out.request)
519 widened_request -= UINT64_C(1) << 32;
521 insert_pending_discard(c, prev_pend, widened_request);
524 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
529 /* If an error occurred when issuing the request, fail immediately. */
533 pthread_mutex_lock(&c->iolock);
534 discard_reply(c, sequence);
535 pthread_mutex_unlock(&c->iolock);
538 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
546 return 1; /* would not block */
549 pthread_mutex_lock(&c->iolock);
550 ret = poll_for_reply(c, request, reply, error);
551 pthread_mutex_unlock(&c->iolock);
555 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
557 xcb_generic_event_t *ret;
560 pthread_mutex_lock(&c->iolock);
561 /* get_event returns 0 on empty list. */
562 while(!(ret = get_event(c)))
563 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
566 wake_up_next_reader(c);
567 pthread_mutex_unlock(&c->iolock);
571 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
573 xcb_generic_event_t *ret = 0;
576 pthread_mutex_lock(&c->iolock);
577 /* FIXME: follow X meets Z architecture changes. */
579 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
581 pthread_mutex_unlock(&c->iolock);
586 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
588 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
589 * that would require factoring the locking out of xcb_get_input_focus,
590 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
591 xcb_generic_error_t *ret;
595 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
596 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
598 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
601 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
606 /* Private interface */
608 int _xcb_in_init(_xcb_in *in)
610 if(pthread_cond_init(&in->event_cond, 0))
616 in->request_read = 0;
617 in->request_completed = 0;
619 in->replies = _xcb_map_new();
623 in->current_reply_tail = &in->current_reply;
624 in->events_tail = &in->events;
625 in->pending_replies_tail = &in->pending_replies;
630 void _xcb_in_destroy(_xcb_in *in)
632 pthread_cond_destroy(&in->event_cond);
633 free_reply_list(in->current_reply);
634 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
637 struct event_list *e = in->events;
638 in->events = e->next;
642 while(in->pending_replies)
644 pending_reply *pend = in->pending_replies;
645 in->pending_replies = pend->next;
650 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
652 pending_reply *pend = malloc(sizeof(pending_reply));
653 assert(workaround != WORKAROUND_NONE || flags != 0);
656 _xcb_conn_shutdown(c);
659 pend->first_request = pend->last_request = request;
660 pend->workaround = workaround;
663 *c->in.pending_replies_tail = pend;
664 c->in.pending_replies_tail = &pend->next;
668 void _xcb_in_replies_done(xcb_connection_t *c)
670 struct pending_reply *pend;
671 if (c->in.pending_replies_tail != &c->in.pending_replies)
673 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
674 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
676 pend->last_request = c->out.request;
677 pend->workaround = WORKAROUND_NONE;
682 int _xcb_in_read(xcb_connection_t *c)
685 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
687 int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,0);
690 c->in.queue_len += n;
691 while(read_packet(c))
694 if((n > 0) || (n < 0 && errno == EAGAIN))
696 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
699 _xcb_conn_shutdown(c);
703 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
705 int done = c->in.queue_len;
709 memcpy(buf, c->in.queue, done);
710 c->in.queue_len -= done;
711 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
715 int ret = read_block(c->fd, (char *) buf + done, len - done);
718 _xcb_conn_shutdown(c);