kellex

x11SimpleProg

Sep 11th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. /*
  2.     Remember to compile try:
  3.         1) gcc hi.c -o hi -lX11
  4.         2) gcc hi.c -I /usr/include/X11 -L /usr/X11/lib -lX11
  5.         3) gcc hi.c -I /where/ever -L /who/knows/where -l X11
  6.  
  7.     Brian Hammond 2/9/96.    Feel free to do with this as you will!
  8. */
  9.  
  10.  
  11. /* include the X library headers */
  12. #include <X11/Xlib.h>
  13. #include <X11/Xutil.h>
  14. #include <X11/Xos.h>
  15.  
  16. /* include some silly stuff */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. /* here are our X variables */
  21. Display *dis;
  22. int screen;
  23. Window win;
  24. GC gc;
  25.  
  26. /* here are our X routines declared! */
  27. void init_x();
  28. void close_x();
  29. void redraw();
  30.  
  31. main () {
  32.     XEvent event;       /* the XEvent declaration !!! */
  33.     KeySym key;     /* a dealie-bob to handle KeyPress Events */   
  34.     char text[255];     /* a char buffer for KeyPress Events */
  35.  
  36.     init_x();
  37.  
  38.     /* look for events forever... */
  39.     while(1) {     
  40.         /* get the next event and stuff it into our event variable.
  41.            Note:  only events we set the mask for are detected!
  42.         */
  43.         XNextEvent(dis, &event);
  44.    
  45.         if (event.type==Expose && event.xexpose.count==0) {
  46.         /* the window was exposed redraw it! */
  47.             redraw();
  48.         }
  49.         if (event.type==KeyPress&&
  50.             XLookupString(&event.xkey,text,255,&key,0)==1) {
  51.         /* use the XLookupString routine to convert the invent
  52.            KeyPress data into regular text.  Weird but necessary...
  53.         */
  54.             if (text[0]=='q') {
  55.                 close_x();
  56.             }
  57.             printf("You pressed the %c key!\n",text[0]);
  58.         }
  59.         if (event.type==ButtonPress) {
  60.         /* tell where the mouse Button was Pressed */
  61.             int x=event.xbutton.x,
  62.                 y=event.xbutton.y;
  63.  
  64.             strcpy(text,"X is FUN!");
  65.             XSetForeground(dis,gc,rand()%event.xbutton.x%255);
  66.             XDrawString(dis,win,gc,x,y, text, strlen(text));
  67.         }
  68.     }
  69. }
  70.  
  71. void init_x() {
  72. /* get the colors black and white (see section for details) */        
  73.     unsigned long black,white;
  74.  
  75.     dis=XOpenDisplay((char *)0);
  76.     screen=DefaultScreen(dis);
  77.     black=BlackPixel(dis,screen),
  78.     white=WhitePixel(dis, screen);
  79.     win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
  80.         300, 300, 5,black, white);
  81.     XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL);
  82.     XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
  83.         gc=XCreateGC(dis, win, 0,0);        
  84.     XSetBackground(dis,gc,white);
  85.     XSetForeground(dis,gc,black);
  86.     XClearWindow(dis, win);
  87.     XMapRaised(dis, win);
  88. };
  89.  
  90. void close_x() {
  91.     XFreeGC(dis, gc);
  92.     XDestroyWindow(dis,win);
  93.     XCloseDisplay(dis);
  94.     exit(1);               
  95. };
  96.  
  97. void redraw() {
  98.     XClearWindow(dis, win);
  99. };
Advertisement
Add Comment
Please, Sign In to add comment