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