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;
43 struct pending_reply *next;
46 typedef struct XCBReplyData {
51 static int match_reply(const void *request, const void *data)
53 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
56 static void wake_up_next_reader(XCBConnection *c)
58 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
61 pthreadret = pthread_cond_signal(cur->data);
63 pthreadret = pthread_cond_signal(&c->in.event_cond);
64 assert(pthreadret == 0);
67 static int read_packet(XCBConnection *c)
72 pending_reply *pend = 0;
74 /* Wait for there to be enough data for us to read a whole packet */
75 if(c->in.queue_len < length)
78 /* Get the response type, length, and sequence number. */
79 memcpy(&genrep, c->in.queue, sizeof(genrep));
81 /* Compute 32-bit sequence number of this packet. */
82 if((genrep.response_type & 0x7f) != KeymapNotify)
84 int lastread = c->in.request_read;
85 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
86 if(c->in.request_read != lastread)
88 pending_reply *oldpend = c->in.pending_replies;
89 if(oldpend && oldpend->request == lastread)
91 c->in.pending_replies = oldpend->next;
93 c->in.pending_replies_tail = &c->in.pending_replies;
96 if(!_xcb_queue_is_empty(c->in.current_reply))
98 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
99 c->in.current_reply = _xcb_queue_new();
102 if(c->in.request_read < lastread)
103 c->in.request_read += 0x10000;
106 if(genrep.response_type == 0 || genrep.response_type == 1)
108 pend = c->in.pending_replies;
109 if(pend && pend->request != c->in.request_read)
113 /* For reply packets, check that the entire packet is available. */
114 if(genrep.response_type == 1)
116 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
118 CARD32 *p = (CARD32 *) c->in.queue;
119 genrep.length = p[2] * p[3] * 2;
121 length += genrep.length * 4;
124 buf = malloc(length);
127 if(_xcb_in_read_block(c, buf, length) <= 0)
133 /* reply, or checked error */
134 if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
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 /* event, or unchecked error */
144 _xcb_queue_enqueue(c->in.events, buf);
145 pthread_cond_signal(&c->in.event_cond);
146 return 1; /* I have something for you... */
149 static int read_block(const int fd, void *buf, const size_t len)
154 int ret = read(fd, ((char *) buf) + done, len - done);
157 if(ret < 0 && errno == EAGAIN)
162 ret = select(fd + 1, &fds, 0, 0, 0);
170 /* Public interface */
172 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
174 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
180 pthread_mutex_lock(&c->iolock);
182 /* If this request has not been written yet, write it. */
183 if((signed int) (c->out.request_written - request) < 0)
184 if(!_xcb_out_flush(c))
185 goto done; /* error */
187 if(_xcb_list_find(c->in.readers, match_reply, &request))
188 goto done; /* error */
190 reader.request = request;
192 _xcb_list_append(c->in.readers, &reader);
194 /* If this request has not been read yet, wait for it. */
195 while(((signed int) (c->in.request_read - request) < 0 ||
196 (c->in.request_read == request &&
197 _xcb_queue_is_empty(c->in.current_reply))))
198 if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
201 if(c->in.request_read != request)
203 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
206 ret = _xcb_queue_dequeue(q);
207 if(_xcb_queue_is_empty(q))
208 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
212 ret = _xcb_queue_dequeue(c->in.current_reply);
214 if(ret && ((XCBGenericRep *) ret)->response_type == 0) /* X error */
224 _xcb_list_remove(c->in.readers, match_reply, &request);
225 pthread_cond_destroy(&cond);
227 wake_up_next_reader(c);
228 pthread_mutex_unlock(&c->iolock);
232 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
234 return XCBWaitForEvent(c);
237 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
239 XCBGenericEvent *ret;
240 pthread_mutex_lock(&c->iolock);
241 /* _xcb_list_remove_head returns 0 on empty list. */
242 while(!(ret = _xcb_queue_dequeue(c->in.events)))
243 if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
246 wake_up_next_reader(c);
247 pthread_mutex_unlock(&c->iolock);
251 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
253 XCBGenericEvent *ret = 0;
254 pthread_mutex_lock(&c->iolock);
257 /* FIXME: follow X meets Z architecture changes. */
259 ret = _xcb_queue_dequeue(c->in.events);
264 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
267 pthread_mutex_unlock(&c->iolock);
271 unsigned int XCBGetRequestRead(XCBConnection *c)
274 pthread_mutex_lock(&c->iolock);
275 /* FIXME: follow X meets Z architecture changes. */
277 ret = c->in.request_read;
278 pthread_mutex_unlock(&c->iolock);
282 /* Private interface */
284 int _xcb_in_init(_xcb_in *in)
286 if(pthread_cond_init(&in->event_cond, 0))
292 in->request_read = 0;
293 in->current_reply = _xcb_queue_new();
295 in->replies = _xcb_map_new();
296 in->events = _xcb_queue_new();
297 in->readers = _xcb_list_new();
298 if(!in->current_reply || !in->replies || !in->events || !in->readers)
301 in->pending_replies_tail = &in->pending_replies;
306 void _xcb_in_destroy(_xcb_in *in)
308 pthread_cond_destroy(&in->event_cond);
309 _xcb_queue_delete(in->current_reply, free);
310 _xcb_map_delete(in->replies, free);
311 _xcb_queue_delete(in->events, free);
312 _xcb_list_delete(in->readers, 0);
313 while(in->pending_replies)
315 pending_reply *pend = in->pending_replies;
316 in->pending_replies = pend->next;
321 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
323 if(workaround != WORKAROUND_NONE || flags != 0)
325 pending_reply *pend = malloc(sizeof(pending_reply));
328 pend->request = request;
329 pend->workaround = workaround;
332 *c->in.pending_replies_tail = pend;
333 c->in.pending_replies_tail = &pend->next;
338 int _xcb_in_read(XCBConnection *c)
340 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
342 c->in.queue_len += n;
343 while(read_packet(c))
345 return (n > 0) || (n < 0 && errno == EAGAIN);
348 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
350 int done = c->in.queue_len;
354 memcpy(buf, c->in.queue, done);
355 c->in.queue_len -= done;
356 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
360 int ret = read_block(c->fd, (char *) buf + done, len - done);