xcb_in.c revision f1c62215
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
2821298544Smrg#ifdef HAVE_CONFIG_H
2921298544Smrg#include "config.h"
3021298544Smrg#endif
3121298544Smrg
32602e473dSmrg#include <assert.h>
33602e473dSmrg#include <string.h>
34602e473dSmrg#include <stdlib.h>
35602e473dSmrg#include <unistd.h>
36602e473dSmrg#include <stdio.h>
37602e473dSmrg#include <errno.h>
38602e473dSmrg
39602e473dSmrg#if USE_POLL
40602e473dSmrg#include <poll.h>
4121298544Smrg#endif
4221298544Smrg#ifndef _WIN32
43602e473dSmrg#include <sys/select.h>
4421298544Smrg#include <sys/socket.h>
45602e473dSmrg#endif
46602e473dSmrg
4721298544Smrg#ifdef _WIN32
4821298544Smrg#include "xcb_windefs.h"
4921298544Smrg#endif /* _WIN32 */
5021298544Smrg
5108e87a6cSwiz#include "xcb.h"
5208e87a6cSwiz#include "xcbext.h"
5308e87a6cSwiz#include "xcbint.h"
5408e87a6cSwiz
55602e473dSmrg#define XCB_ERROR 0
56602e473dSmrg#define XCB_REPLY 1
57602e473dSmrg#define XCB_XGE_EVENT 35
58602e473dSmrg
59602e473dSmrgstruct event_list {
60602e473dSmrg    xcb_generic_event_t *event;
61602e473dSmrg    struct event_list *next;
62602e473dSmrg};
63602e473dSmrg
642f94b0afSmrgstruct xcb_special_event {
652f94b0afSmrg
662f94b0afSmrg    struct xcb_special_event *next;
672f94b0afSmrg
682f94b0afSmrg    /* Match XGE events for the specific extension and event ID (the
692f94b0afSmrg     * first 32 bit word after evtype)
702f94b0afSmrg     */
712f94b0afSmrg    uint8_t     extension;
722f94b0afSmrg    uint32_t    eid;
732f94b0afSmrg    uint32_t    *stamp;
742f94b0afSmrg
752f94b0afSmrg    struct event_list   *events;
762f94b0afSmrg    struct event_list   **events_tail;
772f94b0afSmrg
782f94b0afSmrg    pthread_cond_t special_event_cond;
792f94b0afSmrg};
802f94b0afSmrg
81602e473dSmrgstruct reply_list {
82602e473dSmrg    void *reply;
83602e473dSmrg    struct reply_list *next;
84602e473dSmrg};
85602e473dSmrg
86602e473dSmrgtypedef struct pending_reply {
87602e473dSmrg    uint64_t first_request;
88602e473dSmrg    uint64_t last_request;
89602e473dSmrg    enum workarounds workaround;
90602e473dSmrg    int flags;
91602e473dSmrg    struct pending_reply *next;
92602e473dSmrg} pending_reply;
93602e473dSmrg
94602e473dSmrgtypedef struct reader_list {
9521298544Smrg    uint64_t request;
96602e473dSmrg    pthread_cond_t *data;
97602e473dSmrg    struct reader_list *next;
98602e473dSmrg} reader_list;
99602e473dSmrg
100f1c62215Smrgtypedef struct special_list {
101f1c62215Smrg    xcb_special_event_t *se;
102f1c62215Smrg    struct special_list *next;
103f1c62215Smrg} special_list;
104f1c62215Smrg
10521298544Smrgstatic void remove_finished_readers(reader_list **prev_reader, uint64_t completed)
10621298544Smrg{
10721298544Smrg    while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, completed))
10821298544Smrg    {
10921298544Smrg        /* If you don't have what you're looking for now, you never
11021298544Smrg         * will. Wake up and leave me alone. */
11121298544Smrg        pthread_cond_signal((*prev_reader)->data);
11221298544Smrg        *prev_reader = (*prev_reader)->next;
11321298544Smrg    }
11421298544Smrg}
11521298544Smrg
1162f94b0afSmrg#if HAVE_SENDMSG
1172f94b0afSmrgstatic int read_fds(xcb_connection_t *c, int *fds, int nfd)
1182f94b0afSmrg{
1192f94b0afSmrg    int *ifds = &c->in.in_fd.fd[c->in.in_fd.ifd];
1202f94b0afSmrg    int infd = c->in.in_fd.nfd - c->in.in_fd.ifd;
1212f94b0afSmrg
1222f94b0afSmrg    if (nfd > infd)
1232f94b0afSmrg        return 0;
1242f94b0afSmrg    memcpy(fds, ifds, nfd * sizeof (int));
1252f94b0afSmrg    c->in.in_fd.ifd += nfd;
1262f94b0afSmrg    return 1;
1272f94b0afSmrg}
1282f94b0afSmrg#endif
1292f94b0afSmrg
1302f94b0afSmrgtypedef struct xcb_ge_special_event_t {
1312f94b0afSmrg    uint8_t  response_type; /**<  */
1322f94b0afSmrg    uint8_t  extension; /**<  */
1332f94b0afSmrg    uint16_t sequence; /**<  */
1342f94b0afSmrg    uint32_t length; /**<  */
1352f94b0afSmrg    uint16_t evtype; /**<  */
1362f94b0afSmrg    uint8_t  pad0[2]; /**< */
1372f94b0afSmrg    uint32_t eid; /**< */
1382f94b0afSmrg    uint8_t  pad1[16]; /**<  */
1392f94b0afSmrg} xcb_ge_special_event_t;
1402f94b0afSmrg
1412f94b0afSmrgstatic int event_special(xcb_connection_t *c,
1422f94b0afSmrg                         struct event_list *event)
1432f94b0afSmrg{
1442f94b0afSmrg    struct xcb_special_event *special_event;
1452f94b0afSmrg    struct xcb_ge_special_event_t *ges = (void *) event->event;
1462f94b0afSmrg
1472f94b0afSmrg    /* Special events are always XGE events */
1482f94b0afSmrg    if ((ges->response_type & 0x7f) != XCB_XGE_EVENT)
1492f94b0afSmrg        return 0;
1502f94b0afSmrg
1512f94b0afSmrg    for (special_event = c->in.special_events;
1522f94b0afSmrg         special_event;
1532f94b0afSmrg         special_event = special_event->next)
1542f94b0afSmrg    {
1552f94b0afSmrg        if (ges->extension == special_event->extension &&
1562f94b0afSmrg            ges->eid == special_event->eid)
1572f94b0afSmrg        {
1582f94b0afSmrg            *special_event->events_tail = event;
1592f94b0afSmrg            special_event->events_tail = &event->next;
1602f94b0afSmrg            if (special_event->stamp)
1612f94b0afSmrg                ++(*special_event->stamp);
1622f94b0afSmrg            pthread_cond_signal(&special_event->special_event_cond);
1632f94b0afSmrg            return 1;
1642f94b0afSmrg        }
1652f94b0afSmrg    }
1662f94b0afSmrg
1672f94b0afSmrg    return 0;
1682f94b0afSmrg}
1692f94b0afSmrg
170602e473dSmrgstatic int read_packet(xcb_connection_t *c)
171602e473dSmrg{
172602e473dSmrg    xcb_generic_reply_t genrep;
173190694daSmrg    uint64_t length = 32;
174190694daSmrg    uint64_t eventlength = 0; /* length after first 32 bytes for GenericEvents */
1752f94b0afSmrg    int nfd = 0;         /* Number of file descriptors attached to the reply */
176190694daSmrg    uint64_t bufsize;
177602e473dSmrg    void *buf;
178602e473dSmrg    pending_reply *pend = 0;
179602e473dSmrg    struct event_list *event;
180602e473dSmrg
181602e473dSmrg    /* Wait for there to be enough data for us to read a whole packet */
182602e473dSmrg    if(c->in.queue_len < length)
183602e473dSmrg        return 0;
184602e473dSmrg
185602e473dSmrg    /* Get the response type, length, and sequence number. */
186602e473dSmrg    memcpy(&genrep, c->in.queue, sizeof(genrep));
187602e473dSmrg
188602e473dSmrg    /* Compute 32-bit sequence number of this packet. */
189602e473dSmrg    if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
190602e473dSmrg    {
191602e473dSmrg        uint64_t lastread = c->in.request_read;
192602e473dSmrg        c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
193602e473dSmrg        if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
194602e473dSmrg            c->in.request_read += 0x10000;
195602e473dSmrg        if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
196602e473dSmrg            c->in.request_expected = c->in.request_read;
197602e473dSmrg
198602e473dSmrg        if(c->in.request_read != lastread)
199602e473dSmrg        {
200602e473dSmrg            if(c->in.current_reply)
201602e473dSmrg            {
202602e473dSmrg                _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
203602e473dSmrg                c->in.current_reply = 0;
204602e473dSmrg                c->in.current_reply_tail = &c->in.current_reply;
205602e473dSmrg            }
206602e473dSmrg            c->in.request_completed = c->in.request_read - 1;
207602e473dSmrg        }
208602e473dSmrg
20908e87a6cSwiz        while(c->in.pending_replies &&
210602e473dSmrg              c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
21108e87a6cSwiz              XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
212602e473dSmrg        {
213602e473dSmrg            pending_reply *oldpend = c->in.pending_replies;
214602e473dSmrg            c->in.pending_replies = oldpend->next;
215602e473dSmrg            if(!oldpend->next)
216602e473dSmrg                c->in.pending_replies_tail = &c->in.pending_replies;
217602e473dSmrg            free(oldpend);
218602e473dSmrg        }
219602e473dSmrg
220602e473dSmrg        if(genrep.response_type == XCB_ERROR)
221602e473dSmrg            c->in.request_completed = c->in.request_read;
22221298544Smrg
22321298544Smrg        remove_finished_readers(&c->in.readers, c->in.request_completed);
224602e473dSmrg    }
225602e473dSmrg
226602e473dSmrg    if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
227602e473dSmrg    {
228602e473dSmrg        pend = c->in.pending_replies;
229602e473dSmrg        if(pend &&
230602e473dSmrg           !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
231602e473dSmrg             (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
232602e473dSmrg              XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
233602e473dSmrg            pend = 0;
234602e473dSmrg    }
235602e473dSmrg
236602e473dSmrg    /* For reply packets, check that the entire packet is available. */
237602e473dSmrg    if(genrep.response_type == XCB_REPLY)
238602e473dSmrg    {
239602e473dSmrg        if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
240602e473dSmrg        {
241602e473dSmrg            uint32_t *p = (uint32_t *) c->in.queue;
242602e473dSmrg            genrep.length = p[2] * p[3] * 2;
243602e473dSmrg        }
244602e473dSmrg        length += genrep.length * 4;
2452f94b0afSmrg
2462f94b0afSmrg        /* XXX a bit of a hack -- we "know" that all FD replys place
2472f94b0afSmrg         * the number of fds in the pad0 byte */
2482f94b0afSmrg        if (pend && pend->flags & XCB_REQUEST_REPLY_FDS)
2492f94b0afSmrg            nfd = genrep.pad0;
250602e473dSmrg    }
251602e473dSmrg
252602e473dSmrg    /* XGE events may have sizes > 32 */
25321298544Smrg    if ((genrep.response_type & 0x7f) == XCB_XGE_EVENT)
254b20a2039Smrg        eventlength = genrep.length * 4;
255602e473dSmrg
2562f94b0afSmrg    bufsize = length + eventlength + nfd * sizeof(int)  +
257190694daSmrg        (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t));
258190694daSmrg    if (bufsize < INT32_MAX)
259190694daSmrg        buf = malloc((size_t) bufsize);
260190694daSmrg    else
261190694daSmrg        buf = NULL;
262602e473dSmrg    if(!buf)
263602e473dSmrg    {
26421298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
265602e473dSmrg        return 0;
266602e473dSmrg    }
267602e473dSmrg
268602e473dSmrg    if(_xcb_in_read_block(c, buf, length) <= 0)
269602e473dSmrg    {
270602e473dSmrg        free(buf);
271602e473dSmrg        return 0;
272602e473dSmrg    }
273602e473dSmrg
274602e473dSmrg    /* pull in XGE event data if available, append after event struct */
275602e473dSmrg    if (eventlength)
276602e473dSmrg    {
277602e473dSmrg        if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
278602e473dSmrg        {
279602e473dSmrg            free(buf);
280602e473dSmrg            return 0;
281602e473dSmrg        }
282602e473dSmrg    }
283602e473dSmrg
2842f94b0afSmrg#if HAVE_SENDMSG
2852f94b0afSmrg    if (nfd)
2862f94b0afSmrg    {
2872f94b0afSmrg        if (!read_fds(c, (int *) &((char *) buf)[length], nfd))
2882f94b0afSmrg        {
2892f94b0afSmrg            free(buf);
2902f94b0afSmrg            return 0;
2912f94b0afSmrg        }
2922f94b0afSmrg    }
2932f94b0afSmrg#endif
2942f94b0afSmrg
295602e473dSmrg    if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
296602e473dSmrg    {
297602e473dSmrg        free(buf);
298602e473dSmrg        return 1;
299602e473dSmrg    }
300602e473dSmrg
301602e473dSmrg    if(genrep.response_type != XCB_REPLY)
302602e473dSmrg        ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
303602e473dSmrg
304602e473dSmrg    /* reply, or checked error */
305602e473dSmrg    if( genrep.response_type == XCB_REPLY ||
306602e473dSmrg       (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
307602e473dSmrg    {
308602e473dSmrg        struct reply_list *cur = malloc(sizeof(struct reply_list));
309602e473dSmrg        if(!cur)
310602e473dSmrg        {
31121298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
312602e473dSmrg            free(buf);
313602e473dSmrg            return 0;
314602e473dSmrg        }
315602e473dSmrg        cur->reply = buf;
316602e473dSmrg        cur->next = 0;
317602e473dSmrg        *c->in.current_reply_tail = cur;
318602e473dSmrg        c->in.current_reply_tail = &cur->next;
31921298544Smrg        if(c->in.readers && c->in.readers->request == c->in.request_read)
32021298544Smrg            pthread_cond_signal(c->in.readers->data);
321602e473dSmrg        return 1;
322602e473dSmrg    }
323602e473dSmrg
324602e473dSmrg    /* event, or unchecked error */
325602e473dSmrg    event = malloc(sizeof(struct event_list));
326602e473dSmrg    if(!event)
327602e473dSmrg    {
32821298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
329602e473dSmrg        free(buf);
330602e473dSmrg        return 0;
331602e473dSmrg    }
332602e473dSmrg    event->event = buf;
333602e473dSmrg    event->next = 0;
3342f94b0afSmrg
3352f94b0afSmrg    if (!event_special(c, event)) {
3362f94b0afSmrg        *c->in.events_tail = event;
3372f94b0afSmrg        c->in.events_tail = &event->next;
3382f94b0afSmrg        pthread_cond_signal(&c->in.event_cond);
3392f94b0afSmrg    }
340602e473dSmrg    return 1; /* I have something for you... */
341602e473dSmrg}
342602e473dSmrg
343602e473dSmrgstatic xcb_generic_event_t *get_event(xcb_connection_t *c)
344602e473dSmrg{
345602e473dSmrg    struct event_list *cur = c->in.events;
346602e473dSmrg    xcb_generic_event_t *ret;
347602e473dSmrg    if(!c->in.events)
348602e473dSmrg        return 0;
349602e473dSmrg    ret = cur->event;
350602e473dSmrg    c->in.events = cur->next;
351602e473dSmrg    if(!cur->next)
352602e473dSmrg        c->in.events_tail = &c->in.events;
353602e473dSmrg    free(cur);
354602e473dSmrg    return ret;
355602e473dSmrg}
356602e473dSmrg
357602e473dSmrgstatic void free_reply_list(struct reply_list *head)
358602e473dSmrg{
359602e473dSmrg    while(head)
360602e473dSmrg    {
361602e473dSmrg        struct reply_list *cur = head;
362602e473dSmrg        head = cur->next;
363602e473dSmrg        free(cur->reply);
364602e473dSmrg        free(cur);
365602e473dSmrg    }
366602e473dSmrg}
367602e473dSmrg
368602e473dSmrgstatic int read_block(const int fd, void *buf, const ssize_t len)
369602e473dSmrg{
370602e473dSmrg    int done = 0;
371602e473dSmrg    while(done < len)
372602e473dSmrg    {
37321298544Smrg        int ret = recv(fd, ((char *) buf) + done, len - done, 0);
374602e473dSmrg        if(ret > 0)
375602e473dSmrg            done += ret;
37621298544Smrg#ifndef _WIN32
377602e473dSmrg        if(ret < 0 && errno == EAGAIN)
37821298544Smrg#else
37921298544Smrg        if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
38021298544Smrg#endif /* !_Win32 */
381602e473dSmrg        {
382602e473dSmrg#if USE_POLL
383602e473dSmrg            struct pollfd pfd;
384602e473dSmrg            pfd.fd = fd;
385602e473dSmrg            pfd.events = POLLIN;
386602e473dSmrg            pfd.revents = 0;
387602e473dSmrg            do {
388602e473dSmrg                ret = poll(&pfd, 1, -1);
389602e473dSmrg            } while (ret == -1 && errno == EINTR);
390602e473dSmrg#else
391602e473dSmrg            fd_set fds;
392602e473dSmrg            FD_ZERO(&fds);
393602e473dSmrg            FD_SET(fd, &fds);
39421298544Smrg
39508e87a6cSwiz            /* Initializing errno here makes sure that for Win32 this loop will execute only once */
39608e87a6cSwiz            errno = 0;
39708e87a6cSwiz            do {
39808e87a6cSwiz                ret = select(fd + 1, &fds, 0, 0, 0);
39908e87a6cSwiz            } while (ret == -1 && errno == EINTR);
40021298544Smrg#endif /* USE_POLL */
401602e473dSmrg        }
402602e473dSmrg        if(ret <= 0)
403602e473dSmrg            return ret;
404602e473dSmrg    }
405602e473dSmrg    return len;
406602e473dSmrg}
407602e473dSmrg
40821298544Smrgstatic int poll_for_reply(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
409602e473dSmrg{
410602e473dSmrg    struct reply_list *head;
411602e473dSmrg
412602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
413602e473dSmrg    if(!request)
414602e473dSmrg        head = 0;
415602e473dSmrg    /* We've read requests past the one we want, so if it has replies we have
416602e473dSmrg     * them all and they're in the replies map. */
41721298544Smrg    else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
418602e473dSmrg    {
419602e473dSmrg        head = _xcb_map_remove(c->in.replies, request);
420602e473dSmrg        if(head && head->next)
421602e473dSmrg            _xcb_map_put(c->in.replies, request, head->next);
422602e473dSmrg    }
423602e473dSmrg    /* We're currently processing the responses to the request we want, and we
424602e473dSmrg     * have a reply ready to return. So just return it without blocking. */
42521298544Smrg    else if(request == c->in.request_read && c->in.current_reply)
426602e473dSmrg    {
427602e473dSmrg        head = c->in.current_reply;
428602e473dSmrg        c->in.current_reply = head->next;
429602e473dSmrg        if(!head->next)
430602e473dSmrg            c->in.current_reply_tail = &c->in.current_reply;
431602e473dSmrg    }
432602e473dSmrg    /* We know this request can't have any more replies, and we've already
433602e473dSmrg     * established it doesn't have a reply now. Don't bother blocking. */
43421298544Smrg    else if(request == c->in.request_completed)
435602e473dSmrg        head = 0;
436602e473dSmrg    /* We may have more replies on the way for this request: block until we're
437602e473dSmrg     * sure. */
438602e473dSmrg    else
439602e473dSmrg        return 0;
440602e473dSmrg
441602e473dSmrg    if(error)
442602e473dSmrg        *error = 0;
443602e473dSmrg    *reply = 0;
444602e473dSmrg
445602e473dSmrg    if(head)
446602e473dSmrg    {
447602e473dSmrg        if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
448602e473dSmrg        {
449602e473dSmrg            if(error)
450602e473dSmrg                *error = head->reply;
451602e473dSmrg            else
452602e473dSmrg                free(head->reply);
453602e473dSmrg        }
454602e473dSmrg        else
455602e473dSmrg            *reply = head->reply;
456602e473dSmrg
457602e473dSmrg        free(head);
458602e473dSmrg    }
459602e473dSmrg
460602e473dSmrg    return 1;
461602e473dSmrg}
462602e473dSmrg
46321298544Smrgstatic void insert_reader(reader_list **prev_reader, reader_list *reader, uint64_t request, pthread_cond_t *cond)
464602e473dSmrg{
46521298544Smrg    while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request))
46621298544Smrg        prev_reader = &(*prev_reader)->next;
46721298544Smrg    reader->request = request;
46821298544Smrg    reader->data = cond;
46921298544Smrg    reader->next = *prev_reader;
47021298544Smrg    *prev_reader = reader;
47121298544Smrg}
472602e473dSmrg
47321298544Smrgstatic void remove_reader(reader_list **prev_reader, reader_list *reader)
47421298544Smrg{
47521298544Smrg    while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, reader->request))
47621298544Smrg        if(*prev_reader == reader)
47721298544Smrg        {
47821298544Smrg            *prev_reader = (*prev_reader)->next;
47921298544Smrg            break;
48021298544Smrg        }
48121298544Smrg}
482602e473dSmrg
483f1c62215Smrgstatic void insert_special(special_list **prev_special, special_list *special, xcb_special_event_t *se)
484f1c62215Smrg{
485f1c62215Smrg    special->se = se;
486f1c62215Smrg    special->next = *prev_special;
487f1c62215Smrg    *prev_special = special;
488f1c62215Smrg}
489f1c62215Smrg
490f1c62215Smrgstatic void remove_special(special_list **prev_special, special_list *special)
491f1c62215Smrg{
492f1c62215Smrg    while(*prev_special)
493f1c62215Smrg    {
494f1c62215Smrg        if(*prev_special == special)
495f1c62215Smrg        {
496f1c62215Smrg            *prev_special = (*prev_special)->next;
497f1c62215Smrg            break;
498f1c62215Smrg        }
499f1c62215Smrg        prev_special = &(*prev_special)->next;
500f1c62215Smrg    }
501f1c62215Smrg}
502f1c62215Smrg
50321298544Smrgstatic void *wait_for_reply(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
50421298544Smrg{
50521298544Smrg    void *ret = 0;
506602e473dSmrg
507602e473dSmrg    /* If this request has not been written yet, write it. */
50821298544Smrg    if(c->out.return_socket || _xcb_out_flush_to(c, request))
509602e473dSmrg    {
510602e473dSmrg        pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
511602e473dSmrg        reader_list reader;
51221298544Smrg
51321298544Smrg        insert_reader(&c->in.readers, &reader, request, &cond);
514602e473dSmrg
515602e473dSmrg        while(!poll_for_reply(c, request, &ret, e))
516602e473dSmrg            if(!_xcb_conn_wait(c, &cond, 0, 0))
517602e473dSmrg                break;
518602e473dSmrg
51921298544Smrg        remove_reader(&c->in.readers, &reader);
520602e473dSmrg        pthread_cond_destroy(&cond);
521602e473dSmrg    }
522602e473dSmrg
523b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
52421298544Smrg    return ret;
52521298544Smrg}
52621298544Smrg
52721298544Smrgstatic uint64_t widen(xcb_connection_t *c, unsigned int request)
52821298544Smrg{
52921298544Smrg    uint64_t widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
53021298544Smrg    if(widened_request > c->out.request)
53121298544Smrg        widened_request -= UINT64_C(1) << 32;
53221298544Smrg    return widened_request;
53321298544Smrg}
53421298544Smrg
53521298544Smrg/* Public interface */
53621298544Smrg
53721298544Smrgvoid *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
53821298544Smrg{
53921298544Smrg    void *ret;
54021298544Smrg    if(e)
54121298544Smrg        *e = 0;
54221298544Smrg    if(c->has_error)
54321298544Smrg        return 0;
54421298544Smrg
54521298544Smrg    pthread_mutex_lock(&c->iolock);
54621298544Smrg    ret = wait_for_reply(c, widen(c, request), e);
547602e473dSmrg    pthread_mutex_unlock(&c->iolock);
548602e473dSmrg    return ret;
549602e473dSmrg}
550602e473dSmrg
551f1c62215Smrgvoid *xcb_wait_for_reply64(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
552f1c62215Smrg{
553f1c62215Smrg    void *ret;
554f1c62215Smrg    if(e)
555f1c62215Smrg        *e = 0;
556f1c62215Smrg    if(c->has_error)
557f1c62215Smrg        return 0;
558f1c62215Smrg
559f1c62215Smrg    pthread_mutex_lock(&c->iolock);
560f1c62215Smrg    ret = wait_for_reply(c, request, e);
561f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
562f1c62215Smrg    return ret;
563f1c62215Smrg}
564f1c62215Smrg
5652f94b0afSmrgint *xcb_get_reply_fds(xcb_connection_t *c, void *reply, size_t reply_size)
5662f94b0afSmrg{
5672f94b0afSmrg    return (int *) (&((char *) reply)[reply_size]);
5682f94b0afSmrg}
5692f94b0afSmrg
570602e473dSmrgstatic void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
571602e473dSmrg{
572602e473dSmrg    pending_reply *pend;
573602e473dSmrg    pend = malloc(sizeof(*pend));
574602e473dSmrg    if(!pend)
575602e473dSmrg    {
57621298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
577602e473dSmrg        return;
578602e473dSmrg    }
579602e473dSmrg
580602e473dSmrg    pend->first_request = seq;
581602e473dSmrg    pend->last_request = seq;
582602e473dSmrg    pend->workaround = 0;
583602e473dSmrg    pend->flags = XCB_REQUEST_DISCARD_REPLY;
584602e473dSmrg    pend->next = *prev_next;
585602e473dSmrg    *prev_next = pend;
586602e473dSmrg
587602e473dSmrg    if(!pend->next)
588602e473dSmrg        c->in.pending_replies_tail = &pend->next;
589602e473dSmrg}
590602e473dSmrg
59121298544Smrgstatic void discard_reply(xcb_connection_t *c, uint64_t request)
592602e473dSmrg{
59321298544Smrg    void *reply;
594602e473dSmrg    pending_reply **prev_pend;
595602e473dSmrg
59621298544Smrg    /* Free any replies or errors that we've already read. Stop if
59721298544Smrg     * xcb_wait_for_reply would block or we've run out of replies. */
59821298544Smrg    while(poll_for_reply(c, request, &reply, 0) && reply)
59921298544Smrg        free(reply);
600602e473dSmrg
60121298544Smrg    /* If we've proven there are no more responses coming, we're done. */
60221298544Smrg    if(XCB_SEQUENCE_COMPARE(request, <=, c->in.request_completed))
603602e473dSmrg        return;
604602e473dSmrg
605602e473dSmrg    /* Walk the list of pending requests. Mark the first match for deletion. */
606602e473dSmrg    for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
607602e473dSmrg    {
60821298544Smrg        if(XCB_SEQUENCE_COMPARE((*prev_pend)->first_request, >, request))
609602e473dSmrg            break;
610602e473dSmrg
61121298544Smrg        if((*prev_pend)->first_request == request)
612602e473dSmrg        {
613602e473dSmrg            /* Pending reply found. Mark for discard: */
614602e473dSmrg            (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
615602e473dSmrg            return;
616602e473dSmrg        }
617602e473dSmrg    }
618602e473dSmrg
619602e473dSmrg    /* Pending reply not found (likely due to _unchecked request). Create one: */
62021298544Smrg    insert_pending_discard(c, prev_pend, request);
621602e473dSmrg}
622602e473dSmrg
623602e473dSmrgvoid xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
624602e473dSmrg{
625602e473dSmrg    if(c->has_error)
626602e473dSmrg        return;
627602e473dSmrg
628602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
629602e473dSmrg    if(!sequence)
630602e473dSmrg        return;
631602e473dSmrg
632602e473dSmrg    pthread_mutex_lock(&c->iolock);
63321298544Smrg    discard_reply(c, widen(c, sequence));
634602e473dSmrg    pthread_mutex_unlock(&c->iolock);
635602e473dSmrg}
636602e473dSmrg
637f1c62215Smrgvoid xcb_discard_reply64(xcb_connection_t *c, uint64_t sequence)
638f1c62215Smrg{
639f1c62215Smrg    if(c->has_error)
640f1c62215Smrg        return;
641f1c62215Smrg
642f1c62215Smrg    /* If an error occurred when issuing the request, fail immediately. */
643f1c62215Smrg    if(!sequence)
644f1c62215Smrg        return;
645f1c62215Smrg
646f1c62215Smrg    pthread_mutex_lock(&c->iolock);
647f1c62215Smrg    discard_reply(c, sequence);
648f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
649f1c62215Smrg}
650f1c62215Smrg
651602e473dSmrgint xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
652602e473dSmrg{
653602e473dSmrg    int ret;
654602e473dSmrg    if(c->has_error)
655602e473dSmrg    {
656602e473dSmrg        *reply = 0;
657602e473dSmrg        if(error)
658602e473dSmrg            *error = 0;
659602e473dSmrg        return 1; /* would not block */
660602e473dSmrg    }
661602e473dSmrg    assert(reply != 0);
662602e473dSmrg    pthread_mutex_lock(&c->iolock);
66321298544Smrg    ret = poll_for_reply(c, widen(c, request), reply, error);
664602e473dSmrg    pthread_mutex_unlock(&c->iolock);
665602e473dSmrg    return ret;
666602e473dSmrg}
667602e473dSmrg
668f1c62215Smrgint xcb_poll_for_reply64(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
669f1c62215Smrg{
670f1c62215Smrg    int ret;
671f1c62215Smrg    if(c->has_error)
672f1c62215Smrg    {
673f1c62215Smrg        *reply = 0;
674f1c62215Smrg        if(error)
675f1c62215Smrg            *error = 0;
676f1c62215Smrg        return 1; /* would not block */
677f1c62215Smrg    }
678f1c62215Smrg    assert(reply != 0);
679f1c62215Smrg    pthread_mutex_lock(&c->iolock);
680f1c62215Smrg    ret = poll_for_reply(c, request, reply, error);
681f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
682f1c62215Smrg    return ret;
683f1c62215Smrg}
684f1c62215Smrg
685602e473dSmrgxcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
686602e473dSmrg{
687602e473dSmrg    xcb_generic_event_t *ret;
688602e473dSmrg    if(c->has_error)
689602e473dSmrg        return 0;
690602e473dSmrg    pthread_mutex_lock(&c->iolock);
691602e473dSmrg    /* get_event returns 0 on empty list. */
692602e473dSmrg    while(!(ret = get_event(c)))
693602e473dSmrg        if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
694602e473dSmrg            break;
695602e473dSmrg
696b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
697602e473dSmrg    pthread_mutex_unlock(&c->iolock);
698602e473dSmrg    return ret;
699602e473dSmrg}
700602e473dSmrg
70121298544Smrgstatic xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued)
702602e473dSmrg{
703602e473dSmrg    xcb_generic_event_t *ret = 0;
704602e473dSmrg    if(!c->has_error)
705602e473dSmrg    {
706602e473dSmrg        pthread_mutex_lock(&c->iolock);
707602e473dSmrg        /* FIXME: follow X meets Z architecture changes. */
708602e473dSmrg        ret = get_event(c);
70921298544Smrg        if(!ret && !queued && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
710602e473dSmrg            ret = get_event(c);
711602e473dSmrg        pthread_mutex_unlock(&c->iolock);
712602e473dSmrg    }
713602e473dSmrg    return ret;
714602e473dSmrg}
715602e473dSmrg
71621298544Smrgxcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
71721298544Smrg{
71821298544Smrg    return poll_for_next_event(c, 0);
71921298544Smrg}
72021298544Smrg
72121298544Smrgxcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c)
72221298544Smrg{
72321298544Smrg    return poll_for_next_event(c, 1);
72421298544Smrg}
72521298544Smrg
726602e473dSmrgxcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
727602e473dSmrg{
72821298544Smrg    uint64_t request;
72921298544Smrg    xcb_generic_error_t *ret = 0;
730602e473dSmrg    void *reply;
731602e473dSmrg    if(c->has_error)
732602e473dSmrg        return 0;
73321298544Smrg    pthread_mutex_lock(&c->iolock);
73421298544Smrg    request = widen(c, cookie.sequence);
73521298544Smrg    if(XCB_SEQUENCE_COMPARE(request, >=, c->in.request_expected)
73621298544Smrg       && XCB_SEQUENCE_COMPARE(request, >, c->in.request_completed))
737602e473dSmrg    {
73821298544Smrg        _xcb_out_send_sync(c);
73921298544Smrg        _xcb_out_flush_to(c, c->out.request);
740602e473dSmrg    }
74121298544Smrg    reply = wait_for_reply(c, request, &ret);
742602e473dSmrg    assert(!reply);
74321298544Smrg    pthread_mutex_unlock(&c->iolock);
744602e473dSmrg    return ret;
745602e473dSmrg}
746602e473dSmrg
7472f94b0afSmrgstatic xcb_generic_event_t *get_special_event(xcb_connection_t *c,
7482f94b0afSmrg                                              xcb_special_event_t *se)
7492f94b0afSmrg{
7502f94b0afSmrg    xcb_generic_event_t *event = NULL;
7512f94b0afSmrg    struct event_list *events;
7522f94b0afSmrg
7532f94b0afSmrg    if ((events = se->events) != NULL) {
7542f94b0afSmrg        event = events->event;
7552f94b0afSmrg        if (!(se->events = events->next))
7562f94b0afSmrg            se->events_tail = &se->events;
7572f94b0afSmrg        free (events);
7582f94b0afSmrg    }
7592f94b0afSmrg    return event;
7602f94b0afSmrg}
7612f94b0afSmrg
7622f94b0afSmrgxcb_generic_event_t *xcb_poll_for_special_event(xcb_connection_t *c,
7632f94b0afSmrg                                                xcb_special_event_t *se)
7642f94b0afSmrg{
7652f94b0afSmrg    xcb_generic_event_t *event;
7662f94b0afSmrg
7672f94b0afSmrg    if(c->has_error)
7682f94b0afSmrg        return 0;
7692f94b0afSmrg    pthread_mutex_lock(&c->iolock);
7702f94b0afSmrg    event = get_special_event(c, se);
7712f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
7722f94b0afSmrg    return event;
7732f94b0afSmrg}
7742f94b0afSmrg
7752f94b0afSmrgxcb_generic_event_t *xcb_wait_for_special_event(xcb_connection_t *c,
7762f94b0afSmrg                                                xcb_special_event_t *se)
7772f94b0afSmrg{
778f1c62215Smrg    special_list special;
7792f94b0afSmrg    xcb_generic_event_t *event;
7802f94b0afSmrg
7812f94b0afSmrg    if(c->has_error)
7822f94b0afSmrg        return 0;
7832f94b0afSmrg    pthread_mutex_lock(&c->iolock);
7842f94b0afSmrg
785f1c62215Smrg    insert_special(&c->in.special_waiters, &special, se);
786f1c62215Smrg
7872f94b0afSmrg    /* get_special_event returns 0 on empty list. */
7882f94b0afSmrg    while(!(event = get_special_event(c, se)))
7892f94b0afSmrg        if(!_xcb_conn_wait(c, &se->special_event_cond, 0, 0))
7902f94b0afSmrg            break;
7912f94b0afSmrg
792f1c62215Smrg    remove_special(&c->in.special_waiters, &special);
793f1c62215Smrg
794f1c62215Smrg    _xcb_in_wake_up_next_reader(c);
7952f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
7962f94b0afSmrg    return event;
7972f94b0afSmrg}
7982f94b0afSmrg
7992f94b0afSmrgxcb_special_event_t *
8002f94b0afSmrgxcb_register_for_special_xge(xcb_connection_t *c,
8012f94b0afSmrg                             xcb_extension_t *ext,
8022f94b0afSmrg                             uint32_t eid,
8032f94b0afSmrg                             uint32_t *stamp)
8042f94b0afSmrg{
8052f94b0afSmrg    xcb_special_event_t *se;
8062f94b0afSmrg    const xcb_query_extension_reply_t   *ext_reply;
8072f94b0afSmrg
8082f94b0afSmrg    if(c->has_error)
8092f94b0afSmrg        return NULL;
8102f94b0afSmrg    ext_reply = xcb_get_extension_data(c, ext);
8112f94b0afSmrg    if (!ext_reply)
8122f94b0afSmrg        return NULL;
8132f94b0afSmrg    pthread_mutex_lock(&c->iolock);
8142f94b0afSmrg    for (se = c->in.special_events; se; se = se->next) {
8152f94b0afSmrg        if (se->extension == ext_reply->major_opcode &&
8162f94b0afSmrg            se->eid == eid) {
8172f94b0afSmrg            pthread_mutex_unlock(&c->iolock);
8182f94b0afSmrg            return NULL;
8192f94b0afSmrg        }
8202f94b0afSmrg    }
8212f94b0afSmrg    se = calloc(1, sizeof(xcb_special_event_t));
8222f94b0afSmrg    if (!se) {
8232f94b0afSmrg        pthread_mutex_unlock(&c->iolock);
8242f94b0afSmrg        return NULL;
8252f94b0afSmrg    }
82608e87a6cSwiz
8272f94b0afSmrg    se->extension = ext_reply->major_opcode;
8282f94b0afSmrg    se->eid = eid;
8292f94b0afSmrg
8302f94b0afSmrg    se->events = NULL;
8312f94b0afSmrg    se->events_tail = &se->events;
8322f94b0afSmrg    se->stamp = stamp;
8332f94b0afSmrg
8342f94b0afSmrg    pthread_cond_init(&se->special_event_cond, 0);
8352f94b0afSmrg
8362f94b0afSmrg    se->next = c->in.special_events;
8372f94b0afSmrg    c->in.special_events = se;
8382f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
8392f94b0afSmrg    return se;
8402f94b0afSmrg}
8412f94b0afSmrg
8422f94b0afSmrgvoid
8432f94b0afSmrgxcb_unregister_for_special_event(xcb_connection_t *c,
8442f94b0afSmrg                                 xcb_special_event_t *se)
8452f94b0afSmrg{
8462f94b0afSmrg    xcb_special_event_t *s, **prev;
8472f94b0afSmrg    struct event_list   *events, *next;
8482f94b0afSmrg
8492f94b0afSmrg    if (!se)
8502f94b0afSmrg        return;
8512f94b0afSmrg
8522f94b0afSmrg    if (c->has_error)
8532f94b0afSmrg        return;
8542f94b0afSmrg
8552f94b0afSmrg    pthread_mutex_lock(&c->iolock);
8562f94b0afSmrg
8572f94b0afSmrg    for (prev = &c->in.special_events; (s = *prev) != NULL; prev = &(s->next)) {
8582f94b0afSmrg        if (s == se) {
8592f94b0afSmrg            *prev = se->next;
8602f94b0afSmrg            for (events = se->events; events; events = next) {
8612f94b0afSmrg                next = events->next;
8622f94b0afSmrg                free (events->event);
8632f94b0afSmrg                free (events);
8642f94b0afSmrg            }
8652f94b0afSmrg            pthread_cond_destroy(&se->special_event_cond);
8662f94b0afSmrg            free (se);
8672f94b0afSmrg            break;
8682f94b0afSmrg        }
8692f94b0afSmrg    }
8702f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
8712f94b0afSmrg}
8722f94b0afSmrg
873602e473dSmrg/* Private interface */
874602e473dSmrg
875602e473dSmrgint _xcb_in_init(_xcb_in *in)
876602e473dSmrg{
877602e473dSmrg    if(pthread_cond_init(&in->event_cond, 0))
878602e473dSmrg        return 0;
879602e473dSmrg    in->reading = 0;
880602e473dSmrg
881602e473dSmrg    in->queue_len = 0;
882602e473dSmrg
883602e473dSmrg    in->request_read = 0;
884602e473dSmrg    in->request_completed = 0;
885602e473dSmrg
886602e473dSmrg    in->replies = _xcb_map_new();
887602e473dSmrg    if(!in->replies)
888602e473dSmrg        return 0;
889602e473dSmrg
890602e473dSmrg    in->current_reply_tail = &in->current_reply;
891602e473dSmrg    in->events_tail = &in->events;
892602e473dSmrg    in->pending_replies_tail = &in->pending_replies;
893602e473dSmrg
894602e473dSmrg    return 1;
895602e473dSmrg}
896602e473dSmrg
897602e473dSmrgvoid _xcb_in_destroy(_xcb_in *in)
898602e473dSmrg{
899602e473dSmrg    pthread_cond_destroy(&in->event_cond);
900602e473dSmrg    free_reply_list(in->current_reply);
901602e473dSmrg    _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
902602e473dSmrg    while(in->events)
903602e473dSmrg    {
904602e473dSmrg        struct event_list *e = in->events;
905602e473dSmrg        in->events = e->next;
906602e473dSmrg        free(e->event);
907602e473dSmrg        free(e);
908602e473dSmrg    }
909602e473dSmrg    while(in->pending_replies)
910602e473dSmrg    {
911602e473dSmrg        pending_reply *pend = in->pending_replies;
912602e473dSmrg        in->pending_replies = pend->next;
913602e473dSmrg        free(pend);
914602e473dSmrg    }
915602e473dSmrg}
916602e473dSmrg
917b20a2039Smrgvoid _xcb_in_wake_up_next_reader(xcb_connection_t *c)
918b20a2039Smrg{
919b20a2039Smrg    int pthreadret;
920b20a2039Smrg    if(c->in.readers)
921b20a2039Smrg        pthreadret = pthread_cond_signal(c->in.readers->data);
922f1c62215Smrg    else if(c->in.special_waiters)
923f1c62215Smrg        pthreadret = pthread_cond_signal(&c->in.special_waiters->se->special_event_cond);
924b20a2039Smrg    else
925b20a2039Smrg        pthreadret = pthread_cond_signal(&c->in.event_cond);
926b20a2039Smrg    assert(pthreadret == 0);
927b20a2039Smrg}
928b20a2039Smrg
929602e473dSmrgint _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
930602e473dSmrg{
931602e473dSmrg    pending_reply *pend = malloc(sizeof(pending_reply));
932602e473dSmrg    assert(workaround != WORKAROUND_NONE || flags != 0);
933602e473dSmrg    if(!pend)
934602e473dSmrg    {
93521298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
936602e473dSmrg        return 0;
937602e473dSmrg    }
938602e473dSmrg    pend->first_request = pend->last_request = request;
939602e473dSmrg    pend->workaround = workaround;
940602e473dSmrg    pend->flags = flags;
941602e473dSmrg    pend->next = 0;
942602e473dSmrg    *c->in.pending_replies_tail = pend;
943602e473dSmrg    c->in.pending_replies_tail = &pend->next;
944602e473dSmrg    return 1;
945602e473dSmrg}
946602e473dSmrg
947602e473dSmrgvoid _xcb_in_replies_done(xcb_connection_t *c)
948602e473dSmrg{
949602e473dSmrg    struct pending_reply *pend;
950602e473dSmrg    if (c->in.pending_replies_tail != &c->in.pending_replies)
951602e473dSmrg    {
952602e473dSmrg        pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
953602e473dSmrg        if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
954602e473dSmrg        {
955602e473dSmrg            pend->last_request = c->out.request;
956602e473dSmrg            pend->workaround = WORKAROUND_NONE;
957602e473dSmrg        }
958602e473dSmrg    }
959602e473dSmrg}
960602e473dSmrg
961602e473dSmrgint _xcb_in_read(xcb_connection_t *c)
962602e473dSmrg{
9632f94b0afSmrg    int n;
9642f94b0afSmrg
9652f94b0afSmrg#if HAVE_SENDMSG
9662f94b0afSmrg    struct iovec    iov = {
9672f94b0afSmrg        .iov_base = c->in.queue + c->in.queue_len,
9682f94b0afSmrg        .iov_len = sizeof(c->in.queue) - c->in.queue_len,
9692f94b0afSmrg    };
9702f94b0afSmrg    union {
9712f94b0afSmrg        struct cmsghdr cmsghdr;
9722f94b0afSmrg        char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
9732f94b0afSmrg    } cmsgbuf;
9742f94b0afSmrg    struct msghdr msg = {
9752f94b0afSmrg        .msg_name = NULL,
9762f94b0afSmrg        .msg_namelen = 0,
9772f94b0afSmrg        .msg_iov = &iov,
9782f94b0afSmrg        .msg_iovlen = 1,
9792f94b0afSmrg        .msg_control = cmsgbuf.buf,
9802f94b0afSmrg        .msg_controllen = CMSG_SPACE(sizeof(int) * (XCB_MAX_PASS_FD - c->in.in_fd.nfd)),
9812f94b0afSmrg    };
9822f94b0afSmrg    n = recvmsg(c->fd, &msg, 0);
9832f94b0afSmrg
9842f94b0afSmrg    /* Check for truncation errors. Only MSG_CTRUNC is
9852f94b0afSmrg     * probably possible here, which would indicate that
9862f94b0afSmrg     * the sender tried to transmit more than XCB_MAX_PASS_FD
9872f94b0afSmrg     * file descriptors.
9882f94b0afSmrg     */
9892f94b0afSmrg    if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
9902f94b0afSmrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
9912f94b0afSmrg        return 0;
9922f94b0afSmrg    }
9932f94b0afSmrg#else
9942f94b0afSmrg    n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len, 0);
9952f94b0afSmrg#endif
9962f94b0afSmrg    if(n > 0) {
9972f94b0afSmrg#if HAVE_SENDMSG
9982f94b0afSmrg        struct cmsghdr *hdr;
9992f94b0afSmrg
10002f94b0afSmrg        if (msg.msg_controllen >= sizeof (struct cmsghdr)) {
10012f94b0afSmrg            for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) {
10022f94b0afSmrg                if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) {
10032f94b0afSmrg                    int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int);
10042f94b0afSmrg                    memcpy(&c->in.in_fd.fd[c->in.in_fd.nfd], CMSG_DATA(hdr), nfd * sizeof (int));
10052f94b0afSmrg                    c->in.in_fd.nfd += nfd;
10062f94b0afSmrg                }
10072f94b0afSmrg            }
10082f94b0afSmrg        }
10092f94b0afSmrg#endif
1010602e473dSmrg        c->in.queue_len += n;
10112f94b0afSmrg    }
1012602e473dSmrg    while(read_packet(c))
1013602e473dSmrg        /* empty */;
10142f94b0afSmrg#if HAVE_SENDMSG
10152f94b0afSmrg    if (c->in.in_fd.nfd) {
10162f94b0afSmrg        c->in.in_fd.nfd -= c->in.in_fd.ifd;
10172f94b0afSmrg        memmove(&c->in.in_fd.fd[0],
10182f94b0afSmrg                &c->in.in_fd.fd[c->in.in_fd.ifd],
10192f94b0afSmrg                c->in.in_fd.nfd * sizeof (int));
10202f94b0afSmrg        c->in.in_fd.ifd = 0;
10212f94b0afSmrg
10222f94b0afSmrg        /* If we have any left-over file descriptors after emptying
10232f94b0afSmrg         * the input buffer, then the server sent some that we weren't
10242f94b0afSmrg         * expecting.  Close them and mark the connection as broken;
10252f94b0afSmrg         */
10262f94b0afSmrg        if (c->in.queue_len == 0 && c->in.in_fd.nfd != 0) {
10272f94b0afSmrg            int i;
10282f94b0afSmrg            for (i = 0; i < c->in.in_fd.nfd; i++)
10292f94b0afSmrg                close(c->in.in_fd.fd[i]);
10302f94b0afSmrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
10312f94b0afSmrg            return 0;
10322f94b0afSmrg        }
10332f94b0afSmrg    }
10342f94b0afSmrg#endif
103521298544Smrg#ifndef _WIN32
1036602e473dSmrg    if((n > 0) || (n < 0 && errno == EAGAIN))
103721298544Smrg#else
103821298544Smrg    if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
103921298544Smrg#endif /* !_WIN32 */
1040602e473dSmrg        return 1;
104121298544Smrg    _xcb_conn_shutdown(c, XCB_CONN_ERROR);
1042602e473dSmrg    return 0;
1043602e473dSmrg}
1044602e473dSmrg
1045602e473dSmrgint _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
1046602e473dSmrg{
1047602e473dSmrg    int done = c->in.queue_len;
1048602e473dSmrg    if(len < done)
1049602e473dSmrg        done = len;
1050602e473dSmrg
1051602e473dSmrg    memcpy(buf, c->in.queue, done);
1052602e473dSmrg    c->in.queue_len -= done;
1053602e473dSmrg    memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
1054602e473dSmrg
1055602e473dSmrg    if(len > done)
1056602e473dSmrg    {
1057602e473dSmrg        int ret = read_block(c->fd, (char *) buf + done, len - done);
1058602e473dSmrg        if(ret <= 0)
1059602e473dSmrg        {
106021298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_ERROR);
1061602e473dSmrg            return ret;
1062602e473dSmrg        }
1063602e473dSmrg    }
1064602e473dSmrg
1065602e473dSmrg    return len;
1066602e473dSmrg}
1067