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. */
43 XCBGenericEvent *event;
44 struct event_list *next;
49 struct reply_list *next;
52 typedef struct pending_reply {
54 enum workarounds workaround;
56 struct pending_reply *next;
59 typedef struct reader_list {
62 struct reader_list *next;
65 static void wake_up_next_reader(XCBConnection *c)
69 pthreadret = pthread_cond_signal(c->in.readers->data);
71 pthreadret = pthread_cond_signal(&c->in.event_cond);
72 assert(pthreadret == 0);
75 static int read_packet(XCBConnection *c)
80 pending_reply *pend = 0;
81 struct event_list *event;
83 /* Wait for there to be enough data for us to read a whole packet */
84 if(c->in.queue_len < length)
87 /* Get the response type, length, and sequence number. */
88 memcpy(&genrep, c->in.queue, sizeof(genrep));
90 /* Compute 32-bit sequence number of this packet. */
91 if((genrep.response_type & 0x7f) != XCBKeymapNotify)
93 int lastread = c->in.request_read;
94 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
95 if(c->in.request_read < lastread)
96 c->in.request_read += 0x10000;
97 if(c->in.request_read > c->in.request_expected)
98 c->in.request_expected = c->in.request_read;
100 if(c->in.request_read != lastread)
102 if(c->in.current_reply)
104 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
105 c->in.current_reply = 0;
106 c->in.current_reply_tail = &c->in.current_reply;
108 c->in.request_completed = c->in.request_read - 1;
110 if(genrep.response_type != XCBReply) /* error or event */
111 c->in.request_completed = c->in.request_read; /* XXX: does event/error imply no more replies? */
113 while(c->in.pending_replies && c->in.pending_replies->request <= c->in.request_completed)
115 pending_reply *oldpend = c->in.pending_replies;
116 c->in.pending_replies = oldpend->next;
118 c->in.pending_replies_tail = &c->in.pending_replies;
123 if(genrep.response_type == XCBError || genrep.response_type == XCBReply)
125 pend = c->in.pending_replies;
126 if(pend && pend->request != c->in.request_read)
130 /* For reply packets, check that the entire packet is available. */
131 if(genrep.response_type == XCBReply)
133 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
135 CARD32 *p = (CARD32 *) c->in.queue;
136 genrep.length = p[2] * p[3] * 2;
138 length += genrep.length * 4;
141 buf = malloc(length + (genrep.response_type == XCBReply ? 0 : sizeof(CARD32)));
144 if(_xcb_in_read_block(c, buf, length) <= 0)
149 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
155 if(genrep.response_type != XCBReply)
156 ((XCBGenericEvent *) buf)->full_sequence = c->in.request_read;
158 /* reply, or checked error */
159 if( genrep.response_type == XCBReply ||
160 (genrep.response_type == XCBError && pend && (pend->flags & XCB_REQUEST_CHECKED)))
163 struct reply_list *cur = malloc(sizeof(struct reply_list));
168 *c->in.current_reply_tail = cur;
169 c->in.current_reply_tail = &cur->next;
170 for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
171 if(reader->request == c->in.request_read)
173 pthread_cond_signal(reader->data);
179 /* event, or unchecked error */
180 event = malloc(sizeof(struct event_list));
188 *c->in.events_tail = event;
189 c->in.events_tail = &event->next;
190 pthread_cond_signal(&c->in.event_cond);
191 return 1; /* I have something for you... */
194 static XCBGenericEvent *get_event(XCBConnection *c)
196 struct event_list *cur = c->in.events;
197 XCBGenericEvent *ret;
201 c->in.events = cur->next;
203 c->in.events_tail = &c->in.events;
208 static void free_reply_list(struct reply_list *head)
212 struct reply_list *cur = head;
219 static int read_block(const int fd, void *buf, const size_t len)
224 int ret = read(fd, ((char *) buf) + done, len - done);
227 if(ret < 0 && errno == EAGAIN)
233 ret = select(fd + 1, &fds, 0, 0, 0);
234 } while (ret == -1 && errno == EINTR);
242 static int poll_for_reply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
244 struct reply_list *head;
248 else if(c->in.request_completed >= request)
250 head = _xcb_map_remove(c->in.replies, request);
251 if(head && head->next)
252 _xcb_map_put(c->in.replies, request, head->next);
254 else if(c->in.request_read == request && c->in.current_reply)
256 head = c->in.current_reply;
257 c->in.current_reply = head->next;
259 c->in.current_reply_tail = &c->in.current_reply;
262 /* Would block: do nothing. */
271 if(((XCBGenericRep *) head->reply)->response_type == XCBError)
274 *error = head->reply;
279 *reply = head->reply;
287 /* Public interface */
289 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
291 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
293 reader_list **prev_reader;
294 struct reply_list *head;
299 /* If an error occurred when issuing the request, fail immediately. */
303 pthread_mutex_lock(&c->iolock);
305 /* If this request has not been written yet, write it. */
306 if(!_xcb_out_flush_to(c, request))
307 goto done; /* error */
309 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
310 if((*prev_reader)->request == request)
311 goto done; /* error */
313 reader.request = request;
315 reader.next = *prev_reader;
316 *prev_reader = &reader;
318 /* If this request has not completed yet and has no reply waiting,
320 while(c->in.request_completed < request &&
321 !(c->in.request_read == request && c->in.current_reply))
322 if(!_xcb_conn_wait(c, &cond, 0, 0))
325 if(c->in.request_read != request)
327 head = _xcb_map_remove(c->in.replies, request);
328 if(head && head->next)
329 _xcb_map_put(c->in.replies, request, head->next);
333 head = c->in.current_reply;
336 c->in.current_reply = head->next;
338 c->in.current_reply_tail = &c->in.current_reply;
347 if(((XCBGenericRep *) ret)->response_type == XCBError)
358 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
359 if(*prev_reader == &reader)
361 *prev_reader = (*prev_reader)->next;
364 pthread_cond_destroy(&cond);
366 wake_up_next_reader(c);
367 pthread_mutex_unlock(&c->iolock);
371 int XCBPollForReply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
375 pthread_mutex_lock(&c->iolock);
376 ret = poll_for_reply(c, request, reply, error);
377 pthread_mutex_unlock(&c->iolock);
381 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
383 return XCBWaitForEvent(c);
386 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
388 XCBGenericEvent *ret;
389 pthread_mutex_lock(&c->iolock);
390 /* get_event returns 0 on empty list. */
391 while(!(ret = get_event(c)))
392 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
395 wake_up_next_reader(c);
396 pthread_mutex_unlock(&c->iolock);
400 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
402 XCBGenericEvent *ret = 0;
403 pthread_mutex_lock(&c->iolock);
406 /* FIXME: follow X meets Z architecture changes. */
413 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
416 pthread_mutex_unlock(&c->iolock);
420 unsigned int XCBGetRequestRead(XCBConnection *c)
423 pthread_mutex_lock(&c->iolock);
424 /* FIXME: follow X meets Z architecture changes. */
426 ret = c->in.request_read;
427 pthread_mutex_unlock(&c->iolock);
431 /* Private interface */
433 int _xcb_in_init(_xcb_in *in)
435 if(pthread_cond_init(&in->event_cond, 0))
441 in->request_read = 0;
442 in->request_completed = 0;
444 in->replies = _xcb_map_new();
448 in->current_reply_tail = &in->current_reply;
449 in->events_tail = &in->events;
450 in->pending_replies_tail = &in->pending_replies;
455 void _xcb_in_destroy(_xcb_in *in)
457 pthread_cond_destroy(&in->event_cond);
458 free_reply_list(in->current_reply);
459 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
462 struct event_list *e = in->events;
463 in->events = e->next;
467 while(in->pending_replies)
469 pending_reply *pend = in->pending_replies;
470 in->pending_replies = pend->next;
475 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
477 if(workaround != WORKAROUND_NONE || flags != 0)
479 pending_reply *pend = malloc(sizeof(pending_reply));
482 pend->request = request;
483 pend->workaround = workaround;
486 *c->in.pending_replies_tail = pend;
487 c->in.pending_replies_tail = &pend->next;
492 int _xcb_in_read(XCBConnection *c)
494 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
496 c->in.queue_len += n;
497 while(read_packet(c))
499 return (n > 0) || (n < 0 && errno == EAGAIN);
502 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
504 int done = c->in.queue_len;
508 memcpy(buf, c->in.queue, done);
509 c->in.queue_len -= done;
510 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
514 int ret = read_block(c->fd, (char *) buf + done, len - done);