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