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. */
33 #include <sys/select.h>
42 #define XCB_XGE_EVENT 35
45 xcb_generic_event_t *event;
46 struct event_list *next;
51 struct reply_list *next;
54 typedef struct pending_reply {
55 uint64_t first_request;
56 uint64_t last_request;
57 enum workarounds workaround;
59 struct pending_reply *next;
62 typedef struct reader_list {
65 struct reader_list *next;
68 static void wake_up_next_reader(xcb_connection_t *c)
72 pthreadret = pthread_cond_signal(c->in.readers->data);
74 pthreadret = pthread_cond_signal(&c->in.event_cond);
75 assert(pthreadret == 0);
78 static int read_packet(xcb_connection_t *c)
80 xcb_generic_reply_t genrep;
82 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
84 pending_reply *pend = 0;
85 struct event_list *event;
87 /* Wait for there to be enough data for us to read a whole packet */
88 if(c->in.queue_len < length)
91 /* Get the response type, length, and sequence number. */
92 memcpy(&genrep, c->in.queue, sizeof(genrep));
94 /* Compute 32-bit sequence number of this packet. */
95 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
97 uint64_t lastread = c->in.request_read;
98 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
99 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
100 c->in.request_read += 0x10000;
101 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
102 c->in.request_expected = c->in.request_read;
104 if(c->in.request_read != lastread)
106 if(c->in.current_reply)
108 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
109 c->in.current_reply = 0;
110 c->in.current_reply_tail = &c->in.current_reply;
112 c->in.request_completed = c->in.request_read - 1;
115 while(c->in.pending_replies &&
116 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
118 pending_reply *oldpend = c->in.pending_replies;
119 c->in.pending_replies = oldpend->next;
121 c->in.pending_replies_tail = &c->in.pending_replies;
125 if(genrep.response_type == XCB_ERROR)
126 c->in.request_completed = c->in.request_read;
129 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
131 pend = c->in.pending_replies;
133 (XCB_SEQUENCE_COMPARE(c->in.request_read, <, pend->first_request) ||
134 XCB_SEQUENCE_COMPARE(c->in.request_read, >, pend->last_request)))
138 /* For reply packets, check that the entire packet is available. */
139 if(genrep.response_type == XCB_REPLY)
141 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
143 uint32_t *p = (uint32_t *) c->in.queue;
144 genrep.length = p[2] * p[3] * 2;
146 length += genrep.length * 4;
149 /* XGE events may have sizes > 32 */
150 if (genrep.response_type == XCB_XGE_EVENT)
152 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
155 buf = malloc(length + eventlength +
156 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
159 _xcb_conn_shutdown(c);
163 if(_xcb_in_read_block(c, buf, length) <= 0)
169 /* pull in XGE event data if available, append after event struct */
172 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
179 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
185 if(genrep.response_type != XCB_REPLY)
186 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
188 /* reply, or checked error */
189 if( genrep.response_type == XCB_REPLY ||
190 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
193 struct reply_list *cur = malloc(sizeof(struct reply_list));
196 _xcb_conn_shutdown(c);
202 *c->in.current_reply_tail = cur;
203 c->in.current_reply_tail = &cur->next;
204 for(reader = c->in.readers;
206 XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
207 reader = reader->next)
209 if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
211 pthread_cond_signal(reader->data);
218 /* event, or unchecked error */
219 event = malloc(sizeof(struct event_list));
222 _xcb_conn_shutdown(c);
228 *c->in.events_tail = event;
229 c->in.events_tail = &event->next;
230 pthread_cond_signal(&c->in.event_cond);
231 return 1; /* I have something for you... */
234 static xcb_generic_event_t *get_event(xcb_connection_t *c)
236 struct event_list *cur = c->in.events;
237 xcb_generic_event_t *ret;
241 c->in.events = cur->next;
243 c->in.events_tail = &c->in.events;
248 static void free_reply_list(struct reply_list *head)
252 struct reply_list *cur = head;
259 static int read_block(const int fd, void *buf, const ssize_t len)
264 int ret = read(fd, ((char *) buf) + done, len - done);
267 if(ret < 0 && errno == EAGAIN)
273 ret = select(fd + 1, &fds, 0, 0, 0);
274 } while (ret == -1 && errno == EINTR);
282 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
284 struct reply_list *head;
286 /* If an error occurred when issuing the request, fail immediately. */
289 /* We've read requests past the one we want, so if it has replies we have
290 * them all and they're in the replies map. */
291 else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
293 head = _xcb_map_remove(c->in.replies, request);
294 if(head && head->next)
295 _xcb_map_put(c->in.replies, request, head->next);
297 /* We're currently processing the responses to the request we want, and we
298 * have a reply ready to return. So just return it without blocking. */
299 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
301 head = c->in.current_reply;
302 c->in.current_reply = head->next;
304 c->in.current_reply_tail = &c->in.current_reply;
306 /* We know this request can't have any more replies, and we've already
307 * established it doesn't have a reply now. Don't bother blocking. */
308 else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
310 /* We may have more replies on the way for this request: block until we're
321 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
324 *error = head->reply;
329 *reply = head->reply;
337 /* Public interface */
339 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
341 uint64_t widened_request;
348 pthread_mutex_lock(&c->iolock);
350 widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
351 if(widened_request > c->out.request)
352 widened_request -= UINT64_C(1) << 32;
354 /* If this request has not been written yet, write it. */
355 if(_xcb_out_flush_to(c, widened_request))
357 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
359 reader_list **prev_reader;
361 for(prev_reader = &c->in.readers;
363 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
364 prev_reader = &(*prev_reader)->next)
368 reader.request = request;
370 reader.next = *prev_reader;
371 *prev_reader = &reader;
373 while(!poll_for_reply(c, request, &ret, e))
374 if(!_xcb_conn_wait(c, &cond, 0, 0))
377 for(prev_reader = &c->in.readers;
379 XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
380 prev_reader = &(*prev_reader)->next)
382 if(*prev_reader == &reader)
384 *prev_reader = (*prev_reader)->next;
388 pthread_cond_destroy(&cond);
391 wake_up_next_reader(c);
392 pthread_mutex_unlock(&c->iolock);
396 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
404 return 1; /* would not block */
407 pthread_mutex_lock(&c->iolock);
408 ret = poll_for_reply(c, request, reply, error);
409 pthread_mutex_unlock(&c->iolock);
413 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
415 xcb_generic_event_t *ret;
418 pthread_mutex_lock(&c->iolock);
419 /* get_event returns 0 on empty list. */
420 while(!(ret = get_event(c)))
421 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
424 wake_up_next_reader(c);
425 pthread_mutex_unlock(&c->iolock);
429 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
431 xcb_generic_event_t *ret = 0;
434 pthread_mutex_lock(&c->iolock);
435 /* FIXME: follow X meets Z architecture changes. */
437 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
439 pthread_mutex_unlock(&c->iolock);
444 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
446 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
447 * that would require factoring the locking out of xcb_get_input_focus,
448 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
449 xcb_generic_error_t *ret;
453 if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
454 && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
456 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
459 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
464 /* Private interface */
466 int _xcb_in_init(_xcb_in *in)
468 if(pthread_cond_init(&in->event_cond, 0))
474 in->request_read = 0;
475 in->request_completed = 0;
477 in->replies = _xcb_map_new();
481 in->current_reply_tail = &in->current_reply;
482 in->events_tail = &in->events;
483 in->pending_replies_tail = &in->pending_replies;
488 void _xcb_in_destroy(_xcb_in *in)
490 pthread_cond_destroy(&in->event_cond);
491 free_reply_list(in->current_reply);
492 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
495 struct event_list *e = in->events;
496 in->events = e->next;
500 while(in->pending_replies)
502 pending_reply *pend = in->pending_replies;
503 in->pending_replies = pend->next;
508 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
510 pending_reply *pend = malloc(sizeof(pending_reply));
511 assert(workaround != WORKAROUND_NONE || flags != 0);
514 _xcb_conn_shutdown(c);
517 pend->first_request = pend->last_request = request;
518 pend->workaround = workaround;
521 *c->in.pending_replies_tail = pend;
522 c->in.pending_replies_tail = &pend->next;
526 int _xcb_in_read(xcb_connection_t *c)
528 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
530 c->in.queue_len += n;
531 while(read_packet(c))
533 if((n > 0) || (n < 0 && errno == EAGAIN))
535 _xcb_conn_shutdown(c);
539 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
541 int done = c->in.queue_len;
545 memcpy(buf, c->in.queue, done);
546 c->in.queue_len -= done;
547 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
551 int ret = read_block(c->fd, (char *) buf + done, len - done);
554 _xcb_conn_shutdown(c);