Replace events generic queue with hand-written 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 struct event_list {
40     XCBGenericEvent *event;
41     struct event_list *next;
42 };
43
44 typedef struct pending_reply {
45     unsigned int request;
46     enum workarounds workaround;
47     int flags;
48     struct pending_reply *next;
49 } pending_reply;
50
51 typedef struct XCBReplyData {
52     unsigned int request;
53     void *data;
54 } XCBReplyData;
55
56 static int match_reply(const void *request, const void *data)
57 {
58     return ((XCBReplyData *) data)->request == *(unsigned int *) request;
59 }
60
61 static void wake_up_next_reader(XCBConnection *c)
62 {
63     XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
64     int pthreadret;
65     if(cur)
66         pthreadret = pthread_cond_signal(cur->data);
67     else
68         pthreadret = pthread_cond_signal(&c->in.event_cond);
69     assert(pthreadret == 0);
70 }
71
72 static int read_packet(XCBConnection *c)
73 {
74     XCBGenericRep genrep;
75     int length = 32;
76     void *buf;
77     pending_reply *pend = 0;
78     struct event_list *event;
79
80     /* Wait for there to be enough data for us to read a whole packet */
81     if(c->in.queue_len < length)
82         return 0;
83
84     /* Get the response type, length, and sequence number. */
85     memcpy(&genrep, c->in.queue, sizeof(genrep));
86
87     /* Compute 32-bit sequence number of this packet. */
88     if((genrep.response_type & 0x7f) != KeymapNotify)
89     {
90         int lastread = c->in.request_read;
91         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
92         if(c->in.request_read != lastread)
93         {
94             pending_reply *oldpend = c->in.pending_replies;
95             if(oldpend && oldpend->request == lastread)
96             {
97                 c->in.pending_replies = oldpend->next;
98                 if(!oldpend->next)
99                     c->in.pending_replies_tail = &c->in.pending_replies;
100                 free(oldpend);
101             }
102             if(!_xcb_queue_is_empty(c->in.current_reply))
103             {
104                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
105                 c->in.current_reply = _xcb_queue_new();
106             }
107         }
108         if(c->in.request_read < lastread)
109             c->in.request_read += 0x10000;
110     }
111
112     if(genrep.response_type == 0 || genrep.response_type == 1)
113     {
114         pend = c->in.pending_replies;
115         if(pend && pend->request != c->in.request_read)
116             pend = 0;
117     }
118
119     /* For reply packets, check that the entire packet is available. */
120     if(genrep.response_type == 1)
121     {
122         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
123         {
124             CARD32 *p = (CARD32 *) c->in.queue;
125             genrep.length = p[2] * p[3] * 2;
126         }
127         length += genrep.length * 4;
128     }
129
130     buf = malloc(length);
131     if(!buf)
132         return 0;
133     if(_xcb_in_read_block(c, buf, length) <= 0)
134     {
135         free(buf);
136         return 0;
137     }
138
139     /* reply, or checked error */
140     if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
141     {
142         XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
143         _xcb_queue_enqueue(c->in.current_reply, buf);
144         if(reader)
145             pthread_cond_signal(reader->data);
146         return 1;
147     }
148
149     /* event, or unchecked error */
150     event = malloc(sizeof(struct event_list));
151     if(!event)
152     {
153         free(buf);
154         return 0;
155     }
156     event->event = buf;
157     event->next = 0;
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... */
162 }
163
164 static XCBGenericEvent *get_event(XCBConnection *c)
165 {
166     struct event_list *cur = c->in.events;
167     XCBGenericEvent *ret;
168     if(!c->in.events)
169         return 0;
170     ret = cur->event;
171     c->in.events = cur->next;
172     if(!cur->next)
173         c->in.events_tail = &c->in.events;
174     free(cur);
175     return ret;
176 }
177
178 static int read_block(const int fd, void *buf, const size_t len)
179 {
180     int done = 0;
181     while(done < len)
182     {
183         int ret = read(fd, ((char *) buf) + done, len - done);
184         if(ret > 0)
185             done += ret;
186         if(ret < 0 && errno == EAGAIN)
187         {
188             fd_set fds;
189             FD_ZERO(&fds);
190             FD_SET(fd, &fds);
191             ret = select(fd + 1, &fds, 0, 0, 0);
192         }
193         if(ret <= 0)
194             return ret;
195     }
196     return len;
197 }
198
199 /* Public interface */
200
201 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
202 {
203     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
204     XCBReplyData reader;
205     void *ret = 0;
206     if(e)
207         *e = 0;
208
209     pthread_mutex_lock(&c->iolock);
210
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 */
215
216     if(_xcb_list_find(c->in.readers, match_reply, &request))
217         goto done; /* error */
218
219     reader.request = request;
220     reader.data = &cond;
221     _xcb_list_append(c->in.readers, &reader);
222
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))
228             goto done;
229
230     if(c->in.request_read != request)
231     {
232         _xcb_queue *q = _xcb_map_get(c->in.replies, request);
233         if(q)
234         {
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);
238         }
239     }
240     else
241         ret = _xcb_queue_dequeue(c->in.current_reply);
242
243     if(ret && ((XCBGenericRep *) ret)->response_type == 0) /* X error */
244     {
245         if(e)
246             *e = ret;
247         else
248             free(ret);
249         ret = 0;
250     }
251
252 done:
253     _xcb_list_remove(c->in.readers, match_reply, &request);
254     pthread_cond_destroy(&cond);
255
256     wake_up_next_reader(c);
257     pthread_mutex_unlock(&c->iolock);
258     return ret;
259 }
260
261 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
262 {
263     return XCBWaitForEvent(c);
264 }
265
266 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
267 {
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))
273             break;
274
275     wake_up_next_reader(c);
276     pthread_mutex_unlock(&c->iolock);
277     return ret;
278 }
279
280 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
281 {
282     XCBGenericEvent *ret = 0;
283     pthread_mutex_lock(&c->iolock);
284     if(error)
285         *error = 0;
286     /* FIXME: follow X meets Z architecture changes. */
287     if(_xcb_in_read(c))
288         ret = get_event(c);
289     else if(error)
290         *error = -1;
291     else
292     {
293         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
294         abort();
295     }
296     pthread_mutex_unlock(&c->iolock);
297     return ret;
298 }
299
300 unsigned int XCBGetRequestRead(XCBConnection *c)
301 {
302     unsigned int ret;
303     pthread_mutex_lock(&c->iolock);
304     /* FIXME: follow X meets Z architecture changes. */
305     _xcb_in_read(c);
306     ret = c->in.request_read;
307     pthread_mutex_unlock(&c->iolock);
308     return ret;
309 }
310
311 /* Private interface */
312
313 int _xcb_in_init(_xcb_in *in)
314 {
315     if(pthread_cond_init(&in->event_cond, 0))
316         return 0;
317     in->reading = 0;
318
319     in->queue_len = 0;
320
321     in->request_read = 0;
322     in->current_reply = _xcb_queue_new();
323
324     in->replies = _xcb_map_new();
325     in->readers = _xcb_list_new();
326     if(!in->current_reply || !in->replies || !in->readers)
327         return 0;
328
329     in->events_tail = &in->events;
330     in->pending_replies_tail = &in->pending_replies;
331
332     return 1;
333 }
334
335 void _xcb_in_destroy(_xcb_in *in)
336 {
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);
341     while(in->events)
342     {
343         struct event_list *e = in->events;
344         in->events = e->next;
345         free(e->event);
346         free(e);
347     }
348     while(in->pending_replies)
349     {
350         pending_reply *pend = in->pending_replies;
351         in->pending_replies = pend->next;
352         free(pend);
353     }
354 }
355
356 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
357 {
358     if(workaround != WORKAROUND_NONE || flags != 0)
359     {
360         pending_reply *pend = malloc(sizeof(pending_reply));
361         if(!pend)
362             return 0;
363         pend->request = request;
364         pend->workaround = workaround;
365         pend->flags = flags;
366         pend->next = 0;
367         *c->in.pending_replies_tail = pend;
368         c->in.pending_replies_tail = &pend->next;
369     }
370     return 1;
371 }
372
373 int _xcb_in_read(XCBConnection *c)
374 {
375     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
376     if(n > 0)
377         c->in.queue_len += n;
378     while(read_packet(c))
379         /* empty */;
380     return (n > 0) || (n < 0 && errno == EAGAIN);
381 }
382
383 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
384 {
385     int done = c->in.queue_len;
386     if(len < done)
387         done = len;
388
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);
392
393     if(len > done)
394     {
395         int ret = read_block(c->fd, (char *) buf + done, len - done);
396         if(ret <= 0)
397             return ret;
398     }
399
400     return len;
401 }