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. */
39 typedef struct pending_reply {
41 enum workarounds workaround;
42 struct pending_reply *next;
45 typedef struct XCBReplyData {
48 XCBGenericError **error;
51 static int match_request_error(const void *request, const void *data)
53 const XCBGenericError *e = data;
54 return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
57 static int match_reply(const void *request, const void *data)
59 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
62 static void wake_up_next_reader(XCBConnection *c)
64 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
67 pthreadret = pthread_cond_signal(cur->data);
69 pthreadret = pthread_cond_signal(&c->in.event_cond);
70 assert(pthreadret == 0);
73 static int read_packet(XCBConnection *c)
79 /* Wait for there to be enough data for us to read a whole packet */
80 if(c->in.queue_len < length)
83 /* Get the response type, length, and sequence number. */
84 memcpy(&genrep, c->in.queue, sizeof(genrep));
86 /* Compute 32-bit sequence number of this packet. */
87 if((genrep.response_type & 0x7f) != KeymapNotify)
89 int lastread = c->in.request_read;
90 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
91 if(c->in.request_read != lastread && !_xcb_queue_is_empty(c->in.current_reply))
93 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
94 c->in.current_reply = _xcb_queue_new();
96 if(c->in.request_read < lastread)
97 c->in.request_read += 0x10000;
100 /* For reply packets, check that the entire packet is available. */
101 if(genrep.response_type == 1)
103 pending_reply *pend = c->in.pending_replies;
104 if(pend && pend->request == c->in.request_read)
106 switch(pend->workaround)
108 case WORKAROUND_NONE:
110 case WORKAROUND_GLX_GET_FB_CONFIGS_BUG:
112 CARD32 *p = (CARD32 *) c->in.queue;
113 genrep.length = p[2] * p[3] * 2;
117 c->in.pending_replies = pend->next;
119 c->in.pending_replies_tail = &c->in.pending_replies;
122 length += genrep.length * 4;
125 buf = malloc(length);
128 if(_xcb_in_read_block(c, buf, length) <= 0)
134 if(buf[0] == 1) /* response is a reply */
136 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
137 _xcb_queue_enqueue(c->in.current_reply, buf);
139 pthread_cond_signal(reader->data);
143 if(buf[0] == 0) /* response is an error */
145 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
146 if(reader && reader->error)
148 *reader->error = (XCBGenericError *) buf;
149 pthread_cond_signal(reader->data);
154 /* event, or error without a waiting reader */
155 _xcb_queue_enqueue(c->in.events, buf);
156 pthread_cond_signal(&c->in.event_cond);
157 return 1; /* I have something for you... */
160 static int read_block(const int fd, void *buf, const size_t len)
165 int ret = read(fd, ((char *) buf) + done, len - done);
168 if(ret < 0 && errno == EAGAIN)
173 ret = select(fd + 1, &fds, 0, 0, 0);
181 /* Public interface */
183 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
185 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
191 pthread_mutex_lock(&c->iolock);
193 /* If this request has not been written yet, write it. */
194 if((signed int) (c->out.request_written - request) < 0)
195 if(!_xcb_out_flush(c))
196 goto done; /* error */
198 if(_xcb_list_find(c->in.readers, match_reply, &request))
199 goto done; /* error */
203 *e = _xcb_list_remove(c->in.events, match_request_error, &request);
208 reader.request = request;
211 _xcb_list_append(c->in.readers, &reader);
213 /* If this request has not been read yet, wait for it. */
214 while(!(e && *e) && ((signed int) (c->in.request_read - request) < 0 ||
215 (c->in.request_read == request &&
216 _xcb_queue_is_empty(c->in.current_reply))))
217 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
220 if(c->in.request_read != request)
222 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
225 ret = _xcb_queue_dequeue(q);
226 if(_xcb_queue_is_empty(q))
227 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
231 ret = _xcb_queue_dequeue(c->in.current_reply);
234 _xcb_list_remove(c->in.readers, match_reply, &request);
235 pthread_cond_destroy(&cond);
237 wake_up_next_reader(c);
238 pthread_mutex_unlock(&c->iolock);
242 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
244 return XCBWaitForEvent(c);
247 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
249 XCBGenericEvent *ret;
250 pthread_mutex_lock(&c->iolock);
251 /* _xcb_list_remove_head returns 0 on empty list. */
252 while(!(ret = _xcb_queue_dequeue(c->in.events)))
253 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
256 wake_up_next_reader(c);
257 pthread_mutex_unlock(&c->iolock);
261 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
263 XCBGenericEvent *ret = 0;
264 pthread_mutex_lock(&c->iolock);
267 /* FIXME: follow X meets Z architecture changes. */
269 ret = _xcb_queue_dequeue(c->in.events);
274 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
277 pthread_mutex_unlock(&c->iolock);
281 unsigned int XCBGetRequestRead(XCBConnection *c)
284 pthread_mutex_lock(&c->iolock);
285 /* FIXME: follow X meets Z architecture changes. */
287 ret = c->in.request_read;
288 pthread_mutex_unlock(&c->iolock);
292 /* Private interface */
294 int _xcb_in_init(_xcb_in *in)
296 if(pthread_cond_init(&in->event_cond, 0))
302 in->request_read = 0;
303 in->current_reply = _xcb_queue_new();
305 in->replies = _xcb_map_new();
306 in->events = _xcb_queue_new();
307 in->readers = _xcb_list_new();
308 if(!in->current_reply || !in->replies || !in->events || !in->readers)
311 in->pending_replies_tail = &in->pending_replies;
316 void _xcb_in_destroy(_xcb_in *in)
318 pthread_cond_destroy(&in->event_cond);
319 _xcb_queue_delete(in->current_reply, free);
320 _xcb_map_delete(in->replies, free);
321 _xcb_queue_delete(in->events, free);
322 _xcb_list_delete(in->readers, 0);
323 while(in->pending_replies)
325 pending_reply *pend = in->pending_replies;
326 in->pending_replies = pend->next;
331 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround)
333 if(workaround != WORKAROUND_NONE)
335 pending_reply *pend = malloc(sizeof(pending_reply));
338 pend->request = request;
339 pend->workaround = workaround;
341 *c->in.pending_replies_tail = pend;
342 c->in.pending_replies_tail = &pend->next;
347 int _xcb_in_read(XCBConnection *c)
349 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
351 c->in.queue_len += n;
352 while(read_packet(c))
354 return (n > 0) || (n < 0 && errno == EAGAIN);
357 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
359 int done = c->in.queue_len;
363 memcpy(buf, c->in.queue, done);
364 c->in.queue_len -= done;
365 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
369 int ret = read_block(c->fd, (char *) buf + done, len - done);