I am currently working with XLib in order to code an Application that can send Mouse and Keyboard Events to a certain window (Not display).
I was able to send these Events to a window using The "window"-handle obtained by the "XGetInputFocus()" method. Unfortunately I need to get the window-handle of another window (not the focused one) and send fake Input Events to that window. Therefore I wrote a method "enumerateWindows" to obtain this handle.
Here comes the problem. I can find several windows, that are called the same. Like at least 3 "eclipse" windows in the tree. Why is that? Because I didn't know which "Window" to pick, I just compared all of them to the Window I obtained from the "XGetInputFocus()" Method. There was no match (when comparing with the object identity). Nowhere in the tree. Why is that? How else can I get the same Window reference that I get from "XGetInputFocus()"?
Here is my Code and the Output:
#define EMPTY_WINDOW NULL
/* rootWindow: Window that is being looked from
* toSearch : Window that I want to obtain
*/
Window* enumerateWindows(Display *display, Window rootWindow, Window toSearch)
{
Window parent;
Window *children;
unsigned int nNumChildren;
char *name;
// Compare Object identity !!
if(&rootWindow == &toSearch){
cout << "Found correct Window!!" << endl;
return &rootWindow;
}
// Descend tree..
int status = XQueryTree(display, rootWindow, &rootWindow, &parent, &children, &nNumChildren);
if (status == 0)
{
cout << "Cant query further.." << endl;
return EMPTY_WINDOW;
}
if (nNumChildren == 0)
{
return EMPTY_WINDOW;
}
for (int i = 0; i < nNumChildren; i++)
{
Window *ret = enumerateWindows(display, children[i],toSearch);
if(ret != EMPTY_WINDOW ){
return ret;
}
}
XFree((char*) children);
return EMPTY_WINDOW;
}
int main()
{
// Obtain the X11 display.
Display *display = XOpenDisplay(0);
if(display == NULL) return -1;
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
// The important part: Obtain Current focus window!
XGetInputFocus(display, &winFocus, &revert);
// Try to obtain the same window from the QueryTree
Window *win = enumerateWindows(display, winRoot, winFocus);
if( win == &winFocus){
cout << "Object Identity same! Problem solved!" << endl;
}else{
cout << "Didn't find the correct object.." << endl;
}
XCloseDisplay(display);
return 0;
}
I appreciate your help! Thank you in advance!
Regards