Guest User

Untitled

a guest
Dec 18th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <X11/Xlib.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #include <unistd.h>
  6.  
  7. const int SIZE = 128;
  8.  
  9. int main()
  10. {
  11.     //Open connection to the X server
  12.     Display* dpy = XOpenDisplay(NULL);
  13.    
  14.     //Get the pointer position.. Have to go through all the screens
  15.     bool found       = false;
  16.     int  foundScreen = -1;
  17.     int  x, y; //location of the cursor
  18.     for (int screenNum = 0; screenNum < XScreenCount(dpy); ++screenNum)
  19.     {
  20.         Window tmp1, tmp2;    //Don't care about the windows
  21.         int    tmpx, tmpy;    //Don't care about the local X, Y
  22.         unsigned int tmpMask; //Don't care about the mask
  23.        
  24.         if (XQueryPointer(dpy, RootWindow(dpy, screenNum), &tmp1, &tmp2, &x, &y, &tmpx, &tmpy, &tmpMask))
  25.         {
  26.             found       = true;
  27.             foundScreen = screenNum;
  28.             break;
  29.         }
  30.     }
  31.    
  32.     if (!found) //Don't do something unexepected
  33.         return -1;
  34.    
  35.     //Now, create the indicator window.
  36.    
  37.     Window ourWindow = XCreateSimpleWindow(dpy, RootWindow(dpy, foundScreen),
  38.                                            x - SIZE/2, y - SIZE/2,
  39.                                            SIZE,       SIZE,
  40.                                            0,  0, BlackPixel(dpy, foundScreen));
  41.    
  42.     //Don't deal with the WM
  43.     XSetWindowAttributes attrs;
  44.     attrs.override_redirect = True;
  45.     XChangeWindowAttributes(dpy, ourWindow, CWOverrideRedirect, &attrs);
  46.     XSelectInput(dpy, ourWindow, ButtonPressMask | ExposureMask);
  47.     XMapWindow(dpy, ourWindow);
  48.    
  49.     bool quit = false;
  50.    
  51.     //We also grab the mouse so any click closes it.
  52.     //(perhaps it would be better if left-click recentered, right-click closed or such?)
  53.     XGrabPointer(dpy, RootWindow(dpy, foundScreen), False, ButtonPressMask, GrabModeAsync, GrabModeAsync,
  54.                  None, None, CurrentTime);
  55.    
  56.     while (!quit)
  57.     {
  58.         XEvent ev;
  59.         XNextEvent(dpy, &ev);
  60.        
  61.         switch (ev.type)
  62.         {
  63.             case Expose:
  64.             case GraphicsExpose:
  65.             {
  66.                 //Paint the target thingy. Really simple: 4 bands, using black and white, alternating.
  67.                 GC gc = XCreateGC(dpy, ourWindow, 0, 0);
  68.                 XSetFillStyle(dpy, gc, FillSolid);
  69.                
  70.                 //Outter band is already black, so we paint w/white 1/8th way in
  71.                 XSetForeground(dpy, gc, WhitePixel(dpy, foundScreen));
  72.                 XFillRectangle(dpy, ourWindow, gc, SIZE/8, SIZE/8, SIZE - SIZE/4, SIZE - SIZE/4);
  73.                
  74.                 //Now another black band
  75.                 XSetForeground(dpy, gc, BlackPixel(dpy, foundScreen));
  76.                 XFillRectangle(dpy, ourWindow, gc, SIZE/4, SIZE/4, SIZE - SIZE/2, SIZE - SIZE/2);
  77.                
  78.                 //And the inner white one
  79.                 XSetForeground(dpy, gc, WhitePixel(dpy, foundScreen));
  80.                 XFillRectangle(dpy, ourWindow, gc, 3*SIZE/8, 3*SIZE/8, SIZE/4, SIZE/4);
  81.                
  82.                 XFreeGC(dpy, gc);
  83.                 break;
  84.             }
  85.             case ButtonPress:
  86.                 quit = true;
  87.                 break;
  88.             default:
  89.                 break;
  90.         }
  91.            
  92.     }
  93.    
  94.    
  95.     XUngrabPointer(dpy, CurrentTime);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment