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

4

22

This can either be some sample C code or a utility that will show me either gui or on the console it doesn't matter, but I have to be able to "command" it to grab the co-ordinates at an exact time which makes xev not very useful (that I could figure out).

Downes answered 27/8, 2010 at 15:45 Comment(0)
D
20

I'm not a C programmer by any means but I looked at a couple of online tutorials and think this is how you are supposed to read the current mouse position. This is my own code and I'd done nothing with Xlib before so it could be completely broken (for example, the error handler shouldn't just do nothing for every error) but it works. So here is another solution:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>

static int _XlibErrorHandler(Display *display, XErrorEvent *event) {
    fprintf(stderr, "An error occured detecting the mouse position\n");
    return True;
}

int main(void) {
    int number_of_screens;
    int i;
    Bool result;
    Window *root_windows;
    Window window_returned;
    int root_x, root_y;
    int win_x, win_y;
    unsigned int mask_return;
    
    Display *display = XOpenDisplay(NULL);
    assert(display);
    XSetErrorHandler(_XlibErrorHandler);
    number_of_screens = XScreenCount(display);
    fprintf(stderr, "There are %d screens available in this X session\n", number_of_screens);
    root_windows = malloc(sizeof(Window) * number_of_screens);
    for (i = 0; i < number_of_screens; i++) {
        root_windows[i] = XRootWindow(display, i);
    }
    for (i = 0; i < number_of_screens; i++) {
        result = XQueryPointer(display, root_windows[i], &window_returned,
                &window_returned, &root_x, &root_y, &win_x, &win_y,
                &mask_return);
        if (result == True) {
            break;
        }
    }
    if (result != True) {
        fprintf(stderr, "No mouse found.\n");
        return -1;
    }
    printf("Mouse is at (%d,%d)\n", root_x, root_y);
    
    free(root_windows);
    XCloseDisplay(display);
    return 0;
}
Downes answered 28/8, 2010 at 17:18 Comment(2)
Maybe that will save someone some time: To compile it, save it under pointerposition.c and run: gcc pointerposition.c -o pointerposition -lX11Crossfade
Some may find this useful to know: to get the active window, you can useWindow focused; int revert_to; XGetInputFocus(display, &focused, &revert_to); I replaced the for loops with this one call in order to get the active window, rather than whatever window the example is trying to findSoerabaja
B
13

xdotool might be the best tool for this.

For C, you can use libxdo.

Bors answered 3/1, 2012 at 19:15 Comment(2)
As an example for xdotool, xdotool getmouselocation gives me x:1973 y:54 screen:0 window:8389001.Brandibrandice
@Brandibrandice add the --shell option to have the output formatted for each shell usageKhorma
D
1

Actually, xev is very useful if you supply it with the window id grabbed using xwininfo, then it can easily perform this task for you. There are no doubt much more elegant solutions but it works.

Downes answered 27/8, 2010 at 15:50 Comment(0)
G
0

xinput can be used to print the full device state of any input device.

First you need to discover your device id:

$ xinput --list | grep -i mouse                                                                                                                                
⎜   ↳ Logitech USB Receiver Mouse               id=11   [slave  pointer  (2)]

then you can ask for state:

$ xinput --query-state 11;
2 classes :
ButtonClass
        button[1]=up
        button[2]=up
        button[3]=up
        button[4]=up
        button[5]=up
        button[6]=up
        button[7]=up
        button[8]=up
        button[9]=up
        button[10]=up
        button[11]=up
        button[12]=up
        button[13]=up
        button[14]=up
        button[15]=up
        button[16]=up
        button[17]=up
        button[18]=up
        button[19]=up
        button[20]=up
ValuatorClass Mode=Relative Proximity=In
        valuator[0]=274
        valuator[1]=886
        valuator[2]=0
        valuator[3]=675

Or just a loop:

while sleep .2; do xinput --query-state $(xinput --list | grep -i mouse | cut -d= -f2 | cut -f1| head -1); done
Geri answered 6/1, 2020 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.