/*** xhello.c -- a simple X program ***/

#include <X11/Xlib.h>
#include <X11/Xutil.h>

char hello[] = {"Hello, World."};  /* messages to be displayed */
char hi[] = {"Hi!"};

main(argc,argv)
   int argc;
   char **argv;

{

Display *mydisplay;    /* pointer to display connection data structure */
Window mywindow;       /* window and graphics context resources IDs */
GC mygc;
XEvent myevent;        /* union of structures of different kinds of events */
KeySym mykey;          /* describes a keyboard key */
XSizeHints myhint;     /* to hold our window's preferred size & location */
int myscreen;          /* workstation screen to be used to display window */
unsigned long myforeground, mybackground;
int i;
char text[10];
int done;

mydisplay = XOpenDisplay("");         /* establish a display connection */
myscreen = DefaultScreen(mydisplay);  /* get the w.s.'s default screen */
mybackground = WhitePixel(mydisplay,myscreen); /* pixel values for...     */
myforeground = BlackPixel(mydisplay,myscreen); /* black and white on w.s. */
myhint.x=200; myhint.y=300;                    /* desired initial position */
myhint.width=350; myhint.height=250;           /* desired initial size */
myhint.flags = PPosition|PSize;                /* pgm is choosing these */

mywindow = XCreateSimpleWindow(mydisplay,      /* create the window */
               DefaultRootWindow(mydisplay),
               myhint.x, myhint.y, myhint.width, myhint.height,
               5, myforeground, mybackground);

XSetStandardProperties(mydisplay, mywindow, hello, hello, None,
                        argv, argc, &myhint);  /* describe it to others */

mygc = XCreateGC(mydisplay, mywindow, 0, 0);   /* create a graphics context */

XSetBackground(mydisplay, mygc, mybackground); /* set backgnd & foregnd... */
XSetForeground(mydisplay, mygc, myforeground); /* colors into the gc       */

XSelectInput(mydisplay, mywindow,              /* types of events accepted */
             ButtonPressMask|KeyPressMask|ExposureMask);

XMapWindow(mydisplay,mywindow);        /* make the window visible!! */

done = 0;                              /* event loop control variable */

while (done == 0)                      /* This is the main event loop */

   {

   XNextEvent(mydisplay, &myevent);    /* get the next event */
   switch (myevent.type)               /* take appropriate action... */
                                       /* depending on event type.   */
      {
      case Expose:
         if (myevent.xexpose.count == 0)
            XDrawImageString(myevent.xexpose.display,
                             myevent.xexpose.window, mygc, 50, 50,
                             hello, strlen(hello));
         break;

       case ButtonPress:
         XDrawImageString(myevent.xbutton.display, myevent.xbutton.window,
                          mygc, myevent.xbutton.x, myevent.xbutton.y,
                          hi, strlen(hi));
         break;

       case KeyPress:
         i = XLookupString(&myevent,text,10,&mykey,0);
         if (i==1 && text[0]=='q')
             done = 1;
         break;

      }

   }

XFreeGC(mydisplay, mygc);               /* Termination */
XDestroyWindow(mydisplay, mywindow);
XCloseDisplay(mydisplay);
exit(0);

}