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