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. */
41 enum workarounds workaround;
44 typedef struct XCBReplyData {
47 XCBGenericError **error;
50 static int match_request_error(const void *request, const void *data)
52 const XCBGenericError *e = data;
53 return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
56 static int match_reply(const void *request, const void *data)
58 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
61 static void wake_up_next_reader(XCBConnection *c)
63 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
66 pthreadret = pthread_cond_signal(cur->data);
68 pthreadret = pthread_cond_signal(&c->in.event_cond);
69 assert(pthreadret == 0);
72 static int read_packet(XCBConnection *c)
78 /* Wait for there to be enough data for us to read a whole packet */
79 if(c->in.queue_len < length)
82 /* Get the response type, length, and sequence number. */
83 memcpy(&genrep, c->in.queue, sizeof(genrep));
85 /* Compute 32-bit sequence number of this packet. */
86 if((genrep.response_type & 0x7f) != KeymapNotify)
88 int lastread = c->in.request_read;
89 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
90 if(c->in.request_read != lastread && !_xcb_queue_is_empty(c->in.current_reply))
92 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
93 c->in.current_reply = _xcb_queue_new();
95 if(c->in.request_read < lastread)
96 c->in.request_read += 0x10000;
99 /* For reply packets, check that the entire packet is available. */
100 if(genrep.response_type == 1)
102 pending_reply *pend = _xcb_list_peek_head(c->in.pending_replies);
103 if(pend && pend->request == c->in.request_read)
105 switch(pend->workaround)
107 case WORKAROUND_NONE:
109 case WORKAROUND_GLX_GET_FB_CONFIGS_BUG:
111 CARD32 *p = (CARD32 *) c->in.queue;
112 genrep.length = p[2] * p[3] * 2;
116 free(_xcb_queue_dequeue(c->in.pending_replies));
118 length += genrep.length * 4;
121 buf = malloc(length);
124 if(_xcb_in_read_block(c, buf, length) <= 0)
130 if(buf[0] == 1) /* response is a reply */
132 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
133 _xcb_queue_enqueue(c->in.current_reply, buf);
135 pthread_cond_signal(reader->data);
139 if(buf[0] == 0) /* response is an error */
141 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
142 if(reader && reader->error)
144 *reader->error = (XCBGenericError *) buf;
145 pthread_cond_signal(reader->data);
150 /* event, or error without a waiting reader */
151 _xcb_queue_enqueue(c->in.events, buf);
152 pthread_cond_signal(&c->in.event_cond);
153 return 1; /* I have something for you... */
156 /* Public interface */
158 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
160 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
166 pthread_mutex_lock(&c->iolock);
168 /* If this request has not been written yet, write it. */
169 if((signed int) (c->out.request_written - request) < 0)
170 if(!_xcb_out_flush(c))
171 goto done; /* error */
173 if(_xcb_list_find(c->in.readers, match_reply, &request))
174 goto done; /* error */
178 *e = _xcb_list_remove(c->in.events, match_request_error, &request);
183 reader.request = request;
186 _xcb_list_append(c->in.readers, &reader);
188 /* If this request has not been read yet, wait for it. */
189 while(!(e && *e) && ((signed int) (c->in.request_read - request) < 0 ||
190 (c->in.request_read == request &&
191 _xcb_queue_is_empty(c->in.current_reply))))
192 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
195 if(c->in.request_read != request)
197 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
200 ret = _xcb_queue_dequeue(q);
201 if(_xcb_queue_is_empty(q))
202 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
206 ret = _xcb_queue_dequeue(c->in.current_reply);
209 _xcb_list_remove(c->in.readers, match_reply, &request);
210 pthread_cond_destroy(&cond);
212 wake_up_next_reader(c);
213 pthread_mutex_unlock(&c->iolock);
217 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
219 return XCBWaitForEvent(c);
222 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
224 XCBGenericEvent *ret;
227 fprintf(stderr, "Entering XCBWaitEvent\n");
230 pthread_mutex_lock(&c->iolock);
231 /* _xcb_list_remove_head returns 0 on empty list. */
232 while(!(ret = _xcb_queue_dequeue(c->in.events)))
233 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
236 wake_up_next_reader(c);
237 pthread_mutex_unlock(&c->iolock);
240 fprintf(stderr, "Leaving XCBWaitEvent, event type %d\n", ret ? ret->response_type : -1);
246 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
248 XCBGenericEvent *ret = 0;
249 pthread_mutex_lock(&c->iolock);
252 /* FIXME: follow X meets Z architecture changes. */
254 ret = _xcb_queue_dequeue(c->in.events);
259 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
262 pthread_mutex_unlock(&c->iolock);
266 unsigned int XCBGetRequestRead(XCBConnection *c)
269 pthread_mutex_lock(&c->iolock);
270 /* FIXME: follow X meets Z architecture changes. */
272 ret = c->in.request_read;
273 pthread_mutex_unlock(&c->iolock);
277 /* Private interface */
279 int _xcb_in_init(_xcb_in *in)
281 if(pthread_cond_init(&in->event_cond, 0))
287 in->request_read = 0;
288 in->current_reply = _xcb_queue_new();
290 in->replies = _xcb_map_new();
291 in->events = _xcb_queue_new();
292 in->readers = _xcb_list_new();
294 in->pending_replies = _xcb_queue_new();
295 if(!in->current_reply || !in->replies || !in->events || !in->readers || !in->pending_replies)
301 void _xcb_in_destroy(_xcb_in *in)
303 pthread_cond_destroy(&in->event_cond);
304 _xcb_queue_delete(in->current_reply, free);
305 _xcb_map_delete(in->replies, free);
306 _xcb_queue_delete(in->events, free);
307 _xcb_list_delete(in->readers, 0);
308 _xcb_queue_delete(in->pending_replies, free);
311 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround)
313 if(workaround != WORKAROUND_NONE)
315 pending_reply *pend = malloc(sizeof(pending_reply));
318 pend->request = request;
319 pend->workaround = workaround;
320 if(!_xcb_queue_enqueue(c->in.pending_replies, pend))
329 int _xcb_in_read(XCBConnection *c)
331 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
333 c->in.queue_len += n;
334 while(read_packet(c))
336 return (n > 0) || (n < 0 && errno == EAGAIN);
339 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
341 int done = c->in.queue_len;
345 memcpy(buf, c->in.queue, done);
346 c->in.queue_len -= done;
347 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
351 int ret = _xcb_read_block(c->fd, (char *) buf + done, len - done);