More return value changes.
[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 <stdio.h>
32 #include <errno.h>
33
34 #include "xcb.h"
35 #include "xcbext.h"
36 #include "xcbint.h"
37
38 typedef struct {
39     unsigned int request;
40     enum workarounds workaround;
41 } pending_reply;
42
43 typedef struct XCBReplyData {
44     unsigned int request;
45     void *data;
46     XCBGenericError **error;
47 } XCBReplyData;
48
49 static int match_request_error(const void *request, const void *data)
50 {
51     const XCBGenericError *e = data;
52     return e->response_type == 0 && e->sequence == ((*(unsigned int *) request) & 0xffff);
53 }
54
55 static int match_reply(const void *request, const void *data)
56 {
57     return ((XCBReplyData *) data)->request == *(unsigned int *) request;
58 }
59
60 static void wake_up_next_reader(XCBConnection *c)
61 {
62     XCBReplyData *cur = _xcb_list_peek_head(c->in.readers);
63     int pthreadret;
64     if(cur)
65         pthreadret = pthread_cond_signal(cur->data);
66     else
67         pthreadret = pthread_cond_signal(&c->in.event_cond);
68     assert(pthreadret == 0);
69 }
70
71 /* Public interface */
72
73 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
74 {
75     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
76     XCBReplyData reader;
77     void *ret = 0;
78     if(e)
79         *e = 0;
80
81     pthread_mutex_lock(&c->iolock);
82
83     /* If this request has not been written yet, write it. */
84     if((signed int) (c->out.request_written - request) < 0)
85         if(!_xcb_out_flush(c))
86             goto done; /* error */
87
88     if(_xcb_list_find(c->in.readers, match_reply, &request))
89         goto done; /* error */
90
91     if(e)
92     {
93         *e = _xcb_list_remove(c->in.events, match_request_error, &request);
94         if(*e)
95             goto done;
96     }
97
98     reader.request = request;
99     reader.data = &cond;
100     reader.error = e;
101     _xcb_list_append(c->in.readers, &reader);
102
103     /* If this request has not been read yet, wait for it. */
104     while(!(e && *e) && ((signed int) (c->in.request_read - request) < 0 ||
105             (c->in.request_read == request &&
106              _xcb_queue_is_empty(c->in.current_reply))))
107         if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
108             goto done;
109
110     if(c->in.request_read != request)
111     {
112         _xcb_queue *q = _xcb_map_get(c->in.replies, request);
113         if(q)
114         {
115             ret = _xcb_queue_dequeue(q);
116             if(_xcb_queue_is_empty(q))
117                 _xcb_queue_delete(_xcb_map_remove(c->in.replies, request), free);
118         }
119     }
120     else
121         ret = _xcb_queue_dequeue(c->in.current_reply);
122
123 done:
124     _xcb_list_remove(c->in.readers, match_reply, &request);
125     pthread_cond_destroy(&cond);
126
127     wake_up_next_reader(c);
128     pthread_mutex_unlock(&c->iolock);
129     return ret;
130 }
131
132 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
133 {
134     return XCBWaitForEvent(c);
135 }
136
137 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
138 {
139     XCBGenericEvent *ret;
140
141 #if XCBTRACEEVENT
142     fprintf(stderr, "Entering XCBWaitEvent\n");
143 #endif
144
145     pthread_mutex_lock(&c->iolock);
146     /* _xcb_list_remove_head returns 0 on empty list. */
147     while(!(ret = _xcb_queue_dequeue(c->in.events)))
148         if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
149             break;
150
151     wake_up_next_reader(c);
152     pthread_mutex_unlock(&c->iolock);
153
154 #if XCBTRACEEVENT
155     fprintf(stderr, "Leaving XCBWaitEvent, event type %d\n", ret ? ret->response_type : -1);
156 #endif
157
158     return ret;
159 }
160
161 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error)
162 {
163     XCBGenericEvent *ret = 0;
164     pthread_mutex_lock(&c->iolock);
165     if(error)
166         *error = 0;
167     /* FIXME: follow X meets Z architecture changes. */
168     if(_xcb_in_read(c) >= 0)
169         ret = _xcb_queue_dequeue(c->in.events);
170     else if(error)
171         *error = -1;
172     else
173     {
174         fprintf(stderr, "XCBPollForEvent: I/O error occured, but no handler provided.\n");
175         abort();
176     }
177     pthread_mutex_unlock(&c->iolock);
178     return ret;
179 }
180
181 unsigned int XCBGetRequestRead(XCBConnection *c)
182 {
183     unsigned int ret;
184     pthread_mutex_lock(&c->iolock);
185     /* FIXME: follow X meets Z architecture changes. */
186     _xcb_in_read(c);
187     ret = c->in.request_read;
188     pthread_mutex_unlock(&c->iolock);
189     return ret;
190 }
191
192 /* Private interface */
193
194 int _xcb_in_init(_xcb_in *in)
195 {
196     if(pthread_cond_init(&in->event_cond, 0))
197         return 0;
198     in->reading = 0;
199
200     in->queue_len = 0;
201
202     in->request_read = 0;
203     in->current_reply = _xcb_queue_new();
204
205     in->replies = _xcb_map_new();
206     in->events = _xcb_queue_new();
207     in->readers = _xcb_list_new();
208
209     in->pending_replies = _xcb_queue_new();
210     if(!in->current_reply || !in->replies || !in->events || !in->readers || !in->pending_replies)
211         return 0;
212
213     return 1;
214 }
215
216 void _xcb_in_destroy(_xcb_in *in)
217 {
218     pthread_cond_destroy(&in->event_cond);
219     _xcb_queue_delete(in->current_reply, free);
220     _xcb_map_delete(in->replies, free);
221     _xcb_queue_delete(in->events, free);
222     _xcb_list_delete(in->readers, 0);
223     _xcb_queue_delete(in->pending_replies, free);
224 }
225
226 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround)
227 {
228     if(workaround != WORKAROUND_NONE)
229     {
230         pending_reply *pend = malloc(sizeof(pending_reply));
231         if(!pend)
232             return 0;
233         pend->request = request;
234         pend->workaround = workaround;
235         if(!_xcb_queue_enqueue(c->in.pending_replies, pend))
236         {
237             free(pend);
238             return 0;
239         }
240     }
241     return 1;
242 }
243
244 int _xcb_in_read_packet(XCBConnection *c)
245 {
246     XCBGenericRep genrep;
247     int length = 32;
248     unsigned char *buf;
249
250     /* Wait for there to be enough data for us to read a whole packet */
251     if(c->in.queue_len < length)
252         return 0;
253
254     /* Get the response type, length, and sequence number. */
255     memcpy(&genrep, c->in.queue, sizeof(genrep));
256
257     /* Compute 32-bit sequence number of this packet. */
258     if((genrep.response_type & 0x7f) != KeymapNotify)
259     {
260         int lastread = c->in.request_read;
261         c->in.request_read = (lastread & 0xffff0000) | genrep.sequence;
262         if(c->in.request_read != lastread && !_xcb_queue_is_empty(c->in.current_reply))
263         {
264             _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
265             c->in.current_reply = _xcb_queue_new();
266         }
267         if(c->in.request_read < lastread)
268             c->in.request_read += 0x10000;
269     }
270
271     /* For reply packets, check that the entire packet is available. */
272     if(genrep.response_type == 1)
273     {
274         pending_reply *pend = _xcb_list_peek_head(c->in.pending_replies);
275         if(pend && pend->request == c->in.request_read)
276         {
277             switch(pend->workaround)
278             {
279             case WORKAROUND_NONE:
280                 break;
281             case WORKAROUND_GLX_GET_FB_CONFIGS_BUG:
282                 {
283                     CARD32 *p = (CARD32 *) c->in.queue;
284                     genrep.length = p[2] * p[3] * 2;
285                 }
286                 break;
287             }
288             free(_xcb_queue_dequeue(c->in.pending_replies));
289         }
290         length += genrep.length * 4;
291     }
292
293     buf = malloc(length);
294     if(!buf)
295         return 0;
296     if(_xcb_in_read_block(c, buf, length) <= 0)
297     {
298         free(buf);
299         return 0;
300     }
301
302     if(buf[0] == 1) /* response is a reply */
303     {
304         XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
305         _xcb_queue_enqueue(c->in.current_reply, buf);
306         if(reader)
307             pthread_cond_signal(reader->data);
308         return 1;
309     }
310
311     if(buf[0] == 0) /* response is an error */
312     {
313         XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read);
314         if(reader && reader->error)
315         {
316             *reader->error = (XCBGenericError *) buf;
317             pthread_cond_signal(reader->data);
318             return 1;
319         }
320     }
321
322     /* event, or error without a waiting reader */
323     _xcb_queue_enqueue(c->in.events, buf);
324     pthread_cond_signal(&c->in.event_cond);
325     return 1; /* I have something for you... */
326 }
327
328 int _xcb_in_read(XCBConnection *c)
329 {
330     int n = _xcb_readn(c->fd, c->in.queue, sizeof(c->in.queue), &c->in.queue_len);
331     if(n < 0 && errno == EAGAIN)
332         n = 1;
333     while(_xcb_in_read_packet(c) > 0)
334         /* empty */;
335     return n;
336 }
337
338 int _xcb_in_read_block(XCBConnection *c, void *buf, int len)
339 {
340     int done = c->in.queue_len;
341     if(len < done)
342         done = len;
343
344     memcpy(buf, c->in.queue, done);
345     c->in.queue_len -= done;
346     memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
347
348     if(len > done)
349     {
350         int ret = _xcb_read_block(c->fd, (char *) buf + done, len - done);
351         if(ret <= 0)
352             return ret;
353     }
354
355     return len;
356 }