Graphics Using Xlib, Part 2: Writing Programs

Graphics with Xlib

Graphics with Xlib

This article applies the concepts explained in Part l of this series with a sample program.

In case you have forgotten the concepts, a quick recap might be helpful. A client (user program) and the server communicate with each other through Xlib, which is running on either the local or a remote system. The X protocol defines each packet of information transferred in both directions, in the form of requests, replies, events and errors. I have assumed a basic knowledge of C on your part, as a requisite to learning to use Xlib.

Before we commence with programming, you should be aware of certain important conventions and concepts defined to be standard. Also remember that X is not a part of the OS, but is an application.

Windows: The window system in Xlib follows a hierarchical architecture, which makes it easy to identify the parent and child, and also control them. This hierarchy could be deep, to handle more complexities. Each window has its own coordinate system, which has the X axis (horizontal) and the Y axis (vertical) with the origin [0, 0] at the upper-left corner of the window.

Coordinates are integral, in terms of pixels, and coincide with pixel centres. For a window, the origin is inside the border at the inside, upper-left corner. The X window system can support one or more screens on the monitor. A collection of screens that belongs to a user, along with the keyboard and mouse, is called a display. X provides graphics, text, and raster operations for the windows.

Function naming conventions:

  1. All function names and user-visible data structures start with X.
  2. Macro names are entirely in upper-case letters.
  3. The names of all elements in a data structure are lower-case.
  4. Compound words are separated by an underscore (_).
Errors: If the function fails, it returns a zero. When Xlib detects an error, it calls an error handler, which your program can provide. If you do not provide an error handler, the error is printed, and your program terminates.

Writing programs in X

If you have read my previous article, the main emphasis was on understanding the working of X. The outline of an X client program could be as follows:

  1. Establish a connection with the server.
  2. Open a window(s).
  3. Declare variables/allocate memory.
  4. Send requests and get replies from the server.
  5. Handle all the errors appropriately.
  6. Free all the variables.
  7. Close the window(s).
  8. Close the connection with the server.

Let’s begin programming by following these steps, in order to avoid complexities and confusion. We will write a very simple program, with the least necessary operations, to understand the steps listed above. Almost all programs that you write using Xlib will have pretty much the same basic steps, with additional code providing application-specific functionality.

The program

Our simple program opens a window with a black background, and closes it after a short delay. The code is as follows:

#include <X11/Xlib.h> /*Xlib header file*/
#include <stdio.h>
int main(void)
    Display dis; /*Variable for handling display*/
    Window win; /*Variable for handling window*/
    dis = XopenDisplay(NULL);
    /*We have just opened a connection with the X server.
    If this fails, the function will return a '0' */
    win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), BlackPixel (dis, 0));
    /*This function will create a simple window of 500x500 with black background*/
    XMapWindow(dis, win);
    /*The XMapWindow function maps the window and all of its subwindows that have had map requests. */
    Xflush(dis); /*The XFlush function flushes the output buffer.*/
    sleep(5); /*Freeze for 5 seconds before closing.*/
    XcloseDisplay(dis); /*Close the connection with the X server */

Compile the program with: gcc ip.c -o op -lX11; and run it with: ./op


These are the steps necessary to write a program with Xlib. C programmers ought not to face any difficulty in understanding the program, since most parts are self-explanatory.

We might do a program to plot graph points in a future article, but I would like you to explore some part of Xlib on your own; I suggest you take a look at the graphics functions, like drawing a rectangle. Obtaining some insight before the next article would definitely help you understand the scope of Xlib.

Feature image courtesy: Nate Steiner. Reused under the terms of CC-BY 2.0 License.


  1. There seems to be a few little typos mostly case.

    1 Display dis; should be Display *dis;
    2 dis = XopenDislpay(NULL); should be XOpenDisplay(NULL): //capital O
    3 Xflush(dis); should be XFlush(dis); //capital F
    4 XcloseDisplay(dis); should be XCloseDisplay(dis); //capital C


Please enter your comment!
Please enter your name here