rex123 rex123 - 2 months ago 16
C Question

getting mouseclick coordinates with Xlib

I would like to know how to get the x and y coordinates of a mouseclick with Xlib anywhere on the screen. I've found this post which gets the current pointer position

How can I get the current mouse (pointer) position co-ordinates in X ,

but I don't know how to modify it so it gets the x y coordinates when the mouse is clicked.
I've tried to write this code but it does nothing.

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main (){
int x=-1,y=-1;
XEvent event;
int button;
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot connect to X server!\n");
exit (EXIT_FAILURE);
}
Window root= XDefaultRootWindow(display);
XSelectInput(display, root, ButtonReleaseMask) ;
while(1){
XNextEvent(display,&event);
switch(event.type){
case ButtonRelease:
switch(event.xbutton.button){
case Button1:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button1;
break;

case Button3:
x=event.xbutton.x;
y=event.xbutton.y;
button=Button3;
break;
default:
break;

}
break;
default:
break;
}
if(x>=0 && y>=0)break;
}
if(button==Button1)printf("leftclick at %d %d \n",x,y);
else printf("rightclick at %d %d \n",x,y);
XCloseDisplay(display);
return 0;
}


The events are probably send to other windows and that is the reason it's not working. Another problem is that when I add ButtonPressMask to the XSelectInput function I get the following error:

X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 2 (X_ChangeWindowAttributes)
Serial number of failed request: 7
Current serial number in output stream: 7


If there is simpler way to do this in C, I am happy to hear it.

Answer

You'll probably need to grab the pointer.

I don't know if you want just want button presses of releases. I've changed it to presses, but you can pick up both with:

XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ;

and add the ButtonRelease case back in.

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main (){
    int x=-1,y=-1;
    XEvent event;
    int button;
    Display *display = XOpenDisplay(NULL);
    if (display == NULL) {
    fprintf(stderr, "Cannot connect to X server!\n");
    exit (EXIT_FAILURE);
    }
    Window root= XDefaultRootWindow(display);
    XGrabPointer(display, root, False, ButtonPressMask, GrabModeAsync,
         GrabModeAsync, None, None, CurrentTime);

    XSelectInput(display, root, ButtonPressMask) ;
    while(1){
    XNextEvent(display,&event);
    switch(event.type){
    case ButtonPress:
        switch(event.xbutton.button){
        case Button1:
        x=event.xbutton.x;
        y=event.xbutton.y;
        button=Button1;
        break;

        case Button3:
        x=event.xbutton.x;
        y=event.xbutton.y;
        button=Button3;
        break;
        default:
        break;

        }
        break;
    default:
        break;
    }
    if(x>=0 && y>=0)break;
    }
    if(button==Button1)printf("leftclick at %d %d \n",x,y);
    else printf("rightclick at %d %d \n",x,y);
    XCloseDisplay(display);
    return 0;
}
Comments