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>
46 #define XCB_XGE_EVENT 35
49 xcb_generic_event_t *event;
50 struct event_list *next;
55 struct reply_list *next;
58 typedef struct pending_reply {
59 uint64_t first_request;
60 uint64_t last_request;
61 enum workarounds workaround;
63 struct pending_reply *next;
66 typedef struct reader_list {
69 struct reader_list *next;
72 static int read_packet(xcb_connection_t *c)
74 xcb_generic_reply_t genrep;
76 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
78 pending_reply *pend = 0;
79 struct event_list *event;
81 /* Wait for there to be enough data for us to read a whole packet */
82 if(c->in.queue_len < length)
85 /* Get the response type, length, and sequence number. */
86 memcpy(&genrep, c->in.queue, sizeof(genrep));
88 /* Compute 32-bit sequence number of this packet. */
89 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
91 uint64_t lastread = c->in.request_read;
92 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
93 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
94 c->in.request_read += 0x10000;
95 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
96 c->in.request_expected = c->in.request_read;
98 if(c->in.request_read != lastread)
100 if(c->in.current_reply)
102 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
103 c->in.current_reply = 0;
104 c->in.current_reply_tail = &c->in.current_reply;
106 c->in.request_completed = c->in.request_read - 1;
109 while(c->in.pending_replies &&
110 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
111 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
113 pending_reply *oldpend = c->in.pending_replies;
114 c->in.pending_replies = oldpend->next;
116 c->in.pending_replies_tail = &c->in.pending_replies;
120 if(genrep.response_type == XCB_ERROR)
121 c->in.request_completed = c->in.request_read;
124 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
126 pend = c->in.pending_replies;
128 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
129 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
130 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
134 /* For reply packets, check that the entire packet is available. */
135 if(genrep.response_type == XCB_REPLY)
137 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
139 uint32_t *p = (uint32_t *) c->in.queue;
140 genrep.length = p[2] * p[3] * 2;
142 length += genrep.length * 4;
145 /* XGE events may have sizes > 32 */
146 if (genrep.response_type == XCB_XGE_EVENT)
147 eventlength = genrep.length * 4;
149 buf = malloc(length + eventlength +
150 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
153 _xcb_conn_shutdown(c);
157 if(_xcb_in_read_block(c, buf, length) <= 0)
163 /* pull in XGE event data if available, append after event struct */
166 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
173 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
179 if(genrep.response_type != XCB_REPLY)
180 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
182 /* reply, or checked error */
183 if( genrep.response_type == XCB_REPLY ||
184 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
187 struct reply_list *cur = malloc(sizeof(struct reply_list));
190 _xcb_conn_shutdown(c);
196 *c->in.current_reply_tail = cur;
197 c->in.current_reply_tail = &cur->next;
198 for(reader = c->in.readers;
200 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
201 reader = reader->next)
203 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
205 pthread_cond_signal(reader->data);
212 /* event, or unchecked error */
213 event = malloc(sizeof(struct event_list));
216 _xcb_conn_shutdown(c);
222 *c->in.events_tail = event;
223 c->in.events_tail = &event->next;
224 pthread_cond_signal(&c->in.event_cond);
225 return 1; /* I have something for you... */
228 static xcb_generic_event_t *get_event(xcb_connection_t *c)
230 struct event_list *cur = c->in.events;
231 xcb_generic_event_t *ret;
235 c->in.events = cur->next;
237 c->in.events_tail = &c->in.events;
242 static void free_reply_list(struct reply_list *head)
246 struct reply_list *cur = head;
253 static int read_block(const int fd, void *buf, const ssize_t len)
258 int ret = read(fd, ((char *) buf) + done, len - done);
261 if(ret < 0 && errno == EAGAIN)
269 ret = poll(&pfd, 1, -1);
270 } while (ret == -1 && errno == EINTR);
276 ret = select(fd + 1, &fds, 0, 0, 0);
277 } while (ret == -1 && errno == EINTR);
286 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
288 struct reply_list *head;
290 /* If an error occurred when issuing the request, fail immediately. */
293 /* We've read requests past the one we want, so if it has replies we have
294 * them all and they're in the replies map. */
295 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
297 head = _xcb_map_remove(c->in.replies, request);
298 if(head && head->next)
299 _xcb_map_put(c->in.replies, request, head->next);
301 /* We're currently processing the responses to the request we want, and we
302 * have a reply ready to return. So just return it without blocking. */
303 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
305 head = c->in.current_reply;
306 c->in.current_reply = head->next;
308 c->in.current_reply_tail = &c->in.current_reply;
310 /* We know this request can't have any more replies, and we've already
311 * established it doesn't have a reply now. Don't bother blocking. */
312 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
314 /* We may have more replies on the way for this request: block until we're
325 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
328 *error = head->reply;
333 *reply = head->reply;
341 /* Public interface */
343 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
345 uint64_t widened_request;
352 pthread_mutex_lock(&c->iolock);
354 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
355 if(widened_request > c->out.request)
356 widened_request -= UINT64_C(1) << 32;
358 /* If this request has not been written yet, write it. */
359 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
361 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
363 reader_list **prev_reader;
365 for(prev_reader = &c->in.readers;
367 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
368 prev_reader = &(*prev_reader)->next)
372 reader.request = request;
374 reader.next = *prev_reader;
375 *prev_reader = &reader;
377 while(!poll_for_reply(c, request, &ret, e))
378 if(!_xcb_conn_wait(c, &cond, 0, 0))
381 for(prev_reader = &c->in.readers;
383 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
384 prev_reader = &(*prev_reader)->next)
386 if(*prev_reader == &reader)
388 *prev_reader = (*prev_reader)->next;
392 pthread_cond_destroy(&cond);
395 _xcb_in_wake_up_next_reader(c);
396 pthread_mutex_unlock(&c->iolock);
400 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
403 pend = malloc(sizeof(*pend));
406 _xcb_conn_shutdown(c);
410 pend->first_request = seq;
411 pend->last_request = seq;
412 pend->workaround = 0;
413 pend->flags = XCB_REQUEST_DISCARD_REPLY;
414 pend->next = *prev_next;
418 c->in.pending_replies_tail = &pend->next;
421 static void discard_reply(xcb_connection_t *c, unsigned int request)
423 pending_reply *pend = 0;
424 pending_reply **prev_pend;
425 uint64_t widened_request;
427 /* We've read requests past the one we want, so if it has replies we have
428 * them all and they're in the replies map. */
429 if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
431 struct reply_list *head;
432 head = _xcb_map_remove(c->in.replies, request);
435 struct reply_list *next = head->next;
443 /* We're currently processing the responses to the request we want, and we
444 * have a reply ready to return. Free it, and mark the pend to free any further
446 if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
448 struct reply_list *head;
449 head = c->in.current_reply;
450 c->in.current_reply = NULL;
451 c->in.current_reply_tail = &c->in.current_reply;
454 struct reply_list *next = head->next;
460 pend = c->in.pending_replies;
462 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
463 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
464 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
467 pend->flags |= XCB_REQUEST_DISCARD_REPLY;
469 insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
474 /* Walk the list of pending requests. Mark the first match for deletion. */
475 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
477 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
480 if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
482 /* Pending reply found. Mark for discard: */
483 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
488 /* Pending reply not found (likely due to _unchecked request). Create one: */
489 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
490 if(widened_request > c->out.request)
491 widened_request -= UINT64_C(1) << 32;
493 insert_pending_discard(c, prev_pend, widened_request);
496 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
501 /* If an error occurred when issuing the request, fail immediately. */
505 pthread_mutex_lock(&c->iolock);
506 discard_reply(c, sequence);
507 pthread_mutex_unlock(&c->iolock);
510 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
518 return 1; /* would not block */
521 pthread_mutex_lock(&c->iolock);
522 ret = poll_for_reply(c, request, reply, error);
523 pthread_mutex_unlock(&c->iolock);
527 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
529 xcb_generic_event_t *ret;
532 pthread_mutex_lock(&c->iolock);
533 /* get_event returns 0 on empty list. */
534 while(!(ret = get_event(c)))
535 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
538 _xcb_in_wake_up_next_reader(c);
539 pthread_mutex_unlock(&c->iolock);
543 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
545 xcb_generic_event_t *ret = 0;
548 pthread_mutex_lock(&c->iolock);
549 /* FIXME: follow X meets Z architecture changes. */
551 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
553 pthread_mutex_unlock(&c->iolock);
558 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
560 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
561 * that would require factoring the locking out of xcb_get_input_focus,
562 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
563 xcb_generic_error_t *ret;
567 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
568 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
570 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
573 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
578 /* Private interface */
580 int _xcb_in_init(_xcb_in *in)
582 if(pthread_cond_init(&in->event_cond, 0))
588 in->request_read = 0;
589 in->request_completed = 0;
591 in->replies = _xcb_map_new();
595 in->current_reply_tail = &in->current_reply;
596 in->events_tail = &in->events;
597 in->pending_replies_tail = &in->pending_replies;
602 void _xcb_in_destroy(_xcb_in *in)
604 pthread_cond_destroy(&in->event_cond);
605 free_reply_list(in->current_reply);
606 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
609 struct event_list *e = in->events;
610 in->events = e->next;
614 while(in->pending_replies)
616 pending_reply *pend = in->pending_replies;
617 in->pending_replies = pend->next;
622 void _xcb_in_wake_up_next_reader(xcb_connection_t *c)
626 pthreadret = pthread_cond_signal(c->in.readers->data);
628 pthreadret = pthread_cond_signal(&c->in.event_cond);
629 assert(pthreadret == 0);
632 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
634 pending_reply *pend = malloc(sizeof(pending_reply));
635 assert(workaround != WORKAROUND_NONE || flags != 0);
638 _xcb_conn_shutdown(c);
641 pend->first_request = pend->last_request = request;
642 pend->workaround = workaround;
645 *c->in.pending_replies_tail = pend;
646 c->in.pending_replies_tail = &pend->next;
650 void _xcb_in_replies_done(xcb_connection_t *c)
652 struct pending_reply *pend;
653 if (c->in.pending_replies_tail != &c->in.pending_replies)
655 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
656 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
658 pend->last_request = c->out.request;
659 pend->workaround = WORKAROUND_NONE;
664 int _xcb_in_read(xcb_connection_t *c)
666 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
668 c->in.queue_len += n;
669 while(read_packet(c))
671 if((n > 0) || (n < 0 && errno == EAGAIN))
673 _xcb_conn_shutdown(c);
677 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
679 int done = c->in.queue_len;
683 memcpy(buf, c->in.queue, done);
684 c->in.queue_len -= done;
685 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
689 int ret = read_block(c->fd, (char *) buf + done, len - done);
692 _xcb_conn_shutdown(c);