Skip to content

Opening and closing the connection to an X server

An X program first needs to open the connection to the X server. There is a function that opens a connection. It requires the display name, or NULL. In the latter case, the display name will be the one in the environment variable DISPLAY.

xcb_connection_t *xcb_connect (const char *displayname,
                               int        *screenp);
xcb_connection_t *xcb_connect (const char *displayname,
                               int        *screenp);

The second parameter returns the screen number used for the connection. The returned structure describes an XCB connection and is opaque. Here is how the connection can be opened:

#include <xcb/xcb.h>

int
main ()
{
  xcb_connection_t *c;

  /* Open the connection to the X server. Use the DISPLAY environment variable as the default display name */
  c = xcb_connect (NULL, NULL);

  return 0;
}
#include <xcb/xcb.h>

int
main ()
{
  xcb_connection_t *c;

  /* Open the connection to the X server. Use the DISPLAY environment variable as the default display name */
  c = xcb_connect (NULL, NULL);

  return 0;
}

To close a connection, it suffices to use:

void xcb_disconnect (xcb_connection_t *c);
void xcb_disconnect (xcb_connection_t *c);
Comparison Xlib/XCB
  • XOpenDisplay ()
  • xcb_connect ()
  • XCloseDisplay ()
  • xcb_disconnect ()