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