xcb.h revision 709d36bb
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 47extern "C" { 48#endif 49 50/** 51 * @file xcb.h 52 */ 53 54#define XCB_PACKED __attribute__((__packed__)) 55 56/** 57 * @defgroup XCB_Core_API XCB Core API 58 * @brief Core API of the XCB library. 59 * 60 * @{ 61 */ 62 63/* Pre-defined constants */ 64 65/** Current protocol version */ 66#define X_PROTOCOL 11 67 68/** Current minor version */ 69#define X_PROTOCOL_REVISION 0 70 71/** X_TCP_PORT + display number = server port for TCP transport */ 72#define X_TCP_PORT 6000 73 74/** xcb connection errors because of socket, pipe and other stream errors. */ 75#define XCB_CONN_ERROR 1 76 77/** xcb connection shutdown because of extension not supported */ 78#define XCB_CONN_CLOSED_EXT_NOTSUPPORTED 2 79 80/** malloc(), calloc() and realloc() error upon failure, for eg ENOMEM */ 81#define XCB_CONN_CLOSED_MEM_INSUFFICIENT 3 82 83/** Connection closed, exceeding request length that server accepts. */ 84#define XCB_CONN_CLOSED_REQ_LEN_EXCEED 4 85 86/** Connection closed, error during parsing display string. */ 87#define XCB_CONN_CLOSED_PARSE_ERR 5 88 89/** Connection closed because the server does not have a screen matching the display. */ 90#define XCB_CONN_CLOSED_INVALID_SCREEN 6 91 92/** Connection closed because some FD passing operation failed */ 93#define XCB_CONN_CLOSED_FDPASSING_FAILED 7 94 95#define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1)) 96 97/* Opaque structures */ 98 99/** 100 * @brief XCB Connection structure. 101 * 102 * A structure that contain all data that XCB needs to communicate with an X server. 103 */ 104typedef struct xcb_connection_t xcb_connection_t; /**< Opaque structure containing all data that XCB needs to communicate with an X server. */ 105 106 107/* Other types */ 108 109/** 110 * @brief Generic iterator. 111 * 112 * A generic iterator structure. 113 */ 114typedef struct { 115 void *data; /**< Data of the current iterator */ 116 int rem; /**< remaining elements */ 117 int index; /**< index of the current iterator */ 118} xcb_generic_iterator_t; 119 120/** 121 * @brief Generic reply. 122 * 123 * A generic reply structure. 124 */ 125typedef struct { 126 uint8_t response_type; /**< Type of the response */ 127 uint8_t pad0; /**< Padding */ 128 uint16_t sequence; /**< Sequence number */ 129 uint32_t length; /**< Length of the response */ 130} xcb_generic_reply_t; 131 132/** 133 * @brief Generic event. 134 * 135 * A generic event structure. 136 */ 137typedef struct { 138 uint8_t response_type; /**< Type of the response */ 139 uint8_t pad0; /**< Padding */ 140 uint16_t sequence; /**< Sequence number */ 141 uint32_t pad[7]; /**< Padding */ 142 uint32_t full_sequence; /**< full sequence */ 143} xcb_generic_event_t; 144 145/** 146 * @brief GE event 147 * 148 * An event as sent by the XGE extension. The length field specifies the 149 * number of 4-byte blocks trailing the struct. 150 * 151 * @deprecated Since some fields in this struct have unfortunate names, it is 152 * recommended to use xcb_ge_generic_event_t instead. 153 */ 154typedef struct { 155 uint8_t response_type; /**< Type of the response */ 156 uint8_t pad0; /**< Padding */ 157 uint16_t sequence; /**< Sequence number */ 158 uint32_t length; 159 uint16_t event_type; 160 uint16_t pad1; 161 uint32_t pad[5]; /**< Padding */ 162 uint32_t full_sequence; /**< full sequence */ 163} xcb_ge_event_t; 164 165/** 166 * @brief Generic error. 167 * 168 * A generic error structure. 169 */ 170typedef struct { 171 uint8_t response_type; /**< Type of the response */ 172 uint8_t error_code; /**< Error code */ 173 uint16_t sequence; /**< Sequence number */ 174 uint32_t resource_id; /** < Resource ID for requests with side effects only */ 175 uint16_t minor_code; /** < Minor opcode of the failed request */ 176 uint8_t major_code; /** < Major opcode of the failed request */ 177 uint8_t pad0; 178 uint32_t pad[5]; /**< Padding */ 179 uint32_t full_sequence; /**< full sequence */ 180} xcb_generic_error_t; 181 182/** 183 * @brief Generic cookie. 184 * 185 * A generic cookie structure. 186 */ 187typedef struct { 188 unsigned int sequence; /**< Sequence number */ 189} xcb_void_cookie_t; 190 191 192/* Include the generated xproto header. */ 193#include "xproto.h" 194 195 196/** XCB_NONE is the universal null resource or null atom parameter value for many core X requests */ 197#define XCB_NONE 0L 198 199/** XCB_COPY_FROM_PARENT can be used for many xcb_create_window parameters */ 200#define XCB_COPY_FROM_PARENT 0L 201 202/** XCB_CURRENT_TIME can be used in most requests that take an xcb_timestamp_t */ 203#define XCB_CURRENT_TIME 0L 204 205/** XCB_NO_SYMBOL fills in unused entries in xcb_keysym_t tables */ 206#define XCB_NO_SYMBOL 0L 207 208 209/* xcb_auth.c */ 210 211/** 212 * @brief Container for authorization information. 213 * 214 * A container for authorization information to be sent to the X server. 215 */ 216typedef struct xcb_auth_info_t { 217 int namelen; /**< Length of the string name (as returned by strlen). */ 218 char *name; /**< String containing the authentication protocol name, such as "MIT-MAGIC-COOKIE-1" or "XDM-AUTHORIZATION-1". */ 219 int datalen; /**< Length of the data member. */ 220 char *data; /**< Data interpreted in a protocol-specific manner. */ 221} xcb_auth_info_t; 222 223 224/* xcb_out.c */ 225 226/** 227 * @brief Forces any buffered output to be written to the server. 228 * @param c: The connection to the X server. 229 * @return > @c 0 on success, <= @c 0 otherwise. 230 * 231 * Forces any buffered output to be written to the server. Blocks 232 * until the write is complete. 233 */ 234int xcb_flush(xcb_connection_t *c); 235 236/** 237 * @brief Returns the maximum request length that this server accepts. 238 * @param c: The connection to the X server. 239 * @return The maximum request length field. 240 * 241 * In the absence of the BIG-REQUESTS extension, returns the 242 * maximum request length field from the connection setup data, which 243 * may be as much as 65535. If the server supports BIG-REQUESTS, then 244 * the maximum request length field from the reply to the 245 * BigRequestsEnable request will be returned instead. 246 * 247 * Note that this length is measured in four-byte units, making the 248 * theoretical maximum lengths roughly 256kB without BIG-REQUESTS and 249 * 16GB with. 250 */ 251uint32_t xcb_get_maximum_request_length(xcb_connection_t *c); 252 253/** 254 * @brief Prefetch the maximum request length without blocking. 255 * @param c: The connection to the X server. 256 * 257 * Without blocking, does as much work as possible toward computing 258 * the maximum request length accepted by the X server. 259 * 260 * Invoking this function may cause a call to xcb_big_requests_enable, 261 * but will not block waiting for the reply. 262 * xcb_get_maximum_request_length will return the prefetched data 263 * after possibly blocking while the reply is retrieved. 264 * 265 * Note that in order for this function to be fully non-blocking, the 266 * application must previously have called 267 * xcb_prefetch_extension_data(c, &xcb_big_requests_id) and the reply 268 * must have already arrived. 269 */ 270void xcb_prefetch_maximum_request_length(xcb_connection_t *c); 271 272 273/* xcb_in.c */ 274 275/** 276 * @brief Returns the next event or error from the server. 277 * @param c: The connection to the X server. 278 * @return The next event from the server. 279 * 280 * Returns the next event or error from the server, or returns null in 281 * the event of an I/O error. Blocks until either an event or error 282 * arrive, or an I/O error occurs. 283 */ 284xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c); 285 286/** 287 * @brief Returns the next event or error from the server. 288 * @param c: The connection to the X server. 289 * @return The next event from the server. 290 * 291 * Returns the next event or error from the server, if one is 292 * available, or returns @c NULL otherwise. If no event is available, that 293 * might be because an I/O error like connection close occurred while 294 * attempting to read the next event, in which case the connection is 295 * shut down when this function returns. 296 */ 297xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c); 298 299/** 300 * @brief Returns the next event without reading from the connection. 301 * @param c: The connection to the X server. 302 * @return The next already queued event from the server. 303 * 304 * This is a version of xcb_poll_for_event that only examines the 305 * event queue for new events. The function doesn't try to read new 306 * events from the connection if no queued events are found. 307 * 308 * This function is useful for callers that know in advance that all 309 * interesting events have already been read from the connection. For 310 * example, callers might use xcb_wait_for_reply and be interested 311 * only of events that preceded a specific reply. 312 */ 313xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c); 314 315typedef struct xcb_special_event xcb_special_event_t; 316 317/** 318 * @brief Returns the next event from a special queue 319 */ 320xcb_generic_event_t *xcb_poll_for_special_event(xcb_connection_t *c, 321 xcb_special_event_t *se); 322 323/** 324 * @brief Returns the next event from a special queue, blocking until one arrives 325 */ 326xcb_generic_event_t *xcb_wait_for_special_event(xcb_connection_t *c, 327 xcb_special_event_t *se); 328/** 329 * @typedef typedef struct xcb_extension_t xcb_extension_t 330 */ 331typedef struct xcb_extension_t xcb_extension_t; /**< Opaque structure used as key for xcb_get_extension_data_t. */ 332 333/** 334 * @brief Listen for a special event 335 */ 336xcb_special_event_t *xcb_register_for_special_xge(xcb_connection_t *c, 337 xcb_extension_t *ext, 338 uint32_t eid, 339 uint32_t *stamp); 340 341/** 342 * @brief Stop listening for a special event 343 */ 344void xcb_unregister_for_special_event(xcb_connection_t *c, 345 xcb_special_event_t *se); 346 347/** 348 * @brief Return the error for a request, or NULL if none can ever arrive. 349 * @param c: The connection to the X server. 350 * @param cookie: The request cookie. 351 * @return The error for the request, or NULL if none can ever arrive. 352 * 353 * The xcb_void_cookie_t cookie supplied to this function must have resulted 354 * from a call to xcb_[request_name]_checked(). This function will block 355 * until one of two conditions happens. If an error is received, it will be 356 * returned. If a reply to a subsequent request has already arrived, no error 357 * can arrive for this request, so this function will return NULL. 358 * 359 * Note that this function will perform a sync if needed to ensure that the 360 * sequence number will advance beyond that provided in cookie; this is a 361 * convenience to avoid races in determining whether the sync is needed. 362 */ 363xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie); 364 365/** 366 * @brief Discards the reply for a request. 367 * @param c: The connection to the X server. 368 * @param sequence: The request sequence number from a cookie. 369 * 370 * Discards the reply for a request. Additionally, any error generated 371 * by the request is also discarded (unless it was an _unchecked request 372 * and the error has already arrived). 373 * 374 * This function will not block even if the reply is not yet available. 375 * 376 * Note that the sequence really does have to come from an xcb cookie; 377 * this function is not designed to operate on socket-handoff replies. 378 */ 379void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence); 380 381/** 382 * @brief Discards the reply for a request, given by a 64bit sequence number 383 * @param c: The connection to the X server. 384 * @param sequence: 64-bit sequence number as returned by xcb_send_request64(). 385 * 386 * Discards the reply for a request. Additionally, any error generated 387 * by the request is also discarded (unless it was an _unchecked request 388 * and the error has already arrived). 389 * 390 * This function will not block even if the reply is not yet available. 391 * 392 * Note that the sequence really does have to come from xcb_send_request64(); 393 * the cookie sequence number is defined as "unsigned" int and therefore 394 * not 64-bit on all platforms. 395 * This function is not designed to operate on socket-handoff replies. 396 * 397 * Unlike its xcb_discard_reply() counterpart, the given sequence number is not 398 * automatically "widened" to 64-bit. 399 */ 400void xcb_discard_reply64(xcb_connection_t *c, uint64_t sequence); 401 402/* xcb_ext.c */ 403 404/** 405 * @brief Caches reply information from QueryExtension requests. 406 * @param c: The connection. 407 * @param ext: The extension data. 408 * @return A pointer to the xcb_query_extension_reply_t for the extension. 409 * 410 * This function is the primary interface to the "extension cache", 411 * which caches reply information from QueryExtension 412 * requests. Invoking this function may cause a call to 413 * xcb_query_extension to retrieve extension information from the 414 * server, and may block until extension data is received from the 415 * server. 416 * 417 * The result must not be freed. This storage is managed by the cache 418 * itself. 419 */ 420const struct xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext); 421 422/** 423 * @brief Prefetch of extension data into the extension cache 424 * @param c: The connection. 425 * @param ext: The extension data. 426 * 427 * This function allows a "prefetch" of extension data into the 428 * extension cache. Invoking the function may cause a call to 429 * xcb_query_extension, but will not block waiting for the 430 * reply. xcb_get_extension_data will return the prefetched data after 431 * possibly blocking while it is retrieved. 432 */ 433void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext); 434 435 436/* xcb_conn.c */ 437 438/** 439 * @brief Access the data returned by the server. 440 * @param c: The connection. 441 * @return A pointer to an xcb_setup_t structure. 442 * 443 * Accessor for the data returned by the server when the xcb_connection_t 444 * was initialized. This data includes 445 * - the server's required format for images, 446 * - a list of available visuals, 447 * - a list of available screens, 448 * - the server's maximum request length (in the absence of the 449 * BIG-REQUESTS extension), 450 * - and other assorted information. 451 * 452 * See the X protocol specification for more details. 453 * 454 * The result must not be freed. 455 */ 456const struct xcb_setup_t *xcb_get_setup(xcb_connection_t *c); 457 458/** 459 * @brief Access the file descriptor of the connection. 460 * @param c: The connection. 461 * @return The file descriptor. 462 * 463 * Accessor for the file descriptor that was passed to the 464 * xcb_connect_to_fd call that returned @p c. 465 */ 466int xcb_get_file_descriptor(xcb_connection_t *c); 467 468/** 469 * @brief Test whether the connection has shut down due to a fatal error. 470 * @param c: The connection. 471 * @return > 0 if the connection is in an error state; 0 otherwise. 472 * 473 * Some errors that occur in the context of an xcb_connection_t 474 * are unrecoverable. When such an error occurs, the 475 * connection is shut down and further operations on the 476 * xcb_connection_t have no effect, but memory will not be freed until 477 * xcb_disconnect() is called on the xcb_connection_t. 478 * 479 * @return XCB_CONN_ERROR, because of socket errors, pipe errors or other stream errors. 480 * @return XCB_CONN_CLOSED_EXT_NOTSUPPORTED, when extension not supported. 481 * @return XCB_CONN_CLOSED_MEM_INSUFFICIENT, when memory not available. 482 * @return XCB_CONN_CLOSED_REQ_LEN_EXCEED, exceeding request length that server accepts. 483 * @return XCB_CONN_CLOSED_PARSE_ERR, error during parsing display string. 484 * @return XCB_CONN_CLOSED_INVALID_SCREEN, because the server does not have a screen matching the display. 485 */ 486int xcb_connection_has_error(xcb_connection_t *c); 487 488/** 489 * @brief Connects to the X server. 490 * @param fd: The file descriptor. 491 * @param auth_info: Authentication data. 492 * @return A newly allocated xcb_connection_t structure. 493 * 494 * Connects to an X server, given the open socket @p fd and the 495 * xcb_auth_info_t @p auth_info. The file descriptor @p fd is 496 * bidirectionally connected to an X server. If the connection 497 * should be unauthenticated, @p auth_info must be @c 498 * NULL. 499 * 500 * Always returns a non-NULL pointer to a xcb_connection_t, even on failure. 501 * Callers need to use xcb_connection_has_error() to check for failure. 502 * When finished, use xcb_disconnect() to close the connection and free 503 * the structure. 504 */ 505xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info); 506 507/** 508 * @brief Closes the connection. 509 * @param c: The connection. 510 * 511 * Closes the file descriptor and frees all memory associated with the 512 * connection @c c. If @p c is @c NULL, nothing is done. 513 */ 514void xcb_disconnect(xcb_connection_t *c); 515 516 517/* xcb_util.c */ 518 519/** 520 * @brief Parses a display string name in the form documented by X(7x). 521 * @param name: The name of the display. 522 * @param host: A pointer to a malloc'd copy of the hostname. 523 * @param display: A pointer to the display number. 524 * @param screen: A pointer to the screen number. 525 * @return 0 on failure, non 0 otherwise. 526 * 527 * Parses the display string name @p display_name in the form 528 * documented by X(7x). Has no side effects on failure. If 529 * @p displayname is @c NULL or empty, it uses the environment 530 * variable DISPLAY. @p hostp is a pointer to a newly allocated string 531 * that contain the host name. @p displayp is set to the display 532 * number and @p screenp to the preferred screen number. @p screenp 533 * can be @c NULL. If @p displayname does not contain a screen number, 534 * it is set to @c 0. 535 */ 536int xcb_parse_display(const char *name, char **host, int *display, int *screen); 537 538/** 539 * @brief Connects to the X server. 540 * @param displayname: The name of the display. 541 * @param screenp: A pointer to a preferred screen number. 542 * @return A newly allocated xcb_connection_t structure. 543 * 544 * Connects to the X server specified by @p displayname. If @p 545 * displayname is @c NULL, uses the value of the DISPLAY environment 546 * variable. If a particular screen on that server is preferred, the 547 * int pointed to by @p screenp (if not @c NULL) will be set to that 548 * screen; otherwise the screen will be set to 0. 549 * 550 * Always returns a non-NULL pointer to a xcb_connection_t, even on failure. 551 * Callers need to use xcb_connection_has_error() to check for failure. 552 * When finished, use xcb_disconnect() to close the connection and free 553 * the structure. 554 */ 555xcb_connection_t *xcb_connect(const char *displayname, int *screenp); 556 557/** 558 * @brief Connects to the X server, using an authorization information. 559 * @param display: The name of the display. 560 * @param auth: The authorization information. 561 * @param screen: A pointer to a preferred screen number. 562 * @return A newly allocated xcb_connection_t structure. 563 * 564 * Connects to the X server specified by @p displayname, using the 565 * authorization @p auth. If a particular screen on that server is 566 * preferred, the int pointed to by @p screenp (if not @c NULL) will 567 * be set to that screen; otherwise @p screenp will be set to 0. 568 * 569 * Always returns a non-NULL pointer to a xcb_connection_t, even on failure. 570 * Callers need to use xcb_connection_has_error() to check for failure. 571 * When finished, use xcb_disconnect() to close the connection and free 572 * the structure. 573 */ 574xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *display, xcb_auth_info_t *auth, int *screen); 575 576 577/* xcb_xid.c */ 578 579/** 580 * @brief Allocates an XID for a new object. 581 * @param c: The connection. 582 * @return A newly allocated XID. 583 * 584 * Allocates an XID for a new object. Typically used just prior to 585 * various object creation functions, such as xcb_create_window. 586 */ 587uint32_t xcb_generate_id(xcb_connection_t *c); 588 589 590/** 591 * @} 592 */ 593 594#ifdef __cplusplus 595} 596#endif 597 598 599#endif /* __XCB_H__ */ 600