From 2062b62455963bd62c0be570b3cac4ad481336d0 Mon Sep 17 00:00:00 2001
From: marha <marha@users.sourceforge.net>
Date: Wed, 1 Sep 2010 06:25:26 +0000
Subject: libxcb git update 1/9/2010

---
 libxcb/src/xcb.h         | 978 ++++++++++++++++++++++++-----------------------
 libxcb/src/xcb_auth.c    | 707 +++++++++++++++++-----------------
 libxcb/src/xcb_conn.c    |  49 ++-
 libxcb/src/xcb_in.c      |  30 +-
 libxcb/src/xcb_util.c    |  26 +-
 libxcb/src/xcb_windefs.h |  45 +++
 6 files changed, 985 insertions(+), 850 deletions(-)
 create mode 100644 libxcb/src/xcb_windefs.h

diff --git a/libxcb/src/xcb.h b/libxcb/src/xcb.h
index 35d876883..f781e87f9 100644
--- a/libxcb/src/xcb.h
+++ b/libxcb/src/xcb.h
@@ -1,487 +1,491 @@
-/*
- * Copyright (C) 2001-2006 Bart Massey, Jamey Sharp, and Josh Triplett.
- * All Rights Reserved.
- *
- * 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.
- */
-
-#ifndef __XCB_H__
-#define __XCB_H__
-#include <sys/types.h>
-
-#if defined(__solaris__)
-#include <inttypes.h>
-#else
-#include <stdint.h>
-#endif
-
-#include <sys/uio.h>
-#include <pthread.h>
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * @file xcb.h
- */
-
-/**
- * @defgroup XCB_Core_API XCB Core API
- * @brief Core API of the XCB library.
- *
- * @{
- */
-
-/* Pre-defined constants */
-
-/** Current protocol version */
-#define X_PROTOCOL 11
-
-/** Current minor version */
-#define X_PROTOCOL_REVISION 0
-
-/** X_TCP_PORT + display number = server port for TCP transport */
-#define X_TCP_PORT 6000
-
-#define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1))
-
-/* Opaque structures */
-
-/**
- * @brief XCB Connection structure.
- *
- * A structure that contain all data that  XCB needs to communicate with an X server.
- */
-typedef struct xcb_connection_t xcb_connection_t;  /**< Opaque structure containing all data that  XCB needs to communicate with an X server. */
-
-
-/* Other types */
-
-/**
- * @brief Generic iterator.
- *
- * A generic iterator structure.
- */
-typedef struct {
-    void *data;   /**< Data of the current iterator */
-    int rem;    /**< remaining elements */
-    int index;  /**< index of the current iterator */
-} xcb_generic_iterator_t;
-
-/**
- * @brief Generic reply.
- *
- * A generic reply structure.
- */
-typedef struct {
-    uint8_t   response_type;  /**< Type of the response */
-    uint8_t  pad0;           /**< Padding */
-    uint16_t sequence;       /**< Sequence number */
-    uint32_t length;         /**< Length of the response */
-} xcb_generic_reply_t;
-
-/**
- * @brief Generic event.
- *
- * A generic event structure.
- */
-typedef struct {
-    uint8_t   response_type;  /**< Type of the response */
-    uint8_t  pad0;           /**< Padding */
-    uint16_t sequence;       /**< Sequence number */
-    uint32_t pad[7];         /**< Padding */
-    uint32_t full_sequence;  /**< full sequence */
-} xcb_generic_event_t;
-
-/**
- * @brief GE event
- *
- * An event as sent by the XGE extension. The length field specifies the
- * number of 4-byte blocks trailing the struct.
- */
-typedef struct {
-    uint8_t  response_type;  /**< Type of the response */
-    uint8_t  pad0;           /**< Padding */
-    uint16_t sequence;       /**< Sequence number */
-    uint32_t length;
-    uint16_t event_type;
-    uint16_t pad1;
-    uint32_t pad[5];         /**< Padding */
-    uint32_t full_sequence;  /**< full sequence */
-} xcb_ge_event_t;
-
-/**
- * @brief Generic error.
- *
- * A generic error structure.
- */
-typedef struct {
-    uint8_t   response_type;  /**< Type of the response */
-    uint8_t   error_code;     /**< Error code */
-    uint16_t sequence;       /**< Sequence number */
-    uint32_t resource_id;     /** < Resource ID for requests with side effects only */
-    uint16_t minor_code;      /** < Minor opcode of the failed request */
-    uint8_t major_code;       /** < Major opcode of the failed request */
-    uint8_t pad0;
-    uint32_t pad[5];         /**< Padding */
-    uint32_t full_sequence;  /**< full sequence */
-} xcb_generic_error_t;
-
-/**
- * @brief Generic cookie.
- *
- * A generic cookie structure.
- */
-typedef struct {
-    unsigned int sequence;  /**< Sequence number */
-} xcb_void_cookie_t;
-
-
-/* Include the generated xproto header. */
-#include "xproto.h"
-
-
-/** XCB_NONE is the universal null resource or null atom parameter value for many core X requests */
-#define XCB_NONE 0L
-
-/** XCB_COPY_FROM_PARENT can be used for many xcb_create_window parameters */
-#define XCB_COPY_FROM_PARENT 0L
-
-/** XCB_CURRENT_TIME can be used in most requests that take an xcb_timestamp_t */
-#define XCB_CURRENT_TIME 0L
-
-/** XCB_NO_SYMBOL fills in unused entries in xcb_keysym_t tables */
-#define XCB_NO_SYMBOL 0L
-
-
-/* xcb_auth.c */
-
-/**
- * @brief Container for authorization information.
- *
- * A container for authorization information to be sent to the X server.
- */
-typedef struct xcb_auth_info_t {
-    int   namelen;  /**< Length of the string name (as returned by strlen). */
-    char *name;     /**< String containing the authentication protocol name, such as "MIT-MAGIC-COOKIE-1" or "XDM-AUTHORIZATION-1". */
-    int   datalen;  /**< Length of the data member. */
-    char *data;   /**< Data interpreted in a protocol-specific manner. */
-} xcb_auth_info_t;
-
-
-/* xcb_out.c */
-
-/**
- * @brief Forces any buffered output to be written to the server.
- * @param c: The connection to the X server.
- * @return > @c 0 on success, <= @c 0 otherwise.
- *
- * Forces any buffered output to be written to the server. Blocks
- * until the write is complete.
- */
-int xcb_flush(xcb_connection_t *c);
-
-/**
- * @brief Returns the maximum request length that this server accepts.
- * @param c: The connection to the X server.
- * @return The maximum request length field.
- *
- * In the absence of the BIG-REQUESTS extension, returns the
- * maximum request length field from the connection setup data, which
- * may be as much as 65535. If the server supports BIG-REQUESTS, then
- * the maximum request length field from the reply to the
- * BigRequestsEnable request will be returned instead.
- *
- * Note that this length is measured in four-byte units, making the
- * theoretical maximum lengths roughly 256kB without BIG-REQUESTS and
- * 16GB with.
- */
-uint32_t xcb_get_maximum_request_length(xcb_connection_t *c);
-
-/**
- * @brief Prefetch the maximum request length without blocking.
- * @param c: The connection to the X server.
- *
- * Without blocking, does as much work as possible toward computing
- * the maximum request length accepted by the X server.
- *
- * Invoking this function may cause a call to xcb_big_requests_enable,
- * but will not block waiting for the reply.
- * xcb_get_maximum_request_length will return the prefetched data
- * after possibly blocking while the reply is retrieved.
- *
- * Note that in order for this function to be fully non-blocking, the
- * application must previously have called
- * xcb_prefetch_extension_data(c, &xcb_big_requests_id) and the reply
- * must have already arrived.
- */
-void xcb_prefetch_maximum_request_length(xcb_connection_t *c);
-
-
-/* xcb_in.c */
-
-/**
- * @brief Returns the next event or error from the server.
- * @param c: The connection to the X server.
- * @return The next event from the server.
- *
- * Returns the next event or error from the server, or returns null in
- * the event of an I/O error. Blocks until either an event or error
- * arrive, or an I/O error occurs.
- */
-xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
-
-/**
- * @brief Returns the next event or error from the server.
- * @param c: The connection to the X server.
- * error status of the operation.
- * @return The next event from the server.
- *
- * Returns the next event or error from the server, if one is
- * available, or returns @c NULL otherwise. If no event is available, that
- * might be because an I/O error like connection close occurred while
- * attempting to read the next event, in which case the connection is
- * shut down when this function returns.
- */
-xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
-
-/**
- * @brief Return the error for a request, or NULL if none can ever arrive.
- * @param c: The connection to the X server.
- * @param cookie: The request cookie.
- * @return The error for the request, or NULL if none can ever arrive.
- *
- * The xcb_void_cookie_t cookie supplied to this function must have resulted
- * from a call to xcb_[request_name]_checked().  This function will block
- * until one of two conditions happens.  If an error is received, it will be
- * returned.  If a reply to a subsequent request has already arrived, no error
- * can arrive for this request, so this function will return NULL.
- *
- * Note that this function will perform a sync if needed to ensure that the
- * sequence number will advance beyond that provided in cookie; this is a
- * convenience to avoid races in determining whether the sync is needed.
- */
-xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie);
-
-/**
- * @brief Discards the reply for a request.
- * @param c: The connection to the X server.
- * @param sequence: The request sequence number from a cookie.
- *
- * Discards the reply for a request. Additionally, any error generated
- * by the request is also discarded (unless it was an _unchecked request
- * and the error has already arrived).
- *
- * This function will not block even if the reply is not yet available.
- *
- * Note that the sequence really does have to come from an xcb cookie;
- * this function is not designed to operate on socket-handoff replies.
- */
-void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence);
-
-
-/* xcb_ext.c */
-
-/**
- * @typedef typedef struct xcb_extension_t xcb_extension_t
- */
-typedef struct xcb_extension_t xcb_extension_t;  /**< Opaque structure used as key for xcb_get_extension_data_t. */
-
-/**
- * @brief Caches reply information from QueryExtension requests.
- * @param c: The connection.
- * @param ext: The extension data.
- * @return A pointer to the xcb_query_extension_reply_t for the extension.
- *
- * This function is the primary interface to the "extension cache",
- * which caches reply information from QueryExtension
- * requests. Invoking this function may cause a call to
- * xcb_query_extension to retrieve extension information from the
- * server, and may block until extension data is received from the
- * server.
- *
- * The result must not be freed. This storage is managed by the cache
- * itself.
- */
-const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
-
-/**
- * @brief Prefetch of extension data into the extension cache
- * @param c: The connection.
- * @param ext: The extension data.
- *
- * This function allows a "prefetch" of extension data into the
- * extension cache. Invoking the function may cause a call to
- * xcb_query_extension, but will not block waiting for the
- * reply. xcb_get_extension_data will return the prefetched data after
- * possibly blocking while it is retrieved.
- */
-void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
-
-
-/* xcb_conn.c */
-
-/**
- * @brief Access the data returned by the server.
- * @param c: The connection.
- * @return A pointer to an xcb_setup_t structure.
- *
- * Accessor for the data returned by the server when the xcb_connection_t
- * was initialized. This data includes
- * - the server's required format for images,
- * - a list of available visuals,
- * - a list of available screens,
- * - the server's maximum request length (in the absence of the
- * BIG-REQUESTS extension),
- * - and other assorted information.
- *
- * See the X protocol specification for more details.
- *
- * The result must not be freed.
- */
-const xcb_setup_t *xcb_get_setup(xcb_connection_t *c);
-
-/**
- * @brief Access the file descriptor of the connection.
- * @param c: The connection.
- * @return The file descriptor.
- *
- * Accessor for the file descriptor that was passed to the
- * xcb_connect_to_fd call that returned @p c.
- */
-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.
- *
- * 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.
- */
-int xcb_connection_has_error(xcb_connection_t *c);
-
-/**
- * @brief Connects to the X server.
- * @param fd: The file descriptor.
- * @param auth_info: Authentication data.
- * @return A newly allocated xcb_connection_t structure.
- *
- * Connects to an X server, given the open socket @p fd and the
- * xcb_auth_info_t @p auth_info. The file descriptor @p fd is
- * bidirectionally connected to an X server. If the connection
- * should be unauthenticated, @p auth_info must be @c
- * NULL.
- */
-xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info);
-
-/**
- * @brief Closes the connection.
- * @param c: The connection.
- *
- * Closes the file descriptor and frees all memory associated with the
- * connection @c c.
- */
-void xcb_disconnect(xcb_connection_t *c);
-
-
-/* xcb_util.c */
-
-/**
- * @brief Parses a display string name in the form documented by X(7x).
- * @param name: The name of the display.
- * @param host: A pointer to a malloc'd copy of the hostname.
- * @param display: A pointer to the display number.
- * @param screen: A pointer to the screen number.
- * @return 0 on failure, non 0 otherwise.
- *
- * Parses the display string name @p display_name in the form
- * documented by X(7x). Has no side effects on failure. If
- * @p displayname is @c NULL or empty, it uses the environment
- * variable DISPLAY. @p hostp is a pointer to a newly allocated string
- * that contain the host name. @p displayp is set to the display
- * number and @p screenp to the preferred screen number. @p screenp
- * can be @c NULL. If @p displayname does not contain a screen number,
- * it is set to @c 0.
- */
-int xcb_parse_display(const char *name, char **host, int *display, int *screen);
-
-/**
- * @brief Connects to the X server.
- * @param displayname: The name of the display.
- * @param screenp: A pointer to a preferred screen number.
- * @return A newly allocated xcb_connection_t structure.
- *
- * Connects to the X server specified by @p displayname. If @p
- * displayname is @c NULL, uses the value of the DISPLAY environment
- * variable. If a particular screen on that server is preferred, the
- * int pointed to by @p screenp (if not @c NULL) will be set to that
- * screen; otherwise the screen will be set to 0.
- */
-xcb_connection_t *xcb_connect(const char *displayname, int *screenp);
-
-/**
- * @brief Connects to the X server, using an authorization information.
- * @param display: The name of the display.
- * @param auth: The authorization information.
- * @param screen: A pointer to a preferred screen number.
- * @return A newly allocated xcb_connection_t structure.
- *
- * Connects to the X server specified by @p displayname, using the
- * authorization @p auth. If a particular screen on that server is
- * preferred, the int pointed to by @p screenp (if not @c NULL) will
- * be set to that screen; otherwise @p screenp will be set to 0.
- */
-xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *display, xcb_auth_info_t *auth, int *screen);
-
-
-/* xcb_xid.c */
-
-/**
- * @brief Allocates an XID for a new object.
- * @param c: The connection.
- * @return A newly allocated XID.
- *
- * Allocates an XID for a new object. Typically used just prior to
- * various object creation functions, such as xcb_create_window.
- */
-uint32_t xcb_generate_id(xcb_connection_t *c);
-
-
-/**
- * @}
- */
-
-#ifdef __cplusplus
-}
-#endif
-
-
-#endif /* __XCB_H__ */
+/*
+ * Copyright (C) 2001-2006 Bart Massey, Jamey Sharp, and Josh Triplett.
+ * All Rights Reserved.
+ *
+ * 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.
+ */
+
+#ifndef __XCB_H__
+#define __XCB_H__
+#include <sys/types.h>
+
+#if defined(__solaris__)
+#include <inttypes.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifndef _WIN32
+#include <sys/uio.h>
+#else
+#include "xcb_windefs.h"
+#endif
+#include <pthread.h>
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file xcb.h
+ */
+
+/**
+ * @defgroup XCB_Core_API XCB Core API
+ * @brief Core API of the XCB library.
+ *
+ * @{
+ */
+
+/* Pre-defined constants */
+
+/** Current protocol version */
+#define X_PROTOCOL 11
+
+/** Current minor version */
+#define X_PROTOCOL_REVISION 0
+
+/** X_TCP_PORT + display number = server port for TCP transport */
+#define X_TCP_PORT 6000
+
+#define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1))
+
+/* Opaque structures */
+
+/**
+ * @brief XCB Connection structure.
+ *
+ * A structure that contain all data that  XCB needs to communicate with an X server.
+ */
+typedef struct xcb_connection_t xcb_connection_t;  /**< Opaque structure containing all data that  XCB needs to communicate with an X server. */
+
+
+/* Other types */
+
+/**
+ * @brief Generic iterator.
+ *
+ * A generic iterator structure.
+ */
+typedef struct {
+    void *data;   /**< Data of the current iterator */
+    int rem;    /**< remaining elements */
+    int index;  /**< index of the current iterator */
+} xcb_generic_iterator_t;
+
+/**
+ * @brief Generic reply.
+ *
+ * A generic reply structure.
+ */
+typedef struct {
+    uint8_t   response_type;  /**< Type of the response */
+    uint8_t  pad0;           /**< Padding */
+    uint16_t sequence;       /**< Sequence number */
+    uint32_t length;         /**< Length of the response */
+} xcb_generic_reply_t;
+
+/**
+ * @brief Generic event.
+ *
+ * A generic event structure.
+ */
+typedef struct {
+    uint8_t   response_type;  /**< Type of the response */
+    uint8_t  pad0;           /**< Padding */
+    uint16_t sequence;       /**< Sequence number */
+    uint32_t pad[7];         /**< Padding */
+    uint32_t full_sequence;  /**< full sequence */
+} xcb_generic_event_t;
+
+/**
+ * @brief GE event
+ *
+ * An event as sent by the XGE extension. The length field specifies the
+ * number of 4-byte blocks trailing the struct.
+ */
+typedef struct {
+    uint8_t  response_type;  /**< Type of the response */
+    uint8_t  pad0;           /**< Padding */
+    uint16_t sequence;       /**< Sequence number */
+    uint32_t length;
+    uint16_t event_type;
+    uint16_t pad1;
+    uint32_t pad[5];         /**< Padding */
+    uint32_t full_sequence;  /**< full sequence */
+} xcb_ge_event_t;
+
+/**
+ * @brief Generic error.
+ *
+ * A generic error structure.
+ */
+typedef struct {
+    uint8_t   response_type;  /**< Type of the response */
+    uint8_t   error_code;     /**< Error code */
+    uint16_t sequence;       /**< Sequence number */
+    uint32_t resource_id;     /** < Resource ID for requests with side effects only */
+    uint16_t minor_code;      /** < Minor opcode of the failed request */
+    uint8_t major_code;       /** < Major opcode of the failed request */
+    uint8_t pad0;
+    uint32_t pad[5];         /**< Padding */
+    uint32_t full_sequence;  /**< full sequence */
+} xcb_generic_error_t;
+
+/**
+ * @brief Generic cookie.
+ *
+ * A generic cookie structure.
+ */
+typedef struct {
+    unsigned int sequence;  /**< Sequence number */
+} xcb_void_cookie_t;
+
+
+/* Include the generated xproto header. */
+#include "xproto.h"
+
+
+/** XCB_NONE is the universal null resource or null atom parameter value for many core X requests */
+#define XCB_NONE 0L
+
+/** XCB_COPY_FROM_PARENT can be used for many xcb_create_window parameters */
+#define XCB_COPY_FROM_PARENT 0L
+
+/** XCB_CURRENT_TIME can be used in most requests that take an xcb_timestamp_t */
+#define XCB_CURRENT_TIME 0L
+
+/** XCB_NO_SYMBOL fills in unused entries in xcb_keysym_t tables */
+#define XCB_NO_SYMBOL 0L
+
+
+/* xcb_auth.c */
+
+/**
+ * @brief Container for authorization information.
+ *
+ * A container for authorization information to be sent to the X server.
+ */
+typedef struct xcb_auth_info_t {
+    int   namelen;  /**< Length of the string name (as returned by strlen). */
+    char *name;     /**< String containing the authentication protocol name, such as "MIT-MAGIC-COOKIE-1" or "XDM-AUTHORIZATION-1". */
+    int   datalen;  /**< Length of the data member. */
+    char *data;   /**< Data interpreted in a protocol-specific manner. */
+} xcb_auth_info_t;
+
+
+/* xcb_out.c */
+
+/**
+ * @brief Forces any buffered output to be written to the server.
+ * @param c: The connection to the X server.
+ * @return > @c 0 on success, <= @c 0 otherwise.
+ *
+ * Forces any buffered output to be written to the server. Blocks
+ * until the write is complete.
+ */
+int xcb_flush(xcb_connection_t *c);
+
+/**
+ * @brief Returns the maximum request length that this server accepts.
+ * @param c: The connection to the X server.
+ * @return The maximum request length field.
+ *
+ * In the absence of the BIG-REQUESTS extension, returns the
+ * maximum request length field from the connection setup data, which
+ * may be as much as 65535. If the server supports BIG-REQUESTS, then
+ * the maximum request length field from the reply to the
+ * BigRequestsEnable request will be returned instead.
+ *
+ * Note that this length is measured in four-byte units, making the
+ * theoretical maximum lengths roughly 256kB without BIG-REQUESTS and
+ * 16GB with.
+ */
+uint32_t xcb_get_maximum_request_length(xcb_connection_t *c);
+
+/**
+ * @brief Prefetch the maximum request length without blocking.
+ * @param c: The connection to the X server.
+ *
+ * Without blocking, does as much work as possible toward computing
+ * the maximum request length accepted by the X server.
+ *
+ * Invoking this function may cause a call to xcb_big_requests_enable,
+ * but will not block waiting for the reply.
+ * xcb_get_maximum_request_length will return the prefetched data
+ * after possibly blocking while the reply is retrieved.
+ *
+ * Note that in order for this function to be fully non-blocking, the
+ * application must previously have called
+ * xcb_prefetch_extension_data(c, &xcb_big_requests_id) and the reply
+ * must have already arrived.
+ */
+void xcb_prefetch_maximum_request_length(xcb_connection_t *c);
+
+
+/* xcb_in.c */
+
+/**
+ * @brief Returns the next event or error from the server.
+ * @param c: The connection to the X server.
+ * @return The next event from the server.
+ *
+ * Returns the next event or error from the server, or returns null in
+ * the event of an I/O error. Blocks until either an event or error
+ * arrive, or an I/O error occurs.
+ */
+xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
+
+/**
+ * @brief Returns the next event or error from the server.
+ * @param c: The connection to the X server.
+ * error status of the operation.
+ * @return The next event from the server.
+ *
+ * Returns the next event or error from the server, if one is
+ * available, or returns @c NULL otherwise. If no event is available, that
+ * might be because an I/O error like connection close occurred while
+ * attempting to read the next event, in which case the connection is
+ * shut down when this function returns.
+ */
+xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
+
+/**
+ * @brief Return the error for a request, or NULL if none can ever arrive.
+ * @param c: The connection to the X server.
+ * @param cookie: The request cookie.
+ * @return The error for the request, or NULL if none can ever arrive.
+ *
+ * The xcb_void_cookie_t cookie supplied to this function must have resulted
+ * from a call to xcb_[request_name]_checked().  This function will block
+ * until one of two conditions happens.  If an error is received, it will be
+ * returned.  If a reply to a subsequent request has already arrived, no error
+ * can arrive for this request, so this function will return NULL.
+ *
+ * Note that this function will perform a sync if needed to ensure that the
+ * sequence number will advance beyond that provided in cookie; this is a
+ * convenience to avoid races in determining whether the sync is needed.
+ */
+xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie);
+
+/**
+ * @brief Discards the reply for a request.
+ * @param c: The connection to the X server.
+ * @param sequence: The request sequence number from a cookie.
+ *
+ * Discards the reply for a request. Additionally, any error generated
+ * by the request is also discarded (unless it was an _unchecked request
+ * and the error has already arrived).
+ *
+ * This function will not block even if the reply is not yet available.
+ *
+ * Note that the sequence really does have to come from an xcb cookie;
+ * this function is not designed to operate on socket-handoff replies.
+ */
+void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence);
+
+
+/* xcb_ext.c */
+
+/**
+ * @typedef typedef struct xcb_extension_t xcb_extension_t
+ */
+typedef struct xcb_extension_t xcb_extension_t;  /**< Opaque structure used as key for xcb_get_extension_data_t. */
+
+/**
+ * @brief Caches reply information from QueryExtension requests.
+ * @param c: The connection.
+ * @param ext: The extension data.
+ * @return A pointer to the xcb_query_extension_reply_t for the extension.
+ *
+ * This function is the primary interface to the "extension cache",
+ * which caches reply information from QueryExtension
+ * requests. Invoking this function may cause a call to
+ * xcb_query_extension to retrieve extension information from the
+ * server, and may block until extension data is received from the
+ * server.
+ *
+ * The result must not be freed. This storage is managed by the cache
+ * itself.
+ */
+const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
+
+/**
+ * @brief Prefetch of extension data into the extension cache
+ * @param c: The connection.
+ * @param ext: The extension data.
+ *
+ * This function allows a "prefetch" of extension data into the
+ * extension cache. Invoking the function may cause a call to
+ * xcb_query_extension, but will not block waiting for the
+ * reply. xcb_get_extension_data will return the prefetched data after
+ * possibly blocking while it is retrieved.
+ */
+void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
+
+
+/* xcb_conn.c */
+
+/**
+ * @brief Access the data returned by the server.
+ * @param c: The connection.
+ * @return A pointer to an xcb_setup_t structure.
+ *
+ * Accessor for the data returned by the server when the xcb_connection_t
+ * was initialized. This data includes
+ * - the server's required format for images,
+ * - a list of available visuals,
+ * - a list of available screens,
+ * - the server's maximum request length (in the absence of the
+ * BIG-REQUESTS extension),
+ * - and other assorted information.
+ *
+ * See the X protocol specification for more details.
+ *
+ * The result must not be freed.
+ */
+const xcb_setup_t *xcb_get_setup(xcb_connection_t *c);
+
+/**
+ * @brief Access the file descriptor of the connection.
+ * @param c: The connection.
+ * @return The file descriptor.
+ *
+ * Accessor for the file descriptor that was passed to the
+ * xcb_connect_to_fd call that returned @p c.
+ */
+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.
+ *
+ * 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.
+ */
+int xcb_connection_has_error(xcb_connection_t *c);
+
+/**
+ * @brief Connects to the X server.
+ * @param fd: The file descriptor.
+ * @param auth_info: Authentication data.
+ * @return A newly allocated xcb_connection_t structure.
+ *
+ * Connects to an X server, given the open socket @p fd and the
+ * xcb_auth_info_t @p auth_info. The file descriptor @p fd is
+ * bidirectionally connected to an X server. If the connection
+ * should be unauthenticated, @p auth_info must be @c
+ * NULL.
+ */
+xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info);
+
+/**
+ * @brief Closes the connection.
+ * @param c: The connection.
+ *
+ * Closes the file descriptor and frees all memory associated with the
+ * connection @c c.
+ */
+void xcb_disconnect(xcb_connection_t *c);
+
+
+/* xcb_util.c */
+
+/**
+ * @brief Parses a display string name in the form documented by X(7x).
+ * @param name: The name of the display.
+ * @param host: A pointer to a malloc'd copy of the hostname.
+ * @param display: A pointer to the display number.
+ * @param screen: A pointer to the screen number.
+ * @return 0 on failure, non 0 otherwise.
+ *
+ * Parses the display string name @p display_name in the form
+ * documented by X(7x). Has no side effects on failure. If
+ * @p displayname is @c NULL or empty, it uses the environment
+ * variable DISPLAY. @p hostp is a pointer to a newly allocated string
+ * that contain the host name. @p displayp is set to the display
+ * number and @p screenp to the preferred screen number. @p screenp
+ * can be @c NULL. If @p displayname does not contain a screen number,
+ * it is set to @c 0.
+ */
+int xcb_parse_display(const char *name, char **host, int *display, int *screen);
+
+/**
+ * @brief Connects to the X server.
+ * @param displayname: The name of the display.
+ * @param screenp: A pointer to a preferred screen number.
+ * @return A newly allocated xcb_connection_t structure.
+ *
+ * Connects to the X server specified by @p displayname. If @p
+ * displayname is @c NULL, uses the value of the DISPLAY environment
+ * variable. If a particular screen on that server is preferred, the
+ * int pointed to by @p screenp (if not @c NULL) will be set to that
+ * screen; otherwise the screen will be set to 0.
+ */
+xcb_connection_t *xcb_connect(const char *displayname, int *screenp);
+
+/**
+ * @brief Connects to the X server, using an authorization information.
+ * @param display: The name of the display.
+ * @param auth: The authorization information.
+ * @param screen: A pointer to a preferred screen number.
+ * @return A newly allocated xcb_connection_t structure.
+ *
+ * Connects to the X server specified by @p displayname, using the
+ * authorization @p auth. If a particular screen on that server is
+ * preferred, the int pointed to by @p screenp (if not @c NULL) will
+ * be set to that screen; otherwise @p screenp will be set to 0.
+ */
+xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *display, xcb_auth_info_t *auth, int *screen);
+
+
+/* xcb_xid.c */
+
+/**
+ * @brief Allocates an XID for a new object.
+ * @param c: The connection.
+ * @return A newly allocated XID.
+ *
+ * Allocates an XID for a new object. Typically used just prior to
+ * various object creation functions, such as xcb_create_window.
+ */
+uint32_t xcb_generate_id(xcb_connection_t *c);
+
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* __XCB_H__ */
diff --git a/libxcb/src/xcb_auth.c b/libxcb/src/xcb_auth.c
index d774d1069..e60cb5f98 100644
--- a/libxcb/src/xcb_auth.c
+++ b/libxcb/src/xcb_auth.c
@@ -1,351 +1,356 @@
-/* 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.
- */
-
-/* Authorization systems for the X protocol. */
-
-#include <assert.h>
-#include <X11/Xauth.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/un.h>
-#include <sys/param.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#include "xcb.h"
-#include "xcbint.h"
-
-#ifdef HASXDMAUTH
-#include <X11/Xdmcp.h>
-#endif
-
-enum auth_protos {
-#ifdef HASXDMAUTH
-    AUTH_XA1,
-#endif
-    AUTH_MC1,
-    N_AUTH_PROTOS
-};
-
-#define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
-#define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
-
-static char *authnames[N_AUTH_PROTOS] = {
-#ifdef HASXDMAUTH
-    AUTH_PROTO_XDM_AUTHORIZATION,
-#endif
-    AUTH_PROTO_MIT_MAGIC_COOKIE,
-};
-
-static int authnameslen[N_AUTH_PROTOS] = {
-#ifdef HASXDMAUTH
-    sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
-#endif
-    sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
-};
-
-static size_t memdup(char **dst, void *src, size_t len)
-{
-    if(len)
-	*dst = malloc(len);
-    else
-	*dst = 0;
-    if(!*dst)
-	return 0;
-    memcpy(*dst, src, len);
-    return len;
-}
-
-static int authname_match(enum auth_protos kind, char *name, size_t namelen)
-{
-    if(authnameslen[kind] != namelen)
-	return 0;
-    if(memcmp(authnames[kind], name, namelen))
-	return 0;
-    return 1;
-}
-
-#define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
-
-static Xauth *get_authptr(struct sockaddr *sockname, int display)
-{
-    char *addr = 0;
-    int addrlen = 0;
-    unsigned short family;
-    char hostnamebuf[256];   /* big enough for max hostname */
-    char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
-    int dispbuflen;
-
-    family = FamilyLocal; /* 256 */
-    switch(sockname->sa_family)
-    {
-#ifdef AF_INET6
-    case AF_INET6:
-        addr = (char *) SIN6_ADDR(sockname);
-        addrlen = sizeof(*SIN6_ADDR(sockname));
-        if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
-        {
-            if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
-                family = XCB_FAMILY_INTERNET_6;
-            break;
-        }
-        addr += 12;
-        /* if v4-mapped, fall through. */
-#endif
-    case AF_INET:
-        if(!addr)
-            addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
-        addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
-        if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
-            family = XCB_FAMILY_INTERNET;
-        break;
-    case AF_UNIX:
-        break;
-    default:
-        return 0;   /* cannot authenticate this family */
-    }
-
-    dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
-    if(dispbuflen < 0)
-        return 0;
-    /* snprintf may have truncate our text */
-    dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
-
-    if (family == FamilyLocal) {
-        if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
-            return 0;   /* do not know own hostname */
-        addr = hostnamebuf;
-        addrlen = strlen(addr);
-    }
-
-    return XauGetBestAuthByAddr (family,
-                                 (unsigned short) addrlen, addr,
-                                 (unsigned short) dispbuflen, dispbuf,
-                                 N_AUTH_PROTOS, authnames, authnameslen);
-}
-
-#ifdef HASXDMAUTH
-static int next_nonce(void)
-{
-    static int nonce = 0;
-    static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
-    int ret;
-    pthread_mutex_lock(&nonce_mutex);
-    ret = nonce++;
-    pthread_mutex_unlock(&nonce_mutex);
-    return ret;
-}
-
-static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
-    memcpy(buf + *idxp, val, valsize);
-    *idxp += valsize;
-}
-#endif
-     
-static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
-{
-    if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
-        info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
-        if(!info->datalen)
-            return 0;
-        return 1;
-    }
-#ifdef HASXDMAUTH
-#define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
-    if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
-	int j;
-
-	info->data = malloc(192 / 8);
-	if(!info->data)
-	    return 0;
-
-	for (j = 0; j < 8; j++)
-	    info->data[j] = authptr->data[j];
-	switch(sockname->sa_family) {
-        case AF_INET:
-            /*block*/ {
-	    struct sockaddr_in *si = (struct sockaddr_in *) sockname;
-	    APPEND(info->data, j, si->sin_addr.s_addr);
-	    APPEND(info->data, j, si->sin_port);
-	}
-	break;
-#ifdef AF_INET6
-        case AF_INET6:
-            /*block*/ {
-            struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
-            if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
-            {
-                do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4);
-                APPEND(info->data, j, si6->sin6_port);
-            }
-            else
-            {
-                /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly.  Do the
-                   same thing Xlib does: use all zeroes for the 4-byte address
-                   and 2-byte port number. */
-                uint32_t fakeaddr = 0;
-                uint16_t fakeport = 0;
-                APPEND(info->data, j, fakeaddr);
-                APPEND(info->data, j, fakeport);
-            }
-        }
-        break;
-#endif
-        case AF_UNIX:
-            /*block*/ {
-	    uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
-	    uint16_t fakeport = htons(getpid());
-	    APPEND(info->data, j, fakeaddr);
-	    APPEND(info->data, j, fakeport);
-	}
-	break;
-        default:
-            free(info->data);
-            return 0;   /* do not know how to build this */
-	}
-	{
-	    uint32_t now = htonl(time(0));
-	    APPEND(info->data, j, now);
-	}
-	assert(j <= 192 / 8);
-	while (j < 192 / 8)
-	    info->data[j++] = 0;
-	info->datalen = j;
-	XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
-	return 1;
-    }
-#undef APPEND
-#endif
-
-    return 0;   /* Unknown authorization type */
-}
-
-/* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */
-#define INITIAL_SOCKNAME_SLACK 108
-
-/* Return a dynamically allocated socket address structure according
-   to the value returned by either getpeername() or getsockname()
-   (according to POSIX, applications should not assume a particular
-   length for `sockaddr_un.sun_path') */
-static struct sockaddr *get_peer_sock_name(int (*socket_func)(int,
-							      struct sockaddr *,
-							      socklen_t *),
-					   int fd)
-{
-    socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK;
-    socklen_t actual_socknamelen = socknamelen;
-    struct sockaddr *sockname = malloc(socknamelen), *new_sockname = NULL;
-
-    if (sockname == NULL)
-        return NULL;
-
-    /* Both getpeername() and getsockname() truncates sockname if
-       there is not enough space and set the required length in
-       actual_socknamelen */
-    if (socket_func(fd, sockname, &actual_socknamelen) == -1)
-        goto sock_or_realloc_error;
-
-    if (actual_socknamelen > socknamelen)
-    {
-        socknamelen = actual_socknamelen;
-
-        if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL ||
-            socket_func(fd, new_sockname, &actual_socknamelen) == -1 ||
-            actual_socknamelen > socknamelen) 
-            goto sock_or_realloc_error;
-
-        sockname = new_sockname;
-    }
-
-    return sockname;
-
- sock_or_realloc_error:
-    free(sockname);
-    return NULL;
-}
-
-int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
-{
-    /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
-       xtrans/Xtransutils.c */
-    struct sockaddr *sockname = NULL;
-    int gotsockname = 0;
-    Xauth *authptr = 0;
-    int ret = 1;
-
-    /* Some systems like hpux or Hurd do not expose peer names
-     * for UNIX Domain Sockets, but this is irrelevant,
-     * since compute_auth() ignores the peer name in this
-     * case anyway.*/
-    if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL)
-    {
-        if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
-            return 0;   /* can only authenticate sockets */
-        if (sockname->sa_family != AF_UNIX)
-        {
-            free(sockname);
-            return 0;   /* except for AF_UNIX, sockets should have peernames */
-        }
-        gotsockname = 1;
-    }
-
-    authptr = get_authptr(sockname, display);
-    if (authptr == 0)
-    {
-        free(sockname);
-        return 0;   /* cannot find good auth data */
-    }
-
-    info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
-    if (!info->namelen)
-        goto no_auth;   /* out of memory */
-
-    if (!gotsockname && (sockname = get_peer_sock_name(getsockname, fd)) == NULL)
-    {
-        free(info->name);
-        goto no_auth;   /* can only authenticate sockets */
-    }
-
-    ret = compute_auth(info, authptr, sockname);
-    if(!ret)
-    {
-        free(info->name);
-        goto no_auth;   /* cannot build auth record */
-    }
-
-    free(sockname);
-    sockname = NULL;
-
-    XauDisposeAuth(authptr);
-    return ret;
-
- no_auth:
-    free(sockname);
-
-    info->name = 0;
-    info->namelen = 0;
-    XauDisposeAuth(authptr);
-    return 0;
-}
+/* 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.
+ */
+
+/* Authorization systems for the X protocol. */
+
+#include <assert.h>
+#include <X11/Xauth.h>
+#include <sys/param.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#ifdef _WIN32
+#include "xcb_windefs.h"
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+#endif /* _WIN32 */
+
+#include "xcb.h"
+#include "xcbint.h"
+
+#ifdef HASXDMAUTH
+#include <X11/Xdmcp.h>
+#endif
+
+enum auth_protos {
+#ifdef HASXDMAUTH
+    AUTH_XA1,
+#endif
+    AUTH_MC1,
+    N_AUTH_PROTOS
+};
+
+#define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
+#define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
+
+static char *authnames[N_AUTH_PROTOS] = {
+#ifdef HASXDMAUTH
+    AUTH_PROTO_XDM_AUTHORIZATION,
+#endif
+    AUTH_PROTO_MIT_MAGIC_COOKIE,
+};
+
+static int authnameslen[N_AUTH_PROTOS] = {
+#ifdef HASXDMAUTH
+    sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
+#endif
+    sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
+};
+
+static size_t memdup(char **dst, void *src, size_t len)
+{
+    if(len)
+	*dst = malloc(len);
+    else
+	*dst = 0;
+    if(!*dst)
+	return 0;
+    memcpy(*dst, src, len);
+    return len;
+}
+
+static int authname_match(enum auth_protos kind, char *name, size_t namelen)
+{
+    if(authnameslen[kind] != namelen)
+	return 0;
+    if(memcmp(authnames[kind], name, namelen))
+	return 0;
+    return 1;
+}
+
+#define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
+
+static Xauth *get_authptr(struct sockaddr *sockname, int display)
+{
+    char *addr = 0;
+    int addrlen = 0;
+    unsigned short family;
+    char hostnamebuf[256];   /* big enough for max hostname */
+    char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
+    int dispbuflen;
+
+    family = FamilyLocal; /* 256 */
+    switch(sockname->sa_family)
+    {
+#ifdef AF_INET6
+    case AF_INET6:
+        addr = (char *) SIN6_ADDR(sockname);
+        addrlen = sizeof(*SIN6_ADDR(sockname));
+        if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
+        {
+            if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
+                family = XCB_FAMILY_INTERNET_6;
+            break;
+        }
+        addr += 12;
+        /* if v4-mapped, fall through. */
+#endif
+    case AF_INET:
+        if(!addr)
+            addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
+        addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
+        if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
+            family = XCB_FAMILY_INTERNET;
+        break;
+    case AF_UNIX:
+        break;
+    default:
+        return 0;   /* cannot authenticate this family */
+    }
+
+    dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
+    if(dispbuflen < 0)
+        return 0;
+    /* snprintf may have truncate our text */
+    dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
+
+    if (family == FamilyLocal) {
+        if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
+            return 0;   /* do not know own hostname */
+        addr = hostnamebuf;
+        addrlen = strlen(addr);
+    }
+
+    return XauGetBestAuthByAddr (family,
+                                 (unsigned short) addrlen, addr,
+                                 (unsigned short) dispbuflen, dispbuf,
+                                 N_AUTH_PROTOS, authnames, authnameslen);
+}
+
+#ifdef HASXDMAUTH
+static int next_nonce(void)
+{
+    static int nonce = 0;
+    static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
+    int ret;
+    pthread_mutex_lock(&nonce_mutex);
+    ret = nonce++;
+    pthread_mutex_unlock(&nonce_mutex);
+    return ret;
+}
+
+static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
+    memcpy(buf + *idxp, val, valsize);
+    *idxp += valsize;
+}
+#endif
+     
+static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
+{
+    if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
+        info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
+        if(!info->datalen)
+            return 0;
+        return 1;
+    }
+#ifdef HASXDMAUTH
+#define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
+    if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
+	int j;
+
+	info->data = malloc(192 / 8);
+	if(!info->data)
+	    return 0;
+
+	for (j = 0; j < 8; j++)
+	    info->data[j] = authptr->data[j];
+	switch(sockname->sa_family) {
+        case AF_INET:
+            /*block*/ {
+	    struct sockaddr_in *si = (struct sockaddr_in *) sockname;
+	    APPEND(info->data, j, si->sin_addr.s_addr);
+	    APPEND(info->data, j, si->sin_port);
+	}
+	break;
+#ifdef AF_INET6
+        case AF_INET6:
+            /*block*/ {
+            struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
+            if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
+            {
+                do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4);
+                APPEND(info->data, j, si6->sin6_port);
+            }
+            else
+            {
+                /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly.  Do the
+                   same thing Xlib does: use all zeroes for the 4-byte address
+                   and 2-byte port number. */
+                uint32_t fakeaddr = 0;
+                uint16_t fakeport = 0;
+                APPEND(info->data, j, fakeaddr);
+                APPEND(info->data, j, fakeport);
+            }
+        }
+        break;
+#endif
+        case AF_UNIX:
+            /*block*/ {
+	    uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
+	    uint16_t fakeport = htons(getpid());
+	    APPEND(info->data, j, fakeaddr);
+	    APPEND(info->data, j, fakeport);
+	}
+	break;
+        default:
+            free(info->data);
+            return 0;   /* do not know how to build this */
+	}
+	{
+	    uint32_t now = htonl(time(0));
+	    APPEND(info->data, j, now);
+	}
+	assert(j <= 192 / 8);
+	while (j < 192 / 8)
+	    info->data[j++] = 0;
+	info->datalen = j;
+	XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
+	return 1;
+    }
+#undef APPEND
+#endif
+
+    return 0;   /* Unknown authorization type */
+}
+
+/* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */
+#define INITIAL_SOCKNAME_SLACK 108
+
+/* Return a dynamically allocated socket address structure according
+   to the value returned by either getpeername() or getsockname()
+   (according to POSIX, applications should not assume a particular
+   length for `sockaddr_un.sun_path') */
+static struct sockaddr *get_peer_sock_name(int (*socket_func)(int,
+							      struct sockaddr *,
+							      socklen_t *),
+					   int fd)
+{
+    socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK;
+    socklen_t actual_socknamelen = socknamelen;
+    struct sockaddr *sockname = malloc(socknamelen), *new_sockname = NULL;
+
+    if (sockname == NULL)
+        return NULL;
+
+    /* Both getpeername() and getsockname() truncates sockname if
+       there is not enough space and set the required length in
+       actual_socknamelen */
+    if (socket_func(fd, sockname, &actual_socknamelen) == -1)
+        goto sock_or_realloc_error;
+
+    if (actual_socknamelen > socknamelen)
+    {
+        socknamelen = actual_socknamelen;
+
+        if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL ||
+            socket_func(fd, new_sockname, &actual_socknamelen) == -1 ||
+            actual_socknamelen > socknamelen) 
+            goto sock_or_realloc_error;
+
+        sockname = new_sockname;
+    }
+
+    return sockname;
+
+ sock_or_realloc_error:
+    free(sockname);
+    return NULL;
+}
+
+int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
+{
+    /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
+       xtrans/Xtransutils.c */
+    struct sockaddr *sockname = NULL;
+    int gotsockname = 0;
+    Xauth *authptr = 0;
+    int ret = 1;
+
+    /* Some systems like hpux or Hurd do not expose peer names
+     * for UNIX Domain Sockets, but this is irrelevant,
+     * since compute_auth() ignores the peer name in this
+     * case anyway.*/
+    if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL)
+    {
+        if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
+            return 0;   /* can only authenticate sockets */
+        if (sockname->sa_family != AF_UNIX)
+        {
+            free(sockname);
+            return 0;   /* except for AF_UNIX, sockets should have peernames */
+        }
+        gotsockname = 1;
+    }
+
+    authptr = get_authptr(sockname, display);
+    if (authptr == 0)
+    {
+        free(sockname);
+        return 0;   /* cannot find good auth data */
+    }
+
+    info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
+    if (!info->namelen)
+        goto no_auth;   /* out of memory */
+
+    if (!gotsockname && (sockname = get_peer_sock_name(getsockname, fd)) == NULL)
+    {
+        free(info->name);
+        goto no_auth;   /* can only authenticate sockets */
+    }
+
+    ret = compute_auth(info, authptr, sockname);
+    if(!ret)
+    {
+        free(info->name);
+        goto no_auth;   /* cannot build auth record */
+    }
+
+    free(sockname);
+    sockname = NULL;
+
+    XauDisposeAuth(authptr);
+    return ret;
+
+ no_auth:
+    free(sockname);
+
+    info->name = 0;
+    info->namelen = 0;
+    XauDisposeAuth(authptr);
+    return 0;
+}
diff --git a/libxcb/src/xcb_conn.c b/libxcb/src/xcb_conn.c
index 2ab84eac0..f0b9dd08c 100644
--- a/libxcb/src/xcb_conn.c
+++ b/libxcb/src/xcb_conn.c
@@ -30,7 +30,6 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
-#include <netinet/in.h>
 #include <fcntl.h>
 #include <errno.h>
 
