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