Finish removing deprecated functions. Fixes build failure (oops).
[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 <sys/select.h>
34 #include <errno.h>
35
36 #include "xcb.h"
37 #include "xcbext.h"
38 #include "xcbint.h"
39
40 #define XCBError 0
41 #define XCBReply 1
42
43 struct event_list {
44     XCBGenericEvent *event;
45     struct event_list *next;
46 };
47
48 struct reply_list {
49     void *reply;
50     struct reply_list *next;
51 };
52
53 typedef struct pending_reply {
54     unsigned int request;
55     enum workarounds workaround;
56     int flags;
57     struct pending_reply *next;
58 } pending_reply;
59
60 typedef struct reader_list {
61     unsigned int request;
62     pthread_cond_t *data;
63     struct reader_list *next;
64 } reader_list;
65
66 static void wake_up_next_reader(XCBConnection *c)
67 {
68     int pthreadret;
69     if(c->in.readers)
70         pthreadret = pthread_cond_signal(c->in.readers->data);
71     else
72         pthreadret = pthread_cond_signal(&c->in.event_cond);
73     assert(pthreadret == 0);
74 }
75
76 static int read_packet(XCBConnection *c)
77 {
78     XCBGenericRep genrep;
79     int length = 32;
80     void *buf;
81     pending_reply *pend = 0;
82     struct event_list *event;
83
84     /* Wait for there to be enough data for us to read a whole packet */
85     if(c->in.queue_len < length)
86         return 0;
87
88     /* Get the response type, length, and sequence number. */
89     memcpy(&genrep, c->in.queue, sizeof(genrep));
90
91     /* Compute 32-bit sequence number of this packet. */
92     if((genrep.response_type & 0x7f) != XCBKeymapNotify)
93     {
94         unsigned int lastread = c->in.request_read;
95         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
96         if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
97             c->in.request_read += 0x10000;
98         if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
99             c->in.request_expected = c->in.request_read;
100
101         if(c->in.request_read != lastread)
102         {
103             if(c->in.current_reply)
104             {
105                 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
106                 c->in.current_reply = 0;
107                 c->in.current_reply_tail = &c->in.current_reply;
108             }
109             c->in.request_completed = c->in.request_read - 1;
110         }
111         if(genrep.response_type != XCBReply) /* error or event */
112             c->in.request_completed = c->in.request_read; /* XXX: does event/error imply no more replies? */
113
114         while(c->in.pending_replies && 
115               XCB_SEQUENCE_COMPARE (c->in.pending_replies->request, <=, c->in.request_completed))
116         {
117             pending_reply *oldpend = c->in.pending_replies;
118             c->in.pending_replies = oldpend->next;
119             if(!oldpend->next)
120                 c->in.pending_replies_tail = &c->in.pending_replies;
121             free(oldpend);
122         }
123     }
124
125     if(genrep.response_type == XCBError || genrep.response_type == XCBReply)
126     {
127         pend = c->in.pending_replies;
128         if(pend && pend->request != c->in.request_read)
129             pend = 0;
130     }
131
132     /* For reply packets, check that the entire packet is available. */
133     if(genrep.response_type == XCBReply)
134     {
135         if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
136         {
137             CARD32 *p = (CARD32 *) c->in.queue;
138             genrep.length = p[2] * p[3] * 2;
139         }
140         length += genrep.length * 4;
141     }
142
143     buf = malloc(length + (genrep.response_type == XCBReply ? 0 : sizeof(CARD32)));
144     if(!buf)
145         return 0;
146     if(_xcb_in_read_block(c, buf, length) <= 0)
147     {
148         free(buf);
149         return 0;
150     }
151     if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
152     {
153         free(buf);
154         return 1;
155     }
156
157     if(genrep.response_type != XCBReply)
158         ((XCBGenericEvent *) buf)->full_sequence = c->in.request_read;
159
160     /* reply, or checked error */
161     if( genrep.response_type == XCBReply ||
162        (genrep.response_type == XCBError && pend && (pend->flags & XCB_REQUEST_CHECKED)))
163     {
164         reader_list *reader;
165         struct reply_list *cur = malloc(sizeof(struct reply_list));
166         if(!cur)
167             return 0;
168         cur->reply = buf;
169         cur->next = 0;
170         *c->in.current_reply_tail = cur;
171         c->in.current_reply_tail = &cur->next;
172         for(reader = c->in.readers; 
173             reader && 
174             XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read);
175             reader = reader->next)
176         {
177             if(reader->request == c->in.request_read)
178             {
179                 pthread_cond_signal(reader->data);
180                 break;
181             }
182         }
183         return 1;
184     }
185
186     /* event, or unchecked error */
187     event = malloc(sizeof(struct event_list));
188     if(!event)
189     {
190         free(buf);
191         return 0;
192     }
193     event->event = buf;
194     event->next = 0;
195     *c->in.events_tail = event;
196     c->in.events_tail = &event->next;
197     pthread_cond_signal(&c->in.event_cond);
198     return 1; /* I have something for you... */
199 }
200
201 static XCBGenericEvent *get_event(XCBConnection *c)
202 {
203     struct event_list *cur = c->in.events;
204     XCBGenericEvent *ret;
205     if(!c->in.events)
206         return 0;
207     ret = cur->event;
208     c->in.events = cur->next;
209     if(!cur->next)
210         c->in.events_tail = &c->in.events;
211     free(cur);
212     return ret;
213 }
214
215 static void free_reply_list(struct reply_list *head)
216 {
217     while(head)
218     {
219         struct reply_list *cur = head;
220         head = cur->next;
221         free(cur->reply);
222         free(cur);
223     }
224 }
225
226 static int read_block(const int fd, void *buf, const size_t len)
227 {
228     int done = 0;
229     while(done < len)
230     {
231         int ret = read(fd, ((char *) buf) + done, len - done);
232         if(ret > 0)
233             done += ret;
234         if(ret < 0 && errno == EAGAIN)
235         {
236             fd_set fds;
237             FD_ZERO(&fds);
238             FD_SET(fd, &fds);
239             do {
240                 ret = select(fd + 1, &fds, 0, 0, 0);
241             } while (ret == -1 && errno == EINTR);
242         }
243         if(ret <= 0)
244             return ret;
245     }
246     return len;
247 }
248
249 static int poll_for_reply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
250 {
251     struct reply_list *head;
252
253     /* If an error occurred when issuing the request, fail immediately. */
254     if(!request)
255         head = 0;
256     /* We've read requests past the one we want, so if it has replies we have
257      * them all and they're in the replies map. */
258     else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
259     {
260         head = _xcb_map_remove(c->in.replies, request);
261         if(head && head->next)
262             _xcb_map_put(c->in.replies, request, head->next);
263     }
264     /* We're currently processing the responses to the request we want, and we
265      * have a reply ready to return. So just return it without blocking. */
266     else if(request == c->in.request_read && c->in.current_reply)
267     {
268         head = c->in.current_reply;
269         c->in.current_reply = head->next;
270         if(!head->next)
271             c->in.current_reply_tail = &c->in.current_reply;
272     }
273     /* We know this request can't have any more replies, and we've already
274      * established it doesn't have a reply now. Don't bother blocking. */
275     else if(request == c->in.request_completed)
276         head = 0;
277     /* We may have more replies on the way for this request: block until we're
278      * sure. */
279     else
280         return 0;
281
282     if(error)
283         *error = 0;
284     *reply = 0;
285
286     if(head)
287     {
288         if(((XCBGenericRep *) head->reply)->response_type == XCBError)
289         {
290             if(error)
291                 *error = head->reply;
292             else
293                 free(head->reply);
294         }
295         else
296             *reply = head->reply;
297
298         free(head);
299     }
300
301     return 1;
302 }
303
304 /* Public interface */
305
306 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
307 {
308     void *ret = 0;
309     if(e)
310         *e = 0;
311
312     pthread_mutex_lock(&c->iolock);
313
314     /* If this request has not been written yet, write it. */
315     if(_xcb_out_flush_to(c, request))
316     {
317         pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
318         reader_list reader;
319         reader_list **prev_reader;
320
321         for(prev_reader = &c->in.readers; 
322             *prev_reader && 
323             XCB_SEQUENCE_COMPARE ((*prev_reader)->request, <=, request);
324             prev_reader = &(*prev_reader)->next)
325         {
326             /* empty */;
327         }
328         reader.request = request;
329         reader.data = &cond;
330         reader.next = *prev_reader;
331         *prev_reader = &reader;
332
333         while(!poll_for_reply(c, request, &ret, e))
334             if(!_xcb_conn_wait(c, &cond, 0, 0))
335                 break;
336
337         for(prev_reader = &c->in.readers;
338             *prev_reader && 
339             XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
340             prev_reader = &(*prev_reader)->next)
341         {
342             if(*prev_reader == &reader)
343             {
344                 *prev_reader = (*prev_reader)->next;
345                 break;
346             }
347         }
348         pthread_cond_destroy(&cond);
349     }
350
351     wake_up_next_reader(c);
352     pthread_mutex_unlock(&c->iolock);
353     return ret;
354 }
355
356 int XCBPollForReply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
357 {
358     int ret;
359     assert(reply != 0);
360     pthread_mutex_lock(&c->iolock);
361     ret = poll_for_reply(c, request, reply, error);
362     pthread_mutex_unlock(&c->iolock);
363     return ret;
364 }
365
366 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
367 {
368     XCBGenericEvent *ret;
369     pthread_mutex_lock(&c->iolock);
370     /* get_event returns 0 on empty list. */
371     while(!(ret = get_event(c)))
372         if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
373             break;
374
375     wake_up_next_reader(c);
376     pthread_mutex_unlock(&c->iolock);
377     return ret;
378 }
379
380 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
381 {
382     XCBGenericEvent *ret = 0;
383     pthread_mutex_lock(&c->iolock);
384     if(error)
385         *error = 0;
386     /* FIXME: follow X meets Z architecture changes. */
387     if(_xcb_in_read(c))
388         ret = get_event(c);
389     else if(error)
390         *error = -1;
391     else
392     {
393         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
394         abort();
395     }
396     pthread_mutex_unlock(&c->iolock);
397     return ret;
398 }
399
400 XCBGenericError *XCBRequestCheck(XCBConnection *c, XCBVoidCookie cookie)
401 {
402     /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
403      * that would require factoring the locking out of XCBGetInputFocus,
404      * XCBGetInputFocusReply, and XCBWaitForReply. */
405     XCBGenericError *ret;
406     void *reply;
407     if(XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_expected)
408        && XCB_SEQUENCE_COMPARE(cookie.sequence,>,c->in.request_completed))
409     {
410         free(XCBGetInputFocusReply(c, XCBGetInputFocus(c), &ret));
411         assert(!ret);
412     }
413     reply = XCBWaitForReply(c, cookie.sequence, &ret);
414     assert(!reply);
415     return ret;
416 }
417
418 /* Private interface */
419
420 int _xcb_in_init(_xcb_in *in)
421 {
422     if(pthread_cond_init(&in->event_cond, 0))
423         return 0;
424     in->reading = 0;
425
426     in->queue_len = 0;
427
428     in->request_read = 0;
429     in->request_completed = 0;
430
431     in->replies = _xcb_map_new();
432     if(!in->replies)
433         return 0;
434
435     in->current_reply_tail = &in->current_reply;
436     in->events_tail = &in->events;
437     in->pending_replies_tail = &in->pending_replies;
438
439     return 1;
440 }
441
442 void _xcb_in_destroy(_xcb_in *in)
443 {
444     pthread_cond_destroy(&in->event_cond);
445     free_reply_list(in->current_reply);
446     _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
447     while(in->events)
448     {
449         struct event_list *e = in->events;
450         in->events = e->next;
451         free(e->event);
452         free(e);
453     }
454     while(in->pending_replies)
455     {
456         pending_reply *pend = in->pending_replies;
457         in->pending_replies = pend->next;
458         free(pend);
459     }
460 }
461
462 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround, int flags)
463 {
464     pending_reply *pend = malloc(sizeof(pending_reply));
465     assert(workaround != WORKAROUND_NONE || flags != 0);
466     if(!pend)
467         return 0;
468     pend->request = request;
469     pend->workaround = workaround;
470     pend->flags = flags;
471     pend->next = 0;
472     *c->in.pending_replies_tail = pend;
473     c->in.pending_replies_tail = &pend->next;
474     return 1;
475 }
476
477 int _xcb_in_read(XCBConnection *c)
478 {
479     int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
480     if(n > 0)
481         c->in.queue_len += n;
482     while(read_packet(c))
483         /* empty */;
484     return (n > 0) || (n < 0 && errno == EAGAIN);
485 }
486
487 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
488 {
489     int done = c->in.queue_len;
490     if(len < done)
491         done = len;
492
493     memcpy(buf, c->in.queue, done);
494     c->in.queue_len -= done;
495     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
496
497     if(len > done)
498     {
499         int ret = read_block(c->fd, (char *) buf + done, len - done);
500         if(ret <= 0)
501             return ret;
502     }
503
504     return len;
505 }