aboutsummaryrefslogtreecommitdiff
path: root/libxcb/src
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2012-01-13 17:04:47 +0100
committermarha <marha@users.sourceforge.net>2012-01-13 17:04:47 +0100
commite2e1bdced7e09e39fed613dae1f2f3096cb18118 (patch)
tree63c7066c79d1053c7656b6175e22e2ced6f11e01 /libxcb/src
parentcc96d00cae473f472e371328ce8dc572ad258073 (diff)
parent5cfbe97cd797d8f78ece208bb5114704b83d8aab (diff)
downloadvcxsrv-e2e1bdced7e09e39fed613dae1f2f3096cb18118.tar.gz
vcxsrv-e2e1bdced7e09e39fed613dae1f2f3096cb18118.tar.bz2
vcxsrv-e2e1bdced7e09e39fed613dae1f2f3096cb18118.zip
Merge remote-tracking branch 'origin/released'
Conflicts: libxcb/src/xcb_auth.c libxcb/src/xcb_conn.c libxcb/src/xcb_out.c libxcb/src/xcb_util.c xorg-server/dix/dispatch.c xorg-server/include/dixstruct.h xorg-server/include/misc.h xorg-server/os/connection.c
Diffstat (limited to 'libxcb/src')
-rw-r--r--libxcb/src/xcb.h24
-rw-r--r--libxcb/src/xcb_auth.c9
-rw-r--r--libxcb/src/xcb_conn.c907
-rw-r--r--libxcb/src/xcb_in.c14
-rw-r--r--libxcb/src/xcb_out.c752
-rw-r--r--libxcb/src/xcb_util.c19
-rw-r--r--libxcb/src/xcbint.h7
7 files changed, 900 insertions, 832 deletions
diff --git a/libxcb/src/xcb.h b/libxcb/src/xcb.h
index 3ee7965d2..44f650ad7 100644
--- a/libxcb/src/xcb.h
+++ b/libxcb/src/xcb.h
@@ -69,6 +69,21 @@ extern "C" {
/** X_TCP_PORT + display number = server port for TCP transport */
#define X_TCP_PORT 6000
+/** xcb connection errors because of socket, pipe and other stream errors. */
+#define XCB_CONN_ERROR 1
+
+/** xcb connection shutdown because of extension not sppported */
+#define XCB_CONN_CLOSED_EXT_NOTSUPPORTED 2
+
+/** malloc(), calloc() and realloc() error upon failure, for eg ENOMEM */
+#define XCB_CONN_CLOSED_MEM_INSUFFICIENT 3
+
+/** Connection closed, exceeding request length that server accepts. */
+#define XCB_CONN_CLOSED_REQ_LEN_EXCEED 4
+
+/** Connection closed, error during parsing display string. */
+#define XCB_CONN_CLOSED_PARSE_ERR 5
+
#define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1))
/* Opaque structures */
@@ -396,15 +411,18 @@ int xcb_get_file_descriptor(xcb_connection_t *c);
/**
* @brief Test whether the connection has shut down due to a fatal error.
* @param c: The connection.
- * @return 1 if the connection is in an error state; 0 otherwise.
+ * @return > 0 if the connection is in an error state; 0 otherwise.
*
* Some errors that occur in the context of an xcb_connection_t
* are unrecoverable. When such an error occurs, the
* connection is shut down and further operations on the
* xcb_connection_t have no effect.
*
- * @todo Other functions should document the conditions in
- * which they shut down the connection.
+ * @return XCB_CONN_ERROR, because of socket errors, pipe errors or other stream errors.
+ * @return XCB_CONN_CLOSED_EXT_NOTSUPPORTED, when extension not supported.
+ * @return XCB_CONN_CLOSED_MEM_INSUFFICIENT, when memory not available.
+ * @return XCB_CONN_CLOSED_REQ_LEN_EXCEED, exceeding request length that server accepts.
+ * @return XCB_CONN_CLOSED_PARSE_ERR, error during parsing display string.
*/
int xcb_connection_has_error(xcb_connection_t *c);
diff --git a/libxcb/src/xcb_auth.c b/libxcb/src/xcb_auth.c
index aeb2374ed..ec1d2027a 100644
--- a/libxcb/src/xcb_auth.c
+++ b/libxcb/src/xcb_auth.c
@@ -31,7 +31,6 @@
#include <unistd.h>
#include <stdlib.h>
#ifndef _MSC_VER
-#include <arpa/inet.h>
#endif
#ifdef __INTERIX
@@ -39,9 +38,14 @@
#include <rpc/types.h>
#endif
+#ifdef HASXDMAUTH
+#include <X11/Xdmcp.h>
+#endif
+
#ifdef _WIN32
#include "xcb_windefs.h"
#else
+#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
@@ -50,9 +54,6 @@
#include "xcb.h"
#include "xcbint.h"
-#ifdef HASXDMAUTH
-#include <X11/Xdmcp.h>
-#endif
enum auth_protos {
#ifdef HASXDMAUTH
diff --git a/libxcb/src/xcb_conn.c b/libxcb/src/xcb_conn.c
index 4c5b613e7..ed6f06bdd 100644
--- a/libxcb/src/xcb_conn.c
+++ b/libxcb/src/xcb_conn.c
@@ -1,438 +1,469 @@
-/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice, the names of the authors or their
- * institutions shall not be used in advertising or otherwise to promote the
- * sale, use or other dealings in this Software without prior written
- * authorization from the authors.
- */
-
-/* Connection management: the core of XCB. */
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include "xcb.h"
-#include "xcbint.h"
-#if USE_POLL
-#include <poll.h>
-#elif !defined _WIN32
-#include <sys/select.h>
-#endif
-
-#ifdef _WIN32
-#include "xcb_windefs.h"
-#else
-#include <sys/socket.h>
-#include <netinet/in.h>
-#endif /* _WIN32 */
-
-#include <X11/Xtrans/Xtrans.h>
-
-#ifdef _MSC_VER
-#define _close(fd) closesocket(fd)
-#endif
-
-/* SHUT_RDWR is fairly recent and is not available on all platforms */
-#if !defined(SHUT_RDWR)
-#define SHUT_RDWR 2
-#endif
-
-typedef struct {
- uint8_t status;
- uint8_t pad0[5];
- uint16_t length;
-} xcb_setup_generic_t;
-
-const int error_connection = 1;
-
-static int set_fd_flags(const int fd)
-{
-/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
-
-#ifdef _WIN32
- u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
- int ret;
-
- ret = ioctlsocket(fd, FIONBIO, &iMode);
- if(ret != 0)
- return 0;
- return 1;
-#else
- int flags = fcntl(fd, F_GETFL, 0);
- if(flags == -1)
- return 0;
- flags |= O_NONBLOCK;
- if(fcntl(fd, F_SETFL, flags) == -1)
- return 0;
- if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
- return 0;
- return 1;
-#endif /* _WIN32 */
-}
-
-static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
-{
- static const char pad[3];
- xcb_setup_request_t out;
- struct iovec parts[6];
- int count = 0;
- static const uint32_t endian = 0x01020304;
- int ret;
-
- memset(&out, 0, sizeof(out));
-
- /* B = 0x42 = MSB first, l = 0x6c = LSB first */
- if(htonl(endian) == endian)
- out.byte_order = 0x42;
- else
- out.byte_order = 0x6c;
- out.protocol_major_version = X_PROTOCOL;
- out.protocol_minor_version = X_PROTOCOL_REVISION;
- out.authorization_protocol_name_len = 0;
- out.authorization_protocol_data_len = 0;
- parts[count].iov_len = sizeof(xcb_setup_request_t);
- parts[count++].iov_base = (caddr_t) &out;
- parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
- parts[count++].iov_base = (char *) pad;
-
- if(auth_info)
- {
- parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
- parts[count++].iov_base = auth_info->name;
- parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
- parts[count++].iov_base = (char *) pad;
- parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
- parts[count++].iov_base = auth_info->data;
- parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
- parts[count++].iov_base = (char *) pad;
- }
- assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
-
- pthread_mutex_lock(&c->iolock);
- ret = _xcb_out_send(c, parts, count);
- pthread_mutex_unlock(&c->iolock);
- return ret;
-}
-
-static int read_setup(xcb_connection_t *c)
-{
- /* Read the server response */
- c->setup = malloc(sizeof(xcb_setup_generic_t));
- if(!c->setup)
- return 0;
-
- if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
- return 0;
-
- {
- void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
- if(!tmp)
- return 0;
- c->setup = tmp;
- }
-
- if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
- return 0;
-
- /* 0 = failed, 2 = authenticate, 1 = success */
- switch(c->setup->status)
- {
- case 0: /* failed */
- {
- xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
- write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
- return 0;
- }
-
- case 2: /* authenticate */
- {
- xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
- write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
- return 0;
- }
- }
-
- return 1;
-}
-
-/* precondition: there must be something for us to write. */
-static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
-{
- int n;
-
-#ifdef _WIN32
- int i = 0;
- int cnt=*count;
- struct iovec *vec;
- n = 0;
- assert(!c->out.queue_len);
-
- /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
- an iovec would require more work and I'm not sure of the benefit....works for now */
- vec = *vector;
- while(i < cnt)
- {
- char *p= vec->iov_base;
- size_t l= vec->iov_len;
- while (l > 0)
- {
- int ret = send(c->fd, p, l, 0);
- if(ret == SOCKET_ERROR)
- {
- int err = WSAGetLastError();
- if(err == WSAEWOULDBLOCK)
- {
- if (n)
- {
- /* already return the data */
- i=cnt;
- break;
- }
- else
- return 1;
- }
- }
- p += ret;
- l -= ret;
- n += ret;
- }
- vec++;
- i++;
- }
-#else
- assert(!c->out.queue_len);
- n = writev(c->fd, *vector, *count);
- if(n < 0 && errno == EAGAIN)
- return 1;
-#endif /* _WIN32 */
-
- if(n <= 0)
- {
- _xcb_conn_shutdown(c);
- return 0;
- }
-
- for(; *count; --*count, ++*vector)
- {
- int cur = (*vector)->iov_len;
- if(cur > n)
- cur = n;
- (*vector)->iov_len -= cur;
- (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
- n -= cur;
- if((*vector)->iov_len)
- break;
- }
- if(!*count)
- *vector = 0;
- assert(n == 0);
- return 1;
-}
-
-/* Public interface */
-
-const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
-{
- if(c->has_error)
- return 0;
- /* doesn't need locking because it's never written to. */
- return c->setup;
-}
-
-int xcb_get_file_descriptor(xcb_connection_t *c)
-{
- if(c->has_error)
- return -1;
- /* doesn't need locking because it's never written to. */
- return c->fd;
-}
-
-int xcb_connection_has_error(xcb_connection_t *c)
-{
- /* doesn't need locking because it's read and written atomically. */
- return c->has_error;
-}
-
-xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
-{
- xcb_connection_t* c;
-
-#ifndef _WIN32
-#ifndef USE_POLL
- if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
- {
- close(fd);
- return (xcb_connection_t *) &error_connection;
- }
-#endif
-#endif /* !_WIN32*/
-
- c = calloc(1, sizeof(xcb_connection_t));
- if(!c) {
- close(fd);
- return (xcb_connection_t *) &error_connection;
- }
-
- c->fd = fd;
-
- if(!(
- set_fd_flags(fd) &&
- pthread_mutex_init(&c->iolock, 0) == 0 &&
- _xcb_in_init(&c->in) &&
- _xcb_out_init(&c->out) &&
- write_setup(c, auth_info) &&
- read_setup(c) &&
- _xcb_ext_init(c) &&
- _xcb_xid_init(c)
- ))
- {
- xcb_disconnect(c);
- return (xcb_connection_t *) &error_connection;
- }
-
- return c;
-}
-
-void xcb_disconnect(xcb_connection_t *c)
-{
- if(c == (xcb_connection_t *) &error_connection)
- return;
-
- free(c->setup);
-
- /* disallow further sends and receives */
- shutdown(c->fd, SHUT_RDWR);
- close(c->fd);
-
- pthread_mutex_destroy(&c->iolock);
- _xcb_in_destroy(&c->in);
- _xcb_out_destroy(&c->out);
-
- _xcb_ext_destroy(c);
- _xcb_xid_destroy(c);
-
- free(c);
-}
-
-/* Private interface */
-
-void _xcb_conn_shutdown(xcb_connection_t *c)
-{
- c->has_error = 1;
-}
-
-int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
-{
- int ret;
-#if USE_POLL
- struct pollfd fd;
-#else
- fd_set rfds, wfds;
-#endif
-
- /* If the thing I should be doing is already being done, wait for it. */
- if(count ? c->out.writing : c->in.reading)
- {
- pthread_cond_wait(cond, &c->iolock);
- return 1;
- }
-
-#if USE_POLL
- memset(&fd, 0, sizeof(fd));
- fd.fd = c->fd;
- fd.events = POLLIN;
-#else
- FD_ZERO(&rfds);
- FD_SET(c->fd, &rfds);
-#endif
- ++c->in.reading;
-
-#if USE_POLL
- if(count)
- {
- fd.events |= POLLOUT;
- ++c->out.writing;
- }
-#else
- FD_ZERO(&wfds);
- if(count)
- {
- FD_SET(c->fd, &wfds);
- ++c->out.writing;
- }
-#endif
-
- pthread_mutex_unlock(&c->iolock);
- do {
-#if USE_POLL
- ret = poll(&fd, 1, -1);
- /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
- * it as if it failed. */
- if(ret >= 0 && (fd.revents & ~fd.events))
- {
- ret = -1;
- break;
- }
-#else
- ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
- if (ret==SOCKET_ERROR)
- {
- ret=-1;
- errno = WSAGetLastError();
- if (errno == WSAEINTR)
- errno=EINTR;
- }
-#endif
- } while (ret == -1 && errno == EINTR);
- if(ret < 0)
- {
- _xcb_conn_shutdown(c);
- ret = 0;
- }
- pthread_mutex_lock(&c->iolock);
-
- if(ret)
- {
-#if USE_POLL
- if((fd.revents & POLLIN) == POLLIN)
-#else
- if(FD_ISSET(c->fd, &rfds))
-#endif
- ret = ret && _xcb_in_read(c);
-
-#if USE_POLL
- if((fd.revents & POLLOUT) == POLLOUT)
-#else
- if(FD_ISSET(c->fd, &wfds))
-#endif
- ret = ret && write_vec(c, vector, count);
- }
-
- if(count)
- --c->out.writing;
- --c->in.reading;
-
- return ret;
-}
+/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the names of the authors or their
+ * institutions shall not be used in advertising or otherwise to promote the
+ * sale, use or other dealings in this Software without prior written
+ * authorization from the authors.
+ */
+
+/* Connection management: the core of XCB. */
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "xcb.h"
+#include "xcbint.h"
+#if USE_POLL
+#include <poll.h>
+#elif !defined _WIN32
+#include <sys/select.h>
+#endif
+
+#ifdef _WIN32
+#include "xcb_windefs.h"
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#endif /* _WIN32 */
+
+#include <X11/Xtrans/Xtrans.h>
+
+#ifdef _MSC_VER
+#define _close(fd) closesocket(fd)
+#endif
+
+/* SHUT_RDWR is fairly recent and is not available on all platforms */
+#if !defined(SHUT_RDWR)
+#define SHUT_RDWR 2
+#endif
+
+typedef struct {
+ uint8_t status;
+ uint8_t pad0[5];
+ uint16_t length;
+} xcb_setup_generic_t;
+
+static const int xcb_con_error = XCB_CONN_ERROR;
+static const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
+static const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR;
+
+static int set_fd_flags(const int fd)
+{
+/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
+
+#ifdef _WIN32
+ u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
+ int ret;
+
+ ret = ioctlsocket(fd, FIONBIO, &iMode);
+ if(ret != 0)
+ return 0;
+ return 1;
+#else
+ int flags = fcntl(fd, F_GETFL, 0);
+ if(flags == -1)
+ return 0;
+ flags |= O_NONBLOCK;
+ if(fcntl(fd, F_SETFL, flags) == -1)
+ return 0;
+ if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+ return 0;
+ return 1;
+#endif /* _WIN32 */
+}
+
+static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
+{
+ static const char pad[3];
+ xcb_setup_request_t out;
+ struct iovec parts[6];
+ int count = 0;
+ static const uint32_t endian = 0x01020304;
+ int ret;
+
+ memset(&out, 0, sizeof(out));
+
+ /* B = 0x42 = MSB first, l = 0x6c = LSB first */
+ if(htonl(endian) == endian)
+ out.byte_order = 0x42;
+ else
+ out.byte_order = 0x6c;
+ out.protocol_major_version = X_PROTOCOL;
+ out.protocol_minor_version = X_PROTOCOL_REVISION;
+ out.authorization_protocol_name_len = 0;
+ out.authorization_protocol_data_len = 0;
+ parts[count].iov_len = sizeof(xcb_setup_request_t);
+ parts[count++].iov_base = (caddr_t) &out;
+ parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
+ parts[count++].iov_base = (char *) pad;
+
+ if(auth_info)
+ {
+ parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
+ parts[count++].iov_base = auth_info->name;
+ parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
+ parts[count++].iov_base = (char *) pad;
+ parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
+ parts[count++].iov_base = auth_info->data;
+ parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
+ parts[count++].iov_base = (char *) pad;
+ }
+ assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
+
+ pthread_mutex_lock(&c->iolock);
+ ret = _xcb_out_send(c, parts, count);
+ pthread_mutex_unlock(&c->iolock);
+ return ret;
+}
+
+static int read_setup(xcb_connection_t *c)
+{
+ /* Read the server response */
+ c->setup = malloc(sizeof(xcb_setup_generic_t));
+ if(!c->setup)
+ return 0;
+
+ if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
+ return 0;
+
+ {
+ void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
+ if(!tmp)
+ return 0;
+ c->setup = tmp;
+ }
+
+ if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
+ return 0;
+
+ /* 0 = failed, 2 = authenticate, 1 = success */
+ switch(c->setup->status)
+ {
+ case 0: /* failed */
+ {
+ xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
+ write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
+ return 0;
+ }
+
+ case 2: /* authenticate */
+ {
+ xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
+ write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+/* precondition: there must be something for us to write. */
+static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
+{
+ int n;
+
+#ifdef _WIN32
+ int i = 0;
+ int cnt=*count;
+ struct iovec *vec;
+ n = 0;
+ assert(!c->out.queue_len);
+
+ /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
+ an iovec would require more work and I'm not sure of the benefit....works for now */
+ vec = *vector;
+ while(i < cnt)
+ {
+ char *p= vec->iov_base;
+ size_t l= vec->iov_len;
+ while (l > 0)
+ {
+ int ret = send(c->fd, p, l, 0);
+ if(ret == SOCKET_ERROR)
+ {
+ int err = WSAGetLastError();
+ if(err == WSAEWOULDBLOCK)
+ {
+ if (n)
+ {
+ /* already return the data */
+ i=cnt;
+ break;
+ }
+ else
+ return 1;
+ }
+ }
+ p += ret;
+ l -= ret;
+ n += ret;
+ }
+ vec++;
+ i++;
+ }
+#else
+ assert(!c->out.queue_len);
+ n = writev(c->fd, *vector, *count);
+ if(n < 0 && errno == EAGAIN)
+ return 1;
+#endif /* _WIN32 */
+
+ if(n <= 0)
+ {
+ _xcb_conn_shutdown(c, XCB_CONN_ERROR);
+ return 0;
+ }
+
+ for(; *count; --*count, ++*vector)
+ {
+ int cur = (*vector)->iov_len;
+ if(cur > n)
+ cur = n;
+ (*vector)->iov_len -= cur;
+ (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
+ n -= cur;
+ if((*vector)->iov_len)
+ break;
+ }
+ if(!*count)
+ *vector = 0;
+ assert(n == 0);
+ return 1;
+}
+
+/* Public interface */
+
+const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
+{
+ if(c->has_error)
+ return 0;
+ /* doesn't need locking because it's never written to. */
+ return c->setup;
+}
+
+int xcb_get_file_descriptor(xcb_connection_t *c)
+{
+ if(c->has_error)
+ return -1;
+ /* doesn't need locking because it's never written to. */
+ return c->fd;
+}
+
+int xcb_connection_has_error(xcb_connection_t *c)
+{
+ /* doesn't need locking because it's read and written atomically. */
+ return c->has_error;
+}
+
+xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
+{
+ xcb_connection_t* c;
+
+#ifndef _WIN32
+#ifndef USE_POLL
+ if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
+ {
+ close(fd);
+ return _xcb_conn_ret_error(XCB_CONN_ERROR);
+ }
+#endif
+#endif /* !_WIN32*/
+
+ c = calloc(1, sizeof(xcb_connection_t));
+ if(!c) {
+ close(fd);
+ return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
+ }
+
+ c->fd = fd;
+
+ if(!(
+ set_fd_flags(fd) &&
+ pthread_mutex_init(&c->iolock, 0) == 0 &&
+ _xcb_in_init(&c->in) &&
+ _xcb_out_init(&c->out) &&
+ write_setup(c, auth_info) &&
+ read_setup(c) &&
+ _xcb_ext_init(c) &&
+ _xcb_xid_init(c)
+ ))
+ {
+ xcb_disconnect(c);
+ return _xcb_conn_ret_error(XCB_CONN_ERROR);
+ }
+
+ return c;
+}
+
+void xcb_disconnect(xcb_connection_t *c)
+{
+ if(c->has_error)
+ return;
+
+ free(c->setup);
+
+ /* disallow further sends and receives */
+ shutdown(c->fd, SHUT_RDWR);
+ close(c->fd);
+
+ pthread_mutex_destroy(&c->iolock);
+ _xcb_in_destroy(&c->in);
+ _xcb_out_destroy(&c->out);
+
+ _xcb_ext_destroy(c);
+ _xcb_xid_destroy(c);
+
+ free(c);
+
+#ifdef _WIN32
+ WSACleanup();
+#endif
+}
+
+/* Private interface */
+
+void _xcb_conn_shutdown(xcb_connection_t *c, int err)
+{
+ c->has_error = err;
+}
+
+/* Return connection error state.
+ * To make thread-safe, I need a seperate static
+ * variable for every possible error.
+ */
+xcb_connection_t *_xcb_conn_ret_error(int err)
+{
+
+ switch(err)
+ {
+ case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
+ {
+ return (xcb_connection_t *) &xcb_con_closed_mem_er;
+ }
+ case XCB_CONN_CLOSED_PARSE_ERR:
+ {
+ return (xcb_connection_t *) &xcb_con_closed_parse_er;
+ }
+ case XCB_CONN_ERROR:
+ default:
+ {
+ return (xcb_connection_t *) &xcb_con_error;
+ }
+ }
+}
+
+int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
+{
+ int ret;
+#if USE_POLL
+ struct pollfd fd;
+#else
+ fd_set rfds, wfds;
+#endif
+
+ /* If the thing I should be doing is already being done, wait for it. */
+ if(count ? c->out.writing : c->in.reading)
+ {
+ pthread_cond_wait(cond, &c->iolock);
+ return 1;
+ }
+
+#if USE_POLL
+ memset(&fd, 0, sizeof(fd));
+ fd.fd = c->fd;
+ fd.events = POLLIN;
+#else
+ FD_ZERO(&rfds);
+ FD_SET(c->fd, &rfds);
+#endif
+ ++c->in.reading;
+
+#if USE_POLL
+ if(count)
+ {
+ fd.events |= POLLOUT;
+ ++c->out.writing;
+ }
+#else
+ FD_ZERO(&wfds);
+ if(count)
+ {
+ FD_SET(c->fd, &wfds);
+ ++c->out.writing;
+ }
+#endif
+
+ pthread_mutex_unlock(&c->iolock);
+ do {
+#if USE_POLL
+ ret = poll(&fd, 1, -1);
+ /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
+ * it as if it failed. */
+ if(ret >= 0 && (fd.revents & ~fd.events))
+ {
+ ret = -1;
+ break;
+ }
+#else
+ ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
+ if (ret==SOCKET_ERROR)
+ {
+ ret=-1;
+ errno = WSAGetLastError();
+ if (errno == WSAEINTR)
+ errno=EINTR;
+ }
+#endif
+ } while (ret == -1 && errno == EINTR);
+ if(ret < 0)
+ {
+ _xcb_conn_shutdown(c, XCB_CONN_ERROR);
+ ret = 0;
+ }
+ pthread_mutex_lock(&c->iolock);
+
+ if(ret)
+ {
+#if USE_POLL
+ if((fd.revents & POLLIN) == POLLIN)
+#else
+ if(FD_ISSET(c->fd, &rfds))
+#endif
+ ret = ret && _xcb_in_read(c);
+
+#if USE_POLL
+ if((fd.revents & POLLOUT) == POLLOUT)
+#else
+ if(FD_ISSET(c->fd, &wfds))
+#endif
+ ret = ret && write_vec(c, vector, count);
+ }
+
+ if(count)
+ --c->out.writing;
+ --c->in.reading;
+
+ return ret;
+}
diff --git a/libxcb/src/xcb_in.c b/libxcb/src/xcb_in.c
index 2825c25c3..c6a405e62 100644
--- a/libxcb/src/xcb_in.c
+++ b/libxcb/src/xcb_in.c
@@ -180,7 +180,7 @@ static int read_packet(xcb_connection_t *c)
(genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
if(!buf)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
return 0;
}
@@ -216,7 +216,7 @@ static int read_packet(xcb_connection_t *c)
struct reply_list *cur = malloc(sizeof(struct reply_list));
if(!cur)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
free(buf);
return 0;
}
@@ -233,7 +233,7 @@ static int read_packet(xcb_connection_t *c)
event = malloc(sizeof(struct event_list));
if(!event)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
free(buf);
return 0;
}
@@ -439,7 +439,7 @@ static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_nex
pend = malloc(sizeof(*pend));
if(!pend)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
return;
}
@@ -639,7 +639,7 @@ int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds
assert(workaround != WORKAROUND_NONE || flags != 0);
if(!pend)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT);
return 0;
}
pend->first_request = pend->last_request = request;
@@ -678,7 +678,7 @@ int _xcb_in_read(xcb_connection_t *c)
if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
#endif /* !_WIN32 */
return 1;
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_ERROR);
return 0;
}
@@ -697,7 +697,7 @@ int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
int ret = read_block(c->fd, (char *) buf + done, len - done);
if(ret <= 0)
{
- _xcb_conn_shutdown(c);
+ _xcb_conn_shutdown(c, XCB_CONN_ERROR);
return ret;
}
}
diff --git a/libxcb/src/xcb_out.c b/libxcb/src/xcb_out.c
index 0e105fffe..c082c98ab 100644
--- a/libxcb/src/xcb_out.c
+++ b/libxcb/src/xcb_out.c
@@ -1,373 +1,379 @@
-/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice, the names of the authors or their
- * institutions shall not be used in advertising or otherwise to promote the
- * sale, use or other dealings in this Software without prior written
- * authorization from the authors.
- */
-
-/* Stuff that sends stuff to the server. */
-
-#include <assert.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <X11/Xtrans/Xtrans.h>
-
-#include "xcb.h"
-#include "xcbext.h"
-#include "xcbint.h"
-#include "bigreq.h"
-
-static __inline void send_request(xcb_connection_t *c, int isvoid, enum workarounds workaround, int flags, struct iovec *vector, int count)
-{
- if(c->has_error)
- return;
-
- ++c->out.request;
- if(!isvoid)
- c->in.request_expected = c->out.request;
- if(workaround != WORKAROUND_NONE || flags != 0)
- _xcb_in_expect_reply(c, c->out.request, workaround, flags);
-
- while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
- {
- memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
- c->out.queue_len += vector[0].iov_len;
- vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
- vector[0].iov_len = 0;
- ++vector, --count;
- }
- if(!count)
- return;
-
- --vector, ++count;
- vector[0].iov_base = c->out.queue;
- vector[0].iov_len = c->out.queue_len;
- c->out.queue_len = 0;
- _xcb_out_send(c, vector, count);
-}
-
-static void send_sync(xcb_connection_t *c)
-{
- static const union {
- struct {
- uint8_t major;
- uint8_t pad;
- uint16_t len;
- } fields;
- uint32_t packet;
- } sync_req = { { /* GetInputFocus */ 43, 0, 1 } };
- struct iovec vector[2];
- vector[1].iov_base = (char *) &sync_req;
- vector[1].iov_len = sizeof(sync_req);
- send_request(c, 0, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY, vector + 1, 1);
-}
-
-static void get_socket_back(xcb_connection_t *c)
-{
- while(c->out.return_socket && c->out.socket_moving)
- pthread_cond_wait(&c->out.socket_cond, &c->iolock);
- if(!c->out.return_socket)
- return;
-
- c->out.socket_moving = 1;
- pthread_mutex_unlock(&c->iolock);
- c->out.return_socket(c->out.socket_closure);
- pthread_mutex_lock(&c->iolock);
- c->out.socket_moving = 0;
-
- pthread_cond_broadcast(&c->out.socket_cond);
- c->out.return_socket = 0;
- c->out.socket_closure = 0;
- _xcb_in_replies_done(c);
-}
-
-/* Public interface */
-
-void xcb_prefetch_maximum_request_length(xcb_connection_t *c)
-{
- if(c->has_error)
- return;
- pthread_mutex_lock(&c->out.reqlenlock);
- if(c->out.maximum_request_length_tag == LAZY_NONE)
- {
- const xcb_query_extension_reply_t *ext;
- ext = xcb_get_extension_data(c, &xcb_big_requests_id);
- if(ext && ext->present)
- {
- c->out.maximum_request_length_tag = LAZY_COOKIE;
- c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
- }
- else
- {
- c->out.maximum_request_length_tag = LAZY_FORCED;
- c->out.maximum_request_length.value = c->setup->maximum_request_length;
- }
- }
- pthread_mutex_unlock(&c->out.reqlenlock);
-}
-
-uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
-{
- if(c->has_error)
- return 0;
- xcb_prefetch_maximum_request_length(c);
- pthread_mutex_lock(&c->out.reqlenlock);
- if(c->out.maximum_request_length_tag == LAZY_COOKIE)
- {
- xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
- c->out.maximum_request_length_tag = LAZY_FORCED;
- if(r)
- {
- c->out.maximum_request_length.value = r->maximum_request_length;
- free(r);
- }
- else
- c->out.maximum_request_length.value = c->setup->maximum_request_length;
- }
- pthread_mutex_unlock(&c->out.reqlenlock);
- return c->out.maximum_request_length.value;
-}
-
-unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
-{
- uint64_t request;
- uint32_t prefix[2];
- int veclen = req->count;
- enum workarounds workaround = WORKAROUND_NONE;
-
- if(c->has_error)
- return 0;
-
- assert(c != 0);
- assert(vector != 0);
- assert(req->count > 0);
-
- if(!(flags & XCB_REQUEST_RAW))
- {
- static const char pad[3];
- unsigned int i;
- uint16_t shortlen = 0;
- size_t longlen = 0;
- assert(vector[0].iov_len >= 4);
- /* set the major opcode, and the minor opcode for extensions */
- if(req->ext)
- {
- const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
- if(!(extension && extension->present))
- {
- _xcb_conn_shutdown(c);
- return 0;
- }
- ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
- ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
- }
- else
- ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
-
- /* put together the length field, possibly using BIGREQUESTS */
- for(i = 0; i < req->count; ++i)
- {
- longlen += vector[i].iov_len;
- if(!vector[i].iov_base)
- {
- vector[i].iov_base = (char *) pad;
- assert(vector[i].iov_len <= sizeof(pad));
- }
- }
- assert((longlen & 3) == 0);
- longlen >>= 2;
-
- if(longlen <= c->setup->maximum_request_length)
- {
- /* we don't need BIGREQUESTS. */
- shortlen = longlen;
- longlen = 0;
- }
- else if(longlen > xcb_get_maximum_request_length(c))
- {
- _xcb_conn_shutdown(c);
- return 0; /* server can't take this; maybe need BIGREQUESTS? */
- }
-
- /* set the length field. */
- ((uint16_t *) vector[0].iov_base)[1] = shortlen;
- if(!shortlen)
- {
- prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
- prefix[1] = ++longlen;
- vector[0].iov_base = (caddr_t)((uint32_t *) vector[0].iov_base + 1);
- vector[0].iov_len -= sizeof(uint32_t);
- --vector, ++veclen;
- vector[0].iov_base = (caddr_t)prefix;
- vector[0].iov_len = sizeof(prefix);
- }
- }
- flags &= ~XCB_REQUEST_RAW;
-
- /* do we need to work around the X server bug described in glx.xml? */
- /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
- * configuration, but that should be handled here anyway. */
- if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
- ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
- req->opcode == 21))
- workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
-
- /* get a sequence number and arrange for delivery. */
- pthread_mutex_lock(&c->iolock);
- /* wait for other writing threads to get out of my way. */
- while(c->out.writing)
- pthread_cond_wait(&c->out.cond, &c->iolock);
- get_socket_back(c);
-
- /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
- * a reply. */
- if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
- send_sync(c);
- /* Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
- * applications see sequence 0 as that is used to indicate
- * an error in sending the request */
- if((unsigned int) (c->out.request + 1) == 0)
- send_sync(c);
-
- /* The above send_sync calls could drop the I/O lock, but this
- * thread will still exclude any other thread that tries to write,
- * so the sequence number postconditions still hold. */
- send_request(c, req->isvoid, workaround, flags, vector, veclen);
- request = c->has_error ? 0 : c->out.request;
- pthread_mutex_unlock(&c->iolock);
- return request;
-}
-
-int xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
-{
- int ret;
- if(c->has_error)
- return 0;
- pthread_mutex_lock(&c->iolock);
- get_socket_back(c);
- ret = _xcb_out_flush_to(c, c->out.request);
- if(ret)
- {
- c->out.return_socket = return_socket;
- c->out.socket_closure = closure;
- if(flags)
- _xcb_in_expect_reply(c, c->out.request, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
- assert(c->out.request == c->out.request_written);
- *sent = c->out.request;
- }
- pthread_mutex_unlock(&c->iolock);
- return ret;
-}
-
-int xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
-{
- int ret;
- if(c->has_error)
- return 0;
- pthread_mutex_lock(&c->iolock);
- c->out.request += requests;
- ret = _xcb_out_send(c, vector, count);
- pthread_mutex_unlock(&c->iolock);
- return ret;
-}
-
-int xcb_flush(xcb_connection_t *c)
-{
- int ret;
- if(c->has_error)
- return 0;
- pthread_mutex_lock(&c->iolock);
- ret = _xcb_out_flush_to(c, c->out.request);
- pthread_mutex_unlock(&c->iolock);
- return ret;
-}
-
-/* Private interface */
-
-int _xcb_out_init(_xcb_out *out)
-{
- if(pthread_cond_init(&out->socket_cond, 0))
- return 0;
- out->return_socket = 0;
- out->socket_closure = 0;
- out->socket_moving = 0;
-
- if(pthread_cond_init(&out->cond, 0))
- return 0;
- out->writing = 0;
-
- out->queue_len = 0;
-
- out->request = 0;
- out->request_written = 0;
-
- if(pthread_mutex_init(&out->reqlenlock, 0))
- return 0;
- out->maximum_request_length_tag = LAZY_NONE;
-
- return 1;
-}
-
-void _xcb_out_destroy(_xcb_out *out)
-{
- pthread_cond_destroy(&out->cond);
- pthread_mutex_destroy(&out->reqlenlock);
-}
-
-int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
-{
- int ret = 1;
- while(ret && count)
- ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
- c->out.request_written = c->out.request;
- pthread_cond_broadcast(&c->out.cond);
- _xcb_in_wake_up_next_reader(c);
- return ret;
-}
-
-void _xcb_out_send_sync(xcb_connection_t *c)
-{
- /* wait for other writing threads to get out of my way. */
- while(c->out.writing)
- pthread_cond_wait(&c->out.cond, &c->iolock);
- get_socket_back(c);
- send_sync(c);
-}
-
-int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
-{
- assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
- if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
- return 1;
- if(c->out.queue_len)
- {
- struct iovec vec;
- vec.iov_base = c->out.queue;
- vec.iov_len = c->out.queue_len;
- c->out.queue_len = 0;
- return _xcb_out_send(c, &vec, 1);
- }
- while(c->out.writing)
- pthread_cond_wait(&c->out.cond, &c->iolock);
- assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
- return 1;
-}
+/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the names of the authors or their
+ * institutions shall not be used in advertising or otherwise to promote the
+ * sale, use or other dealings in this Software without prior written
+ * authorization from the authors.
+ */
+
+/* Stuff that sends stuff to the server. */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <X11/Xtrans/Xtrans.h>
+
+#include "xcb.h"
+#include "xcbext.h"
+#include "xcbint.h"
+#include "bigreq.h"
+
+static __inline void send_request(xcb_connection_t *c, int isvoid, enum workarounds workaround, int flags, struct iovec *vector, int count)
+{
+ if(c->has_error)
+ return;
+
+ ++c->out.request;
+ if(!isvoid)
+ c->in.request_expected = c->out.request;
+ if(workaround != WORKAROUND_NONE || flags != 0)
+ _xcb_in_expect_reply(c, c->out.request, workaround, flags);
+
+ while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
+ {
+ memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
+ c->out.queue_len += vector[0].iov_len;
+ vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
+ vector[0].iov_len = 0;
+ ++vector, --count;
+ }
+ if(!count)
+ return;
+
+ --vector, ++count;
+ vector[0].iov_base = c->out.queue;
+ vector[0].iov_len = c->out.queue_len;
+ c->out.queue_len = 0;
+ _xcb_out_send(c, vector, count);
+}
+
+static void send_sync(xcb_connection_t *c)
+{
+ static const union {
+ struct {
+ uint8_t major;
+ uint8_t pad;
+ uint16_t len;
+ } fields;
+ uint32_t packet;
+ } sync_req = { { /* GetInputFocus */ 43, 0, 1 } };
+ struct iovec vector[2];
+ vector[1].iov_base = (char *) &sync_req;
+ vector[1].iov_len = sizeof(sync_req);
+ send_request(c, 0, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY, vector + 1, 1);
+}
+
+static void get_socket_back(xcb_connection_t *c)
+{
+ while(c->out.return_socket && c->out.socket_moving)
+ pthread_cond_wait(&c->out.socket_cond, &c->iolock);
+ if(!c->out.return_socket)
+ return;
+
+ c->out.socket_moving = 1;
+ pthread_mutex_unlock(&c->iolock);
+ c->out.return_socket(c->out.socket_closure);
+ pthread_mutex_lock(&c->iolock);
+ c->out.socket_moving = 0;
+
+ pthread_cond_broadcast(&c->out.socket_cond);
+ c->out.return_socket = 0;
+ c->out.socket_closure = 0;
+ _xcb_in_replies_done(c);
+}
+
+/* Public interface */
+
+void xcb_prefetch_maximum_request_length(xcb_connection_t *c)
+{
+ if(c->has_error)
+ return;
+ pthread_mutex_lock(&c->out.reqlenlock);
+ if(c->out.maximum_request_length_tag == LAZY_NONE)
+ {
+ const xcb_query_extension_reply_t *ext;
+ ext = xcb_get_extension_data(c, &xcb_big_requests_id);
+ if(ext && ext->present)
+ {
+ c->out.maximum_request_length_tag = LAZY_COOKIE;
+ c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
+ }
+ else
+ {
+ c->out.maximum_request_length_tag = LAZY_FORCED;
+ c->out.maximum_request_length.value = c->setup->maximum_request_length;
+ }
+ }
+ pthread_mutex_unlock(&c->out.reqlenlock);
+}
+
+uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
+{
+ if(c->has_error)
+ return 0;
+ xcb_prefetch_maximum_request_length(c);
+ pthread_mutex_lock(&c->out.reqlenlock);
+ if(c->out.maximum_request_length_tag == LAZY_COOKIE)
+ {
+ xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
+ c->out.maximum_request_length_tag = LAZY_FORCED;
+ if(r)
+ {
+ c->out.maximum_request_length.value = r->maximum_request_length;
+ free(r);
+ }
+ else
+ c->out.maximum_request_length.value = c->setup->maximum_request_length;
+ }
+ pthread_mutex_unlock(&c->out.reqlenlock);
+ return c->out.maximum_request_length.value;
+}
+
+unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
+{
+ uint64_t request;
+ uint32_t prefix[2];
+ int veclen = req->count;
+ enum workarounds workaround = WORKAROUND_NONE;
+
+ if(c->has_error)
+ return 0;
+
+ assert(c != 0);
+ assert(vector != 0);
+ assert(req->count > 0);
+
+ if(!(flags & XCB_REQUEST_RAW))
+ {
+ static const char pad[3];
+ unsigned int i;
+ uint16_t shortlen = 0;
+ size_t longlen = 0;
+ assert(vector[0].iov_len >= 4);
+ /* set the major opcode, and the minor opcode for extensions */
+ if(req->ext)
+ {
+ const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
+ if(!(extension && extension->present))
+ {
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_EXT_NOTSUPPORTED);
+ return 0;
+ }
+ ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
+ ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
+ }
+ else
+ ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
+
+ /* put together the length field, possibly using BIGREQUESTS */
+ for(i = 0; i < req->count; ++i)
+ {
+ longlen += vector[i].iov_len;
+ if(!vector[i].iov_base)
+ {
+ vector[i].iov_base = (char *) pad;
+ assert(vector[i].iov_len <= sizeof(pad));
+ }
+ }
+ assert((longlen & 3) == 0);
+ longlen >>= 2;
+
+ if(longlen <= c->setup->maximum_request_length)
+ {
+ /* we don't need BIGREQUESTS. */
+ shortlen = longlen;
+ longlen = 0;
+ }
+ else if(longlen > xcb_get_maximum_request_length(c))
+ {
+ _xcb_conn_shutdown(c, XCB_CONN_CLOSED_REQ_LEN_EXCEED);
+ return 0; /* server can't take this; maybe need BIGREQUESTS? */
+ }
+
+ /* set the length field. */
+ ((uint16_t *) vector[0].iov_base)[1] = shortlen;
+ if(!shortlen)
+ {
+ prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
+ prefix[1] = ++longlen;
+ vector[0].iov_base = (caddr_t)((uint32_t *) vector[0].iov_base + 1);
+ vector[0].iov_len -= sizeof(uint32_t);
+ --vector, ++veclen;
+ vector[0].iov_base = (caddr_t)prefix;
+ vector[0].iov_len = sizeof(prefix);
+ }
+ }
+ flags &= ~XCB_REQUEST_RAW;
+
+ /* do we need to work around the X server bug described in glx.xml? */
+ /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
+ * configuration, but that should be handled here anyway. */
+ if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
+ ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
+ req->opcode == 21))
+ workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
+
+ /* get a sequence number and arrange for delivery. */
+ pthread_mutex_lock(&c->iolock);
+ /* wait for other writing threads to get out of my way. */
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+ get_socket_back(c);
+
+ /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
+ * a reply. */
+ if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
+ send_sync(c);
+ /* Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
+ * applications see sequence 0 as that is used to indicate
+ * an error in sending the request */
+ if((unsigned int) (c->out.request + 1) == 0)
+ send_sync(c);
+
+ /* The above send_sync calls could drop the I/O lock, but this
+ * thread will still exclude any other thread that tries to write,
+ * so the sequence number postconditions still hold. */
+ send_request(c, req->isvoid, workaround, flags, vector, veclen);
+ request = c->has_error ? 0 : c->out.request;
+ pthread_mutex_unlock(&c->iolock);
+ return request;
+}
+
+int xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
+{
+ int ret;
+ if(c->has_error)
+ return 0;
+ pthread_mutex_lock(&c->iolock);
+ get_socket_back(c);
+
+ /* _xcb_out_flush may drop the iolock allowing other threads to
+ * write requests, so keep flushing until we're done
+ */
+ do
+ ret = _xcb_out_flush_to(c, c->out.request);
+ while (ret && c->out.request != c->out.request_written);
+ if(ret)
+ {
+ c->out.return_socket = return_socket;
+ c->out.socket_closure = closure;
+ if(flags)
+ _xcb_in_expect_reply(c, c->out.request, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
+ assert(c->out.request == c->out.request_written);
+ *sent = c->out.request;
+ }
+ pthread_mutex_unlock(&c->iolock);
+ return ret;
+}
+
+int xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
+{
+ int ret;
+ if(c->has_error)
+ return 0;
+ pthread_mutex_lock(&c->iolock);
+ c->out.request += requests;
+ ret = _xcb_out_send(c, vector, count);
+ pthread_mutex_unlock(&c->iolock);
+ return ret;
+}
+
+int xcb_flush(xcb_connection_t *c)
+{
+ int ret;
+ if(c->has_error)
+ return 0;
+ pthread_mutex_lock(&c->iolock);
+ ret = _xcb_out_flush_to(c, c->out.request);
+ pthread_mutex_unlock(&c->iolock);
+ return ret;
+}
+
+/* Private interface */
+
+int _xcb_out_init(_xcb_out *out)
+{
+ if(pthread_cond_init(&out->socket_cond, 0))
+ return 0;
+ out->return_socket = 0;
+ out->socket_closure = 0;
+ out->socket_moving = 0;
+
+ if(pthread_cond_init(&out->cond, 0))
+ return 0;
+ out->writing = 0;
+
+ out->queue_len = 0;
+
+ out->request = 0;
+ out->request_written = 0;
+
+ if(pthread_mutex_init(&out->reqlenlock, 0))
+ return 0;
+ out->maximum_request_length_tag = LAZY_NONE;
+
+ return 1;
+}
+
+void _xcb_out_destroy(_xcb_out *out)
+{
+ pthread_cond_destroy(&out->cond);
+ pthread_mutex_destroy(&out->reqlenlock);
+}
+
+int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
+{
+ int ret = 1;
+ while(ret && count)
+ ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
+ c->out.request_written = c->out.request;
+ pthread_cond_broadcast(&c->out.cond);
+ _xcb_in_wake_up_next_reader(c);
+ return ret;
+}
+
+void _xcb_out_send_sync(xcb_connection_t *c)
+{
+ /* wait for other writing threads to get out of my way. */
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+ get_socket_back(c);
+ send_sync(c);
+}
+
+int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
+{
+ assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
+ if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
+ return 1;
+ if(c->out.queue_len)
+ {
+ struct iovec vec;
+ vec.iov_base = c->out.queue;
+ vec.iov_len = c->out.queue_len;
+ c->out.queue_len = 0;
+ return _xcb_out_send(c, &vec, 1);
+ }
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+ assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
+ return 1;
+}
diff --git a/libxcb/src/xcb_util.c b/libxcb/src/xcb_util.c
index 4fc8797c5..645d083e4 100644
--- a/libxcb/src/xcb_util.c
+++ b/libxcb/src/xcb_util.c
@@ -35,12 +35,12 @@
#include <unistd.h>
#include <string.h>
#ifndef _MSC_VER
-#include <arpa/inet.h>
#endif
#ifdef _WIN32
#include "xcb_windefs.h"
#else
+#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
@@ -455,13 +455,24 @@ xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname,
int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
if(!parsed) {
- c = (xcb_connection_t *) &error_connection;
+ c = _xcb_conn_ret_error(XCB_CONN_CLOSED_PARSE_ERR);
goto out;
- } else
+ } else {
+#ifdef _WIN32
+ WSADATA wsaData;
+ if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
+ c = (xcb_connection_t *) &error_connection;
+ goto out;
+ }
+#endif
fd = _xcb_open(host, protocol, display);
+ }
if(fd == -1) {
- c = (xcb_connection_t *) &error_connection;
+ c = _xcb_conn_ret_error(XCB_CONN_ERROR);
+#ifdef _WIN32
+ WSACleanup();
+#endif
goto out;
}
diff --git a/libxcb/src/xcbint.h b/libxcb/src/xcbint.h
index 096576c4a..f9e5a52f7 100644
--- a/libxcb/src/xcbint.h
+++ b/libxcb/src/xcbint.h
@@ -174,8 +174,6 @@ void _xcb_ext_destroy(xcb_connection_t *c);
/* xcb_conn.c */
-extern const int error_connection;
-
struct xcb_connection_t {
int has_error;
@@ -193,7 +191,10 @@ struct xcb_connection_t {
_xcb_xid xid;
};
-void _xcb_conn_shutdown(xcb_connection_t *c);
+void _xcb_conn_shutdown(xcb_connection_t *c, int err);
+
+xcb_connection_t *_xcb_conn_ret_error(int err);
+
int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);