/* From csx74@pollux.cs.uga.edu Mon May 26 10:23:01 1997 */
/* FILE = xhello.c */
/* to compile: gcc -g -I/usr/openwin/include xhello.c -lX11 -o xhello */
/* from O Jones, INTRO TO THE X WINDOW SYSTEM, P-H 1989, p 470. */
/* QA76.76.W56J66 1989 */

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

char hello[] = {"Hello, WORLD"};
char hi[] = {"Hi!"};

main(int argc, char** argv){
   Display *myD;
   Window myW;
   GC myGC;
   XEvent myE;
   KeySym myK;
   XSizeHints myH;
   int myS,i,done;
   unsigned long myFG,myBG;
   char text[10];

   myD=XOpenDisplay("");                 /* initialization */
   myS=DefaultScreen(myD);
   myBG=WhitePixel(myD,myS);             /* default pixel values */
   myFG=BlackPixel(myD,myS);
   myH.x=300; myH.y=40; myH.width=250; myH.height=200;
   myH.flags=PPosition | PSize;    /* default window position and size */
   myW=XCreateSimpleWindow(myD,DefaultRootWindow(myD),myH.x,myH.y,
                   myH.width,myH.height,5,myFG,myBG); /* window creation */
   XSetStandardProperties(myD,myW,hello,hello,None,argv,argc,&myH);
   myGC=XCreateGC(myD,myW,0,0);        /* GC creation and initialization */
   XSetBackground(myD,myGC,myBG); XSetForeground(myD,myGC,myFG);
   /* input event selection */
   XSelectInput(myD,myW,ButtonPressMask| KeyPressMask| ExposureMask);
   XMapRaised(myD,myW);                               /* window mapping */
   for(done=0;(!done); ){                    /* main event-reading loop */
      XNextEvent(myD,&myE);                      /* read the next event */
      switch(myE.type){
         case Expose:{               /* repaint window on expose events */
              if(myE.xexpose.count==0){
                 XDrawImageString(myE.xexpose.display,
                      myE.xexpose.window,myGC,50,50, hello,strlen(hello));
              } break;
         }
         case MappingNotify:{        /* process keyboard mapping changes */
              XRefreshKeyboardMapping((XMappingEvent *)&myE); break;
         }
         case ButtonPress:{              /* process mouse-button presses */
              XDrawImageString(myE.xbutton.display,myE.xbutton.window,
                     myGC,myE.xbutton.x,myE.xbutton.y, hi,strlen(hi));
              break;
         }
         case KeyPress:{                     /* process keyboard presses */
              i=XLookupString((XKeyEvent *)&myE,text,10,&myK,0);
              if((i==1)&&(text[0]=='q')){done=1;}
              break;
         }
      }
   }
   XFreeGC(myD,myGC); XDestroyWindow(myD,myW); XCloseDisplay(myD);
   exit(0);                                              /* termination */
}

