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