@@ -38,10 +37,16 @@
 #include "xcbint.h"
 #if USE_POLL
 #include <poll.h>
-#else
+#elif !defined _WIN32
 #include <sys/select.h>
 #endif
 
+#ifdef _WIN32
+#include "xcb_windefs.h"
+#else
+#include <netinet/in.h>
+#endif /* _WIN32 */
+
 /* SHUT_RDWR is fairly recent and is not available on all platforms */
 #if !defined(SHUT_RDWR)
 #define SHUT_RDWR 2
@@ -57,6 +62,17 @@ static 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 = 0;
+
+   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;
@@ -66,6 +82,7 @@ static int set_fd_flags(const int fd)
     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)
@@ -158,9 +175,37 @@ static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
 {
     int n;
     assert(!c->out.queue_len);
+
+#ifdef _WIN32
+    int i = 0;
+    int ret = 0,err = 0;
+    struct iovec *vec;
+    n = 0;
+
+    /* 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 < *count)
+    {         	 
+         ret = send(c->fd,vec->iov_base,vec->iov_len,0);	 
+         if(ret == SOCKET_ERROR)
+         {
+             err  = WSAGetLastError();
+             if(err == WSAEWOULDBLOCK)
+             {
+                 return 1;
+             }
+         }
+         n += ret;
+         *vec++;
+         i++;
+    }
+#else
     n = writev(c->fd, *vector, *count);
     if(n < 0 && errno == EAGAIN)
         return 1;
+#endif /* _WIN32 */    
+
     if(n <= 0)
     {
         _xcb_conn_shutdown(c);
diff --git a/libxcb/src/xcb_in.c b/libxcb/src/xcb_in.c
index 6072cea9a..b4c48fb67 100644
--- a/libxcb/src/xcb_in.c
+++ b/libxcb/src/xcb_in.c
@@ -37,14 +37,25 @@
 #include "xcbint.h"
 #if USE_POLL
 #include <poll.h>
-#else
+#endif
+#ifndef _WIN32
 #include <sys/select.h>
+#include <sys/socket.h>
 #endif
 
+#ifdef _WIN32
+#include "xcb_windefs.h"
+#endif /* _WIN32 */
+
 #define XCB_ERROR 0
 #define XCB_REPLY 1
 #define XCB_XGE_EVENT 35
 
+/* required for compiling for Win32 using MinGW */
+#ifndef MSG_WAITALL
+#define MSG_WAITALL 0
+#endif
+
 struct event_list {
     xcb_generic_event_t *event;
     struct event_list *next;
@@ -255,10 +266,14 @@ static int read_block(const int fd, void *buf, const ssize_t len)
     int done = 0;
     while(done < len)
     {
-        int ret = read(fd, ((char *) buf) + done, len - done);
+        int ret = recv(fd, ((char *) buf) + done, len - done,MSG_WAITALL);
         if(ret > 0)
             done += ret;
+#ifndef _WIN32
         if(ret < 0 && errno == EAGAIN)
+#else
+        if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
+#endif /* !_Win32 */
         {
 #if USE_POLL
             struct pollfd pfd;
@@ -272,10 +287,13 @@ static int read_block(const int fd, void *buf, const ssize_t len)
             fd_set fds;
             FD_ZERO(&fds);
             FD_SET(fd, &fds);
+
+	    /* Initializing errno here makes sure that for Win32 this loop will execute only once */
+	    errno = 0;  
 	    do {
 		ret = select(fd + 1, &fds, 0, 0, 0);
 	    } while (ret == -1 && errno == EINTR);
-#endif
+#endif /* USE_POLL */
         }
         if(ret <= 0)
             return ret;
@@ -663,12 +681,16 @@ void _xcb_in_replies_done(xcb_connection_t *c)
 
 int _xcb_in_read(xcb_connection_t *c)
 {
-    int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len);
+    int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,MSG_WAITALL);
     if(n > 0)
         c->in.queue_len += n;
     while(read_packet(c))
         /* empty */;
+#ifndef _WIN32
     if((n > 0) || (n < 0 && errno == EAGAIN))
+#else
+    if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
+#endif /* !_WIN32 */
         return 1;
     _xcb_conn_shutdown(c);
     return 0;
diff --git a/libxcb/src/xcb_util.c b/libxcb/src/xcb_util.c
index 9cdfbff3f..00d62131b 100644
--- a/libxcb/src/xcb_util.c
+++ b/libxcb/src/xcb_util.c
@@ -27,24 +27,29 @@
 
 #include <assert.h>
 #include <sys/types.h>
-#include <sys/socket.h>
 #include <limits.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
 #ifdef DNETCONN
 #include <netdnet/dnetdb.h>
 #include <netdnet/dn.h>
 #endif
-#include <netdb.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <unistd.h>
-#include <fcntl.h>
 #include <string.h>
 
+#ifdef _WIN32
+#include "xcb_windefs.h"
+#else
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <fcntl.h>
+#include <netdb.h>
+#endif /* _WIN32 */
+
 #include "xcb.h"
 #include "xcbext.h"
 #include "xcbint.h"
@@ -139,7 +144,9 @@ int xcb_parse_display(const char *name, char **host, int *displayp,
 }
 
 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
+#ifndef _WIN32
 static int _xcb_open_unix(char *protocol, const char *file);
+#endif /* !WIN32 */
 #ifdef DNETCONN
 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
 #endif
@@ -188,6 +195,7 @@ static int _xcb_open(const char *host, char *protocol, const int display)
             }
     }
 
+#ifndef _WIN32
     filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
     file = malloc(filelen);
     if(file == NULL)
@@ -220,6 +228,8 @@ static int _xcb_open(const char *host, char *protocol, const int display)
     free(file);
 
     return fd;
+#endif /* !_WIN32 */
+    return -1; /* if control reaches here then something has gone wrong */
 }
 
 static int _xcb_socket(int family, int type, int proto)
