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 XCBReplyData {
61 static int match_reply(const void *request, const void *data)
63 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
66 static void wake_up_next_reader(XCBConnection *c)
68 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
71 pthreadret = pthread_cond_signal(cur->data);
73 pthreadret = pthread_cond_signal(&c->in.event_cond);
74 assert(pthreadret == 0);
77 static int read_packet(XCBConnection *c)
82 pending_reply *pend = 0;
83 struct event_list *event;
85 /* Wait for there to be enough data for us to read a whole packet */
86 if(c->in.queue_len < length)
89 /* Get the response type, length, and sequence number. */
90 memcpy(&genrep, c->in.queue, sizeof(genrep));
92 /* Compute 32-bit sequence number of this packet. */
93 if((genrep.response_type & 0x7f) != KeymapNotify)
95 int lastread = c->in.request_read;
96 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
97 if(c->in.request_read != lastread)
99 pending_reply *oldpend = c->in.pending_replies;
100 if(oldpend && oldpend->request == lastread)
102 c->in.pending_replies = oldpend->next;
104 c->in.pending_replies_tail = &c->in.pending_replies;
107 if(c->in.current_reply)
109 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
110 c->in.current_reply = 0;
111 c->in.current_reply_tail = &c->in.current_reply;
114 if(c->in.request_read < lastread)
115 c->in.request_read += 0x10000;
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)))
148 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
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;
157 pthread_cond_signal(reader->data);
161 /* event, or unchecked error */
162 event = malloc(sizeof(struct event_list));
170 *c->in.events_tail = event;
171 c->in.events_tail = &event->next;
172 pthread_cond_signal(&c->in.event_cond);
173 return 1; /* I have something for you... */
176 static XCBGenericEvent *get_event(XCBConnection *c)
178 struct event_list *cur = c->in.events;
179 XCBGenericEvent *ret;
183 c->in.events = cur->next;
185 c->in.events_tail = &c->in.events;
190 static void free_reply_list(struct reply_list *head)
194 struct reply_list *cur = head;
201 static int read_block(const int fd, void *buf, const size_t len)
206 int ret = read(fd, ((char *) buf) + done, len - done);
209 if(ret < 0 && errno == EAGAIN)
214 ret = select(fd + 1, &fds, 0, 0, 0);
222 /* Public interface */
224 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
226 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
228 struct reply_list *head;
233 pthread_mutex_lock(&c->iolock);
235 /* If this request has not been written yet, write it. */
236 if((signed int) (c->out.request_written - request) < 0)
237 if(!_xcb_out_flush(c))
238 goto done; /* error */
240 if(_xcb_list_find(c->in.readers, match_reply, &request))
241 goto done; /* error */
243 reader.request = request;
245 _xcb_list_append(c->in.readers, &reader);
247 /* If this request has not been read yet, wait for it. */
248 while(((signed int) (c->in.request_read - request) < 0 ||
249 (c->in.request_read == request && !c->in.current_reply)))
250 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
253 if(c->in.request_read != request)
255 head = _xcb_map_remove(c->in.replies, request);
256 if(head && head->next)
257 _xcb_map_put(c->in.replies, request, head->next);
261 head = c->in.current_reply;
264 c->in.current_reply = head->next;
266 c->in.current_reply_tail = &c->in.current_reply;
275 if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
286 _xcb_list_remove(c->in.readers, match_reply, &request);
287 pthread_cond_destroy(&cond);
289 wake_up_next_reader(c);
290 pthread_mutex_unlock(&c->iolock);
294 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
296 return XCBWaitForEvent(c);
299 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
301 XCBGenericEvent *ret;
302 pthread_mutex_lock(&c->iolock);
303 /* get_event returns 0 on empty list. */
304 while(!(ret = get_event(c)))
305 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
308 wake_up_next_reader(c);
309 pthread_mutex_unlock(&c->iolock);
313 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
315 XCBGenericEvent *ret = 0;
316 pthread_mutex_lock(&c->iolock);
319 /* FIXME: follow X meets Z architecture changes. */
326 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
329 pthread_mutex_unlock(&c->iolock);
333 unsigned int XCBGetRequestRead(XCBConnection *c)
336 pthread_mutex_lock(&c->iolock);
337 /* FIXME: follow X meets Z architecture changes. */
339 ret = c->in.request_read;
340 pthread_mutex_unlock(&c->iolock);
344 /* Private interface */
346 int _xcb_in_init(_xcb_in *in)
348 if(pthread_cond_init(&in->event_cond, 0))
354 in->request_read = 0;
356 in->replies = _xcb_map_new();
357 in->readers = _xcb_list_new();
358 if(!in->replies || !in->readers)
361 in->current_reply_tail = &in->current_reply;
362 in->events_tail = &in->events;
363 in->pending_replies_tail = &in->pending_replies;
368 void _xcb_in_destroy(_xcb_in *in)
370 pthread_cond_destroy(&in->event_cond);
371 free_reply_list(in->current_reply);
372 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
373 _xcb_list_delete(in->readers, 0);
376 struct event_list *e = in->events;
377 in->events = e->next;
381 while(in->pending_replies)
383 pending_reply *pend = in->pending_replies;
384 in->pending_replies = pend->next;
389 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
391 if(workaround != WORKAROUND_NONE || flags != 0)
393 pending_reply *pend = malloc(sizeof(pending_reply));
396 pend->request = request;
397 pend->workaround = workaround;
400 *c->in.pending_replies_tail = pend;
401 c->in.pending_replies_tail = &pend->next;
406 int _xcb_in_read(XCBConnection *c)
408 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
410 c->in.queue_len += n;
411 while(read_packet(c))
413 return (n > 0) || (n < 0 && errno == EAGAIN);
416 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
418 int done = c->in.queue_len;
422 memcpy(buf, c->in.queue, done);
423 c->in.queue_len -= done;
424 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
428 int ret = read_block(c->fd, (char *) buf + done, len - done);