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