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/* Connection management: the core of XCB. */
27602e473dSmrg
2821298544Smrg#ifdef HAVE_CONFIG_H
2921298544Smrg#include "config.h"
3021298544Smrg#endif
3121298544Smrg
32602e473dSmrg#include <assert.h>
33602e473dSmrg#include <string.h>
34602e473dSmrg#include <stdio.h>
35602e473dSmrg#include <stdlib.h>
36602e473dSmrg#include <fcntl.h>
37602e473dSmrg#include <errno.h>
3821298544Smrg#include <limits.h>
39602e473dSmrg
40602e473dSmrg#include "xcb.h"
41602e473dSmrg#include "xcbint.h"
42602e473dSmrg#if USE_POLL
43602e473dSmrg#include <poll.h>
4421298544Smrg#elif !defined _WIN32
45602e473dSmrg#include <sys/select.h>
46602e473dSmrg#endif
47602e473dSmrg
4821298544Smrg#ifdef _WIN32
4921298544Smrg#include "xcb_windefs.h"
508ffb90f1Smrg#include <io.h>
5121298544Smrg#else
528ffb90f1Smrg#include <unistd.h>
5321298544Smrg#include <sys/socket.h>
5421298544Smrg#include <netinet/in.h>
5521298544Smrg#endif /* _WIN32 */
5621298544Smrg
5721298544Smrg/* SHUT_RDWR is fairly recent and is not available on all platforms */
5821298544Smrg#if !defined(SHUT_RDWR)
5921298544Smrg#define SHUT_RDWR 2
6021298544Smrg#endif
6121298544Smrg
62602e473dSmrgtypedef struct {
63602e473dSmrg    uint8_t  status;
64602e473dSmrg    uint8_t  pad0[5];
65602e473dSmrg    uint16_t length;
66602e473dSmrg} xcb_setup_generic_t;
67602e473dSmrg
687204935cSmrgstatic const xcb_setup_t xcb_error_setup = {
697204935cSmrg    0,     /* status: failed (but we wouldn't have a xcb_setup_t in this case) */
707204935cSmrg    0,     /* pad0 */
717204935cSmrg    0, 0,  /* protocol version, should be 11.0, but isn't */
727204935cSmrg    0,     /* length, invalid value */
737204935cSmrg    0,     /* release_number */
747204935cSmrg    0, 0,  /* resource_id_{base,mask} */
757204935cSmrg    0,     /* motion_buffer_size */
767204935cSmrg    0,     /* vendor_len */
777204935cSmrg    0,     /* maximum_request_length */
787204935cSmrg    0,     /* roots_len */
797204935cSmrg    0,     /* pixmap_formats_len */
807204935cSmrg    0,     /* image_byte_order */
817204935cSmrg    0,     /* bitmap_format_bit_order */
827204935cSmrg    0,     /* bitmap_format_scanline_unit */
837204935cSmrg    0,     /* bitmap_format_scanline_pad */
847204935cSmrg    0, 0,  /* {min,max}_keycode */
857204935cSmrg    { 0, 0, 0, 0 } /* pad1 */
867204935cSmrg};
877204935cSmrg
881c7386f4Smrg/* Keep this list in sync with is_static_error_conn()! */
8921298544Smrgstatic const int xcb_con_error = XCB_CONN_ERROR;
9021298544Smrgstatic const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
9121298544Smrgstatic const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR;
9221298544Smrgstatic const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN;
93602e473dSmrg
941c7386f4Smrgstatic int is_static_error_conn(xcb_connection_t *c)
951c7386f4Smrg{
961c7386f4Smrg    return c == (xcb_connection_t *) &xcb_con_error ||
971c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_mem_er ||
981c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_parse_er ||
991c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_screen_er;
1001c7386f4Smrg}
1011c7386f4Smrg
102602e473dSmrgstatic int set_fd_flags(const int fd)
103602e473dSmrg{
10421298544Smrg/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
10521298544Smrg
10621298544Smrg#ifdef _WIN32
1071c7386f4Smrg   u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
10821298544Smrg   int ret = 0;
10921298544Smrg
11021298544Smrg   ret = ioctlsocket(fd, FIONBIO, &iMode);
1111c7386f4Smrg   if(ret != 0)
11221298544Smrg       return 0;
11321298544Smrg   return 1;
11421298544Smrg#else
115602e473dSmrg    int flags = fcntl(fd, F_GETFL, 0);
116602e473dSmrg    if(flags == -1)
117602e473dSmrg        return 0;
118602e473dSmrg    flags |= O_NONBLOCK;
119602e473dSmrg    if(fcntl(fd, F_SETFL, flags) == -1)
120602e473dSmrg        return 0;
121602e473dSmrg    if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
122602e473dSmrg        return 0;
123602e473dSmrg    return 1;
12421298544Smrg#endif /* _WIN32 */
125602e473dSmrg}
126602e473dSmrg
127602e473dSmrgstatic int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
128602e473dSmrg{
129602e473dSmrg    static const char pad[3];
130602e473dSmrg    xcb_setup_request_t out;
131602e473dSmrg    struct iovec parts[6];
132602e473dSmrg    int count = 0;
133602e473dSmrg    static const uint32_t endian = 0x01020304;
134602e473dSmrg    int ret;
135602e473dSmrg
136602e473dSmrg    memset(&out, 0, sizeof(out));
137602e473dSmrg
138602e473dSmrg    /* B = 0x42 = MSB first, l = 0x6c = LSB first */
139602e473dSmrg    if(htonl(endian) == endian)
140602e473dSmrg        out.byte_order = 0x42;
141602e473dSmrg    else
142602e473dSmrg        out.byte_order = 0x6c;
143602e473dSmrg    out.protocol_major_version = X_PROTOCOL;
144602e473dSmrg    out.protocol_minor_version = X_PROTOCOL_REVISION;
145602e473dSmrg    out.authorization_protocol_name_len = 0;
146602e473dSmrg    out.authorization_protocol_data_len = 0;
147602e473dSmrg    parts[count].iov_len = sizeof(xcb_setup_request_t);
148602e473dSmrg    parts[count++].iov_base = &out;
149602e473dSmrg    parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
150602e473dSmrg    parts[count++].iov_base = (char *) pad;
151602e473dSmrg
152602e473dSmrg    if(auth_info)
153602e473dSmrg    {
154602e473dSmrg        parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
155602e473dSmrg        parts[count++].iov_base = auth_info->name;
156602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
157602e473dSmrg        parts[count++].iov_base = (char *) pad;
158602e473dSmrg        parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
159602e473dSmrg        parts[count++].iov_base = auth_info->data;
160602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
161602e473dSmrg        parts[count++].iov_base = (char *) pad;
162602e473dSmrg    }
163602e473dSmrg    assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
164602e473dSmrg
165602e473dSmrg    pthread_mutex_lock(&c->iolock);
166b20a2039Smrg    ret = _xcb_out_send(c, parts, count);
167602e473dSmrg    pthread_mutex_unlock(&c->iolock);
168602e473dSmrg    return ret;
169602e473dSmrg}
170602e473dSmrg
171602e473dSmrgstatic int read_setup(xcb_connection_t *c)
172602e473dSmrg{
1738ffb90f1Smrg    const char newline = '\n';
1748ffb90f1Smrg
175602e473dSmrg    /* Read the server response */
176602e473dSmrg    c->setup = malloc(sizeof(xcb_setup_generic_t));
177602e473dSmrg    if(!c->setup)
178602e473dSmrg        return 0;
179602e473dSmrg
180602e473dSmrg    if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
181602e473dSmrg        return 0;
182602e473dSmrg
183602e473dSmrg    {
184602e473dSmrg        void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
185602e473dSmrg        if(!tmp)
186602e473dSmrg            return 0;
187602e473dSmrg        c->setup = tmp;
188602e473dSmrg    }
189602e473dSmrg
190602e473dSmrg    if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
191602e473dSmrg        return 0;
192602e473dSmrg
193602e473dSmrg    /* 0 = failed, 2 = authenticate, 1 = success */
194602e473dSmrg    switch(c->setup->status)
195602e473dSmrg    {
196602e473dSmrg    case 0: /* failed */
197602e473dSmrg        {
198602e473dSmrg            xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
199602e473dSmrg            write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
2008ffb90f1Smrg            write(STDERR_FILENO, &newline, 1);
201602e473dSmrg            return 0;
202602e473dSmrg        }
203602e473dSmrg
204602e473dSmrg    case 2: /* authenticate */
205602e473dSmrg        {
206602e473dSmrg            xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
207602e473dSmrg            write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
2088ffb90f1Smrg            write(STDERR_FILENO, &newline, 1);
209602e473dSmrg            return 0;
210602e473dSmrg        }
211602e473dSmrg    }
212602e473dSmrg
213602e473dSmrg    return 1;
214602e473dSmrg}
215602e473dSmrg
216602e473dSmrg/* precondition: there must be something for us to write. */
217602e473dSmrgstatic int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
218602e473dSmrg{
2198ffb90f1Smrg#ifndef _WIN32
220602e473dSmrg    int n;
2218ffb90f1Smrg#endif
2228ffb90f1Smrg
223602e473dSmrg    assert(!c->out.queue_len);
22421298544Smrg
22521298544Smrg#ifdef _WIN32
22621298544Smrg    /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
22721298544Smrg       an iovec would require more work and I'm not sure of the benefit....works for now */
2288ffb90f1Smrg    while (*count)
2291c7386f4Smrg    {
2308ffb90f1Smrg        struct iovec *vec = *vector;
2318ffb90f1Smrg        if (vec->iov_len)
2328ffb90f1Smrg        {
2338ffb90f1Smrg            int ret = send(c->fd, vec->iov_base, vec->iov_len, 0);
2348ffb90f1Smrg            if (ret == SOCKET_ERROR)
2358ffb90f1Smrg            {
2368ffb90f1Smrg                int err = WSAGetLastError();
2378ffb90f1Smrg                if (err == WSAEWOULDBLOCK)
2388ffb90f1Smrg                {
2398ffb90f1Smrg                    return 1;
2408ffb90f1Smrg                }
2418ffb90f1Smrg            }
2428ffb90f1Smrg            if (ret <= 0)
2438ffb90f1Smrg            {
2448ffb90f1Smrg                _xcb_conn_shutdown(c, XCB_CONN_ERROR);
2458ffb90f1Smrg                return 0;
2468ffb90f1Smrg            }
2478ffb90f1Smrg            c->out.total_written += ret;
2488ffb90f1Smrg            vec->iov_len -= ret;
2498ffb90f1Smrg            vec->iov_base = (char *)vec->iov_base + ret;
2508ffb90f1Smrg        }
2518ffb90f1Smrg        if (vec->iov_len == 0) {
2528ffb90f1Smrg            (*vector)++;
2538ffb90f1Smrg            (*count)--;
2548ffb90f1Smrg        }
25521298544Smrg    }
2568ffb90f1Smrg
2578ffb90f1Smrg    if (!*count)
2588ffb90f1Smrg        *vector = 0;
2598ffb90f1Smrg
26021298544Smrg#else
26121298544Smrg    n = *count;
26221298544Smrg    if (n > IOV_MAX)
2631c7386f4Smrg        n = IOV_MAX;
26421298544Smrg
2651016ad83Smrg#if HAVE_SENDMSG
2661016ad83Smrg    if (c->out.out_fd.nfd) {
2671016ad83Smrg        union {
2681016ad83Smrg            struct cmsghdr cmsghdr;
2691016ad83Smrg            char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
2701016ad83Smrg        } cmsgbuf;
2711016ad83Smrg        struct msghdr msg = {
2721016ad83Smrg            .msg_name = NULL,
2731016ad83Smrg            .msg_namelen = 0,
2741016ad83Smrg            .msg_iov = *vector,
2751016ad83Smrg            .msg_iovlen = n,
2761016ad83Smrg            .msg_control = cmsgbuf.buf,
2771016ad83Smrg            .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)),
2781016ad83Smrg        };
2791016ad83Smrg        int i;
2801016ad83Smrg        struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
2811016ad83Smrg
2821016ad83Smrg        hdr->cmsg_len = msg.msg_controllen;
2831016ad83Smrg        hdr->cmsg_level = SOL_SOCKET;
2841016ad83Smrg        hdr->cmsg_type = SCM_RIGHTS;
2851016ad83Smrg        memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int));
2861016ad83Smrg
2871016ad83Smrg        n = sendmsg(c->fd, &msg, 0);
2881016ad83Smrg        if(n < 0 && errno == EAGAIN)
2891016ad83Smrg            return 1;
2901016ad83Smrg        for (i = 0; i < c->out.out_fd.nfd; i++)
2911016ad83Smrg            close(c->out.out_fd.fd[i]);
2921016ad83Smrg        c->out.out_fd.nfd = 0;
2931016ad83Smrg    } else
2941016ad83Smrg#endif
2951016ad83Smrg    {
2961016ad83Smrg        n = writev(c->fd, *vector, n);
2971016ad83Smrg        if(n < 0 && errno == EAGAIN)
2981016ad83Smrg            return 1;
2991016ad83Smrg    }
3001016ad83Smrg
301602e473dSmrg    if(n <= 0)
302602e473dSmrg    {
30321298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
304602e473dSmrg        return 0;
305602e473dSmrg    }
306602e473dSmrg
307aa30ed02Smrg    c->out.total_written += n;
308602e473dSmrg    for(; *count; --*count, ++*vector)
309602e473dSmrg    {
310602e473dSmrg        int cur = (*vector)->iov_len;
311602e473dSmrg        if(cur > n)
312602e473dSmrg            cur = n;
313b9526c6aSmrg        if(cur) {
314b9526c6aSmrg            (*vector)->iov_len -= cur;
315b9526c6aSmrg            (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
316b9526c6aSmrg            n -= cur;
317b9526c6aSmrg        }
318602e473dSmrg        if((*vector)->iov_len)
319602e473dSmrg            break;
320602e473dSmrg    }
321602e473dSmrg    if(!*count)
322602e473dSmrg        *vector = 0;
323602e473dSmrg    assert(n == 0);
3248ffb90f1Smrg
3258ffb90f1Smrg#endif /* _WIN32 */
3268ffb90f1Smrg
327602e473dSmrg    return 1;
328602e473dSmrg}
329602e473dSmrg
330602e473dSmrg/* Public interface */
331602e473dSmrg
332602e473dSmrgconst xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
333602e473dSmrg{
3347204935cSmrg    if(is_static_error_conn(c))
3357204935cSmrg        return &xcb_error_setup;
336602e473dSmrg    /* doesn't need locking because it's never written to. */
337602e473dSmrg    return c->setup;
338602e473dSmrg}
339602e473dSmrg
340602e473dSmrgint xcb_get_file_descriptor(xcb_connection_t *c)
341602e473dSmrg{
3427204935cSmrg    if(is_static_error_conn(c))
343602e473dSmrg        return -1;
344602e473dSmrg    /* doesn't need locking because it's never written to. */
345602e473dSmrg    return c->fd;
346602e473dSmrg}
347602e473dSmrg
348602e473dSmrgint xcb_connection_has_error(xcb_connection_t *c)
349602e473dSmrg{
350602e473dSmrg    /* doesn't need locking because it's read and written atomically. */
351602e473dSmrg    return c->has_error;
352602e473dSmrg}
353602e473dSmrg
354602e473dSmrgxcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
355602e473dSmrg{
356602e473dSmrg    xcb_connection_t* c;
357602e473dSmrg
35821298544Smrg#ifndef _WIN32
359602e473dSmrg#ifndef USE_POLL
360602e473dSmrg    if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
361602e473dSmrg    {
362602e473dSmrg        close(fd);
36321298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
364602e473dSmrg    }
365602e473dSmrg#endif
36621298544Smrg#endif /* !_WIN32*/
367602e473dSmrg
368602e473dSmrg    c = calloc(1, sizeof(xcb_connection_t));
369602e473dSmrg    if(!c) {
3708ffb90f1Smrg#ifdef _WIN32
3718ffb90f1Smrg        closesocket(fd);
3728ffb90f1Smrg#else
373602e473dSmrg        close(fd);
3748ffb90f1Smrg#endif
37521298544Smrg        return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
376602e473dSmrg    }
377602e473dSmrg
378602e473dSmrg    c->fd = fd;
379602e473dSmrg
380602e473dSmrg    if(!(
381602e473dSmrg        set_fd_flags(fd) &&
382602e473dSmrg        pthread_mutex_init(&c->iolock, 0) == 0 &&
383602e473dSmrg        _xcb_in_init(&c->in) &&
384602e473dSmrg        _xcb_out_init(&c->out) &&
385602e473dSmrg        write_setup(c, auth_info) &&
386602e473dSmrg        read_setup(c) &&
387602e473dSmrg        _xcb_ext_init(c) &&
388602e473dSmrg        _xcb_xid_init(c)
389602e473dSmrg        ))
390602e473dSmrg    {
391602e473dSmrg        xcb_disconnect(c);
39221298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
393602e473dSmrg    }
394602e473dSmrg
395602e473dSmrg    return c;
396602e473dSmrg}
397602e473dSmrg
398602e473dSmrgvoid xcb_disconnect(xcb_connection_t *c)
399602e473dSmrg{
4001c7386f4Smrg    if(c == NULL || is_static_error_conn(c))
401602e473dSmrg        return;
402602e473dSmrg
403602e473dSmrg    free(c->setup);
40421298544Smrg
40521298544Smrg    /* disallow further sends and receives */
40621298544Smrg    shutdown(c->fd, SHUT_RDWR);
4078ffb90f1Smrg#ifdef _WIN32
4088ffb90f1Smrg    closesocket(c->fd);
4098ffb90f1Smrg#else
410602e473dSmrg    close(c->fd);
4118ffb90f1Smrg#endif
412602e473dSmrg
413602e473dSmrg    pthread_mutex_destroy(&c->iolock);
414602e473dSmrg    _xcb_in_destroy(&c->in);
415602e473dSmrg    _xcb_out_destroy(&c->out);
416602e473dSmrg
417602e473dSmrg    _xcb_ext_destroy(c);
418602e473dSmrg    _xcb_xid_destroy(c);
419602e473dSmrg
420602e473dSmrg    free(c);
42121298544Smrg
42221298544Smrg#ifdef _WIN32
42321298544Smrg    WSACleanup();
42421298544Smrg#endif
425602e473dSmrg}
426602e473dSmrg
427602e473dSmrg/* Private interface */
428602e473dSmrg
42921298544Smrgvoid _xcb_conn_shutdown(xcb_connection_t *c, int err)
430602e473dSmrg{
43121298544Smrg    c->has_error = err;
43221298544Smrg}
43321298544Smrg
43421298544Smrg/* Return connection error state.
43521298544Smrg * To make thread-safe, I need a seperate static
43621298544Smrg * variable for every possible error.
4371c7386f4Smrg * has_error is the first field in xcb_connection_t, so just
4381c7386f4Smrg * return a casted int here; checking has_error (and only
4391c7386f4Smrg * has_error) will be safe.
44021298544Smrg */
44121298544Smrgxcb_connection_t *_xcb_conn_ret_error(int err)
44221298544Smrg{
44321298544Smrg
44421298544Smrg    switch(err)
44521298544Smrg    {
44621298544Smrg        case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
44721298544Smrg        {
44821298544Smrg            return (xcb_connection_t *) &xcb_con_closed_mem_er;
44921298544Smrg        }
45021298544Smrg        case XCB_CONN_CLOSED_PARSE_ERR:
45121298544Smrg        {
45221298544Smrg            return (xcb_connection_t *) &xcb_con_closed_parse_er;
45321298544Smrg        }
45421298544Smrg        case XCB_CONN_CLOSED_INVALID_SCREEN:
45521298544Smrg        {
45621298544Smrg            return (xcb_connection_t *) &xcb_con_closed_screen_er;
45721298544Smrg        }
45821298544Smrg        case XCB_CONN_ERROR:
45921298544Smrg        default:
46021298544Smrg        {
46121298544Smrg            return (xcb_connection_t *) &xcb_con_error;
46221298544Smrg        }
46321298544Smrg    }
464602e473dSmrg}
465602e473dSmrg
466602e473dSmrgint _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
467602e473dSmrg{
468602e473dSmrg    int ret;
469602e473dSmrg#if USE_POLL
470602e473dSmrg    struct pollfd fd;
471602e473dSmrg#else
472602e473dSmrg    fd_set rfds, wfds;
473602e473dSmrg#endif
474602e473dSmrg
475602e473dSmrg    /* If the thing I should be doing is already being done, wait for it. */
476602e473dSmrg    if(count ? c->out.writing : c->in.reading)
477602e473dSmrg    {
478602e473dSmrg        pthread_cond_wait(cond, &c->iolock);
479602e473dSmrg        return 1;
480602e473dSmrg    }
481602e473dSmrg
482602e473dSmrg#if USE_POLL
483602e473dSmrg    memset(&fd, 0, sizeof(fd));
484602e473dSmrg    fd.fd = c->fd;
485602e473dSmrg    fd.events = POLLIN;
486602e473dSmrg#else
487602e473dSmrg    FD_ZERO(&rfds);
488602e473dSmrg    FD_SET(c->fd, &rfds);
489602e473dSmrg#endif
490602e473dSmrg    ++c->in.reading;
491602e473dSmrg
492602e473dSmrg#if USE_POLL
493602e473dSmrg    if(count)
494602e473dSmrg    {
495602e473dSmrg        fd.events |= POLLOUT;
496602e473dSmrg        ++c->out.writing;
497602e473dSmrg    }
498602e473dSmrg#else
499602e473dSmrg    FD_ZERO(&wfds);
500602e473dSmrg    if(count)
501602e473dSmrg    {
502602e473dSmrg        FD_SET(c->fd, &wfds);
503602e473dSmrg        ++c->out.writing;
504602e473dSmrg    }
505602e473dSmrg#endif
506602e473dSmrg
507602e473dSmrg    pthread_mutex_unlock(&c->iolock);
508602e473dSmrg    do {
509602e473dSmrg#if USE_POLL
510b20a2039Smrg        ret = poll(&fd, 1, -1);
51121298544Smrg        /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
51221298544Smrg         * it as if it failed. */
51321298544Smrg        if(ret >= 0 && (fd.revents & ~fd.events))
51421298544Smrg        {
51521298544Smrg            ret = -1;
51621298544Smrg            break;
51721298544Smrg        }
518602e473dSmrg#else
519b20a2039Smrg        ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
520602e473dSmrg#endif
521602e473dSmrg    } while (ret == -1 && errno == EINTR);
522b20a2039Smrg    if(ret < 0)
523602e473dSmrg    {
52421298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
525b20a2039Smrg        ret = 0;
526602e473dSmrg    }
527602e473dSmrg    pthread_mutex_lock(&c->iolock);
528602e473dSmrg
529602e473dSmrg    if(ret)
530602e473dSmrg    {
53121298544Smrg        /* The code allows two threads to call select()/poll() at the same time.
53221298544Smrg         * First thread just wants to read, a second thread wants to write, too.
53321298544Smrg         * We have to make sure that we don't steal the reading thread's reply
53421298544Smrg         * and let it get stuck in select()/poll().
53521298544Smrg         * So a thread may read if either:
53621298544Smrg         * - There is no other thread that wants to read (the above situation
53721298544Smrg         *   did not occur).
53821298544Smrg         * - It is the reading thread (above situation occurred).
53921298544Smrg         */
54021298544Smrg        int may_read = c->in.reading == 1 || !count;
541602e473dSmrg#if USE_POLL
5421016ad83Smrg        if(may_read && (fd.revents & POLLIN) != 0)
543602e473dSmrg#else
54421298544Smrg        if(may_read && FD_ISSET(c->fd, &rfds))
545602e473dSmrg#endif
546602e473dSmrg            ret = ret && _xcb_in_read(c);
547602e473dSmrg
548602e473dSmrg#if USE_POLL
5491016ad83Smrg        if((fd.revents & POLLOUT) != 0)
550602e473dSmrg#else
551602e473dSmrg        if(FD_ISSET(c->fd, &wfds))
552602e473dSmrg#endif
553602e473dSmrg            ret = ret && write_vec(c, vector, count);
554602e473dSmrg    }
555602e473dSmrg
556602e473dSmrg    if(count)
557602e473dSmrg        --c->out.writing;
558602e473dSmrg    --c->in.reading;
559602e473dSmrg
560602e473dSmrg    return ret;
561602e473dSmrg}
562aa30ed02Smrg
563aa30ed02Smrguint64_t xcb_total_read(xcb_connection_t *c)
564aa30ed02Smrg{
565aa30ed02Smrg    uint64_t n;
566aa30ed02Smrg
567aa30ed02Smrg    if (xcb_connection_has_error(c))
568aa30ed02Smrg        return 0;
569aa30ed02Smrg
570aa30ed02Smrg    pthread_mutex_lock(&c->iolock);
571aa30ed02Smrg    n = c->in.total_read;
572aa30ed02Smrg    pthread_mutex_unlock(&c->iolock);
573aa30ed02Smrg    return n;
574aa30ed02Smrg}
575aa30ed02Smrg
576aa30ed02Smrguint64_t xcb_total_written(xcb_connection_t *c)
577aa30ed02Smrg{
578aa30ed02Smrg    uint64_t n;
579aa30ed02Smrg
580aa30ed02Smrg    if (xcb_connection_has_error(c))
581aa30ed02Smrg        return 0;
582aa30ed02Smrg
583aa30ed02Smrg    pthread_mutex_lock(&c->iolock);
584aa30ed02Smrg    n = c->out.total_written;
585aa30ed02Smrg    pthread_mutex_unlock(&c->iolock);
586aa30ed02Smrg
587aa30ed02Smrg    return n;
588aa30ed02Smrg}
589