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