Replace readers generic list 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 struct reply_list {
45     void *reply;
46     struct reply_list *next;
47 };
48
49 typedef struct pending_reply {
50     unsigned int request;
51     enum workarounds workaround;
52     int flags;
53     struct pending_reply *next;
54 } pending_reply;
55
56 typedef struct reader_list {
57     unsigned int request;
58     pthread_cond_t *data;
59     struct reader_list *next;
60 } reader_list;
61
62 static void wake_up_next_reader(XCBConnection *c)
63 {
64     int pthreadret;
65     if(c->in.readers)
66         pthreadret = pthread_cond_signal(c->in.readers->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(c->in.current_reply)
103             {
104                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
105                 c->in.current_reply = 0;
106                 c->in.current_reply_tail = &c->in.current_reply;
107             }
108         }
109         if(c->in.request_read < lastread)
110             c->in.request_read += 0x10000;
111     }
112
113     if(genrep.response_type == 0 || genrep.response_type == 1)
114     {
115         pend = c->in.pending_replies;
116         if(pend && pend->request != c->in.request_read)
117             pend = 0;
118     }
119
120     /* For reply packets, check that the entire packet is available. */
121     if(genrep.response_type == 1)
122     {
123         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
124         {
125             CARD32 *p = (CARD32 *) c->in.queue;
126             genrep.length = p[2] * p[3] * 2;
127         }
128         length += genrep.length * 4;
129     }
130
131     buf = malloc(length);
132     if(!buf)
133         return 0;
134     if(_xcb_in_read_block(c, buf, length) <= 0)
135     {
136         free(buf);
137         return 0;
138     }
139
140     /* reply, or checked error */
141     if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED)))
142     {
143         reader_list *reader;
144         struct reply_list *cur = malloc(sizeof(struct reply_list));
145         if(!cur)
146             return 0;
147         cur->reply = buf;
148         cur->next = 0;
149         *c->in.current_reply_tail = cur;
150         c->in.current_reply_tail = &cur->next;
151         for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next)
152             if(reader->request == c->in.request_read)
153             {
154                 pthread_cond_signal(reader->data);
155                 break;
156             }
157         return 1;
158     }
159
160     /* event, or unchecked error */
161     event = malloc(sizeof(struct event_list));
162     if(!event)
163     {
164         free(buf);
165         return 0;
166     }
167     event->event = buf;
168     event->next = 0;
169     *c->in.events_tail = event;
170     c->in.events_tail = &event->next;
171     pthread_cond_signal(&c->in.event_cond);
172     return 1; /* I have something for you... */
173 }
174
175 static XCBGenericEvent *get_event(XCBConnection *c)
176 {
177     struct event_list *cur = c->in.events;
178     XCBGenericEvent *ret;
179     if(!c->in.events)
180         return 0;
181     ret = cur->event;
182     c->in.events = cur->next;
183     if(!cur->next)
184         c->in.events_tail = &c->in.events;
185     free(cur);
186     return ret;
187 }
188
189 static void free_reply_list(struct reply_list *head)
190 {
191     while(head)
192     {
193         struct reply_list *cur = head;
194         head = cur->next;
195         free(cur->reply);
196         free(cur);
197     }
198 }
199
200 static int read_block(const int fd, void *buf, const size_t len)
201 {
202     int done = 0;
203     while(done < len)
204     {
205         int ret = read(fd, ((char *) buf) + done, len - done);
206         if(ret > 0)
207             done += ret;
208         if(ret < 0 && errno == EAGAIN)
209         {
210             fd_set fds;
211             FD_ZERO(&fds);
212             FD_SET(fd, &fds);
213             ret = select(fd + 1, &fds, 0, 0, 0);
214         }
215         if(ret <= 0)
216             return ret;
217     }
218     return len;
219 }
220
221 /* Public interface */
222
223 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
224 {
225     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
226     reader_list reader;
227     reader_list **prev_reader;
228     struct reply_list *head;
229     void *ret = 0;
230     if(e)
231         *e = 0;
232
233     pthread_mutex_lock(&c->iolock);
234
235     /* If this request has not been written yet, write it. */
236     if((signed int) (c->out.request_written - request) < 0)
237         if(!_xcb_out_flush(c))
238             goto done; /* error */
239
240     for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
241         if((*prev_reader)->request == request)
242             goto done; /* error */
243
244     reader.request = request;
245     reader.data = &cond;
246     reader.next = *prev_reader;
247     *prev_reader = &reader;
248
249     /* If this request has not been read yet, wait for it. */
250     while(((signed int) (c->in.request_read - request) < 0 ||
251             (c->in.request_read == request && !c->in.current_reply)))
252         if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
253             goto done;
254
255     if(c->in.request_read != request)
256     {
257         head = _xcb_map_remove(c->in.replies, request);
258         if(head && head->next)
259             _xcb_map_put(c->in.replies, request, head->next);
260     }
261     else
262     {
263         head = c->in.current_reply;
264         if(head)
265         {
266             c->in.current_reply = head->next;
267             if(!head->next)
268                 c->in.current_reply_tail = &c->in.current_reply;
269         }
270     }
271
272     if(head)
273     {
274         ret = head->reply;
275         free(head);
276
277         if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
278         {
279             if(e)
280                 *e = ret;
281             else
282                 free(ret);
283             ret = 0;
284         }
285     }
286
287 done:
288     for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
289         if(*prev_reader == &reader)
290         {
291             *prev_reader = (*prev_reader)->next;
292             break;
293         }
294     pthread_cond_destroy(&cond);
295
296     wake_up_next_reader(c);
297     pthread_mutex_unlock(&c->iolock);
298     return ret;
299 }
300
301 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
302 {
303     return XCBWaitForEvent(c);
304 }
305
306 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
307 {
308     XCBGenericEvent *ret;
309     pthread_mutex_lock(&c->iolock);
310     /* get_event returns 0 on empty list. */
311     while(!(ret = get_event(c)))
312         if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
313             break;
314
315     wake_up_next_reader(c);
316     pthread_mutex_unlock(&c->iolock);
317     return ret;
318 }
319
320 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
321 {
322     XCBGenericEvent *ret = 0;
323     pthread_mutex_lock(&c->iolock);
324     if(error)
325         *error = 0;
326     /* FIXME: follow X meets Z architecture changes. */
327     if(_xcb_in_read(c))
328         ret = get_event(c);
329     else if(error)
330         *error = -1;
331     else
332     {
333         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
334         abort();
335     }
336     pthread_mutex_unlock(&c->iolock);
337     return ret;
338 }
339
340 unsigned int XCBGetRequestRead(XCBConnection *c)
341 {
342     unsigned int ret;
343     pthread_mutex_lock(&c->iolock);
344     /* FIXME: follow X meets Z architecture changes. */
345     _xcb_in_read(c);
346     ret = c->in.request_read;
347     pthread_mutex_unlock(&c->iolock);
348     return ret;
349 }
350
351 /* Private interface */
352
353 int _xcb_in_init(_xcb_in *in)
354 {
355     if(pthread_cond_init(&in->event_cond, 0))
356         return 0;
357     in->reading = 0;
358
359     in->queue_len = 0;
360
361     in->request_read = 0;
362
363     in->replies = _xcb_map_new();
364     if(!in->replies)
365         return 0;
366
367     in->current_reply_tail = &in->current_reply;
368     in->events_tail = &in->events;
369     in->pending_replies_tail = &in->pending_replies;
370
371     return 1;
372 }
373
374 void _xcb_in_destroy(_xcb_in *in)
375 {
376     pthread_cond_destroy(&in->event_cond);
377     free_reply_list(in->current_reply);
378     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
379     while(in->events)
380     {
381         struct event_list *e = in->events;
382         in->events = e->next;
383         free(e->event);
384         free(e);
385     }
386     while(in->pending_replies)
387     {
388         pending_reply *pend = in->pending_replies;
389         in->pending_replies = pend->next;
390         free(pend);
391     }
392 }
393
394 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
395 {
396     if(workaround != WORKAROUND_NONE || flags != 0)
397     {
398         pending_reply *pend = malloc(sizeof(pending_reply));
399         if(!pend)
400             return 0;
401         pend->request = request;
402         pend->workaround = workaround;
403         pend->flags = flags;
404         pend->next = 0;
405         *c->in.pending_replies_tail = pend;
406         c->in.pending_replies_tail = &pend->next;
407     }
408     return 1;
409 }
410
411 int _xcb_in_read(XCBConnection *c)
412 {
413     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
414     if(n > 0)
415         c->in.queue_len += n;
416     while(read_packet(c))
417         /* empty */;
418     return (n > 0) || (n < 0 && errno == EAGAIN);
419 }
420
421 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
422 {
423     int done = c->in.queue_len;
424     if(len < done)
425         done = len;
426
427     memcpy(buf, c->in.queue, done);
428     c->in.queue_len -= done;
429     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
430
431     if(len > done)
432     {
433         int ret = read_block(c->fd, (char *) buf + done, len - done);
434         if(ret <= 0)
435             return ret;
436     }
437
438     return len;
439 }