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 void wake_up_next_reader(xcb_connection_t *c)
76 pthreadret = pthread_cond_signal(c->in.readers->data);
78 pthreadret = pthread_cond_signal(&c->in.event_cond);
79 assert(pthreadret == 0);
82 static int read_packet(xcb_connection_t *c)
84 xcb_generic_reply_t genrep;
86 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
88 pending_reply *pend = 0;
89 struct event_list *event;
91 /* Wait for there to be enough data for us to read a whole packet */
92 if(c->in.queue_len < length)
95 /* Get the response type, length, and sequence number. */
96 memcpy(&genrep, c->in.queue, sizeof(genrep));
98 /* Compute 32-bit sequence number of this packet. */
99 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
101 uint64_t lastread = c->in.request_read;
102 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
103 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
104 c->in.request_read += 0x10000;
105 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
106 c->in.request_expected = c->in.request_read;
108 if(c->in.request_read != lastread)
110 if(c->in.current_reply)
112 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
113 c->in.current_reply = 0;
114 c->in.current_reply_tail = &c->in.current_reply;
116 c->in.request_completed = c->in.request_read - 1;
119 while(c->in.pending_replies &&
120 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
121 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
123 pending_reply *oldpend = c->in.pending_replies;
124 c->in.pending_replies = oldpend->next;
126 c->in.pending_replies_tail = &c->in.pending_replies;
130 if(genrep.response_type == XCB_ERROR)
131 c->in.request_completed = c->in.request_read;
134 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
136 pend = c->in.pending_replies;
138 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
139 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
140 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
144 /* For reply packets, check that the entire packet is available. */
145 if(genrep.response_type == XCB_REPLY)
147 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
149 uint32_t *p = (uint32_t *) c->in.queue;
150 genrep.length = p[2] * p[3] * 2;
152 length += genrep.length * 4;
155 /* XGE events may have sizes > 32 */
156 if (genrep.response_type == XCB_XGE_EVENT)
158 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
161 buf = malloc(length + eventlength +
162 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
165 _xcb_conn_shutdown(c);
169 if(_xcb_in_read_block(c, buf, length) <= 0)
175 /* pull in XGE event data if available, append after event struct */
178 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
185 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
191 if(genrep.response_type != XCB_REPLY)
192 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
194 /* reply, or checked error */
195 if( genrep.response_type == XCB_REPLY ||
196 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
199 struct reply_list *cur = malloc(sizeof(struct reply_list));
202 _xcb_conn_shutdown(c);
208 *c->in.current_reply_tail = cur;
209 c->in.current_reply_tail = &cur->next;
210 for(reader = c->in.readers;
212 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
213 reader = reader->next)
215 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
217 pthread_cond_signal(reader->data);
224 /* event, or unchecked error */
225 event = malloc(sizeof(struct event_list));
228 _xcb_conn_shutdown(c);
234 *c->in.events_tail = event;
235 c->in.events_tail = &event->next;
236 pthread_cond_signal(&c->in.event_cond);
237 return 1; /* I have something for you... */
240 static xcb_generic_event_t *get_event(xcb_connection_t *c)
242 struct event_list *cur = c->in.events;
243 xcb_generic_event_t *ret;
247 c->in.events = cur->next;
249 c->in.events_tail = &c->in.events;
254 static void free_reply_list(struct reply_list *head)
258 struct reply_list *cur = head;
265 static int read_block(const int fd, void *buf, const ssize_t len)
270 int ret = read(fd, ((char *) buf) + done, len - done);
273 if(ret < 0 && errno == EAGAIN)
281 ret = poll(&pfd, 1, -1);
282 } while (ret == -1 && errno == EINTR);
288 ret = select(fd + 1, &fds, 0, 0, 0);
289 } while (ret == -1 && errno == EINTR);
298 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
300 struct reply_list *head;
302 /* If an error occurred when issuing the request, fail immediately. */
305 /* We've read requests past the one we want, so if it has replies we have
306 * them all and they're in the replies map. */
307 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
309 head = _xcb_map_remove(c->in.replies, request);
310 if(head && head->next)
311 _xcb_map_put(c->in.replies, request, head->next);
313 /* We're currently processing the responses to the request we want, and we
314 * have a reply ready to return. So just return it without blocking. */
315 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
317 head = c->in.current_reply;
318 c->in.current_reply = head->next;
320 c->in.current_reply_tail = &c->in.current_reply;
322 /* We know this request can't have any more replies, and we've already
323 * established it doesn't have a reply now. Don't bother blocking. */
324 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
326 /* We may have more replies on the way for this request: block until we're
337 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
340 *error = head->reply;
345 *reply = head->reply;
353 /* Public interface */
355 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
357 uint64_t widened_request;
364 pthread_mutex_lock(&c->iolock);
366 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
367 if(widened_request > c->out.request)
368 widened_request -= UINT64_C(1) << 32;
370 /* If this request has not been written yet, write it. */
371 if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
373 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
375 reader_list **prev_reader;
377 for(prev_reader = &c->in.readers;
379 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
380 prev_reader = &(*prev_reader)->next)
384 reader.request = request;
386 reader.next = *prev_reader;
387 *prev_reader = &reader;
389 while(!poll_for_reply(c, request, &ret, e))
390 if(!_xcb_conn_wait(c, &cond, 0, 0))
393 for(prev_reader = &c->in.readers;
395 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
396 prev_reader = &(*prev_reader)->next)
398 if(*prev_reader == &reader)
400 *prev_reader = (*prev_reader)->next;
404 pthread_cond_destroy(&cond);
407 wake_up_next_reader(c);
408 pthread_mutex_unlock(&c->iolock);
412 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
420 return 1; /* would not block */
423 pthread_mutex_lock(&c->iolock);
424 ret = poll_for_reply(c, request, reply, error);
425 pthread_mutex_unlock(&c->iolock);
429 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
431 xcb_generic_event_t *ret;
434 pthread_mutex_lock(&c->iolock);
435 /* get_event returns 0 on empty list. */
436 while(!(ret = get_event(c)))
437 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
440 wake_up_next_reader(c);
441 pthread_mutex_unlock(&c->iolock);
445 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
447 xcb_generic_event_t *ret = 0;
450 pthread_mutex_lock(&c->iolock);
451 /* FIXME: follow X meets Z architecture changes. */
453 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
455 pthread_mutex_unlock(&c->iolock);
460 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
462 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
463 * that would require factoring the locking out of xcb_get_input_focus,
464 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
465 xcb_generic_error_t *ret;
469 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
470 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
472 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
475 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
480 /* Private interface */
482 int _xcb_in_init(_xcb_in *in)
484 if(pthread_cond_init(&in->event_cond, 0))
490 in->request_read = 0;
491 in->request_completed = 0;
493 in->replies = _xcb_map_new();
497 in->current_reply_tail = &in->current_reply;
498 in->events_tail = &in->events;
499 in->pending_replies_tail = &in->pending_replies;
504 void _xcb_in_destroy(_xcb_in *in)
506 pthread_cond_destroy(&in->event_cond);
507 free_reply_list(in->current_reply);
508 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
511 struct event_list *e = in->events;
512 in->events = e->next;
516 while(in->pending_replies)
518 pending_reply *pend = in->pending_replies;
519 in->pending_replies = pend->next;
524 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
526 pending_reply *pend = malloc(sizeof(pending_reply));
527 assert(workaround != WORKAROUND_NONE || flags != 0);
530 _xcb_conn_shutdown(c);
533 pend->first_request = pend->last_request = request;
534 pend->workaround = workaround;
537 *c->in.pending_replies_tail = pend;
538 c->in.pending_replies_tail = &pend->next;
542 void _xcb_in_replies_done(xcb_connection_t *c)
544 struct pending_reply *pend;
545 if (c->in.pending_replies_tail != &c->in.pending_replies)
547 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
548 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
550 pend->last_request = c->out.request;
551 pend->workaround = WORKAROUND_NONE;
556 int _xcb_in_read(xcb_connection_t *c)
558 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
560 c->in.queue_len += n;
561 while(read_packet(c))
563 if((n > 0) || (n < 0 && errno == EAGAIN))
565 _xcb_conn_shutdown(c);
569 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
571 int done = c->in.queue_len;
575 memcpy(buf, c->in.queue, done);
576 c->in.queue_len -= done;
577 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
581 int ret = read_block(c->fd, (char *) buf + done, len - done);
584 _xcb_conn_shutdown(c);