The XCBConnectionOfDisplay function casts an Xlib Display object to an XCBConnection object.
Function Prototype
XCBConnection *XCBConnectionOfDisplay(Display *dpy);
Example
#include <X11/xcl.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
Display *dpy = XOpenDisplay(NULL);
XCBConnection *c;
XCBSCREEN *screen;
XCBWINDOW window;
const int depth = 0, x = 0, y = 0, width = 150, height = 150,
border_width = 1;
if (! dpy)
{
fprintf(stderr, "Could not open display.\n");
exit(EXIT_FAILURE);
}
/* Now that we have an open Display object, cast it to an
* XCBConnection object so it can be used with native XCB
* functions.
*/
c = XCBConnectionOfDisplay(dpy);
if (! c)
{
fprintf(stderr, "Could not cast the Display object to an "
"XCBConnection object.\n");
exit(EXIT_FAILURE);
}
/* Do something meaningful, fun, and interesting with the new
* XCBConnection object.
*/
window = XCBWINDOWNew(c);
screen = XCBConnSetupSuccessRepRootsIter(XCBGetSetup(c)).data;
XCBCreateWindow(c, depth, window, screen->root, x, y, width, height,
border_width, InputOutput, screen->root_visual, 0, NULL);
XCBMapWindow(c, window);
XCBSync(c, 0);
for (;;) /* Loop forever */ ;
return EXIT_SUCCESS;
}
Some salient points in this example include the following:
- The only header that is included is
xcl.h. The headersXlib.hnorxcb.hare included, yet Xlib and XCB functions are used. This is becausexcl.hincludesxcb.handXlib.h. - The standard Xlib function
XOpenDisplayis used to create a Display object. - The
Displayobject is cast to anXCBConnectionobject usingXCBConnectionOfDisplay. - After the cast, the connection is passed to various other XCB functions.
Though this example is contrived, XCBConnectionOfDisplay is an essential part of the porting process. For example, say you have some function that takes a Display object. You can leave the prototype unchanged, in the body of the function cast it to an XCBConnection, and port only that function. The rest of the code can remain unchanged. In this way, the process of converting the application to use XCB is staggered as necessary, time, knowledge, and knowledge permit.
Casting the Other Way
How do you get a Display object given an XCBConnection object? You can't. So, if you find yourself needing to cast an XCBConnection to a Display, stop, rethink, and find another way to structure your code to work without the cast.
See Also
- XcbPorting
- XCBConnection
- XCBSync
- Mailing list posts related to XCBConnectionOfDisplay.