5.5. Your first X Program   

You can now create your first X Window System program (see later to retrieve it):

  #include <X11/Xlib.h>                         /* X library */

#include <stdio.h> /* std input-output */
#include <X11/Xutil.h> /* X utilities */
main (argc, argv)
int argc;
char **argv;
{
Display *myDisplay; /* pointer to the display */
int myScreen; /* screen we are using */
int myDepth; /* colour plane depth */
XSetWindowAttributes myWindowAttributes; /* structure of window attibutes */
unsigned long myWindowMask; /* mask for window attributes */
Window myWindow; /* window structure */
XSizeHints theSizeHints; /* window hints for window manger */
int x = 200; /* x top left corner of window */
int y = 150; /* y top left corner of window */
unsigned int width = 850; /* width of the window */
unsigned int height = 700; /* height of the window */
int border_width = 20; /* border width of the window */
myDisplay = XOpenDisplay ("");
if (myDisplay == NULL)
{
fprintf (stderr,
"ERROR: Could not open a connection to X on display %s\n",
XDisplayName (NULL));
exit (0);
}
myScreen = DefaultScreen (myDisplay);
myDepth = DefaultDepth (myDisplay, myScreen);
myWindowAttributes.border_pixel = BlackPixel (myDisplay, myScreen);
myWindowAttributes.background_pixel = WhitePixel (myDisplay, myScreen);
myWindowAttributes.override_redirect = True;
myWindowMask = CWBackPixel | CWBorderPixel | CWOverrideRedirect;
myWindow = XCreateWindow (myDisplay,
RootWindow (myDisplay, myScreen),
x, y, width, height, border_width,
myDepth, InputOutput, CopyFromParent,
myWindowMask, &myWindowAttributes);
theSizeHints.flags = PPosition | PSize; /* set mask for the hints */
theSizeHints.x = x; /* x position */
theSizeHints.y = y; /* y position */
theSizeHints.width = width; /* width of the window */
theSizeHints.height = height; /* height of the window */
XSetWMNormalHints (myDisplay, myWindow, &theSizeHints);
XMapWindow (myDisplay, myWindow);
XFlush (myDisplay);
/*-----------------------------------------------------------------------------
SLEEP FOR 10 SECONDS
-----------------------------------------------------------------------------*/
sleep(10);
XDestroyWindow (myDisplay, myWindow);
XCloseDisplay (myDisplay);
exit (0);
}

You will also need to have an Imakefile (see later section on Imakefiles) :

LOCAL_LIBRARIES = $(XLIB) -lm

FILE = simplewindow
SRCS = $(FILE).c
OBJS = $(FILE).o
ComplexProgramTarget($(FILE))

The alternative would be for you to grab the source code (via ) and compile it on your machine.
Imakefile simplewindow.c

After retrieving and saving the code plus Imakefile, you should be able to type:
xmkmf -a ; make FILE=simplewindow
This will produce the executable called simplewindow.

It is possible to execute a (different) simplewindow program and display it on your screen. To run it, your system must be running X and the server control is set to xhost www.cs.curtin.edu.au/. Also it will not work if you are using a proxy server or you are running your browser on a machine different from the one you are displaying it on. Many example programs are available