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