Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <X11/Xlib.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- const int SIZE = 128;
- int main()
- {
- //Open connection to the X server
- Display* dpy = XOpenDisplay(NULL);
- //Get the pointer position.. Have to go through all the screens
- bool found = false;
- int foundScreen = -1;
- int x, y; //location of the cursor
- for (int screenNum = 0; screenNum < XScreenCount(dpy); ++screenNum)
- {
- Window tmp1, tmp2; //Don't care about the windows
- int tmpx, tmpy; //Don't care about the local X, Y
- unsigned int tmpMask; //Don't care about the mask
- if (XQueryPointer(dpy, RootWindow(dpy, screenNum), &tmp1, &tmp2, &x, &y, &tmpx, &tmpy, &tmpMask))
- {
- found = true;
- foundScreen = screenNum;
- break;
- }
- }
- if (!found) //Don't do something unexepected
- return -1;
- //Now, create the indicator window.
- Window ourWindow = XCreateSimpleWindow(dpy, RootWindow(dpy, foundScreen),
- x - SIZE/2, y - SIZE/2,
- SIZE, SIZE,
- 0, 0, BlackPixel(dpy, foundScreen));
- //Don't deal with the WM
- XSetWindowAttributes attrs;
- attrs.override_redirect = True;
- XChangeWindowAttributes(dpy, ourWindow, CWOverrideRedirect, &attrs);
- XSelectInput(dpy, ourWindow, ButtonPressMask | ExposureMask);
- XMapWindow(dpy, ourWindow);
- bool quit = false;
- //We also grab the mouse so any click closes it.
- //(perhaps it would be better if left-click recentered, right-click closed or such?)
- XGrabPointer(dpy, RootWindow(dpy, foundScreen), False, ButtonPressMask, GrabModeAsync, GrabModeAsync,
- None, None, CurrentTime);
- while (!quit)
- {
- XEvent ev;
- XNextEvent(dpy, &ev);
- switch (ev.type)
- {
- case Expose:
- case GraphicsExpose:
- {
- //Paint the target thingy. Really simple: 4 bands, using black and white, alternating.
- GC gc = XCreateGC(dpy, ourWindow, 0, 0);
- XSetFillStyle(dpy, gc, FillSolid);
- //Outter band is already black, so we paint w/white 1/8th way in
- XSetForeground(dpy, gc, WhitePixel(dpy, foundScreen));
- XFillRectangle(dpy, ourWindow, gc, SIZE/8, SIZE/8, SIZE - SIZE/4, SIZE - SIZE/4);
- //Now another black band
- XSetForeground(dpy, gc, BlackPixel(dpy, foundScreen));
- XFillRectangle(dpy, ourWindow, gc, SIZE/4, SIZE/4, SIZE - SIZE/2, SIZE - SIZE/2);
- //And the inner white one
- XSetForeground(dpy, gc, WhitePixel(dpy, foundScreen));
- XFillRectangle(dpy, ourWindow, gc, 3*SIZE/8, 3*SIZE/8, SIZE/4, SIZE/4);
- XFreeGC(dpy, gc);
- break;
- }
- case ButtonPress:
- quit = true;
- break;
- default:
- break;
- }
- }
- XUngrabPointer(dpy, CurrentTime);
- }
Advertisement
Add Comment
Please, Sign In to add comment