'Get window from XEvent

I'm struggling figuring out how to obtain the window a specific event was triggered from. What I want to achieve is the following:

  1. Capture XCursorNotifyEvent
  2. Figure out which window requested the cursor change
  3. Replace the requested cursor with a custom cursor for that specific window

Step 1 works and step 3 would also work if I could figure out the actual window the cursor change was requested from.

Note that I do not want to change the cursor of my own application, it should work for any application. That's why I need to figure out which application/window requests a cursor change!

I know that every event has a window member variable, but apparently that window variable always returns the root window for me. According to the documentation, the window member does not represent the window that triggered the event so there must be another way.

XEvent xev;
Display* _display;
Window _root_window = XRootWindow(display, 0);
XFixesSelectCursorInput(_display, _root_window, XFixesDisplayCursorNotifyMask);

while(1) {
    XNextEvent(_display, &xev);
    XFixesCursorNotifyEvent xc_event = *(reinterpret_cast<XFixesCursorNotifyEvent*>(&xev));

    std::cout << "Window of captured event: " << xc_event.window << std::endl;

    ...
    XDefineCursor(_display, ???, MyCustomCursor);
}

Basically I need to figure out with what to replace the '???' in the XDefineCursor function call.



Solution 1:[1]

It turned out that my question was based on a misunderstanding (as you can read in the comments). To solve my issue, I have to query the focus window by using the XGetInputFocus library function.

Window focus_window;
int revert_to;
focus_window = XGetInputFocus(_display, &focus_window, &revert_to);
XDefineCursor(_display, focus_window, MyCustomCursor);

If you use this code, don't forget to handle possible errors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven B