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)
94 while(c->in.pending_replies && c->in.pending_replies->request < c->in.request_read)
96 pending_reply *oldpend = c->in.pending_replies;
97 c->in.pending_replies = oldpend->next;
99 c->in.pending_replies_tail = &c->in.pending_replies;
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;
109 if(c->in.request_read < lastread)
110 c->in.request_read += 0x10000;
113 if(genrep.response_type == 0 || genrep.response_type == 1)
115 pend = c->in.pending_replies;
116 if(pend && pend->request != c->in.request_read)
120 /* For reply packets, check that the entire packet is available. */
121 if(genrep.response_type == 1)
123 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
125 CARD32 *p = (CARD32 *) c->in.queue;
126 genrep.length = p[2] * p[3] * 2;
128 length += genrep.length * 4;
131 buf = malloc(length);
134 if(_xcb_in_read_block(c, buf, length) <= 0)
140 /* reply, or checked error */
141 if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
144 struct reply_list *cur = malloc(sizeof(struct reply_list));
149 *c->in.current_reply_tail = cur;
150 c->in.current_reply_tail = &cur->next;
151 for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
152 if(reader->request == c->in.request_read)
154 pthread_cond_signal(reader->data);
160 /* event, or unchecked error */
161 event = malloc(sizeof(struct event_list));
169 *c->in.events_tail = event;
170 c->in.events_tail = &event->next;
171 pthread_cond_signal(&c->in.event_cond);
172 return 1; /* I have something for you... */
175 static XCBGenericEvent *get_event(XCBConnection *c)
177 struct event_list *cur = c->in.events;
178 XCBGenericEvent *ret;
182 c->in.events = cur->next;
184 c->in.events_tail = &c->in.events;
189 static void free_reply_list(struct reply_list *head)
193 struct reply_list *cur = head;
200 static int read_block(const int fd, void *buf, const size_t len)
205 int ret = read(fd, ((char *) buf) + done, len - done);
208 if(ret < 0 && errno == EAGAIN)
213 ret = select(fd + 1, &fds, 0, 0, 0);
221 /* Public interface */
223 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
225 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
227 reader_list **prev_reader;
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 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
241 if((*prev_reader)->request == request)
242 goto done; /* error */
244 reader.request = request;
246 reader.next = *prev_reader;
247 *prev_reader = &reader;
249 /* If this request has not been read yet, wait for it. */
250 while(((signed int) (c->in.request_read - request) < 0 ||
251 (c->in.request_read == request && !c->in.current_reply)))
252 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
255 if(c->in.request_read != request)
257 head = _xcb_map_remove(c->in.replies, request);
258 if(head && head->next)
259 _xcb_map_put(c->in.replies, request, head->next);
263 head = c->in.current_reply;
266 c->in.current_reply = head->next;
268 c->in.current_reply_tail = &c->in.current_reply;
277 if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
288 for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
289 if(*prev_reader == &reader)
291 *prev_reader = (*prev_reader)->next;
294 pthread_cond_destroy(&cond);
296 wake_up_next_reader(c);
297 pthread_mutex_unlock(&c->iolock);
301 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
303 return XCBWaitForEvent(c);
306 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
308 XCBGenericEvent *ret;
309 pthread_mutex_lock(&c->iolock);
310 /* get_event returns 0 on empty list. */
311 while(!(ret = get_event(c)))
312 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
315 wake_up_next_reader(c);
316 pthread_mutex_unlock(&c->iolock);
320 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
322 XCBGenericEvent *ret = 0;
323 pthread_mutex_lock(&c->iolock);
326 /* FIXME: follow X meets Z architecture changes. */
333 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
336 pthread_mutex_unlock(&c->iolock);
340 unsigned int XCBGetRequestRead(XCBConnection *c)
343 pthread_mutex_lock(&c->iolock);
344 /* FIXME: follow X meets Z architecture changes. */
346 ret = c->in.request_read;
347 pthread_mutex_unlock(&c->iolock);
351 /* Private interface */
353 int _xcb_in_init(_xcb_in *in)
355 if(pthread_cond_init(&in->event_cond, 0))
361 in->request_read = 0;
363 in->replies = _xcb_map_new();
367 in->current_reply_tail = &in->current_reply;
368 in->events_tail = &in->events;
369 in->pending_replies_tail = &in->pending_replies;
374 void _xcb_in_destroy(_xcb_in *in)
376 pthread_cond_destroy(&in->event_cond);
377 free_reply_list(in->current_reply);
378 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
381 struct event_list *e = in->events;
382 in->events = e->next;
386 while(in->pending_replies)
388 pending_reply *pend = in->pending_replies;
389 in->pending_replies = pend->next;
394 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
396 if(workaround != WORKAROUND_NONE || flags != 0)
398 pending_reply *pend = malloc(sizeof(pending_reply));
401 pend->request = request;
402 pend->workaround = workaround;
405 *c->in.pending_replies_tail = pend;
406 c->in.pending_replies_tail = &pend->next;
411 int _xcb_in_read(XCBConnection *c)
413 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
415 c->in.queue_len += n;
416 while(read_packet(c))
418 return (n > 0) || (n < 0 && errno == EAGAIN);
421 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
423 int done = c->in.queue_len;
427 memcpy(buf, c->in.queue, done);
428 c->in.queue_len -= done;
429 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
433 int ret = read_block(c->fd, (char *) buf + done, len - done);