xcb_in.c revision b20a2039
1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2602e473dSmrg *
3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
4602e473dSmrg * copy of this software and associated documentation files (the "Software"),
5602e473dSmrg * to deal in the Software without restriction, including without limitation
6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the
8602e473dSmrg * Software is furnished to do so, subject to the following conditions:
9602e473dSmrg *
10602e473dSmrg * The above copyright notice and this permission notice shall be included in
11602e473dSmrg * all copies or substantial portions of the Software.
12602e473dSmrg *
13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19602e473dSmrg *
20602e473dSmrg * Except as contained in this notice, the names of the authors or their
21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the
22602e473dSmrg * sale, use or other dealings in this Software without prior written
23602e473dSmrg * authorization from the authors.
24602e473dSmrg */
25602e473dSmrg
26602e473dSmrg/* Stuff that reads stuff from the server. */
27602e473dSmrg
28602e473dSmrg#include <assert.h>
29602e473dSmrg#include <string.h>
30602e473dSmrg#include <stdlib.h>
31602e473dSmrg#include <unistd.h>
32602e473dSmrg#include <stdio.h>
33602e473dSmrg#include <errno.h>
34602e473dSmrg
35602e473dSmrg#include "xcb.h"
36602e473dSmrg#include "xcbext.h"
37602e473dSmrg#include "xcbint.h"
38602e473dSmrg#if USE_POLL
39602e473dSmrg#include <poll.h>
40602e473dSmrg#else
41602e473dSmrg#include <sys/select.h>
42602e473dSmrg#endif
43602e473dSmrg
44602e473dSmrg#define XCB_ERROR 0
45602e473dSmrg#define XCB_REPLY 1
46602e473dSmrg#define XCB_XGE_EVENT 35
47602e473dSmrg
48602e473dSmrgstruct event_list {
49602e473dSmrg    xcb_generic_event_t *event;
50602e473dSmrg    struct event_list *next;
51602e473dSmrg};
52602e473dSmrg
53602e473dSmrgstruct reply_list {
54602e473dSmrg    void *reply;
55602e473dSmrg    struct reply_list *next;
56602e473dSmrg};
57602e473dSmrg
58602e473dSmrgtypedef struct pending_reply {
59602e473dSmrg    uint64_t first_request;
60602e473dSmrg    uint64_t last_request;
61602e473dSmrg    enum workarounds workaround;
62602e473dSmrg    int flags;
63602e473dSmrg    struct pending_reply *next;
64602e473dSmrg} pending_reply;
65602e473dSmrg
66602e473dSmrgtypedef struct reader_list {
67602e473dSmrg    unsigned int request;
68602e473dSmrg    pthread_cond_t *data;
69602e473dSmrg    struct reader_list *next;
70602e473dSmrg} reader_list;
71602e473dSmrg
72602e473dSmrgstatic int read_packet(xcb_connection_t *c)
73602e473dSmrg{
74602e473dSmrg    xcb_generic_reply_t genrep;
75602e473dSmrg    int length = 32;
76602e473dSmrg    int eventlength = 0; /* length after first 32 bytes for GenericEvents */
77602e473dSmrg    void *buf;
78602e473dSmrg    pending_reply *pend = 0;
79602e473dSmrg    struct event_list *event;
80602e473dSmrg
81602e473dSmrg    /* Wait for there to be enough data for us to read a whole packet */
82602e473dSmrg    if(c->in.queue_len < length)
83602e473dSmrg        return 0;
84602e473dSmrg
85602e473dSmrg    /* Get the response type, length, and sequence number. */
86602e473dSmrg    memcpy(&genrep, c->in.queue, sizeof(genrep));
87602e473dSmrg
88602e473dSmrg    /* Compute 32-bit sequence number of this packet. */
89602e473dSmrg    if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
90602e473dSmrg    {
91602e473dSmrg        uint64_t lastread = c->in.request_read;
92602e473dSmrg        c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
93602e473dSmrg        if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
94602e473dSmrg            c->in.request_read += 0x10000;
95602e473dSmrg        if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
96602e473dSmrg            c->in.request_expected = c->in.request_read;
97602e473dSmrg
98602e473dSmrg        if(c->in.request_read != lastread)
99602e473dSmrg        {
100602e473dSmrg            if(c->in.current_reply)
101602e473dSmrg            {
102602e473dSmrg                _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
103602e473dSmrg                c->in.current_reply = 0;
104602e473dSmrg                c->in.current_reply_tail = &c->in.current_reply;
105602e473dSmrg            }
106602e473dSmrg            c->in.request_completed = c->in.request_read - 1;
107602e473dSmrg        }
108602e473dSmrg
109602e473dSmrg        while(c->in.pending_replies &&
110602e473dSmrg              c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
111602e473dSmrg	      XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
112602e473dSmrg        {
113602e473dSmrg            pending_reply *oldpend = c->in.pending_replies;
114602e473dSmrg            c->in.pending_replies = oldpend->next;
115602e473dSmrg            if(!oldpend->next)
116602e473dSmrg                c->in.pending_replies_tail = &c->in.pending_replies;
117602e473dSmrg            free(oldpend);
118602e473dSmrg        }
119602e473dSmrg
120602e473dSmrg        if(genrep.response_type == XCB_ERROR)
121602e473dSmrg            c->in.request_completed = c->in.request_read;
122602e473dSmrg    }
123602e473dSmrg
124602e473dSmrg    if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
125602e473dSmrg    {
126602e473dSmrg        pend = c->in.pending_replies;
127602e473dSmrg        if(pend &&
128602e473dSmrg           !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
129602e473dSmrg             (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
130602e473dSmrg              XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
131602e473dSmrg            pend = 0;
132602e473dSmrg    }
133602e473dSmrg
134602e473dSmrg    /* For reply packets, check that the entire packet is available. */
135602e473dSmrg    if(genrep.response_type == XCB_REPLY)
136602e473dSmrg    {
137602e473dSmrg        if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
138602e473dSmrg        {
139602e473dSmrg            uint32_t *p = (uint32_t *) c->in.queue;
140602e473dSmrg            genrep.length = p[2] * p[3] * 2;
141602e473dSmrg        }
142602e473dSmrg        length += genrep.length * 4;
143602e473dSmrg    }
144602e473dSmrg
145602e473dSmrg    /* XGE events may have sizes > 32 */
146602e473dSmrg    if (genrep.response_type == XCB_XGE_EVENT)
147b20a2039Smrg        eventlength = genrep.length * 4;
148602e473dSmrg
149602e473dSmrg    buf = malloc(length + eventlength +
150602e473dSmrg            (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
151602e473dSmrg    if(!buf)
152602e473dSmrg    {
153602e473dSmrg        _xcb_conn_shutdown(c);
154602e473dSmrg        return 0;
155602e473dSmrg    }
156602e473dSmrg
157602e473dSmrg    if(_xcb_in_read_block(c, buf, length) <= 0)
158602e473dSmrg    {
159602e473dSmrg        free(buf);
160602e473dSmrg        return 0;
161602e473dSmrg    }
162602e473dSmrg
163602e473dSmrg    /* pull in XGE event data if available, append after event struct */
164602e473dSmrg    if (eventlength)
165602e473dSmrg    {
166602e473dSmrg        if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
167602e473dSmrg        {
168602e473dSmrg            free(buf);
169602e473dSmrg            return 0;
170602e473dSmrg        }
171602e473dSmrg    }
172602e473dSmrg
173602e473dSmrg    if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
174602e473dSmrg    {
175602e473dSmrg        free(buf);
176602e473dSmrg        return 1;
177602e473dSmrg    }
178602e473dSmrg
179602e473dSmrg    if(genrep.response_type != XCB_REPLY)
180602e473dSmrg        ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
181602e473dSmrg
182602e473dSmrg    /* reply, or checked error */
183602e473dSmrg    if( genrep.response_type == XCB_REPLY ||
184602e473dSmrg       (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
185602e473dSmrg    {
186602e473dSmrg        reader_list *reader;
187602e473dSmrg        struct reply_list *cur = malloc(sizeof(struct reply_list));
188602e473dSmrg        if(!cur)
189602e473dSmrg        {
190602e473dSmrg            _xcb_conn_shutdown(c);
191602e473dSmrg            free(buf);
192602e473dSmrg            return 0;
193602e473dSmrg        }
194602e473dSmrg        cur->reply = buf;
195602e473dSmrg        cur->next = 0;
196602e473dSmrg        *c->in.current_reply_tail = cur;
197602e473dSmrg        c->in.current_reply_tail = &cur->next;
198602e473dSmrg        for(reader = c->in.readers;
199602e473dSmrg	    reader &&
200602e473dSmrg	    XCB_SEQUENCE_COMPARE_32(reader->request, <=, c->in.request_read);
201602e473dSmrg	    reader = reader->next)
202602e473dSmrg	{
203602e473dSmrg            if(XCB_SEQUENCE_COMPARE_32(reader->request, ==, c->in.request_read))
204602e473dSmrg            {
205602e473dSmrg                pthread_cond_signal(reader->data);
206602e473dSmrg                break;
207602e473dSmrg            }
208602e473dSmrg	}
209602e473dSmrg        return 1;
210602e473dSmrg    }
211602e473dSmrg
212602e473dSmrg    /* event, or unchecked error */
213602e473dSmrg    event = malloc(sizeof(struct event_list));
214602e473dSmrg    if(!event)
215602e473dSmrg    {
216602e473dSmrg        _xcb_conn_shutdown(c);
217602e473dSmrg        free(buf);
218602e473dSmrg        return 0;
219602e473dSmrg    }
220602e473dSmrg    event->event = buf;
221602e473dSmrg    event->next = 0;
222602e473dSmrg    *c->in.events_tail = event;
223602e473dSmrg    c->in.events_tail = &event->next;
224602e473dSmrg    pthread_cond_signal(&c->in.event_cond);
225602e473dSmrg    return 1; /* I have something for you... */
226602e473dSmrg}
227602e473dSmrg
228602e473dSmrgstatic xcb_generic_event_t *get_event(xcb_connection_t *c)
229602e473dSmrg{
230602e473dSmrg    struct event_list *cur = c->in.events;
231602e473dSmrg    xcb_generic_event_t *ret;
232602e473dSmrg    if(!c->in.events)
233602e473dSmrg        return 0;
234602e473dSmrg    ret = cur->event;
235602e473dSmrg    c->in.events = cur->next;
236602e473dSmrg    if(!cur->next)
237602e473dSmrg        c->in.events_tail = &c->in.events;
238602e473dSmrg    free(cur);
239602e473dSmrg    return ret;
240602e473dSmrg}
241602e473dSmrg
242602e473dSmrgstatic void free_reply_list(struct reply_list *head)
243602e473dSmrg{
244602e473dSmrg    while(head)
245602e473dSmrg    {
246602e473dSmrg        struct reply_list *cur = head;
247602e473dSmrg        head = cur->next;
248602e473dSmrg        free(cur->reply);
249602e473dSmrg        free(cur);
250602e473dSmrg    }
251602e473dSmrg}
252602e473dSmrg
253602e473dSmrgstatic int read_block(const int fd, void *buf, const ssize_t len)
254602e473dSmrg{
255602e473dSmrg    int done = 0;
256602e473dSmrg    while(done < len)
257602e473dSmrg    {
258602e473dSmrg        int ret = read(fd, ((char *) buf) + done, len - done);
259602e473dSmrg        if(ret > 0)
260602e473dSmrg            done += ret;
261602e473dSmrg        if(ret < 0 && errno == EAGAIN)
262602e473dSmrg        {
263602e473dSmrg#if USE_POLL
264602e473dSmrg            struct pollfd pfd;
265602e473dSmrg            pfd.fd = fd;
266602e473dSmrg            pfd.events = POLLIN;
267602e473dSmrg            pfd.revents = 0;
268602e473dSmrg            do {
269602e473dSmrg                ret = poll(&pfd, 1, -1);
270602e473dSmrg            } while (ret == -1 && errno == EINTR);
271602e473dSmrg#else
272602e473dSmrg            fd_set fds;
273602e473dSmrg            FD_ZERO(&fds);
274602e473dSmrg            FD_SET(fd, &fds);
275602e473dSmrg	    do {
276602e473dSmrg		ret = select(fd + 1, &fds, 0, 0, 0);
277602e473dSmrg	    } while (ret == -1 && errno == EINTR);
278602e473dSmrg#endif
279602e473dSmrg        }
280602e473dSmrg        if(ret <= 0)
281602e473dSmrg            return ret;
282602e473dSmrg    }
283602e473dSmrg    return len;
284602e473dSmrg}
285602e473dSmrg
286602e473dSmrgstatic int poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
287602e473dSmrg{
288602e473dSmrg    struct reply_list *head;
289602e473dSmrg
290602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
291602e473dSmrg    if(!request)
292602e473dSmrg        head = 0;
293602e473dSmrg    /* We've read requests past the one we want, so if it has replies we have
294602e473dSmrg     * them all and they're in the replies map. */
295602e473dSmrg    else if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
296602e473dSmrg    {
297602e473dSmrg        head = _xcb_map_remove(c->in.replies, request);
298602e473dSmrg        if(head && head->next)
299602e473dSmrg            _xcb_map_put(c->in.replies, request, head->next);
300602e473dSmrg    }
301602e473dSmrg    /* We're currently processing the responses to the request we want, and we
302602e473dSmrg     * have a reply ready to return. So just return it without blocking. */
303602e473dSmrg    else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
304602e473dSmrg    {
305602e473dSmrg        head = c->in.current_reply;
306602e473dSmrg        c->in.current_reply = head->next;
307602e473dSmrg        if(!head->next)
308602e473dSmrg            c->in.current_reply_tail = &c->in.current_reply;
309602e473dSmrg    }
310602e473dSmrg    /* We know this request can't have any more replies, and we've already
311602e473dSmrg     * established it doesn't have a reply now. Don't bother blocking. */
312602e473dSmrg    else if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_completed))
313602e473dSmrg        head = 0;
314602e473dSmrg    /* We may have more replies on the way for this request: block until we're
315602e473dSmrg     * sure. */
316602e473dSmrg    else
317602e473dSmrg        return 0;
318602e473dSmrg
319602e473dSmrg    if(error)
320602e473dSmrg        *error = 0;
321602e473dSmrg    *reply = 0;
322602e473dSmrg
323602e473dSmrg    if(head)
324602e473dSmrg    {
325602e473dSmrg        if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
326602e473dSmrg        {
327602e473dSmrg            if(error)
328602e473dSmrg                *error = head->reply;
329602e473dSmrg            else
330602e473dSmrg                free(head->reply);
331602e473dSmrg        }
332602e473dSmrg        else
333602e473dSmrg            *reply = head->reply;
334602e473dSmrg
335602e473dSmrg        free(head);
336602e473dSmrg    }
337602e473dSmrg
338602e473dSmrg    return 1;
339602e473dSmrg}
340602e473dSmrg
341602e473dSmrg/* Public interface */
342602e473dSmrg
343602e473dSmrgvoid *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
344602e473dSmrg{
345602e473dSmrg    uint64_t widened_request;
346602e473dSmrg    void *ret = 0;
347602e473dSmrg    if(e)
348602e473dSmrg        *e = 0;
349602e473dSmrg    if(c->has_error)
350602e473dSmrg        return 0;
351602e473dSmrg
352602e473dSmrg    pthread_mutex_lock(&c->iolock);
353602e473dSmrg
354602e473dSmrg    widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
355602e473dSmrg    if(widened_request > c->out.request)
356602e473dSmrg        widened_request -= UINT64_C(1) << 32;
357602e473dSmrg
358602e473dSmrg    /* If this request has not been written yet, write it. */
359602e473dSmrg    if(c->out.return_socket || _xcb_out_flush_to(c, widened_request))
360602e473dSmrg    {
361602e473dSmrg        pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
362602e473dSmrg        reader_list reader;
363602e473dSmrg        reader_list **prev_reader;
364602e473dSmrg
365602e473dSmrg        for(prev_reader = &c->in.readers;
366602e473dSmrg	    *prev_reader &&
367602e473dSmrg	    XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
368602e473dSmrg	    prev_reader = &(*prev_reader)->next)
369602e473dSmrg	{
370602e473dSmrg            /* empty */;
371602e473dSmrg	}
372602e473dSmrg        reader.request = request;
373602e473dSmrg        reader.data = &cond;
374602e473dSmrg        reader.next = *prev_reader;
375602e473dSmrg        *prev_reader = &reader;
376602e473dSmrg
377602e473dSmrg        while(!poll_for_reply(c, request, &ret, e))
378602e473dSmrg            if(!_xcb_conn_wait(c, &cond, 0, 0))
379602e473dSmrg                break;
380602e473dSmrg
381602e473dSmrg        for(prev_reader = &c->in.readers;
382602e473dSmrg	    *prev_reader &&
383602e473dSmrg	    XCB_SEQUENCE_COMPARE_32((*prev_reader)->request, <=, request);
384602e473dSmrg	    prev_reader = &(*prev_reader)->next)
385602e473dSmrg	{
386602e473dSmrg            if(*prev_reader == &reader)
387602e473dSmrg            {
388602e473dSmrg                *prev_reader = (*prev_reader)->next;
389602e473dSmrg                break;
390602e473dSmrg            }
391602e473dSmrg	}
392602e473dSmrg        pthread_cond_destroy(&cond);
393602e473dSmrg    }
394602e473dSmrg
395b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
396602e473dSmrg    pthread_mutex_unlock(&c->iolock);
397602e473dSmrg    return ret;
398602e473dSmrg}
399602e473dSmrg
400602e473dSmrgstatic void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
401602e473dSmrg{
402602e473dSmrg    pending_reply *pend;
403602e473dSmrg    pend = malloc(sizeof(*pend));
404602e473dSmrg    if(!pend)
405602e473dSmrg    {
406602e473dSmrg        _xcb_conn_shutdown(c);
407602e473dSmrg        return;
408602e473dSmrg    }
409602e473dSmrg
410602e473dSmrg    pend->first_request = seq;
411602e473dSmrg    pend->last_request = seq;
412602e473dSmrg    pend->workaround = 0;
413602e473dSmrg    pend->flags = XCB_REQUEST_DISCARD_REPLY;
414602e473dSmrg    pend->next = *prev_next;
415602e473dSmrg    *prev_next = pend;
416602e473dSmrg
417602e473dSmrg    if(!pend->next)
418602e473dSmrg        c->in.pending_replies_tail = &pend->next;
419602e473dSmrg}
420602e473dSmrg
421602e473dSmrgstatic void discard_reply(xcb_connection_t *c, unsigned int request)
422602e473dSmrg{
423602e473dSmrg    pending_reply *pend = 0;
424602e473dSmrg    pending_reply **prev_pend;
425602e473dSmrg    uint64_t widened_request;
426602e473dSmrg
427602e473dSmrg    /* We've read requests past the one we want, so if it has replies we have
428602e473dSmrg     * them all and they're in the replies map. */
429602e473dSmrg    if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read))
430602e473dSmrg    {
431602e473dSmrg        struct reply_list *head;
432602e473dSmrg        head = _xcb_map_remove(c->in.replies, request);
433602e473dSmrg        while (head)
434602e473dSmrg        {
435602e473dSmrg            struct reply_list *next = head->next;
436602e473dSmrg            free(head->reply);
437602e473dSmrg            free(head);
438602e473dSmrg            head = next;
439602e473dSmrg        }
440602e473dSmrg        return;
441602e473dSmrg    }
442602e473dSmrg
443602e473dSmrg    /* We're currently processing the responses to the request we want, and we
444602e473dSmrg     * have a reply ready to return. Free it, and mark the pend to free any further
445602e473dSmrg     * replies. */
446602e473dSmrg    if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply)
447602e473dSmrg    {
448602e473dSmrg        struct reply_list *head;
449602e473dSmrg        head = c->in.current_reply;
450602e473dSmrg        c->in.current_reply = NULL;
451602e473dSmrg        c->in.current_reply_tail = &c->in.current_reply;
452602e473dSmrg        while (head)
453602e473dSmrg        {
454602e473dSmrg            struct reply_list *next = head->next;
455602e473dSmrg            free(head->reply);
456602e473dSmrg            free(head);
457602e473dSmrg            head = next;
458602e473dSmrg        }
459602e473dSmrg
460602e473dSmrg        pend = c->in.pending_replies;
461602e473dSmrg        if(pend &&
462602e473dSmrg            !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
463602e473dSmrg             (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
464602e473dSmrg              XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
465602e473dSmrg            pend = 0;
466602e473dSmrg        if(pend)
467602e473dSmrg            pend->flags |= XCB_REQUEST_DISCARD_REPLY;
468602e473dSmrg        else
469602e473dSmrg            insert_pending_discard(c, &c->in.pending_replies, c->in.request_read);
470602e473dSmrg
471602e473dSmrg        return;
472602e473dSmrg    }
473602e473dSmrg
474602e473dSmrg    /* Walk the list of pending requests. Mark the first match for deletion. */
475602e473dSmrg    for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
476602e473dSmrg    {
477602e473dSmrg        if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request))
478602e473dSmrg            break;
479602e473dSmrg
480602e473dSmrg        if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request))
481602e473dSmrg        {
482602e473dSmrg            /* Pending reply found. Mark for discard: */
483602e473dSmrg            (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
484602e473dSmrg            return;
485602e473dSmrg        }
486602e473dSmrg    }
487602e473dSmrg
488602e473dSmrg    /* Pending reply not found (likely due to _unchecked request). Create one: */
489602e473dSmrg    widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
490602e473dSmrg    if(widened_request > c->out.request)
491602e473dSmrg        widened_request -= UINT64_C(1) << 32;
492602e473dSmrg
493602e473dSmrg    insert_pending_discard(c, prev_pend, widened_request);
494602e473dSmrg}
495602e473dSmrg
496602e473dSmrgvoid xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
497602e473dSmrg{
498602e473dSmrg    if(c->has_error)
499602e473dSmrg        return;
500602e473dSmrg
501602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
502602e473dSmrg    if(!sequence)
503602e473dSmrg        return;
504602e473dSmrg
505602e473dSmrg    pthread_mutex_lock(&c->iolock);
506602e473dSmrg    discard_reply(c, sequence);
507602e473dSmrg    pthread_mutex_unlock(&c->iolock);
508602e473dSmrg}
509602e473dSmrg
510602e473dSmrgint xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
511602e473dSmrg{
512602e473dSmrg    int ret;
513602e473dSmrg    if(c->has_error)
514602e473dSmrg    {
515602e473dSmrg        *reply = 0;
516602e473dSmrg        if(error)
517602e473dSmrg            *error = 0;
518602e473dSmrg        return 1; /* would not block */
519602e473dSmrg    }
520602e473dSmrg    assert(reply != 0);
521602e473dSmrg    pthread_mutex_lock(&c->iolock);
522602e473dSmrg    ret = poll_for_reply(c, request, reply, error);
523602e473dSmrg    pthread_mutex_unlock(&c->iolock);
524602e473dSmrg    return ret;
525602e473dSmrg}
526602e473dSmrg
527602e473dSmrgxcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
528602e473dSmrg{
529602e473dSmrg    xcb_generic_event_t *ret;
530602e473dSmrg    if(c->has_error)
531602e473dSmrg        return 0;
532602e473dSmrg    pthread_mutex_lock(&c->iolock);
533602e473dSmrg    /* get_event returns 0 on empty list. */
534602e473dSmrg    while(!(ret = get_event(c)))
535602e473dSmrg        if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
536602e473dSmrg            break;
537602e473dSmrg
538b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
539602e473dSmrg    pthread_mutex_unlock(&c->iolock);
540602e473dSmrg    return ret;
541602e473dSmrg}
542602e473dSmrg
543602e473dSmrgxcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
544602e473dSmrg{
545602e473dSmrg    xcb_generic_event_t *ret = 0;
546602e473dSmrg    if(!c->has_error)
547602e473dSmrg    {
548602e473dSmrg        pthread_mutex_lock(&c->iolock);
549602e473dSmrg        /* FIXME: follow X meets Z architecture changes. */
550602e473dSmrg        ret = get_event(c);
551602e473dSmrg        if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
552602e473dSmrg            ret = get_event(c);
553602e473dSmrg        pthread_mutex_unlock(&c->iolock);
554602e473dSmrg    }
555602e473dSmrg    return ret;
556602e473dSmrg}
557602e473dSmrg
558602e473dSmrgxcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
559602e473dSmrg{
560602e473dSmrg    /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
561602e473dSmrg     * that would require factoring the locking out of xcb_get_input_focus,
562602e473dSmrg     * xcb_get_input_focus_reply, and xcb_wait_for_reply. */
563602e473dSmrg    xcb_generic_error_t *ret;
564602e473dSmrg    void *reply;
565602e473dSmrg    if(c->has_error)
566602e473dSmrg        return 0;
567602e473dSmrg    if(XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_expected)
568602e473dSmrg       && XCB_SEQUENCE_COMPARE_32(cookie.sequence,>,c->in.request_completed))
569602e473dSmrg    {
570602e473dSmrg        free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), &ret));
571602e473dSmrg        assert(!ret);
572602e473dSmrg    }
573602e473dSmrg    reply = xcb_wait_for_reply(c, cookie.sequence, &ret);
574602e473dSmrg    assert(!reply);
575602e473dSmrg    return ret;
576602e473dSmrg}
577602e473dSmrg
578602e473dSmrg/* Private interface */
579602e473dSmrg
580602e473dSmrgint _xcb_in_init(_xcb_in *in)
581602e473dSmrg{
582602e473dSmrg    if(pthread_cond_init(&in->event_cond, 0))
583602e473dSmrg        return 0;
584602e473dSmrg    in->reading = 0;
585602e473dSmrg
586602e473dSmrg    in->queue_len = 0;
587602e473dSmrg
588602e473dSmrg    in->request_read = 0;
589602e473dSmrg    in->request_completed = 0;
590602e473dSmrg
591602e473dSmrg    in->replies = _xcb_map_new();
592602e473dSmrg    if(!in->replies)
593602e473dSmrg        return 0;
594602e473dSmrg
595602e473dSmrg    in->current_reply_tail = &in->current_reply;
596602e473dSmrg    in->events_tail = &in->events;
597602e473dSmrg    in->pending_replies_tail = &in->pending_replies;
598602e473dSmrg
599602e473dSmrg    return 1;
600602e473dSmrg}
601602e473dSmrg
602602e473dSmrgvoid _xcb_in_destroy(_xcb_in *in)
603602e473dSmrg{
604602e473dSmrg    pthread_cond_destroy(&in->event_cond);
605602e473dSmrg    free_reply_list(in->current_reply);
606602e473dSmrg    _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
607602e473dSmrg    while(in->events)
608602e473dSmrg    {
609602e473dSmrg        struct event_list *e = in->events;
610602e473dSmrg        in->events = e->next;
611602e473dSmrg        free(e->event);
612602e473dSmrg        free(e);
613602e473dSmrg    }
614602e473dSmrg    while(in->pending_replies)
615602e473dSmrg    {
616602e473dSmrg        pending_reply *pend = in->pending_replies;
617602e473dSmrg        in->pending_replies = pend->next;
618602e473dSmrg        free(pend);
619602e473dSmrg    }
620602e473dSmrg}
621602e473dSmrg
622b20a2039Smrgvoid _xcb_in_wake_up_next_reader(xcb_connection_t *c)
623b20a2039Smrg{
624b20a2039Smrg    int pthreadret;
625b20a2039Smrg    if(c->in.readers)
626b20a2039Smrg        pthreadret = pthread_cond_signal(c->in.readers->data);
627b20a2039Smrg    else
628b20a2039Smrg        pthreadret = pthread_cond_signal(&c->in.event_cond);
629b20a2039Smrg    assert(pthreadret == 0);
630b20a2039Smrg}
631b20a2039Smrg
632602e473dSmrgint _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
633602e473dSmrg{
634602e473dSmrg    pending_reply *pend = malloc(sizeof(pending_reply));
635602e473dSmrg    assert(workaround != WORKAROUND_NONE || flags != 0);
636602e473dSmrg    if(!pend)
637602e473dSmrg    {
638602e473dSmrg        _xcb_conn_shutdown(c);
639602e473dSmrg        return 0;
640602e473dSmrg    }
641602e473dSmrg    pend->first_request = pend->last_request = request;
642602e473dSmrg    pend->workaround = workaround;
643602e473dSmrg    pend->flags = flags;
644602e473dSmrg    pend->next = 0;
645602e473dSmrg    *c->in.pending_replies_tail = pend;
646602e473dSmrg    c->in.pending_replies_tail = &pend->next;
647602e473dSmrg    return 1;
648602e473dSmrg}
649602e473dSmrg
650602e473dSmrgvoid _xcb_in_replies_done(xcb_connection_t *c)
651602e473dSmrg{
652602e473dSmrg    struct pending_reply *pend;
653602e473dSmrg    if (c->in.pending_replies_tail != &c->in.pending_replies)
654602e473dSmrg    {
655602e473dSmrg        pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
656602e473dSmrg        if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
657602e473dSmrg        {
658602e473dSmrg            pend->last_request = c->out.request;
659602e473dSmrg            pend->workaround = WORKAROUND_NONE;
660602e473dSmrg        }
661602e473dSmrg    }
662602e473dSmrg}
663602e473dSmrg
664602e473dSmrgint _xcb_in_read(xcb_connection_t *c)
665602e473dSmrg{
666602e473dSmrg    int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
667602e473dSmrg    if(n > 0)
668602e473dSmrg        c->in.queue_len += n;
669602e473dSmrg    while(read_packet(c))
670602e473dSmrg        /* empty */;
671602e473dSmrg    if((n > 0) || (n < 0 && errno == EAGAIN))
672602e473dSmrg        return 1;
673602e473dSmrg    _xcb_conn_shutdown(c);
674602e473dSmrg    return 0;
675602e473dSmrg}
676602e473dSmrg
677602e473dSmrgint _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
678602e473dSmrg{
679602e473dSmrg    int done = c->in.queue_len;
680602e473dSmrg    if(len < done)
681602e473dSmrg        done = len;
682602e473dSmrg
683602e473dSmrg    memcpy(buf, c->in.queue, done);
684602e473dSmrg    c->in.queue_len -= done;
685602e473dSmrg    memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
686602e473dSmrg
687602e473dSmrg    if(len > done)
688602e473dSmrg    {
689602e473dSmrg        int ret = read_block(c->fd, (char *) buf + done, len - done);
690602e473dSmrg        if(ret <= 0)
691602e473dSmrg        {
692602e473dSmrg            _xcb_conn_shutdown(c);
693602e473dSmrg            return ret;
694602e473dSmrg        }
695602e473dSmrg    }
696602e473dSmrg
697602e473dSmrg    return len;
698602e473dSmrg}
699