Replace pending_replies generic queue with a hand-implemented typesafe version.
[free-sw/xcb/libxcb] / src / xcb_in.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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.
19  * 
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.
24  */
25
26 /* Stuff that reads stuff from the server. */
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <errno.h>
34
35 #include "xcb.h"
36 #include "xcbext.h"
37 #include "xcbint.h"
38
39 typedef struct pending_reply {
40     unsigned int request;
41     enum workarounds workaround;
42     struct pending_reply *next;
43 } pending_reply;
44
45 typedef struct XCBReplyData {
46     unsigned int request;
47     void *data;
48     XCBGenericError **error;
49 } XCBReplyData;
50
51 static int match_request_error(const void *request, const void *data)
52 {
53     const XCBGenericError *e = data;
54     return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
55 }
56
57 static int match_reply(const void *request, const void *data)
58 {
59     return ((XCBReplyData *) data)->request == *(unsigned int *) request;
60 }
61
62 static void wake_up_next_reader(XCBConnection *c)
63 {
64     XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
65     int pthreadret;
66     if(cur)
67         pthreadret = pthread_cond_signal(cur->data);
68     else
69         pthreadret = pthread_cond_signal(&c->in.event_cond);
70     assert(pthreadret == 0);
71 }
72
73 static int read_packet(XCBConnection *c)
74 {
75     XCBGenericRep genrep;
76     int length = 32;
77     unsigned char *buf;
78
79     /* Wait for there to be enough data for us to read a whole packet */
80     if(c->in.queue_len < length)
81         return 0;
82
83     /* Get the response type, length, and sequence number. */
84     memcpy(&genrep, c->in.queue, sizeof(genrep));
85
86     /* Compute 32-bit sequence number of this packet. */
87     if((genrep.response_type & 0x7f) != KeymapNotify)
88     {
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))
92         {
93             _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
94             c->in.current_reply = _xcb_queue_new();
95         }
96         if(c->in.request_read < lastread)
97             c->in.request_read += 0x10000;
98     }
99
100     /* For reply packets, check that the entire packet is available. */
101     if(genrep.response_type == 1)
102     {
103         pending_reply *pend = c->in.pending_replies;
104         if(pend && pend->request == c->in.request_read)
105         {
106             switch(pend->workaround)
107             {
108             case WORKAROUND_NONE:
109                 break;
110             case WORKAROUND_GLX_GET_FB_CONFIGS_BUG:
111                 {
112                     CARD32 *p = (CARD32 *) c->in.queue;
113                     genrep.length = p[2] * p[3] * 2;
114                 }
115                 break;
116             }
117             c->in.pending_replies = pend->next;
118             if(!pend->next)
119                 c->in.pending_replies_tail = &c->in.pending_replies;
120             free(pend);
121         }
122         length += genrep.length * 4;
123     }
124
125     buf = malloc(length);
126     if(!buf)
127         return 0;
128     if(_xcb_in_read_block(c, buf, length) <= 0)
129     {
130         free(buf);
131         return 0;
132     }
133
134     if(buf[0] == 1) /* response is a reply */
135     {
136         XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
137         _xcb_queue_enqueue(c->in.current_reply, buf);
138         if(reader)
139             pthread_cond_signal(reader->data);
140         return 1;
141     }
142
143     if(buf[0] == 0) /* response is an error */
144     {
145         XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
146         if(reader && reader->error)
147         {
148             *reader->error = (XCBGenericError *) buf;
149             pthread_cond_signal(reader->data);
150             return 1;
151         }
152     }
153
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... */
158 }
159
160 static int read_block(const int fd, void *buf, const size_t len)
161 {
162     int done = 0;
163     while(done < len)
164     {
165         int ret = read(fd, ((char *) buf) + done, len - done);
166         if(ret > 0)
167             done += ret;
168         if(ret < 0 && errno == EAGAIN)
169         {
170             fd_set fds;
171             FD_ZERO(&fds);
172             FD_SET(fd, &fds);
173             ret = select(fd + 1, &fds, 0, 0, 0);
174         }
175         if(ret <= 0)
176             return ret;
177     }
178     return len;
179 }
180
181 /* Public interface */
182
183 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
184 {
185     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
186     XCBReplyData reader;
187     void *ret = 0;
188     if(e)
189         *e = 0;
190
191     pthread_mutex_lock(&c->iolock);
192
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 */
197
198     if(_xcb_list_find(c->in.readers, match_reply, &request))
199         goto done; /* error */
200
201     if(e)
202     {
203         *e = _xcb_list_remove(c->in.events, match_request_error, &request);
204         if(*e)
205             goto done;
206     }
207
208     reader.request = request;
209     reader.data = &cond;
210     reader.error = e;
211     _xcb_list_append(c->in.readers, &reader);
212
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))
218             goto done;
219
220     if(c->in.request_read != request)
221     {
222         _xcb_queue *q = _xcb_map_get(c->in.replies, request);
223         if(q)
224         {
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);
228         }
229     }
230     else
231         ret = _xcb_queue_dequeue(c->in.current_reply);
232
233 done:
234     _xcb_list_remove(c->in.readers, match_reply, &request);
235     pthread_cond_destroy(&cond);
236
237     wake_up_next_reader(c);
238     pthread_mutex_unlock(&c->iolock);
239     return ret;
240 }
241
242 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
243 {
244     return XCBWaitForEvent(c);
245 }
246
247 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
248 {
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))
254             break;
255
256     wake_up_next_reader(c);
257     pthread_mutex_unlock(&c->iolock);
258     return ret;
259 }
260
261 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
262 {
263     XCBGenericEvent *ret = 0;
264     pthread_mutex_lock(&c->iolock);
265     if(error)
266         *error = 0;
267     /* FIXME: follow X meets Z architecture changes. */
268     if(_xcb_in_read(c))
269         ret = _xcb_queue_dequeue(c->in.events);
270     else if(error)
271         *error = -1;
272     else
273     {
274         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
275         abort();
276     }
277     pthread_mutex_unlock(&c->iolock);
278     return ret;
279 }
280
281 unsigned int XCBGetRequestRead(XCBConnection *c)
282 {
283     unsigned int ret;
284     pthread_mutex_lock(&c->iolock);
285     /* FIXME: follow X meets Z architecture changes. */
286     _xcb_in_read(c);
287     ret = c->in.request_read;
288     pthread_mutex_unlock(&c->iolock);
289     return ret;
290 }
291
292 /* Private interface */
293
294 int _xcb_in_init(_xcb_in *in)
295 {
296     if(pthread_cond_init(&in->event_cond, 0))
297         return 0;
298     in->reading = 0;
299
300     in->queue_len = 0;
301
302     in->request_read = 0;
303     in->current_reply = _xcb_queue_new();
304
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)
309         return 0;
310
311     in->pending_replies_tail = &in->pending_replies;
312
313     return 1;
314 }
315
316 void _xcb_in_destroy(_xcb_in *in)
317 {
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)
324     {
325         pending_reply *pend = in->pending_replies;
326         in->pending_replies = pend->next;
327         free(pend);
328     }
329 }
330
331 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround)
332 {
333     if(workaround != WORKAROUND_NONE)
334     {
335         pending_reply *pend = malloc(sizeof(pending_reply));
336         if(!pend)
337             return 0;
338         pend->request = request;
339         pend->workaround = workaround;
340         pend->next = 0;
341         *c->in.pending_replies_tail = pend;
342         c->in.pending_replies_tail = &pend->next;
343     }
344     return 1;
345 }
346
347 int _xcb_in_read(XCBConnection *c)
348 {
349     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
350     if(n > 0)
351         c->in.queue_len += n;
352     while(read_packet(c))
353         /* empty */;
354     return (n > 0) || (n < 0 && errno == EAGAIN);
355 }
356
357 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
358 {
359     int done = c->in.queue_len;
360     if(len < done)
361         done = len;
362
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);
366
367     if(len > done)
368     {
369         int ret = read_block(c->fd, (char *) buf + done, len - done);
370         if(ret <= 0)
371             return ret;
372     }
373
374     return len;
375 }