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