Applying C - Programming X Windows
Written by Harry Fairhead   
Tuesday, 11 November 2025
Article Index
Applying C - Programming X Windows
Device, Screen and Window
An Example
Events

An Example

Putting all of this together we can write a simple demonstration program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char** argv) {
    Display *dpy = XOpenDisplay(None);
    int screen = DefaultScreen(dpy);
    int blackColor = BlackPixel(dpy, screen);
    int whiteColor = WhitePixel(dpy, screen);
    Window w = XCreateSimpleWindow(dpy, 
DefaultRootWindow(dpy), 200, 600, 500, 500, 0, blackColor, blackColor); XMapWindow(dpy, w); XFlush(dpy); sleep(1); GC gc = XCreateGC(dpy, w, 0, 0); XSetForeground(dpy, gc, whiteColor); XDrawLine(dpy, w, gc, 10, 10, 200, 20); XDrawRectangle(dpy, w, gc,30, 200, 100, 200); XFillRectangle(dpy, w, gc,200, 200, 100, 200); XDrawString(dpy, w, gc, 35, 300,
"Hello X World", 13); XFlush(dpy); sleep(10); return (EXIT_SUCCESS); }

Notice that we need the XFlush after the window is mapped to make the system display the window. The one-second wait after the XFlush is to make sure that the window has been mapped. As already discussed, this is not the usual way to do this job, but it avoids having to use events. The final sleep(10) is required so you can see the results as when a process ends any windows it owns are closed.

ACcover

Color

The X11 system is particularly sophisticated when it comes to specifying and managing color. As already mentioned, X11 only guarantees two colors - black and white, although for any given output device there might be other high contrast colors that serve in place of black and white. Other colors are display-dependent and defined in a color map, a table of all the colors the device can display.

You can get a default color map using:

Colormap cmap = DefaultColormap(dpy,screen);

Your program can work in terms of 16-bit RGB values - this gives you the ability to define any color to a high precision. When you want a color specified as RGB, use a function to look up a color that is as close as possible in the color map. This returns the bit pattern you have to store in a pixel to display the color. To make this work we have to use the XColor struct:

typedef struct {
 unsigned long    	pixel;
 unsigned short   	red, green, blue;
 char          	flags;
 char    		pad;
} XColor;

Store the RGB values you want in the red, green and blue fields. The flags specify which of the red, green or blue fields are to be used. You call the XAllocColor function with the color map and the XColor struct with the values of red, green and blue you have specified and it fills in the pixel field with the color value you should use.

For example, in the previous example if we wanted to draw everything in red you would use something like:

Colormap cmap = DefaultColormap(dpy,screen);
XColor color;
color.red=0xFFFF;
color.green=0;
color.blue=0;
XAllocColor(dpy, cmap, &color);
XSetForeground(dpy, gc, color.pixel);

As well as RGB, you can use other systems of color specification. You can even use named colors, which are the same as the color names used by CSS.

The function:

XAllocNamedColor(dpy, cmap, name,&color,&exact)

will fill in the pixel field in color to be the closest match that the hardware supports and the exact RGB values in exact. For example, to set the color to red you could use:

XColor color,exact;
XAllocNamedColor(dpy, cmap,"red",&color,&exact);
XSetForeground(dpy, gc, color.pixel);

The subject of color in X11 is a big one. Not only is it sophisticated, it is often much more than you need. The few functions described here are enough to get you a fairly long way.



Last Updated ( Tuesday, 11 November 2025 )