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. */
40 XCBGenericEvent *event;
41 struct event_list *next;
46 struct reply_list *next;
49 typedef struct pending_reply {
51 enum workarounds workaround;
53 struct pending_reply *next;
56 typedef struct reader_list {
59 struct reader_list *next;
62 static void wake_up_next_reader(XCBConnection *c)
66 pthreadret = pthread_cond_signal(c->in.readers->data);
68 pthreadret = pthread_cond_signal(&c->in.event_cond);
69 assert(pthreadret == 0);
72 static int read_packet(XCBConnection *c)
77 pending_reply *pend = 0;
78 struct event_list *event;
80 /* Wait for there to be enough data for us to read a whole packet */
81 if(c->in.queue_len < length)
84 /* Get the response type, length, and sequence number. */
85 memcpy(&genrep, c->in.queue, sizeof(genrep));
87 /* Compute 32-bit sequence number of this packet. */
88 if((genrep.response_type & 0x7f) != KeymapNotify)
90 int lastread = c->in.request_read;
91 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
92 if(c->in.request_read < lastread)
93 c->in.request_read += 0x10000;
95 if(c->in.request_read != lastread)
97 if(c->in.current_reply)
99 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
100 c->in.current_reply = 0;
101 c->in.current_reply_tail = &c->in.current_reply;
103 c->in.request_completed = c->in.request_read - 1;
105 if(genrep.response_type != 1) /* not reply: error or event */
106 c->in.request_completed = c->in.request_read; /* XXX: does event/error imply no more replies? */
108 while(c->in.pending_replies && c->in.pending_replies->request <= c->in.request_completed)
110 pending_reply *oldpend = c->in.pending_replies;
111 c->in.pending_replies = oldpend->next;
113 c->in.pending_replies_tail = &c->in.pending_replies;
118 if(genrep.response_type == 0 || genrep.response_type == 1)
120 pend = c->in.pending_replies;
121 if(pend && pend->request != c->in.request_read)
125 /* For reply packets, check that the entire packet is available. */
126 if(genrep.response_type == 1)
128 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
130 CARD32 *p = (CARD32 *) c->in.queue;
131 genrep.length = p[2] * p[3] * 2;
133 length += genrep.length * 4;
136 buf = malloc(length + (genrep.response_type == 1 ? 0 : sizeof(CARD32)));
139 if(_xcb_in_read_block(c, buf, length) <= 0)
145 if(genrep.response_type != 1)
146 ((XCBGenericEvent *) buf)->full_sequence = c->in.request_read;
148 /* reply, or checked error */
149 if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
152 struct reply_list *cur = malloc(sizeof(struct reply_list));
157 *c->in.current_reply_tail = cur;
158 c->in.current_reply_tail = &cur->next;
159 for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
160 if(reader->request == c->in.request_read)
162 pthread_cond_signal(reader->data);
168 /* event, or unchecked error */
169 event = malloc(sizeof(struct event_list));
177 *c->in.events_tail = event;
178 c->in.events_tail = &event->next;
179 pthread_cond_signal(&c->in.event_cond);
180 return 1; /* I have something for you... */
183 static XCBGenericEvent *get_event(XCBConnection *c)
185 struct event_list *cur = c->in.events;
186 XCBGenericEvent *ret;
190 c->in.events = cur->next;
192 c->in.events_tail = &c->in.events;
197 static void free_reply_list(struct reply_list *head)
201 struct reply_list *cur = head;
208 static int read_block(const int fd, void *buf, const size_t len)
213 int ret = read(fd, ((char *) buf) + done, len - done);
216 if(ret < 0 && errno == EAGAIN)
221 ret = select(fd + 1, &fds, 0, 0, 0);
229 /* Public interface */
231 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
233 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
235 reader_list **prev_reader;
236 struct reply_list *head;
241 pthread_mutex_lock(&c->iolock);
243 /* If this request has not been written yet, write it. */
244 if((signed int) (c->out.request_written - request) < 0)
245 if(!_xcb_out_flush(c))
246 goto done; /* error */
248 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
249 if((*prev_reader)->request == request)
250 goto done; /* error */
252 reader.request = request;
254 reader.next = *prev_reader;
255 *prev_reader = &reader;
257 /* If this request has not completed yet and has no reply waiting,
259 while(c->in.request_completed < request &&
260 !(c->in.request_read == request && c->in.current_reply))
261 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
264 if(c->in.request_read != request)
266 head = _xcb_map_remove(c->in.replies, request);
267 if(head && head->next)
268 _xcb_map_put(c->in.replies, request, head->next);
272 head = c->in.current_reply;
275 c->in.current_reply = head->next;
277 c->in.current_reply_tail = &c->in.current_reply;
286 if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
297 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
298 if(*prev_reader == &reader)
300 *prev_reader = (*prev_reader)->next;
303 pthread_cond_destroy(&cond);
305 wake_up_next_reader(c);
306 pthread_mutex_unlock(&c->iolock);
310 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
312 return XCBWaitForEvent(c);
315 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
317 XCBGenericEvent *ret;
318 pthread_mutex_lock(&c->iolock);
319 /* get_event returns 0 on empty list. */
320 while(!(ret = get_event(c)))
321 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
324 wake_up_next_reader(c);
325 pthread_mutex_unlock(&c->iolock);
329 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
331 XCBGenericEvent *ret = 0;
332 pthread_mutex_lock(&c->iolock);
335 /* FIXME: follow X meets Z architecture changes. */
342 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
345 pthread_mutex_unlock(&c->iolock);
349 unsigned int XCBGetRequestRead(XCBConnection *c)
352 pthread_mutex_lock(&c->iolock);
353 /* FIXME: follow X meets Z architecture changes. */
355 ret = c->in.request_read;
356 pthread_mutex_unlock(&c->iolock);
360 /* Private interface */
362 int _xcb_in_init(_xcb_in *in)
364 if(pthread_cond_init(&in->event_cond, 0))
370 in->request_read = 0;
371 in->request_completed = 0;
373 in->replies = _xcb_map_new();
377 in->current_reply_tail = &in->current_reply;
378 in->events_tail = &in->events;
379 in->pending_replies_tail = &in->pending_replies;
384 void _xcb_in_destroy(_xcb_in *in)
386 pthread_cond_destroy(&in->event_cond);
387 free_reply_list(in->current_reply);
388 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
391 struct event_list *e = in->events;
392 in->events = e->next;
396 while(in->pending_replies)
398 pending_reply *pend = in->pending_replies;
399 in->pending_replies = pend->next;
404 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
406 if(workaround != WORKAROUND_NONE || flags != 0)
408 pending_reply *pend = malloc(sizeof(pending_reply));
411 pend->request = request;
412 pend->workaround = workaround;
415 *c->in.pending_replies_tail = pend;
416 c->in.pending_replies_tail = &pend->next;
421 int _xcb_in_read(XCBConnection *c)
423 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
425 c->in.queue_len += n;
426 while(read_packet(c))
428 return (n > 0) || (n < 0 && errno == EAGAIN);
431 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
433 int done = c->in.queue_len;
437 memcpy(buf, c->in.queue, done);
438 c->in.queue_len -= done;
439 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
443 int ret = read_block(c->fd, (char *) buf + done, len - done);