XCheckMaskEvent for ButtonMotionMask only returns pointer position of (0,0)

Viewed 28

Using the X11 library on debian jessie, I have the following code:

#include <stdio.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
Display *display;
XEvent xevent;
Window window;
int x,y;
#define BUTTON1MOTION (1L<<8)

int main(int argc, char **argv){
    if( (display = XOpenDisplay(NULL)) == NULL )
        return -1;
    //int curr = 0;
    while (1) {
        XCheckMaskEvent(display, ButtonMotionMask, &xevent);
        printf("x_root: %d y_root: %d \n", xevent.xmotion.x_root, xevent.xmotion.y_root);
    }
}

I would like to capture MotionNotify events associated with the mouse being moved. However, the XCheckMaskEvent returns an endless stream of Motion events, even when no mouse buttons are pressed and no motion is done on the mouse; and x,y coordinates for these events are always (0,0). What can I do to get genuine mouse movement events, with the correct coordinates?

PS. use the -lX11 tag for compilation, i.e. gcc -Wall -lX11

EDIT: I used XCheckMaskEvent which works as it automatically returns, unlike the former, but this returns an endless stream of events even if the mouse is not moving.

1 Answers

From https://linux.die.net/man/3/xcheckmaskevent

The XCheckMaskEvent function searches the event queue and then any events available on the server connection for the first event that matches the specified mask. If it finds a match, XCheckMaskEvent removes that event, copies it into the specified XEvent structure, and returns True.

This function does not wait for such an event. Now imagine what your program does when no event matching ButtonMotionMask is in the event queue.

and x,y coordinates for these events are always (0,0).

You are reading an unintialised variable.

Related