title: Xlib,C++ - Get Window Handle (SendEvent,...)
body: <p>I am currently working with XLib in order to code an Application that can send Mouse and Keyboard Events to a certain <strong>window</strong> (Not display).</p>
  
  <p>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. </p>
  
  <p>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 <strong>identity</strong>). Nowhere in the tree.  Why is that? How else can I get the same Window reference that I get from "XGetInputFocus()"?</p>
  
  <p>Here is my Code and the Output:</p>
  
  <pre><code> #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(&amp;rootWindow == &amp;toSearch){
      cout &lt;&lt; "Found correct Window!!" &lt;&lt; endl;
      return &amp;rootWindow;
  }
  // Descend tree..
  int status = XQueryTree(display, rootWindow, &amp;rootWindow, &amp;parent, &amp;children, &amp;nNumChildren);
  
  if (status == 0)
  {
      cout &lt;&lt; "Cant query further.." &lt;&lt; endl;
      return EMPTY_WINDOW;
  }
  
  if (nNumChildren == 0)
  {
      return EMPTY_WINDOW;
  }
  
  for (int i = 0; i &lt; 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, &amp;winFocus, &amp;revert);
     // Try to obtain the same window from the QueryTree
     Window *win = enumerateWindows(display, winRoot, winFocus);
  
     if( win == &amp;winFocus){
         cout &lt;&lt; "Object Identity same! Problem solved!" &lt;&lt; endl;
     }else{
         cout &lt;&lt; "Didn't find the correct object.." &lt;&lt; endl;
     }
  
     XCloseDisplay(display);
     return 0;
  }
  </code></pre>
  
  <p>I appreciate your help! Thank you in advance!</p>
  
  <p>Regards</p>