2

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

Overholt
  • 867
  • 1
  • 10
  • 23

1 Answers1

2

Okay, I found the answer. This Code works:

Window *getWindowList(Display *disp, unsigned long *len) {
    Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
    int form;
    unsigned long remain;
    unsigned char *list;

    if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,33,
                &type,&form,len,&remain,&list) != Success) {  // XA_WINDOW
        return 0;
    }

    return (Window*)list;
}
char *getWindowName(Display *disp, Window win) {
    Atom prop = XInternAtom(disp,"WM_NAME",False), type;
    int form;
    unsigned long remain, len;
    unsigned char *list;


    if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType,
                &type,&form,&len,&remain,&list) != Success) { // XA_STRING

        return NULL;
    }

    return (char*)list;
}
int main(){
    int i;
    unsigned long len;
    XKeyEvent esend;
    Display *disp = XOpenDisplay(NULL);
    Window *list;
    char *name;

        list = (Window*)getWindowList(disp,&len);
    for (i=0;i<(int)len;i++) {
        name = getWindowName(disp,list[i]);
        cout << i << ": " << name << endl;
        free(name);
        }
}

XLib Window Name Problems

Community
  • 1
  • 1
Overholt
  • 867
  • 1
  • 10
  • 23