I know this is a couple years out so this is informational for others coming along. First, using yet another library does not fit my definition of easier. I was working on something that needed this myself using xlib and C only and I wanted to see how simple I could make the code.
Essentially you only need four lines of code to do this in xlib in an existing program the rest is all in how you want to deal with it, printf/sprintf, grab_pixel_color(Display *display, XColor *color) etc.
/** gcc position.c -o position -lX11 **/
#include <X11/Xlib.h>
#include <stdio.h>
int main (int argc, char **argv) {
Window root_window; //<--one
int root_x, root_y; //<--two
unsigned int mask; //<--three
Display *display = XOpenDisplay(NULL);
/*
Bool XQueryPointer(display, w, root_return, child_return, root_x_return, root_y_return,
win_x_return, win_y_return, mask_return)
Display *display;
Window w;
Window *root_return, *child_return;
int *root_x_return, *root_y_return;
int *win_x_return, *win_y_return;
unsigned int *mask_return;
*/
XQueryPointer(display, DefaultRootWindow(display), &root_window, &root_window, &root_x, &root_y, &root_x, &root_y, &mask); //<--four
printf("Mouse coordinates (X: %d, Y: %d)
", root_x, root_y);
XCloseDisplay(display);
return 0;
}
I hope this is useful to someone else.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…