Home | History | Annotate | Download | only in xcb
      1 /*
      2  * Copyright (C) 2001-2006 Bart Massey, Jamey Sharp, and Josh Triplett.
      3  * All Rights Reserved.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the "Software"),
      7  * to deal in the Software without restriction, including without limitation
      8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9  * and/or sell copies of the Software, and to permit persons to whom the
     10  * Software is furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     21  *
     22  * Except as contained in this notice, the names of the authors or their
     23  * institutions shall not be used in advertising or otherwise to promote the
     24  * sale, use or other dealings in this Software without prior written
     25  * authorization from the authors.
     26  */
     27 
     28 #ifndef __XCB_H__
     29 #define __XCB_H__
     30 #include <sys/types.h>
     31 
     32 #if defined(__solaris__)
     33 #include <inttypes.h>
     34 #else
     35 #include <stdint.h>
     36 #endif
     37 
     38 #ifndef _WIN32
     39 #include <sys/uio.h>
     40 #else
     41 #include "xcb_windefs.h"
     42 #endif
     43 #include <pthread.h>
     44 
     45 
     46 #ifdef __cplusplus
     47 extern "C" {
     48 #endif
     49 
     50 /**
     51  * @file xcb.h
     52  */
     53 
     54 /**
     55  * @defgroup XCB_Core_API XCB Core API
     56  * @brief Core API of the XCB library.
     57  *
     58  * @{
     59  */
     60 
     61 /* Pre-defined constants */
     62 
     63 /** Current protocol version */
     64 #define X_PROTOCOL 11
     65 
     66 /** Current minor version */
     67 #define X_PROTOCOL_REVISION 0
     68 
     69 /** X_TCP_PORT + display number = server port for TCP transport */
     70 #define X_TCP_PORT 6000
     71 
     72 /** xcb connection errors because of socket, pipe and other stream errors. */
     73 #define XCB_CONN_ERROR 1
     74 
     75 /** xcb connection shutdown because of extension not sppported */
     76 #define XCB_CONN_CLOSED_EXT_NOTSUPPORTED 2
     77 
     78 /** malloc(), calloc() and realloc() error upon failure, for eg ENOMEM */
     79 #define XCB_CONN_CLOSED_MEM_INSUFFICIENT 3
     80 
     81 /** Connection closed, exceeding request length that server accepts. */
     82 #define XCB_CONN_CLOSED_REQ_LEN_EXCEED 4
     83 
     84 /** Connection closed, error during parsing display string. */
     85 #define XCB_CONN_CLOSED_PARSE_ERR 5
     86 
     87 #define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1))
     88 
     89 /* Opaque structures */
     90 
     91 /**
     92  * @brief XCB Connection structure.
     93  *
     94  * A structure that contain all data that  XCB needs to communicate with an X server.
     95  */
     96 typedef struct xcb_connection_t xcb_connection_t;  /**< Opaque structure containing all data that  XCB needs to communicate with an X server. */
     97 
     98 
     99 /* Other types */
    100 
    101 /**
    102  * @brief Generic iterator.
    103  *
    104  * A generic iterator structure.
    105  */
    106 typedef struct {
    107     void *data;   /**< Data of the current iterator */
    108     int rem;    /**< remaining elements */
    109     int index;  /**< index of the current iterator */
    110 } xcb_generic_iterator_t;
    111 
    112 /**
    113  * @brief Generic reply.
    114  *
    115  * A generic reply structure.
    116  */
    117 typedef struct {
    118     uint8_t   response_type;  /**< Type of the response */
    119     uint8_t  pad0;           /**< Padding */
    120     uint16_t sequence;       /**< Sequence number */
    121     uint32_t length;         /**< Length of the response */
    122 } xcb_generic_reply_t;
    123 
    124 /**
    125  * @brief Generic event.
    126  *
    127  * A generic event structure.
    128  */
    129 typedef struct {
    130     uint8_t   response_type;  /**< Type of the response */
    131     uint8_t  pad0;           /**< Padding */
    132     uint16_t sequence;       /**< Sequence number */
    133     uint32_t pad[7];         /**< Padding */
    134     uint32_t full_sequence;  /**< full sequence */
    135 } xcb_generic_event_t;
    136 
    137 /**
    138  * @brief GE event
    139  *
    140  * An event as sent by the XGE extension. The length field specifies the
    141  * number of 4-byte blocks trailing the struct.
    142  */
    143 typedef struct {
    144     uint8_t  response_type;  /**< Type of the response */
    145     uint8_t  pad0;           /**< Padding */
    146     uint16_t sequence;       /**< Sequence number */
    147     uint32_t length;
    148     uint16_t event_type;
    149     uint16_t pad1;
    150     uint32_t pad[5];         /**< Padding */
    151     uint32_t full_sequence;  /**< full sequence */
    152 } xcb_ge_event_t;
    153 
    154 /**
    155  * @brief Generic error.
    156  *
    157  * A generic error structure.
    158  */
    159 typedef struct {
    160     uint8_t   response_type;  /**< Type of the response */
    161     uint8_t   error_code;     /**< Error code */
    162     uint16_t sequence;       /**< Sequence number */
    163     uint32_t resource_id;     /** < Resource ID for requests with side effects only */
    164     uint16_t minor_code;      /** < Minor opcode of the failed request */
    165     uint8_t major_code;       /** < Major opcode of the failed request */
    166     uint8_t pad0;
    167     uint32_t pad[5];         /**< Padding */
    168     uint32_t full_sequence;  /**< full sequence */
    169 } xcb_generic_error_t;
    170 
    171 /**
    172  * @brief Generic cookie.
    173  *
    174  * A generic cookie structure.
    175  */
    176 typedef struct {
    177     unsigned int sequence;  /**< Sequence number */
    178 } xcb_void_cookie_t;
    179 
    180 
    181 /* Include the generated xproto header. */
    182 #include "xproto.h"
    183 
    184 
    185 /** XCB_NONE is the universal null resource or null atom parameter value for many core X requests */
    186 #define XCB_NONE 0L
    187 
    188 /** XCB_COPY_FROM_PARENT can be used for many xcb_create_window parameters */
    189 #define XCB_COPY_FROM_PARENT 0L
    190 
    191 /** XCB_CURRENT_TIME can be used in most requests that take an xcb_timestamp_t */
    192 #define XCB_CURRENT_TIME 0L
    193 
    194 /** XCB_NO_SYMBOL fills in unused entries in xcb_keysym_t tables */
    195 #define XCB_NO_SYMBOL 0L
    196 
    197 
    198 /* xcb_auth.c */
    199 
    200 /**
    201  * @brief Container for authorization information.
    202  *
    203  * A container for authorization information to be sent to the X server.
    204  */
    205 typedef struct xcb_auth_info_t {
    206     int   namelen;  /**< Length of the string name (as returned by strlen). */
    207     char *name;     /**< String containing the authentication protocol name, such as "MIT-MAGIC-COOKIE-1" or "XDM-AUTHORIZATION-1". */
    208     int   datalen;  /**< Length of the data member. */
    209     char *data;   /**< Data interpreted in a protocol-specific manner. */
    210 } xcb_auth_info_t;
    211 
    212 
    213 /* xcb_out.c */
    214 
    215 /**
    216  * @brief Forces any buffered output to be written to the server.
    217  * @param c: The connection to the X server.
    218  * @return > @c 0 on success, <= @c 0 otherwise.
    219  *
    220  * Forces any buffered output to be written to the server. Blocks
    221  * until the write is complete.
    222  */
    223 int xcb_flush(xcb_connection_t *c);
    224 
    225 /**
    226  * @brief Returns the maximum request length that this server accepts.
    227  * @param c: The connection to the X server.
    228  * @return The maximum request length field.
    229  *
    230  * In the absence of the BIG-REQUESTS extension, returns the
    231  * maximum request length field from the connection setup data, which
    232  * may be as much as 65535. If the server supports BIG-REQUESTS, then
    233  * the maximum request length field from the reply to the
    234  * BigRequestsEnable request will be returned instead.
    235  *
    236  * Note that this length is measured in four-byte units, making the
    237  * theoretical maximum lengths roughly 256kB without BIG-REQUESTS and
    238  * 16GB with.
    239  */
    240 uint32_t xcb_get_maximum_request_length(xcb_connection_t *c);
    241 
    242 /**
    243  * @brief Prefetch the maximum request length without blocking.
    244  * @param c: The connection to the X server.
    245  *
    246  * Without blocking, does as much work as possible toward computing
    247  * the maximum request length accepted by the X server.
    248  *
    249  * Invoking this function may cause a call to xcb_big_requests_enable,
    250  * but will not block waiting for the reply.
    251  * xcb_get_maximum_request_length will return the prefetched data
    252  * after possibly blocking while the reply is retrieved.
    253  *
    254  * Note that in order for this function to be fully non-blocking, the
    255  * application must previously have called
    256  * xcb_prefetch_extension_data(c, &xcb_big_requests_id) and the reply
    257  * must have already arrived.
    258  */
    259 void xcb_prefetch_maximum_request_length(xcb_connection_t *c);
    260 
    261 
    262 /* xcb_in.c */
    263 
    264 /**
    265  * @brief Returns the next event or error from the server.
    266  * @param c: The connection to the X server.
    267  * @return The next event from the server.
    268  *
    269  * Returns the next event or error from the server, or returns null in
    270  * the event of an I/O error. Blocks until either an event or error
    271  * arrive, or an I/O error occurs.
    272  */
    273 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
    274 
    275 /**
    276  * @brief Returns the next event or error from the server.
    277  * @param c: The connection to the X server.
    278  * error status of the operation.
    279  * @return The next event from the server.
    280  *
    281  * Returns the next event or error from the server, if one is
    282  * available, or returns @c NULL otherwise. If no event is available, that
    283  * might be because an I/O error like connection close occurred while
    284  * attempting to read the next event, in which case the connection is
    285  * shut down when this function returns.
    286  */
    287 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
    288 
    289 /**
    290  * @brief Returns the next event without reading from the connection.
    291  * @param c: The connection to the X server.
    292  * @return The next already queued event from the server.
    293  *
    294  * This is a version of xcb_poll_for_event that only examines the
    295  * event queue for new events. The function doesn't try to read new
    296  * events from the connection if no queued events are found.
    297  *
    298  * This function is useful for callers that know in advance that all
    299  * interesting events have already been read from the connection. For
    300  * example, callers might use xcb_wait_for_reply and be interested
    301  * only of events that preceded a specific reply.
    302  */
    303 xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c);
    304 
    305 /**
    306  * @brief Return the error for a request, or NULL if none can ever arrive.
    307  * @param c: The connection to the X server.
    308  * @param cookie: The request cookie.
    309  * @return The error for the request, or NULL if none can ever arrive.
    310  *
    311  * The xcb_void_cookie_t cookie supplied to this function must have resulted
    312  * from a call to xcb_[request_name]_checked().  This function will block
    313  * until one of two conditions happens.  If an error is received, it will be
    314  * returned.  If a reply to a subsequent request has already arrived, no error
    315  * can arrive for this request, so this function will return NULL.
    316  *
    317  * Note that this function will perform a sync if needed to ensure that the
    318  * sequence number will advance beyond that provided in cookie; this is a
    319  * convenience to avoid races in determining whether the sync is needed.
    320  */
    321 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie);
    322 
    323 /**
    324  * @brief Discards the reply for a request.
    325  * @param c: The connection to the X server.
    326  * @param sequence: The request sequence number from a cookie.
    327  *
    328  * Discards the reply for a request. Additionally, any error generated
    329  * by the request is also discarded (unless it was an _unchecked request
    330  * and the error has already arrived).
    331  *
    332  * This function will not block even if the reply is not yet available.
    333  *
    334  * Note that the sequence really does have to come from an xcb cookie;
    335  * this function is not designed to operate on socket-handoff replies.
    336  */
    337 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence);
    338 
    339 
    340 /* xcb_ext.c */
    341 
    342 /**
    343  * @typedef typedef struct xcb_extension_t xcb_extension_t
    344  */
    345 typedef struct xcb_extension_t xcb_extension_t;  /**< Opaque structure used as key for xcb_get_extension_data_t. */
    346 
    347 /**
    348  * @brief Caches reply information from QueryExtension requests.
    349  * @param c: The connection.
    350  * @param ext: The extension data.
    351  * @return A pointer to the xcb_query_extension_reply_t for the extension.
    352  *
    353  * This function is the primary interface to the "extension cache",
    354  * which caches reply information from QueryExtension
    355  * requests. Invoking this function may cause a call to
    356  * xcb_query_extension to retrieve extension information from the
    357  * server, and may block until extension data is received from the
    358  * server.
    359  *
    360  * The result must not be freed. This storage is managed by the cache
    361  * itself.
    362  */
    363 const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
    364 
    365 /**
    366  * @brief Prefetch of extension data into the extension cache
    367  * @param c: The connection.
    368  * @param ext: The extension data.
    369  *
    370  * This function allows a "prefetch" of extension data into the
    371  * extension cache. Invoking the function may cause a call to
    372  * xcb_query_extension, but will not block waiting for the
    373  * reply. xcb_get_extension_data will return the prefetched data after
    374  * possibly blocking while it is retrieved.
    375  */
    376 void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
    377 
    378 
    379 /* xcb_conn.c */
    380 
    381 /**
    382  * @brief Access the data returned by the server.
    383  * @param c: The connection.
    384  * @return A pointer to an xcb_setup_t structure.
    385  *
    386  * Accessor for the data returned by the server when the xcb_connection_t
    387  * was initialized. This data includes
    388  * - the server's required format for images,
    389  * - a list of available visuals,
    390  * - a list of available screens,
    391  * - the server's maximum request length (in the absence of the
    392  * BIG-REQUESTS extension),
    393  * - and other assorted information.
    394  *
    395  * See the X protocol specification for more details.
    396  *
    397  * The result must not be freed.
    398  */
    399 const xcb_setup_t *xcb_get_setup(xcb_connection_t *c);
    400 
    401 /**
    402  * @brief Access the file descriptor of the connection.
    403  * @param c: The connection.
    404  * @return The file descriptor.
    405  *
    406  * Accessor for the file descriptor that was passed to the
    407  * xcb_connect_to_fd call that returned @p c.
    408  */
    409 int xcb_get_file_descriptor(xcb_connection_t *c);
    410 
    411 /**
    412  * @brief Test whether the connection has shut down due to a fatal error.
    413  * @param c: The connection.
    414  * @return > 0 if the connection is in an error state; 0 otherwise.
    415  *
    416  * Some errors that occur in the context of an xcb_connection_t
    417  * are unrecoverable. When such an error occurs, the
    418  * connection is shut down and further operations on the
    419  * xcb_connection_t have no effect.
    420  *
    421  * @return XCB_CONN_ERROR, because of socket errors, pipe errors or other stream errors.
    422  * @return XCB_CONN_CLOSED_EXT_NOTSUPPORTED, when extension not supported.
    423  * @return XCB_CONN_CLOSED_MEM_INSUFFICIENT, when memory not available.
    424  * @return XCB_CONN_CLOSED_REQ_LEN_EXCEED, exceeding request length that server accepts.
    425  * @return XCB_CONN_CLOSED_PARSE_ERR, error during parsing display string.
    426  */
    427 int xcb_connection_has_error(xcb_connection_t *c);
    428 
    429 /**
    430  * @brief Connects to the X server.
    431  * @param fd: The file descriptor.
    432  * @param auth_info: Authentication data.
    433  * @return A newly allocated xcb_connection_t structure.
    434  *
    435  * Connects to an X server, given the open socket @p fd and the
    436  * xcb_auth_info_t @p auth_info. The file descriptor @p fd is
    437  * bidirectionally connected to an X server. If the connection
    438  * should be unauthenticated, @p auth_info must be @c
    439  * NULL.
    440  */
    441 xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info);
    442 
    443 /**
    444  * @brief Closes the connection.
    445  * @param c: The connection.
    446  *
    447  * Closes the file descriptor and frees all memory associated with the
    448  * connection @c c.
    449  */
    450 void xcb_disconnect(xcb_connection_t *c);
    451 
    452 
    453 /* xcb_util.c */
    454 
    455 /**
    456  * @brief Parses a display string name in the form documented by X(7x).
    457  * @param name: The name of the display.
    458  * @param host: A pointer to a malloc'd copy of the hostname.
    459  * @param display: A pointer to the display number.
    460  * @param screen: A pointer to the screen number.
    461  * @return 0 on failure, non 0 otherwise.
    462  *
    463  * Parses the display string name @p display_name in the form
    464  * documented by X(7x). Has no side effects on failure. If
    465  * @p displayname is @c NULL or empty, it uses the environment
    466  * variable DISPLAY. @p hostp is a pointer to a newly allocated string
    467  * that contain the host name. @p displayp is set to the display
    468  * number and @p screenp to the preferred screen number. @p screenp
    469  * can be @c NULL. If @p displayname does not contain a screen number,
    470  * it is set to @c 0.
    471  */
    472 int xcb_parse_display(const char *name, char **host, int *display, int *screen);
    473 
    474 /**
    475  * @brief Connects to the X server.
    476  * @param displayname: The name of the display.
    477  * @param screenp: A pointer to a preferred screen number.
    478  * @return A newly allocated xcb_connection_t structure.
    479  *
    480  * Connects to the X server specified by @p displayname. If @p
    481  * displayname is @c NULL, uses the value of the DISPLAY environment
    482  * variable. If a particular screen on that server is preferred, the
    483  * int pointed to by @p screenp (if not @c NULL) will be set to that
    484  * screen; otherwise the screen will be set to 0.
    485  */
    486 xcb_connection_t *xcb_connect(const char *displayname, int *screenp);
    487 
    488 /**
    489  * @brief Connects to the X server, using an authorization information.
    490  * @param display: The name of the display.
    491  * @param auth: The authorization information.
    492  * @param screen: A pointer to a preferred screen number.
    493  * @return A newly allocated xcb_connection_t structure.
    494  *
    495  * Connects to the X server specified by @p displayname, using the
    496  * authorization @p auth. If a particular screen on that server is
    497  * preferred, the int pointed to by @p screenp (if not @c NULL) will
    498  * be set to that screen; otherwise @p screenp will be set to 0.
    499  */
    500 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *display, xcb_auth_info_t *auth, int *screen);
    501 
    502 
    503 /* xcb_xid.c */
    504 
    505 /**
    506  * @brief Allocates an XID for a new object.
    507  * @param c: The connection.
    508  * @return A newly allocated XID.
    509  *
    510  * Allocates an XID for a new object. Typically used just prior to
    511  * various object creation functions, such as xcb_create_window.
    512  */
    513 uint32_t xcb_generate_id(xcb_connection_t *c);
    514 
    515 
    516 /**
    517  * @}
    518  */
    519 
    520 #ifdef __cplusplus
    521 }
    522 #endif
    523 
    524 
    525 #endif /* __XCB_H__ */
    526