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);
139 if(_xcb_in_read_block(c, buf, length) <= 0)
145 /* reply, or checked error */
146 if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
149 struct reply_list *cur = malloc(sizeof(struct reply_list));
154 *c->in.current_reply_tail = cur;
155 c->in.current_reply_tail = &cur->next;
156 for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
157 if(reader->request == c->in.request_read)
159 pthread_cond_signal(reader->data);
165 /* event, or unchecked error */
166 event = malloc(sizeof(struct event_list));
174 *c->in.events_tail = event;
175 c->in.events_tail = &event->next;
176 pthread_cond_signal(&c->in.event_cond);
177 return 1; /* I have something for you... */
180 static XCBGenericEvent *get_event(XCBConnection *c)
182 struct event_list *cur = c->in.events;
183 XCBGenericEvent *ret;
187 c->in.events = cur->next;
189 c->in.events_tail = &c->in.events;
194 static void free_reply_list(struct reply_list *head)
198 struct reply_list *cur = head;
205 static int read_block(const int fd, void *buf, const size_t len)
210 int ret = read(fd, ((char *) buf) + done, len - done);
213 if(ret < 0 && errno == EAGAIN)
218 ret = select(fd + 1, &fds, 0, 0, 0);
226 /* Public interface */
228 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
230 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
232 reader_list **prev_reader;
233 struct reply_list *head;
238 pthread_mutex_lock(&c->iolock);
240 /* If this request has not been written yet, write it. */
241 if((signed int) (c->out.request_written - request) < 0)
242 if(!_xcb_out_flush(c))
243 goto done; /* error */
245 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
246 if((*prev_reader)->request == request)
247 goto done; /* error */
249 reader.request = request;
251 reader.next = *prev_reader;
252 *prev_reader = &reader;
254 /* If this request has not completed yet and has no reply waiting,
256 while(c->in.request_completed < request &&
257 !(c->in.request_read == request && c->in.current_reply))
258 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
261 if(c->in.request_read != request)
263 head = _xcb_map_remove(c->in.replies, request);
264 if(head && head->next)
265 _xcb_map_put(c->in.replies, request, head->next);
269 head = c->in.current_reply;
272 c->in.current_reply = head->next;
274 c->in.current_reply_tail = &c->in.current_reply;
283 if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
294 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
295 if(*prev_reader == &reader)
297 *prev_reader = (*prev_reader)->next;
300 pthread_cond_destroy(&cond);
302 wake_up_next_reader(c);
303 pthread_mutex_unlock(&c->iolock);
307 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
309 return XCBWaitForEvent(c);
312 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
314 XCBGenericEvent *ret;
315 pthread_mutex_lock(&c->iolock);
316 /* get_event returns 0 on empty list. */
317 while(!(ret = get_event(c)))
318 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
321 wake_up_next_reader(c);
322 pthread_mutex_unlock(&c->iolock);
326 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
328 XCBGenericEvent *ret = 0;
329 pthread_mutex_lock(&c->iolock);
332 /* FIXME: follow X meets Z architecture changes. */
339 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
342 pthread_mutex_unlock(&c->iolock);
346 unsigned int XCBGetRequestRead(XCBConnection *c)
349 pthread_mutex_lock(&c->iolock);
350 /* FIXME: follow X meets Z architecture changes. */
352 ret = c->in.request_read;
353 pthread_mutex_unlock(&c->iolock);
357 /* Private interface */
359 int _xcb_in_init(_xcb_in *in)
361 if(pthread_cond_init(&in->event_cond, 0))
367 in->request_read = 0;
368 in->request_completed = 0;
370 in->replies = _xcb_map_new();
374 in->current_reply_tail = &in->current_reply;
375 in->events_tail = &in->events;
376 in->pending_replies_tail = &in->pending_replies;
381 void _xcb_in_destroy(_xcb_in *in)
383 pthread_cond_destroy(&in->event_cond);
384 free_reply_list(in->current_reply);
385 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
388 struct event_list *e = in->events;
389 in->events = e->next;
393 while(in->pending_replies)
395 pending_reply *pend = in->pending_replies;
396 in->pending_replies = pend->next;
401 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
403 if(workaround != WORKAROUND_NONE || flags != 0)
405 pending_reply *pend = malloc(sizeof(pending_reply));
408 pend->request = request;
409 pend->workaround = workaround;
412 *c->in.pending_replies_tail = pend;
413 c->in.pending_replies_tail = &pend->next;
418 int _xcb_in_read(XCBConnection *c)
420 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
422 c->in.queue_len += n;
423 while(read_packet(c))
425 return (n > 0) || (n < 0 && errno == EAGAIN);
428 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
430 int done = c->in.queue_len;
434 memcpy(buf, c->in.queue, done);
435 c->in.queue_len -= done;
436 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
440 int ret = read_block(c->fd, (char *) buf + done, len - done);