next up previous contents
Next: Useful Online facilities Up: Overview of X Windows Previous: Using X from a

How to use X in a C program

From the programmer's point of view X consists of a library of subroutines which he may call in order to perform specific functions on the display. These functions are contained in a library, /usr/lib/libX11.a, (or /usr/lib/X11R5/libX11.sl on HPs) which must be linked into the program by the loader when the executable version of the program is generated. This can usually be done by adding -lX11 to the end of the compilation command and, on HPs, -L/usr/lib/X11R5 to the start. The Xlib header file X11/Xlib.h (or /usr/include/X11R5/Xlib.h on HPs)must also be included at the top of every program file which uses the X library. On the HPs this requires the addition of -I/usr/include/X11R5

The coordinate system for X windows has its origin at the top left hand corner of any given window. The x coordinate increases from left to right across the page and the y coordinate increases from top to bottom. The global coordinate system is effectively that of the RootWindow which covers the whole of the screen. The scaling of coordinates on an X display is never changed, however operations in each window are performed relative to that window's origin.

X functions which are described as returning Status produce a non zero value if they have executed successfully and return zero if they have failed. Other routines which return a pointer to something on success usually return the NULL pointer on failure.

To save space, we have only declared arguments whose type or use is not self evident. Most particularly, the following variables are not declared :-

    Display *display;
    Window w;
    Drawable d;   /* either a window you can draw into or
                     off-screen memory (a pixmap) */
    GC gc;        /* a Graphics Context, where current font,
                     foreground color, etc is contained*/
    Colormap cmap;
    Region r;
    unsigned long mask,valuemask;
There is a standard order for arguments :-
    display,resources,gc,
    x,y,w,h,
    array,array_length,
    mask,struct


Tim Love
Mon Mar 11 17:03:18 GMT 1996