From Wikiversity - Reading time: 2 min
Xlib is an application programming interface (API) for X Window. A program that uses Xlib consists of 4 basic tasks:
#include <X11/Xlib.h>
int main(int argc, char **argv) {
Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
return 0;
}
XOpenDisplay is a function that opens a connection to a X Window server. XOpenDisplay gets passed a string (e.g char*) that lets you control what display to connect to. You can also tell Xlib to attempt a connection with the default display by passing NULL instead. If a connection to a X Window server is successful, XOpenDisplay returns an allocated pointer to a Display structure, otherwise a null pointer (e.g (Display*)NULL) is returned.
You should check whether the connection was successful or not before calling any other functions from Xlib. Most other Xlib functions depend on knowing which display is being used. You may also need to know which display is being used in order to get characteristics of the display, such as how many screens there are, what resolutions does each screen support, and what color depths does each screen resolution support. A typical PC only has one screen, but supports several different screen resolutions and color depths for example.
#include <X11/Xlib.h>
int main(int argc, char **argv) {
Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
if (!d) {
return -1;
}
int err = XCloseDisplay(d);
return err;
}