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