Introduction to Xwindows Programming

For many beginners, Xwindows could be scary because of the huge number of
functions, macros, and predefined types provided by the X library.  However,
once you get a hang of it, it can be almost a trivial task.  This tutorial
will give you additional help in programming Xwindows.  It describes the
usages of the X11 library.

Back to table of contents

Basic X Routines Summary

Basic Xlib Routines/Macros

XOpenDisplay Connect to X server DefaultScreen Returns the default screen of a given display WhitePixel Returns white value for a given screen and display BlackPixel Returns black value for a given screen and display Xcreate SimpleWindow Create a simple window DefaultRootWindow Returns the default root of a display XMapRaised Map a window to a screen XFlush Flush the request queue to the server XDestroyWindow Destroys a window along with its related data structures XCloseDisplay Disconnect from X server XSelectInput Choose which type of event to be looked for XNextEvent Wait for the next event specified by XSelectInput XDrawArc Draw an arc XFillArc Similar to XDrawArc but is filled XDrawLine Draw line XDrawRectangle Draws rectangle XFillRectangle Similar to XDrawRectangle but is filled XWarpPointer Moves mouse pointer XLookupString Look up which key is pressed on keyboard XRefreshKeyboard Mapping Changes key mapping to default value XQueryPointer Returns the position of mouse pointer with status of keys/buttons

GC Routines

XCreateGC Creates graphics context XSetForeground Sets foreground attribute of a given GC XSetBackground Sets background attribute of a given GC XSetLineAttributes Sets the attribute of line of a given GC XSetDashes Sets the dash attribute of line of a given GC DisplayPlanes Returns color depth of a screen Defaultcolormap Returns the default colormap of a given screen and display XAllocNamedColor Allocates a color of a given color name XAllocColor Allocates a color of a given RGB attribute
There are many other routines that are available at the Back to table of contents

__________________________________________________________________

Examples

Example 1 - a easy start

This following program simply creates a window with white background for 5 seconds, then self-destruct.

%(1) cat xprog1.c
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

main ()
{
        Display *display;
        Window window;
        int screen;
        unsigned long foreground, background;

        /* connect to the X server */
        display = XOpenDisplay ("");

        if (display == NULL) {
                fprintf (stderr, "cannot connect to server\n");
                exit (EXIT_FAILURE);
        }

        /* get default screen */
        screen = DefaultScreen (display);

        /* get black and white representation on current screen */
        background = WhitePixel (display, screen);
        foreground = BlackPixel (display, screen);

        /* Create window at (100,50), width 350, height 250, border width
           2, in default root  */
        window = XCreateSimpleWindow (display,
                DefaultRootWindow(display), 100, 50, 350, 250, 2,
                foreground, background);

        if (window == NULL) {
                fprintf (stderr, "cannot open window\n");
                exit (EXIT_FAILURE);
        }

        /* pop this window up on the screen */
        XMapRaised (display, window);

	/* flush X request queue to server */
        XFlush (display);
        sleep (5);

	XDestroyWindow(mydisplay, mywindow);
	XCloseDisplay (mydisplay);

        exit (EXIT_SUCCESS);
}


%(4)
Back to table of contents


Example 2 - drawing graphics

The following programs draws a red rectangle on black background

%(1) cat xprog2.c
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

