SHARE
TWEET

Untitled

a guest Feb 17th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *                               Basic X program:
  3. *
  4. *       This is a very simple X program.  It shows the basics needed to get the
  5. *       simplest X program working.   Compile as:
  6. *               cc hello_x11.c -lX11 -o hello_x11
  7. *
  8. *       When run, pops up a window with Hello World written in it.
  9. *       When the mouse is clicked, the word Hi! appears at the spot.
  10. *       If the window is covered, then uncovered ("exposed"), the
  11. *       Hello World is redrawn - but not the various Hi! items.
  12. *       If a key is pressed nothing happens except if it is q in which
  13. *       case the program quits.
  14. *
  15. */
  16.  
  17.  
  18.  
  19.  
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22.  
  23.  
  24.  
  25.  
  26. main(argc,argv)
  27. int argc; char **argv;
  28. {
  29.     Display *mydisplay;
  30.     Window mywindow;
  31.     GC mygc;
  32.     XEvent myevent;
  33.     KeySym mykey;
  34.     XSizeHints myhint;
  35.     int myscreen;
  36.     unsigned long myforeground, mybackground;
  37.     int i;
  38.     char text[10];
  39.     int done;
  40.     char *hello = "Hello World", *hi = "Hi!";
  41.  
  42.  
  43.     mydisplay = XOpenDisplay("");
  44.     myscreen = DefaultScreen(mydisplay);
  45.     mybackground = WhitePixel(mydisplay,myscreen);
  46.     myforeground = BlackPixel(mydisplay,myscreen);
  47.  
  48.  
  49.         /* Suggest where to position the window: */
  50.     myhint.x = 200;
  51.     myhint.y = 200;
  52.     myhint.width = 300;
  53.     myhint.height = 300;
  54.     myhint.flags = PPosition | PSize;
  55.  
  56.  
  57.         /* Create a window - not displayed yet however: */
  58.     mywindow = XCreateSimpleWindow(mydisplay,DefaultRootWindow(mydisplay),
  59.  
  60.  
  61.     myhint.x,myhint.y,myhint.width,myhint.height,5,myforeground,mybackground);
  62.  
  63.  
  64.  
  65.  
  66. XSetStandardProperties(mydisplay,mywindow,hello,hello,None,argv,argc,&myhint
  67. );
  68.  
  69.  
  70.         /* Create a Graphics Context (GC) for the window: */
  71.     mygc = XCreateGC(mydisplay,mywindow,0,0);
  72.     XSetBackground(mydisplay,mygc,mybackground);
  73.     XSetForeground(mydisplay,mygc,myforeground);
  74.         /* Select input devices to listen to: */
  75.     XSelectInput(mydisplay,mywindow,ButtonPressMask|KeyPressMask|ExposureMask);
  76.         /* Actually display the window: */
  77.     XMapRaised(mydisplay,mywindow);
  78.  
  79.  
  80.         /* Main Event Loop: This is the core of any X program: */
  81.     done = 0;
  82.     while (done==0) {
  83.         XNextEvent(mydisplay,&myevent);
  84.         switch(myevent.type) {
  85.         case Expose: /* Repaint window on expose */
  86.             if (myevent.xexpose.count==0)
  87.                 XDrawImageString(myevent.xexpose.display,myevent.xexpose.window,
  88.                         mygc,50,50,hello,strlen(hello));
  89.             break;
  90.         case MappingNotify: /* Process keyboard mapping changes: */
  91.             XRefreshKeyboardMapping(&myevent);
  92.             break;
  93.         case ButtonPress: /* Process mouse click - output Hi! at mouse: */
  94.  
  95.  
  96.         XDrawImageString(myevent.xbutton.display,myevent.xbutton.window,
  97.         mygc,myevent.xbutton.x,myevent.xbutton.y,hi,strlen(hi));
  98.             break;
  99.         case KeyPress: /* Process key press - quit on q: */
  100.             i = XLookupString(&myevent,text,10,&mykey,0);
  101.             if (i==1 && text[0]=='q') done = 1;
  102.             break;
  103.         }
  104.     }
  105.  
  106.  
  107.     XFreeGC(mydisplay,mygc);
  108.     XDestroyWindow(mydisplay,mywindow);
  109.     XCloseDisplay(mydisplay);
  110.     exit(0);
  111. }
RAW Paste Data
Top