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 <stdio.h>
36602e473dSmrg#include <errno.h>
37602e473dSmrg
38602e473dSmrg#if USE_POLL
39602e473dSmrg#include <poll.h>
4021298544Smrg#endif
4121298544Smrg#ifndef _WIN32
421fda38d4Smrg#include <unistd.h>
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;
2421fda38d4Smrg            uint64_t new_length = ((uint64_t)p[2]) * ((uint64_t)p[3]);
2431fda38d4Smrg            if(new_length >= (UINT32_MAX / UINT32_C(16)))
2441fda38d4Smrg            {
2451fda38d4Smrg                _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
2461fda38d4Smrg                return 0;
2471fda38d4Smrg            }
2481fda38d4Smrg            genrep.length = (uint32_t)(new_length * UINT64_C(2));
249602e473dSmrg        }
2501fda38d4Smrg        length += genrep.length * UINT64_C(4);
2512f94b0afSmrg
2522f94b0afSmrg        /* XXX a bit of a hack -- we "know" that all FD replys place
2532f94b0afSmrg         * the number of fds in the pad0 byte */
2542f94b0afSmrg        if (pend && pend->flags & XCB_REQUEST_REPLY_FDS)
2552f94b0afSmrg            nfd = genrep.pad0;
256602e473dSmrg    }
257602e473dSmrg
258602e473dSmrg    /* XGE events may have sizes > 32 */
25921298544Smrg    if ((genrep.response_type & 0x7f) == XCB_XGE_EVENT)
2601fda38d4Smrg        eventlength = genrep.length * UINT64_C(4);
261602e473dSmrg
2622f94b0afSmrg    bufsize = length + eventlength + nfd * sizeof(int)  +
263190694daSmrg        (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t));
264190694daSmrg    if (bufsize < INT32_MAX)
265190694daSmrg        buf = malloc((size_t) bufsize);
266190694daSmrg    else
267190694daSmrg        buf = NULL;
268602e473dSmrg    if(!buf)
269602e473dSmrg    {
27021298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
271602e473dSmrg        return 0;
272602e473dSmrg    }
273602e473dSmrg
274602e473dSmrg    if(_xcb_in_read_block(c, buf, length) <= 0)
275602e473dSmrg    {
276602e473dSmrg        free(buf);
277602e473dSmrg        return 0;
278602e473dSmrg    }
279602e473dSmrg
280602e473dSmrg    /* pull in XGE event data if available, append after event struct */
281602e473dSmrg    if (eventlength)
282602e473dSmrg    {
283602e473dSmrg        if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
284602e473dSmrg        {
285602e473dSmrg            free(buf);
286602e473dSmrg            return 0;
287602e473dSmrg        }
288602e473dSmrg    }
289602e473dSmrg
2902f94b0afSmrg#if HAVE_SENDMSG
2912f94b0afSmrg    if (nfd)
2922f94b0afSmrg    {
2932f94b0afSmrg        if (!read_fds(c, (int *) &((char *) buf)[length], nfd))
2942f94b0afSmrg        {
2952f94b0afSmrg            free(buf);
2962f94b0afSmrg            return 0;
2972f94b0afSmrg        }
2982f94b0afSmrg    }
2992f94b0afSmrg#endif
3002f94b0afSmrg
301602e473dSmrg    if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
302602e473dSmrg    {
303602e473dSmrg        free(buf);
304602e473dSmrg        return 1;
305602e473dSmrg    }
306602e473dSmrg
307602e473dSmrg    if(genrep.response_type != XCB_REPLY)
308602e473dSmrg        ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
309602e473dSmrg
310602e473dSmrg    /* reply, or checked error */
311602e473dSmrg    if( genrep.response_type == XCB_REPLY ||
312602e473dSmrg       (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
313602e473dSmrg    {
314602e473dSmrg        struct reply_list *cur = malloc(sizeof(struct reply_list));
315602e473dSmrg        if(!cur)
316602e473dSmrg        {
31721298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
318602e473dSmrg            free(buf);
319602e473dSmrg            return 0;
320602e473dSmrg        }
321602e473dSmrg        cur->reply = buf;
322602e473dSmrg        cur->next = 0;
323602e473dSmrg        *c->in.current_reply_tail = cur;
324602e473dSmrg        c->in.current_reply_tail = &cur->next;
32521298544Smrg        if(c->in.readers && c->in.readers->request == c->in.request_read)
32621298544Smrg            pthread_cond_signal(c->in.readers->data);
327602e473dSmrg        return 1;
328602e473dSmrg    }
329602e473dSmrg
330602e473dSmrg    /* event, or unchecked error */
331602e473dSmrg    event = malloc(sizeof(struct event_list));
332602e473dSmrg    if(!event)
333602e473dSmrg    {
33421298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
335602e473dSmrg        free(buf);
336602e473dSmrg        return 0;
337602e473dSmrg    }
338602e473dSmrg    event->event = buf;
339602e473dSmrg    event->next = 0;
3402f94b0afSmrg
3412f94b0afSmrg    if (!event_special(c, event)) {
3422f94b0afSmrg        *c->in.events_tail = event;
3432f94b0afSmrg        c->in.events_tail = &event->next;
3442f94b0afSmrg        pthread_cond_signal(&c->in.event_cond);
3452f94b0afSmrg    }
346602e473dSmrg    return 1; /* I have something for you... */
347602e473dSmrg}
348602e473dSmrg
349602e473dSmrgstatic xcb_generic_event_t *get_event(xcb_connection_t *c)
350602e473dSmrg{
351602e473dSmrg    struct event_list *cur = c->in.events;
352602e473dSmrg    xcb_generic_event_t *ret;
353602e473dSmrg    if(!c->in.events)
354602e473dSmrg        return 0;
355602e473dSmrg    ret = cur->event;
356602e473dSmrg    c->in.events = cur->next;
357602e473dSmrg    if(!cur->next)
358602e473dSmrg        c->in.events_tail = &c->in.events;
359602e473dSmrg    free(cur);
360602e473dSmrg    return ret;
361602e473dSmrg}
362602e473dSmrg
363602e473dSmrgstatic void free_reply_list(struct reply_list *head)
364602e473dSmrg{
365602e473dSmrg    while(head)
366602e473dSmrg    {
367602e473dSmrg        struct reply_list *cur = head;
368602e473dSmrg        head = cur->next;
369602e473dSmrg        free(cur->reply);
370602e473dSmrg        free(cur);
371602e473dSmrg    }
372602e473dSmrg}
373602e473dSmrg
3741fda38d4Smrgstatic int read_block(const int fd, void *buf, const intptr_t len)
375602e473dSmrg{
376602e473dSmrg    int done = 0;
377602e473dSmrg    while(done < len)
378602e473dSmrg    {
37921298544Smrg        int ret = recv(fd, ((char *) buf) + done, len - done, 0);
380602e473dSmrg        if(ret > 0)
381602e473dSmrg            done += ret;
38221298544Smrg#ifndef _WIN32
383602e473dSmrg        if(ret < 0 && errno == EAGAIN)
38421298544Smrg#else
38521298544Smrg        if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
38621298544Smrg#endif /* !_Win32 */
387602e473dSmrg        {
388602e473dSmrg#if USE_POLL
389602e473dSmrg            struct pollfd pfd;
390602e473dSmrg            pfd.fd = fd;
391602e473dSmrg            pfd.events = POLLIN;
392602e473dSmrg            pfd.revents = 0;
393602e473dSmrg            do {
394602e473dSmrg                ret = poll(&pfd, 1, -1);
395602e473dSmrg            } while (ret == -1 && errno == EINTR);
396602e473dSmrg#else
397602e473dSmrg            fd_set fds;
398602e473dSmrg            FD_ZERO(&fds);
399602e473dSmrg            FD_SET(fd, &fds);
40021298544Smrg
40108e87a6cSwiz            /* Initializing errno here makes sure that for Win32 this loop will execute only once */
40208e87a6cSwiz            errno = 0;
40308e87a6cSwiz            do {
40408e87a6cSwiz                ret = select(fd + 1, &fds, 0, 0, 0);
40508e87a6cSwiz            } while (ret == -1 && errno == EINTR);
40621298544Smrg#endif /* USE_POLL */
407602e473dSmrg        }
408602e473dSmrg        if(ret <= 0)
409602e473dSmrg            return ret;
410602e473dSmrg    }
411602e473dSmrg    return len;
412602e473dSmrg}
413602e473dSmrg
41421298544Smrgstatic int poll_for_reply(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
415602e473dSmrg{
416602e473dSmrg    struct reply_list *head;
417602e473dSmrg
418602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
419602e473dSmrg    if(!request)
420602e473dSmrg        head = 0;
421602e473dSmrg    /* We've read requests past the one we want, so if it has replies we have
422602e473dSmrg     * them all and they're in the replies map. */
42321298544Smrg    else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
424602e473dSmrg    {
425602e473dSmrg        head = _xcb_map_remove(c->in.replies, request);
426602e473dSmrg        if(head && head->next)
427602e473dSmrg            _xcb_map_put(c->in.replies, request, head->next);
428602e473dSmrg    }
429602e473dSmrg    /* We're currently processing the responses to the request we want, and we
430602e473dSmrg     * have a reply ready to return. So just return it without blocking. */
43121298544Smrg    else if(request == c->in.request_read && c->in.current_reply)
432602e473dSmrg    {
433602e473dSmrg        head = c->in.current_reply;
434602e473dSmrg        c->in.current_reply = head->next;
435602e473dSmrg        if(!head->next)
436602e473dSmrg            c->in.current_reply_tail = &c->in.current_reply;
437602e473dSmrg    }
438602e473dSmrg    /* We know this request can't have any more replies, and we've already
439602e473dSmrg     * established it doesn't have a reply now. Don't bother blocking. */
44021298544Smrg    else if(request == c->in.request_completed)
441602e473dSmrg        head = 0;
442602e473dSmrg    /* We may have more replies on the way for this request: block until we're
443602e473dSmrg     * sure. */
444602e473dSmrg    else
445602e473dSmrg        return 0;
446602e473dSmrg
447602e473dSmrg    if(error)
448602e473dSmrg        *error = 0;
449602e473dSmrg    *reply = 0;
450602e473dSmrg
451602e473dSmrg    if(head)
452602e473dSmrg    {
453602e473dSmrg        if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
454602e473dSmrg        {
455602e473dSmrg            if(error)
456602e473dSmrg                *error = head->reply;
457602e473dSmrg            else
458602e473dSmrg                free(head->reply);
459602e473dSmrg        }
460602e473dSmrg        else
461602e473dSmrg            *reply = head->reply;
462602e473dSmrg
463602e473dSmrg        free(head);
464602e473dSmrg    }
465602e473dSmrg
466602e473dSmrg    return 1;
467602e473dSmrg}
468602e473dSmrg
46921298544Smrgstatic void insert_reader(reader_list **prev_reader, reader_list *reader, uint64_t request, pthread_cond_t *cond)
470602e473dSmrg{
47121298544Smrg    while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request))
47221298544Smrg        prev_reader = &(*prev_reader)->next;
47321298544Smrg    reader->request = request;
47421298544Smrg    reader->data = cond;
47521298544Smrg    reader->next = *prev_reader;
47621298544Smrg    *prev_reader = reader;
47721298544Smrg}
478602e473dSmrg
47921298544Smrgstatic void remove_reader(reader_list **prev_reader, reader_list *reader)
48021298544Smrg{
48121298544Smrg    while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, reader->request))
48221298544Smrg        if(*prev_reader == reader)
48321298544Smrg        {
48421298544Smrg            *prev_reader = (*prev_reader)->next;
48521298544Smrg            break;
48621298544Smrg        }
48721298544Smrg}
488602e473dSmrg
489f1c62215Smrgstatic void insert_special(special_list **prev_special, special_list *special, xcb_special_event_t *se)
490f1c62215Smrg{
491f1c62215Smrg    special->se = se;
492f1c62215Smrg    special->next = *prev_special;
493f1c62215Smrg    *prev_special = special;
494f1c62215Smrg}
495f1c62215Smrg
496f1c62215Smrgstatic void remove_special(special_list **prev_special, special_list *special)
497f1c62215Smrg{
498f1c62215Smrg    while(*prev_special)
499f1c62215Smrg    {
500f1c62215Smrg        if(*prev_special == special)
501f1c62215Smrg        {
502f1c62215Smrg            *prev_special = (*prev_special)->next;
503f1c62215Smrg            break;
504f1c62215Smrg        }
505f1c62215Smrg        prev_special = &(*prev_special)->next;
506f1c62215Smrg    }
507f1c62215Smrg}
508f1c62215Smrg
50921298544Smrgstatic void *wait_for_reply(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
51021298544Smrg{
51121298544Smrg    void *ret = 0;
512602e473dSmrg
513602e473dSmrg    /* If this request has not been written yet, write it. */
51421298544Smrg    if(c->out.return_socket || _xcb_out_flush_to(c, request))
515602e473dSmrg    {
516602e473dSmrg        pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
517602e473dSmrg        reader_list reader;
51821298544Smrg
51921298544Smrg        insert_reader(&c->in.readers, &reader, request, &cond);
520602e473dSmrg
521602e473dSmrg        while(!poll_for_reply(c, request, &ret, e))
522602e473dSmrg            if(!_xcb_conn_wait(c, &cond, 0, 0))
523602e473dSmrg                break;
524602e473dSmrg
52521298544Smrg        remove_reader(&c->in.readers, &reader);
526602e473dSmrg        pthread_cond_destroy(&cond);
527602e473dSmrg    }
528602e473dSmrg
529b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
53021298544Smrg    return ret;
53121298544Smrg}
53221298544Smrg
53321298544Smrgstatic uint64_t widen(xcb_connection_t *c, unsigned int request)
53421298544Smrg{
53521298544Smrg    uint64_t widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
53621298544Smrg    if(widened_request > c->out.request)
53721298544Smrg        widened_request -= UINT64_C(1) << 32;
53821298544Smrg    return widened_request;
53921298544Smrg}
54021298544Smrg
54121298544Smrg/* Public interface */
54221298544Smrg
54321298544Smrgvoid *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
54421298544Smrg{
54521298544Smrg    void *ret;
54621298544Smrg    if(e)
54721298544Smrg        *e = 0;
54821298544Smrg    if(c->has_error)
54921298544Smrg        return 0;
55021298544Smrg
55121298544Smrg    pthread_mutex_lock(&c->iolock);
55221298544Smrg    ret = wait_for_reply(c, widen(c, request), e);
553602e473dSmrg    pthread_mutex_unlock(&c->iolock);
554602e473dSmrg    return ret;
555602e473dSmrg}
556602e473dSmrg
557f1c62215Smrgvoid *xcb_wait_for_reply64(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
558f1c62215Smrg{
559f1c62215Smrg    void *ret;
560f1c62215Smrg    if(e)
561f1c62215Smrg        *e = 0;
562f1c62215Smrg    if(c->has_error)
563f1c62215Smrg        return 0;
564f1c62215Smrg
565f1c62215Smrg    pthread_mutex_lock(&c->iolock);
566f1c62215Smrg    ret = wait_for_reply(c, request, e);
567f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
568f1c62215Smrg    return ret;
569f1c62215Smrg}
570f1c62215Smrg
5712f94b0afSmrgint *xcb_get_reply_fds(xcb_connection_t *c, void *reply, size_t reply_size)
5722f94b0afSmrg{
5732f94b0afSmrg    return (int *) (&((char *) reply)[reply_size]);
5742f94b0afSmrg}
5752f94b0afSmrg
576602e473dSmrgstatic void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
577602e473dSmrg{
578602e473dSmrg    pending_reply *pend;
579602e473dSmrg    pend = malloc(sizeof(*pend));
580602e473dSmrg    if(!pend)
581602e473dSmrg    {
58221298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
583602e473dSmrg        return;
584602e473dSmrg    }
585602e473dSmrg
586602e473dSmrg    pend->first_request = seq;
587602e473dSmrg    pend->last_request = seq;
588602e473dSmrg    pend->workaround = 0;
589602e473dSmrg    pend->flags = XCB_REQUEST_DISCARD_REPLY;
590602e473dSmrg    pend->next = *prev_next;
591602e473dSmrg    *prev_next = pend;
592602e473dSmrg
593602e473dSmrg    if(!pend->next)
594602e473dSmrg        c->in.pending_replies_tail = &pend->next;
595602e473dSmrg}
596602e473dSmrg
59721298544Smrgstatic void discard_reply(xcb_connection_t *c, uint64_t request)
598602e473dSmrg{
59921298544Smrg    void *reply;
600602e473dSmrg    pending_reply **prev_pend;
601602e473dSmrg
60221298544Smrg    /* Free any replies or errors that we've already read. Stop if
60321298544Smrg     * xcb_wait_for_reply would block or we've run out of replies. */
60421298544Smrg    while(poll_for_reply(c, request, &reply, 0) && reply)
60521298544Smrg        free(reply);
606602e473dSmrg
60721298544Smrg    /* If we've proven there are no more responses coming, we're done. */
60821298544Smrg    if(XCB_SEQUENCE_COMPARE(request, <=, c->in.request_completed))
609602e473dSmrg        return;
610602e473dSmrg
611602e473dSmrg    /* Walk the list of pending requests. Mark the first match for deletion. */
612602e473dSmrg    for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
613602e473dSmrg    {
61421298544Smrg        if(XCB_SEQUENCE_COMPARE((*prev_pend)->first_request, >, request))
615602e473dSmrg            break;
616602e473dSmrg
61721298544Smrg        if((*prev_pend)->first_request == request)
618602e473dSmrg        {
619602e473dSmrg            /* Pending reply found. Mark for discard: */
620602e473dSmrg            (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
621602e473dSmrg            return;
622602e473dSmrg        }
623602e473dSmrg    }
624602e473dSmrg
625602e473dSmrg    /* Pending reply not found (likely due to _unchecked request). Create one: */
62621298544Smrg    insert_pending_discard(c, prev_pend, request);
627602e473dSmrg}
628602e473dSmrg
629602e473dSmrgvoid xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
630602e473dSmrg{
631602e473dSmrg    if(c->has_error)
632602e473dSmrg        return;
633602e473dSmrg
634602e473dSmrg    /* If an error occurred when issuing the request, fail immediately. */
635602e473dSmrg    if(!sequence)
636602e473dSmrg        return;
637602e473dSmrg
638602e473dSmrg    pthread_mutex_lock(&c->iolock);
63921298544Smrg    discard_reply(c, widen(c, sequence));
640602e473dSmrg    pthread_mutex_unlock(&c->iolock);
641602e473dSmrg}
642602e473dSmrg
643f1c62215Smrgvoid xcb_discard_reply64(xcb_connection_t *c, uint64_t sequence)
644f1c62215Smrg{
645f1c62215Smrg    if(c->has_error)
646f1c62215Smrg        return;
647f1c62215Smrg
648f1c62215Smrg    /* If an error occurred when issuing the request, fail immediately. */
649f1c62215Smrg    if(!sequence)
650f1c62215Smrg        return;
651f1c62215Smrg
652f1c62215Smrg    pthread_mutex_lock(&c->iolock);
653f1c62215Smrg    discard_reply(c, sequence);
654f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
655f1c62215Smrg}
656f1c62215Smrg
657602e473dSmrgint xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
658602e473dSmrg{
659602e473dSmrg    int ret;
660602e473dSmrg    if(c->has_error)
661602e473dSmrg    {
662602e473dSmrg        *reply = 0;
663602e473dSmrg        if(error)
664602e473dSmrg            *error = 0;
665602e473dSmrg        return 1; /* would not block */
666602e473dSmrg    }
667602e473dSmrg    assert(reply != 0);
668602e473dSmrg    pthread_mutex_lock(&c->iolock);
66921298544Smrg    ret = poll_for_reply(c, widen(c, request), reply, error);
670d05135cfSmrg    if(!ret && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
671d05135cfSmrg        ret = poll_for_reply(c, widen(c, request), reply, error);
672602e473dSmrg    pthread_mutex_unlock(&c->iolock);
673602e473dSmrg    return ret;
674602e473dSmrg}
675602e473dSmrg
676f1c62215Smrgint xcb_poll_for_reply64(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
677f1c62215Smrg{
678f1c62215Smrg    int ret;
679f1c62215Smrg    if(c->has_error)
680f1c62215Smrg    {
681f1c62215Smrg        *reply = 0;
682f1c62215Smrg        if(error)
683f1c62215Smrg            *error = 0;
684f1c62215Smrg        return 1; /* would not block */
685f1c62215Smrg    }
686f1c62215Smrg    assert(reply != 0);
687f1c62215Smrg    pthread_mutex_lock(&c->iolock);
688f1c62215Smrg    ret = poll_for_reply(c, request, reply, error);
689d05135cfSmrg    if(!ret && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
690d05135cfSmrg        ret = poll_for_reply(c, request, reply, error);
691f1c62215Smrg    pthread_mutex_unlock(&c->iolock);
692f1c62215Smrg    return ret;
693f1c62215Smrg}
694f1c62215Smrg
695602e473dSmrgxcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
696602e473dSmrg{
697602e473dSmrg    xcb_generic_event_t *ret;
698602e473dSmrg    if(c->has_error)
699602e473dSmrg        return 0;
700602e473dSmrg    pthread_mutex_lock(&c->iolock);
701602e473dSmrg    /* get_event returns 0 on empty list. */
702602e473dSmrg    while(!(ret = get_event(c)))
703602e473dSmrg        if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
704602e473dSmrg            break;
705602e473dSmrg
706b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
707602e473dSmrg    pthread_mutex_unlock(&c->iolock);
708602e473dSmrg    return ret;
709602e473dSmrg}
710602e473dSmrg
71121298544Smrgstatic xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued)
712602e473dSmrg{
713602e473dSmrg    xcb_generic_event_t *ret = 0;
714602e473dSmrg    if(!c->has_error)
715602e473dSmrg    {
716602e473dSmrg        pthread_mutex_lock(&c->iolock);
717602e473dSmrg        /* FIXME: follow X meets Z architecture changes. */
718602e473dSmrg        ret = get_event(c);
71921298544Smrg        if(!ret && !queued && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
720602e473dSmrg            ret = get_event(c);
721602e473dSmrg        pthread_mutex_unlock(&c->iolock);
722602e473dSmrg    }
723602e473dSmrg    return ret;
724602e473dSmrg}
725602e473dSmrg
72621298544Smrgxcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
72721298544Smrg{
72821298544Smrg    return poll_for_next_event(c, 0);
72921298544Smrg}
73021298544Smrg
73121298544Smrgxcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c)
73221298544Smrg{
73321298544Smrg    return poll_for_next_event(c, 1);
73421298544Smrg}
73521298544Smrg
736602e473dSmrgxcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
737602e473dSmrg{
73821298544Smrg    uint64_t request;
73921298544Smrg    xcb_generic_error_t *ret = 0;
740602e473dSmrg    void *reply;
741602e473dSmrg    if(c->has_error)
742602e473dSmrg        return 0;
74321298544Smrg    pthread_mutex_lock(&c->iolock);
74421298544Smrg    request = widen(c, cookie.sequence);
7451fda38d4Smrg    if (XCB_SEQUENCE_COMPARE(request, >, c->in.request_completed))
746602e473dSmrg    {
7471fda38d4Smrg        if(XCB_SEQUENCE_COMPARE(request, >=, c->in.request_expected))
7481fda38d4Smrg        {
7491fda38d4Smrg            _xcb_out_send_sync(c);
7501fda38d4Smrg        }
7511fda38d4Smrg        if (XCB_SEQUENCE_COMPARE(request, >=, c->out.request_expected_written))
7521fda38d4Smrg        {
7531fda38d4Smrg            _xcb_out_flush_to(c, c->out.request);
7541fda38d4Smrg        }
755602e473dSmrg    }
75621298544Smrg    reply = wait_for_reply(c, request, &ret);
757602e473dSmrg    assert(!reply);
75821298544Smrg    pthread_mutex_unlock(&c->iolock);
759602e473dSmrg    return ret;
760602e473dSmrg}
761602e473dSmrg
7622f94b0afSmrgstatic xcb_generic_event_t *get_special_event(xcb_connection_t *c,
7632f94b0afSmrg                                              xcb_special_event_t *se)
7642f94b0afSmrg{
7652f94b0afSmrg    xcb_generic_event_t *event = NULL;
7662f94b0afSmrg    struct event_list *events;
7672f94b0afSmrg
7682f94b0afSmrg    if ((events = se->events) != NULL) {
7692f94b0afSmrg        event = events->event;
7702f94b0afSmrg        if (!(se->events = events->next))
7712f94b0afSmrg            se->events_tail = &se->events;
7722f94b0afSmrg        free (events);
7732f94b0afSmrg    }
7742f94b0afSmrg    return event;
7752f94b0afSmrg}
7762f94b0afSmrg
7772f94b0afSmrgxcb_generic_event_t *xcb_poll_for_special_event(xcb_connection_t *c,
7782f94b0afSmrg                                                xcb_special_event_t *se)
7792f94b0afSmrg{
7802f94b0afSmrg    xcb_generic_event_t *event;
7812f94b0afSmrg
7822f94b0afSmrg    if(c->has_error)
7832f94b0afSmrg        return 0;
7842f94b0afSmrg    pthread_mutex_lock(&c->iolock);
7852f94b0afSmrg    event = get_special_event(c, se);
786d05135cfSmrg    if(!event && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
787d05135cfSmrg        event = get_special_event(c, se);
7882f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
7892f94b0afSmrg    return event;
7902f94b0afSmrg}
7912f94b0afSmrg
7922f94b0afSmrgxcb_generic_event_t *xcb_wait_for_special_event(xcb_connection_t *c,
7932f94b0afSmrg                                                xcb_special_event_t *se)
7942f94b0afSmrg{
795f1c62215Smrg    special_list special;
7962f94b0afSmrg    xcb_generic_event_t *event;
7972f94b0afSmrg
7982f94b0afSmrg    if(c->has_error)
7992f94b0afSmrg        return 0;
8002f94b0afSmrg    pthread_mutex_lock(&c->iolock);
8012f94b0afSmrg
802f1c62215Smrg    insert_special(&c->in.special_waiters, &special, se);
803f1c62215Smrg
8042f94b0afSmrg    /* get_special_event returns 0 on empty list. */
8052f94b0afSmrg    while(!(event = get_special_event(c, se)))
8062f94b0afSmrg        if(!_xcb_conn_wait(c, &se->special_event_cond, 0, 0))
8072f94b0afSmrg            break;
8082f94b0afSmrg
809f1c62215Smrg    remove_special(&c->in.special_waiters, &special);
810f1c62215Smrg
811f1c62215Smrg    _xcb_in_wake_up_next_reader(c);
8122f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
8132f94b0afSmrg    return event;
8142f94b0afSmrg}
8152f94b0afSmrg
8162f94b0afSmrgxcb_special_event_t *
8172f94b0afSmrgxcb_register_for_special_xge(xcb_connection_t *c,
8182f94b0afSmrg                             xcb_extension_t *ext,
8192f94b0afSmrg                             uint32_t eid,
8202f94b0afSmrg                             uint32_t *stamp)
8212f94b0afSmrg{
8222f94b0afSmrg    xcb_special_event_t *se;
8232f94b0afSmrg    const xcb_query_extension_reply_t   *ext_reply;
8242f94b0afSmrg
8252f94b0afSmrg    if(c->has_error)
8262f94b0afSmrg        return NULL;
8272f94b0afSmrg    ext_reply = xcb_get_extension_data(c, ext);
8282f94b0afSmrg    if (!ext_reply)
8292f94b0afSmrg        return NULL;
8302f94b0afSmrg    pthread_mutex_lock(&c->iolock);
8312f94b0afSmrg    for (se = c->in.special_events; se; se = se->next) {
8322f94b0afSmrg        if (se->extension == ext_reply->major_opcode &&
8332f94b0afSmrg            se->eid == eid) {
8342f94b0afSmrg            pthread_mutex_unlock(&c->iolock);
8352f94b0afSmrg            return NULL;
8362f94b0afSmrg        }
8372f94b0afSmrg    }
8382f94b0afSmrg    se = calloc(1, sizeof(xcb_special_event_t));
8392f94b0afSmrg    if (!se) {
8402f94b0afSmrg        pthread_mutex_unlock(&c->iolock);
8412f94b0afSmrg        return NULL;
8422f94b0afSmrg    }
84308e87a6cSwiz
8442f94b0afSmrg    se->extension = ext_reply->major_opcode;
8452f94b0afSmrg    se->eid = eid;
8462f94b0afSmrg
8472f94b0afSmrg    se->events = NULL;
8482f94b0afSmrg    se->events_tail = &se->events;
8492f94b0afSmrg    se->stamp = stamp;
8502f94b0afSmrg
8512f94b0afSmrg    pthread_cond_init(&se->special_event_cond, 0);
8522f94b0afSmrg
8532f94b0afSmrg    se->next = c->in.special_events;
8542f94b0afSmrg    c->in.special_events = se;
8552f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
8562f94b0afSmrg    return se;
8572f94b0afSmrg}
8582f94b0afSmrg
8592f94b0afSmrgvoid
8602f94b0afSmrgxcb_unregister_for_special_event(xcb_connection_t *c,
8612f94b0afSmrg                                 xcb_special_event_t *se)
8622f94b0afSmrg{
8632f94b0afSmrg    xcb_special_event_t *s, **prev;
8642f94b0afSmrg    struct event_list   *events, *next;
8652f94b0afSmrg
8662f94b0afSmrg    if (!se)
8672f94b0afSmrg        return;
8682f94b0afSmrg
8692f94b0afSmrg    if (c->has_error)
8702f94b0afSmrg        return;
8712f94b0afSmrg
8722f94b0afSmrg    pthread_mutex_lock(&c->iolock);
8732f94b0afSmrg
8742f94b0afSmrg    for (prev = &c->in.special_events; (s = *prev) != NULL; prev = &(s->next)) {
8752f94b0afSmrg        if (s == se) {
8762f94b0afSmrg            *prev = se->next;
8772f94b0afSmrg            for (events = se->events; events; events = next) {
8782f94b0afSmrg                next = events->next;
8792f94b0afSmrg                free (events->event);
8802f94b0afSmrg                free (events);
8812f94b0afSmrg            }
8822f94b0afSmrg            pthread_cond_destroy(&se->special_event_cond);
8832f94b0afSmrg            free (se);
8842f94b0afSmrg            break;
8852f94b0afSmrg        }
8862f94b0afSmrg    }
8872f94b0afSmrg    pthread_mutex_unlock(&c->iolock);
8882f94b0afSmrg}
8892f94b0afSmrg
890602e473dSmrg/* Private interface */
891602e473dSmrg
892602e473dSmrgint _xcb_in_init(_xcb_in *in)
893602e473dSmrg{
894602e473dSmrg    if(pthread_cond_init(&in->event_cond, 0))
895602e473dSmrg        return 0;
896602e473dSmrg    in->reading = 0;
897602e473dSmrg
898602e473dSmrg    in->queue_len = 0;
899602e473dSmrg
900602e473dSmrg    in->request_read = 0;
901602e473dSmrg    in->request_completed = 0;
902602e473dSmrg
903602e473dSmrg    in->replies = _xcb_map_new();
904602e473dSmrg    if(!in->replies)
905602e473dSmrg        return 0;
906602e473dSmrg
907602e473dSmrg    in->current_reply_tail = &in->current_reply;
908602e473dSmrg    in->events_tail = &in->events;
909602e473dSmrg    in->pending_replies_tail = &in->pending_replies;
910602e473dSmrg
911602e473dSmrg    return 1;
912602e473dSmrg}
913602e473dSmrg
914602e473dSmrgvoid _xcb_in_destroy(_xcb_in *in)
915602e473dSmrg{
916602e473dSmrg    pthread_cond_destroy(&in->event_cond);
917602e473dSmrg    free_reply_list(in->current_reply);
918602e473dSmrg    _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
919602e473dSmrg    while(in->events)
920602e473dSmrg    {
921602e473dSmrg        struct event_list *e = in->events;
922602e473dSmrg        in->events = e->next;
923602e473dSmrg        free(e->event);
924602e473dSmrg        free(e);
925602e473dSmrg    }
926602e473dSmrg    while(in->pending_replies)
927602e473dSmrg    {
928602e473dSmrg        pending_reply *pend = in->pending_replies;
929602e473dSmrg        in->pending_replies = pend->next;
930602e473dSmrg        free(pend);
931602e473dSmrg    }
932602e473dSmrg}
933602e473dSmrg
934b20a2039Smrgvoid _xcb_in_wake_up_next_reader(xcb_connection_t *c)
935b20a2039Smrg{
936b20a2039Smrg    int pthreadret;
937b20a2039Smrg    if(c->in.readers)
938b20a2039Smrg        pthreadret = pthread_cond_signal(c->in.readers->data);
939f1c62215Smrg    else if(c->in.special_waiters)
940f1c62215Smrg        pthreadret = pthread_cond_signal(&c->in.special_waiters->se->special_event_cond);
941b20a2039Smrg    else
942b20a2039Smrg        pthreadret = pthread_cond_signal(&c->in.event_cond);
943b20a2039Smrg    assert(pthreadret == 0);
944b20a2039Smrg}
945b20a2039Smrg
946602e473dSmrgint _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
947602e473dSmrg{
948602e473dSmrg    pending_reply *pend = malloc(sizeof(pending_reply));
949602e473dSmrg    assert(workaround != WORKAROUND_NONE || flags != 0);
950602e473dSmrg    if(!pend)
951602e473dSmrg    {
95221298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
953602e473dSmrg        return 0;
954602e473dSmrg    }
955602e473dSmrg    pend->first_request = pend->last_request = request;
956602e473dSmrg    pend->workaround = workaround;
957602e473dSmrg    pend->flags = flags;
958602e473dSmrg    pend->next = 0;
959602e473dSmrg    *c->in.pending_replies_tail = pend;
960602e473dSmrg    c->in.pending_replies_tail = &pend->next;
961602e473dSmrg    return 1;
962602e473dSmrg}
963602e473dSmrg
964602e473dSmrgvoid _xcb_in_replies_done(xcb_connection_t *c)
965602e473dSmrg{
966602e473dSmrg    struct pending_reply *pend;
967602e473dSmrg    if (c->in.pending_replies_tail != &c->in.pending_replies)
968602e473dSmrg    {
969602e473dSmrg        pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
970602e473dSmrg        if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
971602e473dSmrg        {
972ee424013Smrg            if (XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->out.request)) {
973ee424013Smrg                pend->last_request = c->out.request;
974ee424013Smrg                pend->workaround = WORKAROUND_NONE;
975ee424013Smrg            } else {
976ee424013Smrg                /* The socket was taken, but no requests were actually sent
977ee424013Smrg                 * so just discard the pending_reply that was created.
978ee424013Smrg                 */
979ee424013Smrg                struct pending_reply **prev_next = &c->in.pending_replies;
980ee424013Smrg                while (*prev_next != pend)
981ee424013Smrg                    prev_next = &(*prev_next)->next;
982ee424013Smrg                *prev_next = NULL;
983ee424013Smrg                c->in.pending_replies_tail = prev_next;
984ee424013Smrg                free(pend);
985ee424013Smrg            }
986602e473dSmrg        }
987602e473dSmrg    }
988602e473dSmrg}
989602e473dSmrg
990602e473dSmrgint _xcb_in_read(xcb_connection_t *c)
991602e473dSmrg{
9922f94b0afSmrg    int n;
9932f94b0afSmrg
9942f94b0afSmrg#if HAVE_SENDMSG
9952f94b0afSmrg    struct iovec    iov = {
9962f94b0afSmrg        .iov_base = c->in.queue + c->in.queue_len,
9972f94b0afSmrg        .iov_len = sizeof(c->in.queue) - c->in.queue_len,
9982f94b0afSmrg    };
9992f94b0afSmrg    union {
10002f94b0afSmrg        struct cmsghdr cmsghdr;
10012f94b0afSmrg        char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
10022f94b0afSmrg    } cmsgbuf;
10032f94b0afSmrg    struct msghdr msg = {
10042f94b0afSmrg        .msg_name = NULL,
10052f94b0afSmrg        .msg_namelen = 0,
10062f94b0afSmrg        .msg_iov = &iov,
10072f94b0afSmrg        .msg_iovlen = 1,
10082f94b0afSmrg        .msg_control = cmsgbuf.buf,
10092f94b0afSmrg        .msg_controllen = CMSG_SPACE(sizeof(int) * (XCB_MAX_PASS_FD - c->in.in_fd.nfd)),
10102f94b0afSmrg    };
10112f94b0afSmrg    n = recvmsg(c->fd, &msg, 0);
10122f94b0afSmrg
10132f94b0afSmrg    /* Check for truncation errors. Only MSG_CTRUNC is
10142f94b0afSmrg     * probably possible here, which would indicate that
10152f94b0afSmrg     * the sender tried to transmit more than XCB_MAX_PASS_FD
10162f94b0afSmrg     * file descriptors.
10172f94b0afSmrg     */
10182f94b0afSmrg    if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
10192f94b0afSmrg        _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
10202f94b0afSmrg        return 0;
10212f94b0afSmrg    }
10222f94b0afSmrg#else
10232f94b0afSmrg    n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len, 0);
10242f94b0afSmrg#endif
10252f94b0afSmrg    if(n > 0) {
10262f94b0afSmrg#if HAVE_SENDMSG
10272f94b0afSmrg        struct cmsghdr *hdr;
10282f94b0afSmrg
10292f94b0afSmrg        if (msg.msg_controllen >= sizeof (struct cmsghdr)) {
10302f94b0afSmrg            for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) {
10312f94b0afSmrg                if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) {
10322f94b0afSmrg                    int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int);
10332f94b0afSmrg                    memcpy(&c->in.in_fd.fd[c->in.in_fd.nfd], CMSG_DATA(hdr), nfd * sizeof (int));
10342f94b0afSmrg                    c->in.in_fd.nfd += nfd;
10352f94b0afSmrg                }
10362f94b0afSmrg            }
10372f94b0afSmrg        }
10382f94b0afSmrg#endif
103973167859Smrg        c->in.total_read += n;
1040602e473dSmrg        c->in.queue_len += n;
10412f94b0afSmrg    }
1042602e473dSmrg    while(read_packet(c))
1043602e473dSmrg        /* empty */;
10442f94b0afSmrg#if HAVE_SENDMSG
10452f94b0afSmrg    if (c->in.in_fd.nfd) {
10462f94b0afSmrg        c->in.in_fd.nfd -= c->in.in_fd.ifd;
10472f94b0afSmrg        memmove(&c->in.in_fd.fd[0],
10482f94b0afSmrg                &c->in.in_fd.fd[c->in.in_fd.ifd],
10492f94b0afSmrg                c->in.in_fd.nfd * sizeof (int));
10502f94b0afSmrg        c->in.in_fd.ifd = 0;
10512f94b0afSmrg
10522f94b0afSmrg        /* If we have any left-over file descriptors after emptying
10532f94b0afSmrg         * the input buffer, then the server sent some that we weren't
10542f94b0afSmrg         * expecting.  Close them and mark the connection as broken;
10552f94b0afSmrg         */
10562f94b0afSmrg        if (c->in.queue_len == 0 && c->in.in_fd.nfd != 0) {
10572f94b0afSmrg            int i;
10582f94b0afSmrg            for (i = 0; i < c->in.in_fd.nfd; i++)
10592f94b0afSmrg                close(c->in.in_fd.fd[i]);
10602f94b0afSmrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_FDPASSING_FAILED);
10612f94b0afSmrg            return 0;
10622f94b0afSmrg        }
10632f94b0afSmrg    }
10642f94b0afSmrg#endif
106521298544Smrg#ifndef _WIN32
106673167859Smrg    if((n > 0) || (n < 0 && (errno == EAGAIN || errno == EINTR)))
106721298544Smrg#else
106821298544Smrg    if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
106921298544Smrg#endif /* !_WIN32 */
1070602e473dSmrg        return 1;
107121298544Smrg    _xcb_conn_shutdown(c, XCB_CONN_ERROR);
1072602e473dSmrg    return 0;
1073602e473dSmrg}
1074602e473dSmrg
1075602e473dSmrgint _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
1076602e473dSmrg{
1077602e473dSmrg    int done = c->in.queue_len;
1078602e473dSmrg    if(len < done)
1079602e473dSmrg        done = len;
1080602e473dSmrg
1081602e473dSmrg    memcpy(buf, c->in.queue, done);
1082602e473dSmrg    c->in.queue_len -= done;
1083602e473dSmrg    memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
1084602e473dSmrg
1085602e473dSmrg    if(len > done)
1086602e473dSmrg    {
1087602e473dSmrg        int ret = read_block(c->fd, (char *) buf + done, len - done);
1088602e473dSmrg        if(ret <= 0)
1089602e473dSmrg        {
109021298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_ERROR);
1091602e473dSmrg            return ret;
1092602e473dSmrg        }
1093602e473dSmrg    }
1094602e473dSmrg
1095602e473dSmrg    return len;
1096602e473dSmrg}
1097