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;
44 typedef struct pending_reply {
46 enum workarounds workaround;
48 struct pending_reply *next;
51 typedef struct XCBReplyData {
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)
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 pending_reply *oldpend = c->in.pending_replies;
95 if(oldpend && oldpend->request == lastread)
97 c->in.pending_replies = oldpend->next;
99 c->in.pending_replies_tail = &c->in.pending_replies;
102 if(!_xcb_queue_is_empty(c->in.current_reply))
104 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
105 c->in.current_reply = _xcb_queue_new();
108 if(c->in.request_read < lastread)
109 c->in.request_read += 0x10000;
112 if(genrep.response_type == 0 || genrep.response_type == 1)
114 pend = c->in.pending_replies;
115 if(pend && pend->request != c->in.request_read)
119 /* For reply packets, check that the entire packet is available. */
120 if(genrep.response_type == 1)
122 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
124 CARD32 *p = (CARD32 *) c->in.queue;
125 genrep.length = p[2] * p[3] * 2;
127 length += genrep.length * 4;
130 buf = malloc(length);
133 if(_xcb_in_read_block(c, buf, length) <= 0)
139 /* reply, or checked error */
140 if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
142 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
143 _xcb_queue_enqueue(c->in.current_reply, buf);
145 pthread_cond_signal(reader->data);
149 /* event, or unchecked error */
150 event = malloc(sizeof(struct event_list));
158 *c->in.events_tail = event;
159 c->in.events_tail = &event->next;
160 pthread_cond_signal(&c->in.event_cond);
161 return 1; /* I have something for you... */
164 static XCBGenericEvent *get_event(XCBConnection *c)
166 struct event_list *cur = c->in.events;
167 XCBGenericEvent *ret;
171 c->in.events = cur->next;
173 c->in.events_tail = &c->in.events;
178 static int read_block(const int fd, void *buf, const size_t len)
183 int ret = read(fd, ((char *) buf) + done, len - done);
186 if(ret < 0 && errno == EAGAIN)
191 ret = select(fd + 1, &fds, 0, 0, 0);
199 /* Public interface */
201 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
203 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
209 pthread_mutex_lock(&c->iolock);
211 /* If this request has not been written yet, write it. */
212 if((signed int) (c->out.request_written - request) < 0)
213 if(!_xcb_out_flush(c))
214 goto done; /* error */
216 if(_xcb_list_find(c->in.readers, match_reply, &request))
217 goto done; /* error */
219 reader.request = request;
221 _xcb_list_append(c->in.readers, &reader);
223 /* If this request has not been read yet, wait for it. */
224 while(((signed int) (c->in.request_read - request) < 0 ||
225 (c->in.request_read == request &&
226 _xcb_queue_is_empty(c->in.current_reply))))
227 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
230 if(c->in.request_read != request)
232 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
235 ret = _xcb_queue_dequeue(q);
236 if(_xcb_queue_is_empty(q))
237 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
241 ret = _xcb_queue_dequeue(c->in.current_reply);
243 if(ret && ((XCBGenericRep *) ret)->response_type == 0) /* X error */
253 _xcb_list_remove(c->in.readers, match_reply, &request);
254 pthread_cond_destroy(&cond);
256 wake_up_next_reader(c);
257 pthread_mutex_unlock(&c->iolock);
261 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
263 return XCBWaitForEvent(c);
266 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
268 XCBGenericEvent *ret;
269 pthread_mutex_lock(&c->iolock);
270 /* get_event returns 0 on empty list. */
271 while(!(ret = get_event(c)))
272 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
275 wake_up_next_reader(c);
276 pthread_mutex_unlock(&c->iolock);
280 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
282 XCBGenericEvent *ret = 0;
283 pthread_mutex_lock(&c->iolock);
286 /* FIXME: follow X meets Z architecture changes. */
293 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
296 pthread_mutex_unlock(&c->iolock);
300 unsigned int XCBGetRequestRead(XCBConnection *c)
303 pthread_mutex_lock(&c->iolock);
304 /* FIXME: follow X meets Z architecture changes. */
306 ret = c->in.request_read;
307 pthread_mutex_unlock(&c->iolock);
311 /* Private interface */
313 int _xcb_in_init(_xcb_in *in)
315 if(pthread_cond_init(&in->event_cond, 0))
321 in->request_read = 0;
322 in->current_reply = _xcb_queue_new();
324 in->replies = _xcb_map_new();
325 in->readers = _xcb_list_new();
326 if(!in->current_reply || !in->replies || !in->readers)
329 in->events_tail = &in->events;
330 in->pending_replies_tail = &in->pending_replies;
335 void _xcb_in_destroy(_xcb_in *in)
337 pthread_cond_destroy(&in->event_cond);
338 _xcb_queue_delete(in->current_reply, free);
339 _xcb_map_delete(in->replies, free);
340 _xcb_list_delete(in->readers, 0);
343 struct event_list *e = in->events;
344 in->events = e->next;
348 while(in->pending_replies)
350 pending_reply *pend = in->pending_replies;
351 in->pending_replies = pend->next;
356 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
358 if(workaround != WORKAROUND_NONE || flags != 0)
360 pending_reply *pend = malloc(sizeof(pending_reply));
363 pend->request = request;
364 pend->workaround = workaround;
367 *c->in.pending_replies_tail = pend;
368 c->in.pending_replies_tail = &pend->next;
373 int _xcb_in_read(XCBConnection *c)
375 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
377 c->in.queue_len += n;
378 while(read_packet(c))
380 return (n > 0) || (n < 0 && errno == EAGAIN);
383 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
385 int done = c->in.queue_len;
389 memcpy(buf, c->in.queue, done);
390 c->in.queue_len -= done;
391 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
395 int ret = read_block(c->fd, (char *) buf + done, len - done);