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 enum workarounds workaround;
43 typedef struct XCBReplyData {
46 XCBGenericError **error;
49 static int match_request_error(const void *request, const void *data)
51 const XCBGenericError *e = data;
52 return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
55 static int match_reply(const void *request, const void *data)
57 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
60 static void wake_up_next_reader(XCBConnection *c)
62 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
65 pthreadret = pthread_cond_signal(cur->data);
67 pthreadret = pthread_cond_signal(&c->in.event_cond);
68 assert(pthreadret == 0);
71 static int read_packet(XCBConnection *c)
77 /* Wait for there to be enough data for us to read a whole packet */
78 if(c->in.queue_len < length)
81 /* Get the response type, length, and sequence number. */
82 memcpy(&genrep, c->in.queue, sizeof(genrep));
84 /* Compute 32-bit sequence number of this packet. */
85 if((genrep.response_type & 0x7f) != KeymapNotify)
87 int lastread = c->in.request_read;
88 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
89 if(c->in.request_read != lastread && !_xcb_queue_is_empty(c->in.current_reply))
91 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
92 c->in.current_reply = _xcb_queue_new();
94 if(c->in.request_read < lastread)
95 c->in.request_read += 0x10000;
98 /* For reply packets, check that the entire packet is available. */
99 if(genrep.response_type == 1)
101 pending_reply *pend = _xcb_list_peek_head(c->in.pending_replies);
102 if(pend && pend->request == c->in.request_read)
104 switch(pend->workaround)
106 case WORKAROUND_NONE:
108 case WORKAROUND_GLX_GET_FB_CONFIGS_BUG:
110 CARD32 *p = (CARD32 *) c->in.queue;
111 genrep.length = p[2] * p[3] * 2;
115 free(_xcb_queue_dequeue(c->in.pending_replies));
117 length += genrep.length * 4;
120 buf = malloc(length);
123 if(_xcb_in_read_block(c, buf, length) <= 0)
129 if(buf[0] == 1) /* response is a reply */
131 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
132 _xcb_queue_enqueue(c->in.current_reply, buf);
134 pthread_cond_signal(reader->data);
138 if(buf[0] == 0) /* response is an error */
140 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
141 if(reader && reader->error)
143 *reader->error = (XCBGenericError *) buf;
144 pthread_cond_signal(reader->data);
149 /* event, or error without a waiting reader */
150 _xcb_queue_enqueue(c->in.events, buf);
151 pthread_cond_signal(&c->in.event_cond);
152 return 1; /* I have something for you... */
155 /* Public interface */
157 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
159 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
165 pthread_mutex_lock(&c->iolock);
167 /* If this request has not been written yet, write it. */
168 if((signed int) (c->out.request_written - request) < 0)
169 if(!_xcb_out_flush(c))
170 goto done; /* error */
172 if(_xcb_list_find(c->in.readers, match_reply, &request))
173 goto done; /* error */
177 *e = _xcb_list_remove(c->in.events, match_request_error, &request);
182 reader.request = request;
185 _xcb_list_append(c->in.readers, &reader);
187 /* If this request has not been read yet, wait for it. */
188 while(!(e && *e) && ((signed int) (c->in.request_read - request) < 0 ||
189 (c->in.request_read == request &&
190 _xcb_queue_is_empty(c->in.current_reply))))
191 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
194 if(c->in.request_read != request)
196 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
199 ret = _xcb_queue_dequeue(q);
200 if(_xcb_queue_is_empty(q))
201 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
205 ret = _xcb_queue_dequeue(c->in.current_reply);
208 _xcb_list_remove(c->in.readers, match_reply, &request);
209 pthread_cond_destroy(&cond);
211 wake_up_next_reader(c);
212 pthread_mutex_unlock(&c->iolock);
216 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
218 return XCBWaitForEvent(c);
221 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
223 XCBGenericEvent *ret;
226 fprintf(stderr, "Entering XCBWaitEvent\n");
229 pthread_mutex_lock(&c->iolock);
230 /* _xcb_list_remove_head returns 0 on empty list. */
231 while(!(ret = _xcb_queue_dequeue(c->in.events)))
232 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
235 wake_up_next_reader(c);
236 pthread_mutex_unlock(&c->iolock);
239 fprintf(stderr, "Leaving XCBWaitEvent, event type %d\n", ret ? ret->response_type : -1);
245 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
247 XCBGenericEvent *ret = 0;
248 pthread_mutex_lock(&c->iolock);
251 /* FIXME: follow X meets Z architecture changes. */
253 ret = _xcb_queue_dequeue(c->in.events);
258 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
261 pthread_mutex_unlock(&c->iolock);
265 unsigned int XCBGetRequestRead(XCBConnection *c)
268 pthread_mutex_lock(&c->iolock);
269 /* FIXME: follow X meets Z architecture changes. */
271 ret = c->in.request_read;
272 pthread_mutex_unlock(&c->iolock);
276 /* Private interface */
278 int _xcb_in_init(_xcb_in *in)
280 if(pthread_cond_init(&in->event_cond, 0))
286 in->request_read = 0;
287 in->current_reply = _xcb_queue_new();
289 in->replies = _xcb_map_new();
290 in->events = _xcb_queue_new();
291 in->readers = _xcb_list_new();
293 in->pending_replies = _xcb_queue_new();
294 if(!in->current_reply || !in->replies || !in->events || !in->readers || !in->pending_replies)
300 void _xcb_in_destroy(_xcb_in *in)
302 pthread_cond_destroy(&in->event_cond);
303 _xcb_queue_delete(in->current_reply, free);
304 _xcb_map_delete(in->replies, free);
305 _xcb_queue_delete(in->events, free);
306 _xcb_list_delete(in->readers, 0);
307 _xcb_queue_delete(in->pending_replies, free);
310 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround)
312 if(workaround != WORKAROUND_NONE)
314 pending_reply *pend = malloc(sizeof(pending_reply));
317 pend->request = request;
318 pend->workaround = workaround;
319 if(!_xcb_queue_enqueue(c->in.pending_replies, pend))
328 int _xcb_in_read(XCBConnection *c)
330 int n = _xcb_readn(c->fd, c->in.queue, sizeof(c->in.queue), &c->in.queue_len);
331 while(read_packet(c))
333 return (n > 0) || (n < 0 && errno == EAGAIN);
336 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
338 int done = c->in.queue_len;
342 memcpy(buf, c->in.queue, done);
343 c->in.queue_len -= done;
344 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
348 int ret = _xcb_read_block(c->fd, (char *) buf + done, len - done);