For a long time, Xlib remained a mystery to me. My head grew bigger whenever I saw a function starting with an X.

But today I came up with a very simple Xlib program which made me think it’s probably not more complicated than OpenGL. I believe this program will be helpful to everyone in building confidence while learning Xlib. Therefore I decide to share it in public to help more people pick up their confidence.

Here’s the program:

// A simple Xlib program.
//
// Copyright (C) 2016 Cyker Way

#include <X11/Xlib.h>

int main()
{
    Display *d = XOpenDisplay(NULL);
    int white = WhitePixel(d, DefaultScreen(d));
    Window w = XCreateSimpleWindow(
            d, DefaultRootWindow(d), 0, 0, 640, 480, 0, white, white);
    XMapWindow(d, w);
    XFlush(d);
    while (1);
}

The program creates a new window at position (0, 0) with size (640, 480), border width 0 and background white.

Compile and run with:

gcc -o demo demo.c -lX11 && ./demo