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 {
56 enum workarounds workaround;
58 struct pending_reply *next;
61 typedef struct reader_list {
64 struct reader_list *next;
67 static void wake_up_next_reader(xcb_connection_t *c)
71 pthreadret = pthread_cond_signal(c->in.readers->data);
73 pthreadret = pthread_cond_signal(&c->in.event_cond);
74 assert(pthreadret == 0);
77 static int read_packet(xcb_connection_t *c)
79 xcb_generic_reply_t genrep;
81 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
83 pending_reply *pend = 0;
84 struct event_list *event;
86 /* Wait for there to be enough data for us to read a whole packet */
87 if(c->in.queue_len < length)
90 /* Get the response type, length, and sequence number. */
91 memcpy(&genrep, c->in.queue, sizeof(genrep));
93 /* Compute 32-bit sequence number of this packet. */
94 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
96 unsigned int lastread = c->in.request_read;
97 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
98 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
99 c->in.request_read += 0x10000;
100 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
101 c->in.request_expected = c->in.request_read;
103 if(c->in.request_read != lastread)
105 if(c->in.current_reply)
107 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
108 c->in.current_reply = 0;
109 c->in.current_reply_tail = &c->in.current_reply;
111 c->in.request_completed = c->in.request_read - 1;
114 while(c->in.pending_replies &&
115 XCB_SEQUENCE_COMPARE (c->in.pending_replies->request, <=, c->in.request_completed))
117 pending_reply *oldpend = c->in.pending_replies;
118 c->in.pending_replies = oldpend->next;
120 c->in.pending_replies_tail = &c->in.pending_replies;
124 if(genrep.response_type == XCB_ERROR)
125 c->in.request_completed = c->in.request_read;
128 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
130 pend = c->in.pending_replies;
131 if(pend && pend->request != c->in.request_read)
135 /* For reply packets, check that the entire packet is available. */
136 if(genrep.response_type == XCB_REPLY)
138 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
140 uint32_t *p = (uint32_t *) c->in.queue;
141 genrep.length = p[2] * p[3] * 2;
143 length += genrep.length * 4;
146 /* XGE events may have sizes > 32 */
147 if (genrep.response_type == XCB_XGE_EVENT)
149 eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
152 buf = malloc(length + eventlength +
153 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
156 _xcb_conn_shutdown(c);
160 if(_xcb_in_read_block(c, buf, length) <= 0)
166 /* pull in XGE event data if available, append after event struct */
169 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
176 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
182 if(genrep.response_type != XCB_REPLY)
183 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
185 /* reply, or checked error */
186 if( genrep.response_type == XCB_REPLY ||
187 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
190 struct reply_list *cur = malloc(sizeof(struct reply_list));
193 _xcb_conn_shutdown(c);
198 *c->in.current_reply_tail = cur;
199 c->in.current_reply_tail = &cur->next;
200 for(reader = c->in.readers;
202 XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read);
203 reader = reader->next)
205 if(reader->request == c->in.request_read)
207 pthread_cond_signal(reader->data);
214 /* event, or unchecked error */
215 event = malloc(sizeof(struct event_list));
218 _xcb_conn_shutdown(c);
224 *c->in.events_tail = event;
225 c->in.events_tail = &event->next;
226 pthread_cond_signal(&c->in.event_cond);
227 return 1; /* I have something for you... */
230 static xcb_generic_event_t *get_event(xcb_connection_t *c)
232 struct event_list *cur = c->in.events;
233 xcb_generic_event_t *ret;
237 c->in.events = cur->next;
239 c->in.events_tail = &c->in.events;
244 static void free_reply_list(struct reply_list *head)
248 struct reply_list *cur = head;
255 static int read_block(const int fd, void *buf, const ssize_t len)
260 int ret = read(fd, ((char *) buf) + done, len - done);
263 if(ret < 0 && errno == EAGAIN)
269 ret = select(fd + 1, &fds, 0, 0, 0);
270 } while (ret == -1 && errno == EINTR);
278 static int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
280 struct reply_list *head;
282 /* If an error occurred when issuing the request, fail immediately. */
285 /* We've read requests past the one we want, so if it has replies we have
286 * them all and they're in the replies map. */
287 else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
289 head = _xcb_map_remove(c->in.replies, request);
290 if(head && head->next)
291 _xcb_map_put(c->in.replies, request, head->next);
293 /* We're currently processing the responses to the request we want, and we
294 * have a reply ready to return. So just return it without blocking. */
295 else if(request == c->in.request_read && c->in.current_reply)
297 head = c->in.current_reply;
298 c->in.current_reply = head->next;
300 c->in.current_reply_tail = &c->in.current_reply;
302 /* We know this request can't have any more replies, and we've already
303 * established it doesn't have a reply now. Don't bother blocking. */
304 else if(request == c->in.request_completed)
306 /* We may have more replies on the way for this request: block until we're
317 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
320 *error = head->reply;
325 *reply = head->reply;
333 /* Public interface */
335 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
345 /* If this request has not been written yet, write it. */
346 if(_xcb_out_flush_to(c, request))
348 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
350 reader_list **prev_reader;
352 for(prev_reader = &c->in.readers;
354 XCB_SEQUENCE_COMPARE ((*prev_reader)->request, <=, request);
355 prev_reader = &(*prev_reader)->next)
359 reader.request = request;
361 reader.next = *prev_reader;
362 *prev_reader = &reader;
364 while(!poll_for_reply(c, request, &ret, e))
365 if(!_xcb_conn_wait(c, &cond, 0, 0))
368 for(prev_reader = &c->in.readers;
370 XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
371 prev_reader = &(*prev_reader)->next)
373 if(*prev_reader == &reader)
375 *prev_reader = (*prev_reader)->next;
379 pthread_cond_destroy(&cond);
382 wake_up_next_reader(c);
387 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
395 return 1; /* would not block */
399 ret = poll_for_reply(c, request, reply, error);
404 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
406 xcb_generic_event_t *ret;
410 /* get_event returns 0 on empty list. */
411 while(!(ret = get_event(c)))
412 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
415 wake_up_next_reader(c);
420 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
422 xcb_generic_event_t *ret = 0;
426 /* FIXME: follow X meets Z architecture changes. */
428 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
435 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
437 /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
438 * that would require factoring the locking out of xcb_get_input_focus,
439 * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
440 xcb_generic_error_t *ret;
444 if(XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_expected)
445 && XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_completed))
447 free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
450 reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
455 /* Private interface */
457 int _xcb_in_init(_xcb_in *in)
459 if(pthread_cond_init(&in->event_cond, 0))
465 in->request_read = 0;
466 in->request_completed = 0;
468 in->replies = _xcb_map_new();
472 in->current_reply_tail = &in->current_reply;
473 in->events_tail = &in->events;
474 in->pending_replies_tail = &in->pending_replies;
479 void _xcb_in_destroy(_xcb_in *in)
481 pthread_cond_destroy(&in->event_cond);
482 free_reply_list(in->current_reply);
483 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
486 struct event_list *e = in->events;
487 in->events = e->next;
491 while(in->pending_replies)
493 pending_reply *pend = in->pending_replies;
494 in->pending_replies = pend->next;
499 int _xcb_in_expect_reply(xcb_connection_t *c, unsigned int request, enum workarounds workaround, int flags)
501 pending_reply *pend = malloc(sizeof(pending_reply));
502 assert(workaround != WORKAROUND_NONE || flags != 0);
505 _xcb_conn_shutdown(c);
508 pend->request = request;
509 pend->workaround = workaround;
512 *c->in.pending_replies_tail = pend;
513 c->in.pending_replies_tail = &pend->next;
517 int _xcb_in_read(xcb_connection_t *c)
519 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
521 c->in.queue_len += n;
522 while(read_packet(c))
524 if((n > 0) || (n < 0 && errno == EAGAIN))
526 _xcb_conn_shutdown(c);
530 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
532 int done = c->in.queue_len;
536 memcpy(buf, c->in.queue, done);
537 c->in.queue_len -= done;
538 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
542 int ret = read_block(c->fd, (char *) buf + done, len - done);
545 _xcb_conn_shutdown(c);