@@ -232,8 +242,10 @@ static int _xcb_socket(int family, int type, int proto)
 #endif
     {
 	fd = socket(family, type, proto);
+#ifndef _WIN32
 	if (fd >= 0)
 	    fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
     }
     return fd;
 }
@@ -340,6 +352,7 @@ static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short
     return fd;
 }
 
+#ifndef _WIN32
 static int _xcb_open_unix(char *protocol, const char *file)
 {
     int fd;
@@ -362,6 +375,7 @@ static int _xcb_open_unix(char *protocol, const char *file)
     }
     return fd;
 }
+#endif /* !_WIN32 */
 
 #ifdef HAVE_ABSTRACT_SOCKETS
 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
diff --git a/libxcb/src/xcb_windefs.h b/libxcb/src/xcb_windefs.h
new file mode 100644
index 000000000..a8e9524d6
--- /dev/null
+++ b/libxcb/src/xcb_windefs.h
@@ -0,0 +1,45 @@
+/* Copyright (C) 2009 Jatin Golani.
+ *
+ * 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.
+ */
+
+
+#ifndef _XCB_WINDEFS_H
+#define _XCB_WINDEFS_H
+
+#ifndef WINVER
+#define WINVER 0x0501 /* required for getaddrinfo/freeaddrinfo defined only for WinXP and above */
+#endif
+
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <windef.h>
+
+struct iovec  {
+    void *iov_base;	/* Pointer to data.  */
+    int iov_len;	/* Length of data.  */ 
+};
+
+typedef unsigned int in_addr_t;
+
+#endif /* xcb_windefs.h */
-- 
cgit v1.2.3