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