main ()
{
        Display *xdisplay;
        Window xwindow;
        int xscreen;
        unsigned long xforeground, xbackground;
        XEvent xevent;
        GC xgc;
        Colormap cmap;
        XColor color, colorrgb;

        /* connect to the X server */
        xdisplay = XOpenDisplay ("");

        if (xdisplay == NULL) {
                fprintf (stderr, "cannot connect to server\n");
                exit (EXIT_FAILURE);
        }

        /* get default screen */
        xscreen = DefaultScreen (xdisplay);

        /* get black and white representation on current screen */        
	xbackground = BlackPixel (xdisplay, xscreen);
        xforeground = WhitePixel (xdisplay, xscreen);

        /* Create window at (100,50), width 350, height 250, border width
           2, in default root  */
        xwindow = XCreateSimpleWindow (xdisplay,
                DefaultRootWindow(xdisplay), 100, 50, 350, 250, 2,
                xforeground, xbackground);

        if (xwindow == NULL) {
                fprintf (stderr, "cannot open window\n");
                exit (EXIT_FAILURE);
        }

        /* ask for exposure event */
        XSelectInput(xdisplay, xwindow, ExposureMask);

        /* pop this window up on the screen */
        XMapRaised (xdisplay, xwindow);

        /* wait for the window showing up before continuing */
        XNextEvent (xdisplay, &xevent);

        /* set graphics context of rectangle to red */
        xgc= XCreateGC (xdisplay, xwindow, 0, 0);
        if (DisplayPlanes (xdisplay, xscreen) != 1) {
                cmap = DefaultColormap (xdisplay, xscreen);
                if (XAllocNamedColor (xdisplay, cmap, "red", &color, &colorrgb))
                        XSetForeground (xdisplay, xgc, color.pixel);
        }

        /* Draw rectangle at (25,25), width 300, height, 200 */
        XFillRectangle (xdisplay, xwindow, xgc, 25, 25,
                300, 200);

        /* flush X request queue to server */
        XFlush (xdisplay);

        sleep (10);
        XDestroyWindow(xdisplay, xwindow);
        XCloseDisplay (xdisplay);

        return 0;
}

%(4)
Note that if you are on a black and white xterminal, the red rectangle would show up as black.

Back to table of contents


Example 3 - event loops

%(1) cat xprog3.c
#include <stdio.h>
#include <stdlib.hgt;
#include <X11/Xlib.h>

main ()
{
        Display *xdisplay;
        Window xwindow;
        int xscreen;
        unsigned long xforeground, xbackground;
        XEvent xevent;
        GC xgc;
        Colormap cmap;
        XColor color, colorrgb;

        /* connect to the X server */
        xdisplay = XOpenDisplay ("");

        if (xdisplay == NULL) {
                fprintf (stderr, "cannot connect to server\n");
                exit (EXIT_FAILURE);
        }

        /* get default screen */
        xscreen = DefaultScreen (xdisplay);

        /* get black and white representation on current screen */
        xbackground = BlackPixel (xdisplay, xscreen);
        xforeground = WhitePixel (xdisplay, xscreen);

        /* Create window at (100,50), width 350, height 250, border width
           2, in default root  */
        xwindow = XCreateSimpleWindow (xdisplay,
                DefaultRootWindow(xdisplay), 100, 50, 350, 250, 2,
                xforeground, xbackground);

        if (xwindow == NULL) {
                fprintf (stderr, "cannot open window\n");
                exit (EXIT_FAILURE);
        }

        /* ask for exposure event */
        XSelectInput(xdisplay, xwindow, ButtonPressMask | ExposureMask);

        /* pop this window up on the screen */
        XMapRaised (xdisplay, xwindow);

        /* set graphics context of rectangle to red */
        xgc= XCreateGC (xdisplay, xwindow, 0, 0);
        cmap = DefaultColormap (xdisplay,xscreen);

        while (1) {
		/* wait for next event */
                XNextEvent (xdisplay, &xevent);
                switch (xevent.type) {
		/* ignore this event */
                case Expose:
                        break;
                case ButtonPress:
			/* if mouse button is down inside rectangle.. */
                        if ((xevent.xbutton.x>=25)&&(xevent.xbutton.x<=325)
                        &&(xevent.xbutton.y>=25)&&(xevent.xbutton.y<=225)) {
				/* change color */
                                if (color.red == 0xffff)
                                        color.red =0x0;
                                else if (color.green == 0xfff)
                                        color.green=0x0;
                                else if (color.blue == 0xffff)
                                        color.blue=0x0;
                                else {
                                        color.red+=0x1000;
                                        color.green+=0x2000;
                                        color.blue += 0x3000;
                                }

				/* allocate color and redraw rectangle */
                                if (XAllocColor (xdisplay,cmap, &color))
                                        XSetForeground (xdisplay, xgc,
                                        color.pixel);

                                XFillRectangle(xdisplay,xwindow,xgc,25,25,
                                        300,200);
                        }
                        break;
                }
        }

        return 0;
}
%(2) cc -lX11 xprog3.c
%(3) a.out
Hosted by www.Geocities.ws

1