Rename struct XCBReplyData to reader_list to follow my naming conventions and better...
[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 } reader_list;
60
61 static int match_reply(const void *request, const void *data)
62 {
63     return ((reader_list *) data)->request == *(unsigned int *) request;
64 }
65
66 static void wake_up_next_reader(XCBConnection *c)
67 {
68     reader_list *cur = _xcb_list_peek_head(c->in.readers);
69     int pthreadret;
70     if(cur)
71         pthreadret = pthread_cond_signal(cur->data);
72     else
73         pthreadret = pthread_cond_signal(&c->in.event_cond);
74     assert(pthreadret == 0);
75 }
76
77 static int read_packet(XCBConnection *c)
78 {
79     XCBGenericRep genrep;
80     int length = 32;
81     void *buf;
82     pending_reply *pend = 0;
83     struct event_list *event;
84
85     /* Wait for there to be enough data for us to read a whole packet */
86     if(c->in.queue_len < length)
87         return 0;
88
89     /* Get the response type, length, and sequence number. */
90     memcpy(&genrep, c->in.queue, sizeof(genrep));
91
92     /* Compute 32-bit sequence number of this packet. */
93     if((genrep.response_type & 0x7f) != KeymapNotify)
94     {
95         int lastread = c->in.request_read;
96         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
97         if(c->in.request_read != lastread)
98         {
99             pending_reply *oldpend = c->in.pending_replies;
100             if(oldpend && oldpend->request == lastread)
101             {
102                 c->in.pending_replies = oldpend->next;
103                 if(!oldpend->next)
104                     c->in.pending_replies_tail = &c->in.pending_replies;
105                 free(oldpend);
106             }
107             if(c->in.current_reply)
108             {
109                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
110                 c->in.current_reply = 0;
111                 c->in.current_reply_tail = &c->in.current_reply;
112             }
113         }
114         if(c->in.request_read < lastread)
115             c->in.request_read += 0x10000;
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 = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
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         if(reader)
157             pthread_cond_signal(reader->data);
158         return 1;
159     }
160
161     /* event, or unchecked error */
162     event = malloc(sizeof(struct event_list));
163     if(!event)
164     {
165         free(buf);
166         return 0;
167     }
168     event->event = buf;
169     event->next = 0;
170     *c->in.events_tail = event;
171     c->in.events_tail = &event->next;
172     pthread_cond_signal(&c->in.event_cond);
173     return 1; /* I have something for you... */
174 }
175
176 static XCBGenericEvent *get_event(XCBConnection *c)
177 {
178     struct event_list *cur = c->in.events;
179     XCBGenericEvent *ret;
180     if(!c->in.events)
181         return 0;
182     ret = cur->event;
183     c->in.events = cur->next;
184     if(!cur->next)
185         c->in.events_tail = &c->in.events;
186     free(cur);
187     return ret;
188 }
189
190 static void free_reply_list(struct reply_list *head)
191 {
192     while(head)
193     {
194         struct reply_list *cur = head;
195         head = cur->next;
196         free(cur->reply);
197         free(cur);
198     }
199 }
200
201 static int read_block(const int fd, void *buf, const size_t len)
202 {
203     int done = 0;
204     while(done < len)
205     {
206         int ret = read(fd, ((char *) buf) + done, len - done);
207         if(ret > 0)
208             done += ret;
209         if(ret < 0 && errno == EAGAIN)
210         {
211             fd_set fds;
212             FD_ZERO(&fds);
213             FD_SET(fd, &fds);
214             ret = select(fd + 1, &fds, 0, 0, 0);
215         }
216         if(ret <= 0)
217             return ret;
218     }
219     return len;
220 }
221
222 /* Public interface */
223
224 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
225 {
226     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
227     reader_list 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     if(_xcb_list_find(c->in.readers, match_reply, &request))
241         goto done; /* error */
242
243     reader.request = request;
244     reader.data = &cond;
245     _xcb_list_append(c->in.readers, &reader);
246
247     /* If this request has not been read yet, wait for it. */
248     while(((signed int) (c->in.request_read - request) < 0 ||
249             (c->in.request_read == request && !c->in.current_reply)))
250         if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
251             goto done;
252
253     if(c->in.request_read != request)
254     {
255         head = _xcb_map_remove(c->in.replies, request);
256         if(head && head->next)
257             _xcb_map_put(c->in.replies, request, head->next);
258     }
259     else
260     {
261         head = c->in.current_reply;
262         if(head)
263         {
264             c->in.current_reply = head->next;
265             if(!head->next)
266                 c->in.current_reply_tail = &c->in.current_reply;
267         }
268     }
269
270     if(head)
271     {
272         ret = head->reply;
273         free(head);
274
275         if(((XCBGenericRep *) ret)->response_type == 0) /* X error */
276         {
277             if(e)
278                 *e = ret;
279             else
280                 free(ret);
281             ret = 0;
282         }
283     }
284
285 done:
286     _xcb_list_remove(c->in.readers, match_reply, &request);
287     pthread_cond_destroy(&cond);
288
289     wake_up_next_reader(c);
290     pthread_mutex_unlock(&c->iolock);
291     return ret;
292 }
293
294 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
295 {
296     return XCBWaitForEvent(c);
297 }
298
299 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
300 {
301     XCBGenericEvent *ret;
302     pthread_mutex_lock(&c->iolock);
303     /* get_event returns 0 on empty list. */
304     while(!(ret = get_event(c)))
305         if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
306             break;
307
308     wake_up_next_reader(c);
309     pthread_mutex_unlock(&c->iolock);
310     return ret;
311 }
312
313 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
314 {
315     XCBGenericEvent *ret = 0;
316     pthread_mutex_lock(&c->iolock);
317     if(error)
318         *error = 0;
319     /* FIXME: follow X meets Z architecture changes. */
320     if(_xcb_in_read(c))
321         ret = get_event(c);
322     else if(error)
323         *error = -1;
324     else
325     {
326         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
327         abort();
328     }
329     pthread_mutex_unlock(&c->iolock);
330     return ret;
331 }
332
333 unsigned int XCBGetRequestRead(XCBConnection *c)
334 {
335     unsigned int ret;
336     pthread_mutex_lock(&c->iolock);
337     /* FIXME: follow X meets Z architecture changes. */
338     _xcb_in_read(c);
339     ret = c->in.request_read;
340     pthread_mutex_unlock(&c->iolock);
341     return ret;
342 }
343
344 /* Private interface */
345
346 int _xcb_in_init(_xcb_in *in)
347 {
348     if(pthread_cond_init(&in->event_cond, 0))
349         return 0;
350     in->reading = 0;
351
352     in->queue_len = 0;
353
354     in->request_read = 0;
355
356     in->replies = _xcb_map_new();
357     in->readers = _xcb_list_new();
358     if(!in->replies || !in->readers)
359         return 0;
360
361     in->current_reply_tail = &in->current_reply;
362     in->events_tail = &in->events;
363     in->pending_replies_tail = &in->pending_replies;
364
365     return 1;
366 }
367
368 void _xcb_in_destroy(_xcb_in *in)
369 {
370     pthread_cond_destroy(&in->event_cond);
371     free_reply_list(in->current_reply);
372     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
373     _xcb_list_delete(in->readers, 0);
374     while(in->events)
375     {
376         struct event_list *e = in->events;
377         in->events = e->next;
378         free(e->event);
379         free(e);
380     }
381     while(in->pending_replies)
382     {
383         pending_reply *pend = in->pending_replies;
384         in->pending_replies = pend->next;
385         free(pend);
386     }
387 }
388
389 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
390 {
391     if(workaround != WORKAROUND_NONE || flags != 0)
392     {
393         pending_reply *pend = malloc(sizeof(pending_reply));
394         if(!pend)
395             return 0;
396         pend->request = request;
397         pend->workaround = workaround;
398         pend->flags = flags;
399         pend->next = 0;
400         *c->in.pending_replies_tail = pend;
401         c->in.pending_replies_tail = &pend->next;
402     }
403     return 1;
404 }
405
406 int _xcb_in_read(XCBConnection *c)
407 {
408     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
409     if(n > 0)
410         c->in.queue_len += n;
411     while(read_packet(c))
412         /* empty */;
413     return (n > 0) || (n < 0 && errno == EAGAIN);
414 }
415
416 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
417 {
418     int done = c->in.queue_len;
419     if(len < done)
420         done = len;
421
422     memcpy(buf, c->in.queue, done);
423     c->in.queue_len -= done;
424     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
425
426     if(len > done)
427     {
428         int ret = read_block(c->fd, (char *) buf + done, len - done);
429         if(ret <= 0)
430             return ret;
431     }
432
433     return len;
434 }