I have not programmed X11 for more than ten years, so forgive me if I am wrong.
I believe that you can register for mouse movement events in your windows. If you handle such an event by storing the window handle in some variable or another, and then processing the event so that it does not leak down the tree, then at the time you want to identify the window, you can simply request the variable.
However, this will only work when the mouse is above the window on which you registered a suitable event handler, so you will not know about windows belonging to other applications - if there is no way to register for events in other people, which is possible.
The advantage over the other answer is that you do not have to go through the whole tree. The disadvantage is that you need to handle a large number of mouse movement events, and may not work to find other user windows.
I believe that there may also be mouse and mouse input events that will reduce the required processing.
Alastairg
source share