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. */
38 typedef struct XCBReplyData {
41 XCBGenericError **error;
44 static int match_request_error(const void *request, const void *data)
46 const XCBGenericError *e = data;
47 return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
50 static int match_reply(const void *request, const void *data)
52 return ((XCBReplyData *) data)->request == *(unsigned int *) request;
55 static void wake_up_next_reader(XCBConnection *c)
57 XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
60 pthreadret = pthread_cond_signal(cur->data);
62 pthreadret = pthread_cond_signal(&c->in.event_cond);
63 assert(pthreadret == 0);
66 /* Public interface */
68 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
70 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
76 pthread_mutex_lock(&c->iolock);
78 /* If this request has not been written yet, write it. */
79 if((signed int) (c->out.request_written - request) < 0)
80 if(_xcb_out_flush(c) <= 0)
81 goto done; /* error */
83 if(_xcb_list_find(c->in.readers, match_reply, &request))
84 goto done; /* error */
88 *e = _xcb_list_remove(c->in.events, match_request_error, &request);
93 reader.request = request;
96 _xcb_list_append(c->in.readers, &reader);
98 /* If this request has not been read yet, wait for it. */
99 while(!(e && *e) && ((signed int) (c->in.request_read - request) < 0 ||
100 (c->in.request_read == request &&
101 _xcb_queue_is_empty(c->in.current_reply))))
102 if(_xcb_conn_wait(c, /*should_write*/ 0, &cond) <= 0)
105 if(c->in.request_read != request)
107 _xcb_queue *q = _xcb_map_get(c->in.replies, request);
110 ret = _xcb_queue_dequeue(q);
111 if(_xcb_queue_is_empty(q))
112 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
116 ret = _xcb_queue_dequeue(c->in.current_reply);
119 _xcb_list_remove(c->in.readers, match_reply, &request);
120 pthread_cond_destroy(&cond);
122 wake_up_next_reader(c);
123 pthread_mutex_unlock(&c->iolock);
127 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
129 return XCBWaitForEvent(c);
132 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
134 XCBGenericEvent *ret;
137 fprintf(stderr, "Entering XCBWaitEvent\n");
140 pthread_mutex_lock(&c->iolock);
141 /* _xcb_list_remove_head returns 0 on empty list. */
142 while(!(ret = _xcb_queue_dequeue(c->in.events)))
143 if(_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond) <= 0)
146 wake_up_next_reader(c);
147 pthread_mutex_unlock(&c->iolock);
150 fprintf(stderr, "Leaving XCBWaitEvent, event type %d\n", ret ? ret->response_type : -1);
156 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
158 XCBGenericEvent *ret = 0;
159 pthread_mutex_lock(&c->iolock);
162 /* FIXME: follow X meets Z architecture changes. */
163 if(_xcb_in_read(c) >= 0)
164 ret = _xcb_queue_dequeue(c->in.events);
169 fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
172 pthread_mutex_unlock(&c->iolock);
176 unsigned int XCBGetRequestRead(XCBConnection *c)
179 pthread_mutex_lock(&c->iolock);
180 /* FIXME: follow X meets Z architecture changes. */
182 ret = c->in.request_read;
183 pthread_mutex_unlock(&c->iolock);
187 /* Private interface */
189 int _xcb_in_init(_xcb_in *in)
191 if(pthread_cond_init(&in->event_cond, 0))
197 in->request_read = 0;
198 in->current_reply = _xcb_queue_new();
200 in->replies = _xcb_map_new();
201 in->events = _xcb_queue_new();
202 in->readers = _xcb_list_new();
203 if(!in->current_reply || !in->replies || !in->events || !in->readers)
209 void _xcb_in_destroy(_xcb_in *in)
211 pthread_cond_destroy(&in->event_cond);
212 _xcb_queue_delete(in->current_reply, free);
213 _xcb_map_delete(in->replies, free);
214 _xcb_queue_delete(in->events, free);
215 _xcb_list_delete(in->readers, 0);
218 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request)
220 /* XXX: currently a no-op */
224 int _xcb_in_read_packet(XCBConnection *c)
226 XCBGenericRep genrep;
230 /* Wait for there to be enough data for us to read a whole packet */
231 if(c->in.queue_len < length)
234 /* Get the response type, length, and sequence number. */
235 memcpy(&genrep, c->in.queue, sizeof(genrep));
237 /* For reply packets, check that the entire packet is available. */
238 if(genrep.response_type == 1)
239 length += genrep.length * 4;
241 buf = malloc(length);
244 if(_xcb_in_read_block(c, buf, length) <= 0)
250 /* Compute 32-bit sequence number of this packet. */
251 /* XXX: do "sequence lost" check here */
252 if((genrep.response_type & 0x7f) != KeymapNotify)
254 int lastread = c->in.request_read;
255 c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
256 if(c->in.request_read != lastread && !_xcb_queue_is_empty(c->in.current_reply))
258 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
259 c->in.current_reply = _xcb_queue_new();
261 if(c->in.request_read < lastread)
262 c->in.request_read += 0x10000;
265 if(buf[0] == 1) /* response is a reply */
267 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
268 _xcb_queue_enqueue(c->in.current_reply, buf);
270 pthread_cond_signal(reader->data);
274 if(buf[0] == 0) /* response is an error */
276 XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
277 if(reader && reader->error)
279 *reader->error = (XCBGenericError *) buf;
280 pthread_cond_signal(reader->data);
285 /* event, or error without a waiting reader */
286 _xcb_queue_enqueue(c->in.events, buf);
287 pthread_cond_signal(&c->in.event_cond);
288 return 1; /* I have something for you... */
291 int _xcb_in_read(XCBConnection *c)
293 int n = _xcb_readn(c->fd, c->in.queue, sizeof(c->in.queue), &c->in.queue_len);
294 if(n < 0 && errno == EAGAIN)
296 while(_xcb_in_read_packet(c) > 0)
301 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
303 int done = c->in.queue_len;
307 memcpy(buf, c->in.queue, done);
308 c->in.queue_len -= done;
309 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
313 int ret = _xcb_read_block(c->fd, (char *) buf + done, len - done);