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>
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)
295 ret = poll(&pfd, 1, -1);
296 } while (ret == -1 && errno == EINTR);
304 ret = select(fd + 1, &fds, 0, 0, 0);
305 } while (ret == -1 && errno == EINTR);
307 /* the do while loop used for the non-windows version isn't required*/
308 /* for windows since there are no signals in Windows hence no EINTR*/
309 ret = select(fd + 1, &fds, 0, 0, 0);
311 #endif /* USE_POLL */
319 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
321 struct reply_list *head;
323 /* If an error occurred when issuing the request, fail immediately. */
326 /* We've read requests past the one we want, so if it has replies we have
327 * them all and they're in the replies map. */
328 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
330 head = _xcb_map_remove(c->in.replies, request);
331 if(head && head->next)
332 _xcb_map_put(c->in.replies, request, head->next);
334 /* We're currently processing the responses to the request we want, and we
335 * have a reply ready to return. So just return it without blocking. */
336 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
338 head = c->in.current_reply;
339 c->in.current_reply = head->next;
341 c->in.current_reply_tail = &c->in.current_reply;
343 /* We know this request can't have any more replies, and we've already
344 * established it doesn't have a reply now. Don't bother blocking. */
345 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
347 /* We may have more replies on the way for this request: block until we're
358 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
361 *error = head->reply;
366 *reply = head->reply;
374 /* Public interface */
376 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
378 uint64_t widened_request;
385 pthread_mutex_lock(&c->iolock);
387 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
388 if(widened_request > c->out.request)
389 widened_request -= UINT64_C(1) << 32;
391 /* If this request has not been written yet, write it. */
392 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
394 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
396 reader_list **prev_reader;
398 for(prev_reader = &c->in.readers;
400 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
401 prev_reader = &(*prev_reader)->next)
405 reader.request = request;
407 reader.next = *prev_reader;
408 *prev_reader = &reader;
410 while(!poll_for_reply(c, request, &ret, e))
411 if(!_xcb_conn_wait(c, &cond, 0, 0))
414 for(prev_reader = &c->in.readers;
416 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
417 prev_reader = &(*prev_reader)->next)
419 if(*prev_reader == &reader)
421 *prev_reader = (*prev_reader)->next;
425 pthread_cond_destroy(&cond);
428 wake_up_next_reader(c);
429 pthread_mutex_unlock(&c->iolock);
433 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
436 pend = malloc(sizeof(*pend));
439 _xcb_conn_shutdown(c);
443 pend->first_request = seq;
444 pend->last_request = seq;
445 pend->workaround = 0;
446 pend->flags = XCB_REQUEST_DISCARD_REPLY;
447 pend->next = *prev_next;
451 c->in.pending_replies_tail = &pend->next;
454 static void discard_reply(xcb_connection_t *c, unsigned int request)
456 pending_reply *pend = 0;
457 pending_reply **prev_pend;
458 uint64_t widened_request;
460 /* We've read requests past the one we want, so if it has replies we have
461 * them all and they're in the replies map. */
462 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
464 struct reply_list *head;
465 head = _xcb_map_remove(c->in.replies, request);
468 struct reply_list *next = head->next;
476 /* We're currently processing the responses to the request we want, and we
477 * have a reply ready to return. Free it, and mark the pend to free any further
479 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
481 struct reply_list *head;
482 head = c->in.current_reply;
483 c->in.current_reply = NULL;
484 c->in.current_reply_tail = &c->in.current_reply;
487 struct reply_list *next = head->next;
493 pend = c->in.pending_replies;
495 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
496 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
497 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
500 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
502 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
507 /* Walk the list of pending requests. Mark the first match for deletion. */
508 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
510 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
513 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
515 /* Pending reply found. Mark for discard: */
516 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
521 /* Pending reply not found (likely due to _unchecked request). Create one: */
522 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
523 if(widened_request > c->out.request)
524 widened_request -= UINT64_C(1) << 32;
526 insert_pending_discard(c, prev_pend, widened_request);
529 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
534 /* If an error occurred when issuing the request, fail immediately. */
538 pthread_mutex_lock(&c->iolock);
539 discard_reply(c, sequence);
540 pthread_mutex_unlock(&c->iolock);
543 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
551 return 1; /* would not block */
554 pthread_mutex_lock(&c->iolock);
555 ret = poll_for_reply(c, request, reply, error);
556 pthread_mutex_unlock(&c->iolock);
560 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
562 xcb_generic_event_t *ret;
565 pthread_mutex_lock(&c->iolock);
566 /* get_event returns 0 on empty list. */
567 while(!(ret = get_event(c)))
568 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
571 wake_up_next_reader(c);
572 pthread_mutex_unlock(&c->iolock);
576 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
578 xcb_generic_event_t *ret = 0;
581 pthread_mutex_lock(&c->iolock);
582 /* FIXME: follow X meets Z architecture changes. */
584 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
586 pthread_mutex_unlock(&c->iolock);
591 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
593 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
594 * that would require factoring the locking out of xcb_get_input_focus,
595 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
596 xcb_generic_error_t *ret;
600 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
601 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
603 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
606 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
611 /* Private interface */
613 int _xcb_in_init(_xcb_in *in)
615 if(pthread_cond_init(&in->event_cond, 0))
621 in->request_read = 0;
622 in->request_completed = 0;
624 in->replies = _xcb_map_new();
628 in->current_reply_tail = &in->current_reply;
629 in->events_tail = &in->events;
630 in->pending_replies_tail = &in->pending_replies;
635 void _xcb_in_destroy(_xcb_in *in)
637 pthread_cond_destroy(&in->event_cond);
638 free_reply_list(in->current_reply);
639 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
642 struct event_list *e = in->events;
643 in->events = e->next;
647 while(in->pending_replies)
649 pending_reply *pend = in->pending_replies;
650 in->pending_replies = pend->next;
655 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
657 pending_reply *pend = malloc(sizeof(pending_reply));
658 assert(workaround != WORKAROUND_NONE || flags != 0);
661 _xcb_conn_shutdown(c);
664 pend->first_request = pend->last_request = request;
665 pend->workaround = workaround;
668 *c->in.pending_replies_tail = pend;
669 c->in.pending_replies_tail = &pend->next;
673 void _xcb_in_replies_done(xcb_connection_t *c)
675 struct pending_reply *pend;
676 if (c->in.pending_replies_tail != &c->in.pending_replies)
678 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
679 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
681 pend->last_request = c->out.request;
682 pend->workaround = WORKAROUND_NONE;
687 int _xcb_in_read(xcb_connection_t *c)
690 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
692 int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,0);
695 c->in.queue_len += n;
696 while(read_packet(c))
699 if((n > 0) || (n < 0 && errno == EAGAIN))
701 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
704 _xcb_conn_shutdown(c);
708 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
710 int done = c->in.queue_len;
714 memcpy(buf, c->in.queue, done);
715 c->in.queue_len -= done;
716 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
720 int ret = read_block(c->fd, (char *) buf + done, len - done);
723 _xcb_conn_shutdown(c);