cinputx11xlib

XInput 2 Error with RawMotion (BadValue on XISelectEvents with higher values)


I'm writing a pure Xlib app and want to use XInput2 extension for RawMotion events. The problem appears if I use XISelectEvents when I set the mask with any XI2 event higher than XI_PropertyEvent. So, for example, if I use XISetMask(mask, XI_Motion); it works normally unlike with XISetMask(mask, XI_RawMotion);. Also the error appears regardless of the deviceid passed and regardless of other event types set to mask. Why could it happen?

The code example:

#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>

int main() {
    Display* display = XOpenDisplay(nullptr);

    int screen_num = DefaultScreen(display);

    Window window = XCreateSimpleWindow(
            display,
            RootWindow(display, screen_num),
            0, 0,
            640, 480,
            4,
            16711680,
            WhitePixel(display, screen_num)
    );
    XSelectInput(display, window, KeyPressMask);

    int major_opcode, first_event, first_error;
    XQueryExtension(display, "XInputExtension", &major_opcode, &first_event, &first_error);

    unsigned char mask[XIMaskLen(XI_LASTEVENT)] = {0, };
    XISetMask(mask, XI_RawMotion);

    XIEventMask event_mask;
    event_mask.deviceid = XIAllDevices;
    event_mask.mask_len = sizeof(mask);
    event_mask.mask = mask;

    XISelectEvents(display, window, &event_mask, 1);

    XMapWindow(display, window);
    XFlush(display);

    while (true) {
        XEvent ev;
        XNextEvent(display, &ev);
        if (ev.type == KeyPress && ev.xkey.keycode == 9) {
            break;
        }
    }
    XUnmapWindow(display, window);
    XFlush(display);
    XDestroyWindow(display, window);
    XCloseDisplay(display);
    return 0;
}

The error:

X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  131 (XInputExtension)
  Minor opcode of failed request:  46 ()
  Value in failed request:  0xd
  Serial number of failed request:  16
  Current serial number in output stream:  17

Solution

  • The problem is that XInput2 call XISelectEvents with raw events works only if the root window passed. So the solution is to use XISelectEvents(display, DefaultRootWindow(display), &event_mask, 1);