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