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