kellex

X11lib Template

Sep 11th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <X11/Xlib.h>
  2. #include <X11/Xutil.h>
  3. #include <X11/Xos.h>
  4. #include <unistd.h>
  5.  
  6. void start_x(){
  7.     Display *dis;
  8.     Window win;
  9.     GC gc;
  10. };
  11.  
  12. void init_x(){
  13.     //colors black and white
  14.     unsigned log black,white;
  15.     //display, X connection
  16.     dis=XOpenDisplay((char *)0);
  17.     screen=DefaultScreen(dis);
  18.     black=BlackPixel(dis,screen); //get black color
  19.     white=WhitePixel(dis,screen); //get white color
  20.     //create window
  21.     printf("Window, height & width: ");
  22.     int height,width;
  23.     scanf("%d %d",&height,&width);
  24.     win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,height,width,5,white,black);
  25.     //Window Title
  26.     XSetStandardProperties(dis,win,"X11 lib Template","Sup!",None,NULL,0,NULL);
  27.     XSelectInput(dis,win,ExposureMake|ButtonPressMask|KeyPressMask);
  28.     //create the Graphics Context
  29.     gc=XCreateGC(dis,win,0,0);
  30.     //Foreground Background
  31.     XSetBackground(dis,gc,white);
  32.     XSetForeground(dis,gc,black);
  33.     //Clear window
  34.     XClearWindow(dis,win);
  35.     XMapRaised(dis,win);
  36. };
  37.  
  38. void close_x(){
  39.     XFreeGC(dis,gc);
  40.     XDestroyWindow(dis,win);
  41.     XCloseDisplay(dis);
  42.     exit(1);
  43. };
  44.  
  45. void redraw() {
  46.         XClearWindow(dis, win);
  47. };
  48. int main(){
  49.     init_x();
  50.  
  51.     while(1) {            
  52.                 /* get the next event and stuff it into our event variable.
  53.                    Note:  only events we set the mask for are detected!
  54.                 */
  55.                 XNextEvent(dis, &event);
  56.        
  57.                 if (event.type==Expose && event.xexpose.count==0) {
  58.                 /* the window was exposed redraw it! */
  59.                         redraw();
  60.                 }
  61.                 if (event.type==KeyPress&&
  62.                     XLookupString(&event.xkey,text,255,&key,0)==1) {
  63.                 /* use the XLookupString routine to convert the invent
  64.                    KeyPress data into regular text.  Weird but necessary...
  65.                 */
  66.                         if (text[0]=='q') {
  67.                                 close_x();
  68.                         }
  69.                         printf("You pressed the %c key!\n",text[0]);
  70.                 }
  71.                 if (event.type==ButtonPress) {
  72.                 /* tell where the mouse Button was Pressed */
  73.                         int x=event.xbutton.x,
  74.                             y=event.xbutton.y;
  75.  
  76.                 }
  77.         }
  78.  
  79.     sleep(4);
  80.     close_x();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment