Provide a "has error" property for XCBConnection.
[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 /* FIXME: these names conflict with those defined in Xmd.h. */
39 #ifndef XMD_H
40 typedef uint8_t  BYTE;
41 typedef uint8_t  BOOL;
42 typedef uint8_t  CARD8;
43 typedef uint16_t CARD16;
44 typedef uint32_t CARD32;
45 typedef int8_t   INT8;
46 typedef int16_t  INT16;
47 typedef int32_t  INT32;
48 #endif /* XMD_H */
49
50 #include <sys/uio.h>
51 #include <pthread.h>
52
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 /**
59  * @file xcb.h
60  */
61
62 /**
63  * @defgroup XCB_Core_Api XCB Core API
64  * @brief Core API of the XCB library.
65  *
66  * @{
67  */
68
69 /* Pre-defined constants */
70
71 /** Current protocol version */
72 #define X_PROTOCOL 11
73
74 /** Current minor version */
75 #define X_PROTOCOL_REVISION 0
76
77 /** X_TCP_PORT + display number = server port for TCP transport */
78 #define X_TCP_PORT 6000
79
80 #define XCB_TYPE_PAD(T,I) (-(I) & (sizeof(T) > 4 ? 3 : sizeof(T) - 1))
81
82 /* Opaque structures */
83
84 /**
85  * @brief XCB Connection structure.
86  *
87  * A structure that contain all data that  XCB needs to communicate with an X server.
88  */
89 typedef struct XCBConnection XCBConnection;  /**< Opaque structure containing all data that  XCB needs to communicate with an X server. */
90
91
92 /* Other types */
93
94 /**
95  * @brief Generic iterator.
96  *
97  * A generic iterator structure.
98  */
99 typedef struct {
100     void *data;   /**< Data of the current iterator */
101     int rem;    /**< remaining elements */
102     int index;  /**< index of the current iterator */
103 } XCBGenericIter;
104
105 /**
106  * @brief Generic reply.
107  *
108  * A generic reply structure.
109  */
110 typedef struct {
111     BYTE   response_type;  /**< Type of the response */
112     CARD8  pad0;           /**< Padding */
113     CARD16 sequence;       /**< Sequence number */
114     CARD32 length;         /**< Length of the response */
115 } XCBGenericRep;
116
117 /**
118  * @brief Generic event.
119  *
120  * A generic event structure.
121  */
122 typedef struct {
123     BYTE   response_type;  /**< Type of the response */
124     CARD8  pad0;           /**< Padding */
125     CARD16 sequence;       /**< Sequence number */
126     CARD32 pad[7];         /**< Padding */
127     CARD32 full_sequence;
128 } XCBGenericEvent;
129
130 /**
131  * @brief Generic error.
132  *
133  * A generic error structure.
134  */
135 typedef struct {
136     BYTE   response_type;  /**< Type of the response */
137     BYTE   error_code;     /**< Error code */
138     CARD16 sequence;       /**< Sequence number */
139     CARD32 pad[7];         /**< Padding */
140     CARD32 full_sequence;
141 } XCBGenericError;
142
143 /**
144  * @brief Generic cookie.
145  *
146  * A generic cookie structure.
147  */
148 typedef struct {
149     unsigned int sequence;  /**< Sequence number */
150 } XCBVoidCookie;
151
152
153 /* Include the generated xproto header. */
154 #include "xproto.h"
155
156
157 /** XCBNone is the universal null resource or null atom parameter value for many core X requests */
158 #define XCBNone 0L
159
160 /** XCBCopyFromParent can be used for many CreateWindow parameters */
161 #define XCBCopyFromParent 0L
162
163 /** XCBCurrentTime can be used in most requests that take an XCBTIMESTAMP */
164 #define XCBCurrentTime 0L
165
166 /** XCBNoSymbol fills in unused entries in XCBKEYSYM tables */
167 #define XCBNoSymbol 0L
168
169
170 /* xcb_auth.c */
171
172 /**
173  * @brief Container for authorization information.
174  *
175  * A container for authorization information to be sent to the X server.
176  */
177 typedef struct XCBAuthInfo {
178     int   namelen;  /**< Length of the string name (as returned by strlen). */
179     char *name;     /**< String containing the authentication protocol name, such as "MIT-MAGIC-COOKIE-1" or "XDM-AUTHORIZATION-1". */
180     int   datalen;  /**< Length of the data member. */
181     char *data;   /**< Data interpreted in a protocol-specific manner. */
182 } XCBAuthInfo;
183
184
185 /* xcb_out.c */
186
187 /**
188  * @brief Forces any buffered output to be written to the server.
189  * @param c: The connection to the X server.
190  * @return > @c 0 on success, <= @c 0 otherwise.
191  *
192  * Forces any buffered output to be written to the server. Blocks
193  * until the write is complete.
194  */
195 int XCBFlush(XCBConnection *c);
196
197 /**
198  * @brief Returns the maximum request length field from the connection
199  * setup data.
200  * @param c: The connection to the X server.
201  * @return The maximum request length field.
202  *
203  * In the absence of the BIG-REQUESTS extension, returns the
204  * maximum request length field from the connection setup data, which
205  * may be as much as 65535. If the server supports BIG-REQUESTS, then
206  * the maximum request length field from the reply to the
207  * BigRequestsEnable request will be returned instead.
208  *
209  * Note that this length is measured in four-byte units, making the
210  * theoretical maximum lengths roughly 256kB without BIG-REQUESTS and
211  * 16GB with.
212  */
213 CARD32 XCBGetMaximumRequestLength(XCBConnection *c);
214
215
216 /* xcb_in.c */
217
218 /**
219  * @brief Returns the next event or error from the server.
220  * @param c: The connection to the X server.
221  * @return The next event from the server.
222  *
223  * Returns the next event or error from the server, or returns null in
224  * the event of an I/O error. Blocks until either an event or error
225  * arrive, or an I/O error occurs.
226  */
227 XCBGenericEvent *XCBWaitForEvent(XCBConnection *c);
228
229 /**
230  * @brief Returns the next event or error from the server.
231  * @param c: The connection to the X server.
232  * @param error: A pointer to an int to be filled in with the I/O
233  * error status of the operation.
234  * @return The next event from the server.
235  *
236  * Returns the next event or error from the server, if one is
237  * available, or returns @c NULL otherwise. If no event is available, that
238  * might be because an I/O error like connection close occurred while
239  * attempting to read the next event. The @p error parameter is a
240  * pointer to an int to be filled in with the I/O error status of the
241  * operation. If @p error is @c NULL, terminates the application when an
242  * I/O error occurs.
243  */
244 XCBGenericEvent *XCBPollForEvent(XCBConnection *c, int *error);
245
246 /**
247  * @brief Return the error for a request, or NULL if none can ever arrive.
248  * @param c: The connection to the X server.
249  * @param cookie: The request cookie.
250  * @return The error for the request, or NULL if none can ever arrive.
251  *
252  * The XCBVoidCookie cookie supplied to this function must have resulted from
253  * a call to XCB[RequestName]Checked().  This function will block until one of
254  * two conditions happens.  If an error is received, it will be returned.  If
255  * a reply to a subsequent request has already arrived, no error can arrive
256  * for this request, so this function will return NULL.
257  *
258  * Note that this function will perform a sync if needed to ensure that the
259  * sequence number will advance beyond that provided in cookie; this is a
260  * convenience to avoid races in determining whether the sync is needed.
261  */
262 XCBGenericError *XCBRequestCheck(XCBConnection *c, XCBVoidCookie cookie);
263
264
265 /* xcb_ext.c */
266
267 /**
268  * @typedef typedef struct XCBExtension XCBExtension
269  */
270 typedef struct XCBExtension XCBExtension;  /**< Opaque structure used as key for XCBGetExtensionData. */
271
272 /**
273  * @brief Caches reply information from QueryExtension requests.
274  * @param c: The connection.
275  * @param ext: The extension data.
276  * @return A pointer to the XCBQueryExtensionRep for the extension.
277  *
278  * This function is the primary interface to the "extension cache",
279  * which caches reply information from QueryExtension
280  * requests. Invoking this function may cause a call to
281  * XCBQueryExtension to retrieve extension information from the
282  * server, and may block until extension data is received from the
283  * server.
284  *
285  * The result must not be freed. This storage is managed by the cache
286  * itself.
287  */
288 const XCBQueryExtensionRep *XCBGetExtensionData(XCBConnection *c, XCBExtension *ext);
289
290 /**
291  * @brief Prefetch of extension data into the extension cache
292  * @param c: The connection.
293  * @param ext: The extension data.
294  *
295  * This function allows a "prefetch" of extension data into the
296  * extension cache. Invoking the function may cause a call to
297  * XCBQueryExtension, but will not block waiting for the
298  * reply. XCBGetExtensionData will return the prefetched data after
299  * possibly blocking while it is retrieved.
300  */
301 void XCBPrefetchExtensionData(XCBConnection *c, XCBExtension *ext);
302
303
304 /* xcb_conn.c */
305
306 /**
307  * @brief Access the data returned by the server.
308  * @param c: The connection.
309  * @return A pointer to an XCBSetup structure.
310  *
311  * Accessor for the data returned by the server when the XCBConnection
312  * was initialized. This data includes
313  * - the server's required format for images,
314  * - a list of available visuals,
315  * - a list of available screens,
316  * - the server's maximum request length (in the absence of the
317  * BIG-REQUESTS extension),
318  * - and other assorted information.
319  *
320  * See the X protocol specification for more details.
321  *
322  * The result must not be freed.
323  */
324 const XCBSetup *XCBGetSetup(XCBConnection *c);
325
326 /**
327  * @brief Access the file descriptor of the connection.
328  * @param c: The connection.
329  * @return The file descriptor.
330  *
331  * Accessor for the file descriptor that was passed to the
332  * XCBConnectToFD call that returned @p c.
333  */
334 int XCBGetFileDescriptor(XCBConnection *c);
335
336 /**
337  * @brief Test whether the connection has shut down due to a fatal error.
338  * @param c: The connection.
339  * @return 1 if the connection is in an error state; 0 otherwise.
340  *
341  * Some errors that occur in the context of an XCBConnection
342  * are unrecoverable. When such an error occurs, the
343  * connection is shut down and further operations on the
344  * XCBConnection have no effect.
345  *
346  * @todo Other functions should document the conditions in
347  * which they shut down the connection.
348  */
349 int XCBConnectionHasError(XCBConnection *c);
350
351 /**
352  * @brief Connects to the X server.
353  * @param fd: The file descriptor.
354  * @param auth_info: Authentication data.
355  * @return A newly allocated XCBConnection structure.
356  *
357  * Connects to an X server, given the open socket @p fd and the
358  * XCBAuthInfo @p auth_info. The file descriptor @p fd is
359  * bidirectionally connected to an X server. XCBGetTCPFD and
360  * XCBGetUnixFD return appropriate file descriptors. If the connection
361  * should be unauthenticated, @p auth_info must be @c
362  * NULL. XCBGetAuthInfo returns appropriate authentication data.
363  */
364 XCBConnection *XCBConnectToFD(int fd, XCBAuthInfo *auth_info);
365
366 /**
367  * @brief Closes the connection.
368  * @param c: The connection.
369  *
370  * Closes the file descriptor and frees all memory associated with the
371  * connection @c c.
372  */
373 void XCBDisconnect(XCBConnection *c);
374
375
376 /* xcb_util.c */
377
378 /**
379  * @brief Parses a display string name in the form documented by X(7x).
380  * @param displayname: The name of the display.
381  * @param hostp: A pointer to a malloc'd copy of the hostname.
382  * @param displayp: A pointer to the display number.
383  * @param screenp: A pointer to the screen number.
384  * @return 0 on failure, non 0 otherwise.
385  *
386  * Parses the display string name @p display_name in the form
387  * documented by X(7x). Has no side effects on failure. If
388  * @p displayname is @c NULL or empty, it uses the environment
389  * variable DISPLAY. @p hostp is a pointer to a newly allocated string
390  * that contain the host name. @p displayp is set to the display
391  * number and @p screenp to the preferred screen number. @p screenp
392  * can be @c NULL. If @p displayname does not contain a screen number,
393  * it is set to @c 0.
394  */
395 int XCBParseDisplay(const char *name, char **host, int *display, int *screen);
396
397 /**
398  * @brief Connects to the X server.
399  * @param displayname: The name of the display.
400  * @param screenp: A pointer to a preferred screen number.
401  * @return A newly allocated XCBConnection structure.
402  *
403  * Connects to the X server specified by @p displayname. If @p
404  * displayname is @c NULL, uses the value of the DISPLAY environment
405  * variable. If a particular screen on that server is preferred, the
406  * int pointed to by @p screenp (if not @c NULL) will be set to that
407  * screen; otherwise the screen will be set to 0.
408  */
409 XCBConnection *XCBConnect(const char *displayname, int *screenp);
410
411 /**
412  * @brief Connects to the X server, using an authorization information.
413  * @param displayname: The name of the display.
414  * @param auth: The authorization information.
415  * @param screenp: A pointer to a preferred screen number.
416  * @return A newly allocated XCBConnection structure.
417  *
418  * Connects to the X server specified by @p displayname, using the
419  * authorization @p auth. If a particular screen on that server is
420  * preferred, the int pointed to by @p screenp (if not @c NULL) will
421  * be set to that screen; otherwise @p screenp will be set to 0.
422  */
423 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *display, XCBAuthInfo *auth, int *screen);
424
425
426 /**
427  * @}
428  */
429
430 #ifdef __cplusplus
431 }
432 #endif
433
434
435 #endif /* __XCB_H__ */