Find XWindow id for GLUT based program

I am trying to integrate an existing program (not made by me) that is based on GLUT into my Qt program. The whole thing is running on X. It is posssible to integrate X windows into other applications, but you need to have the Window ID for that window. I have tried searching the web and newsgroups archives for information on how to retreive this ID, but I came up with loads of unrelated links and nothing usefull. Does anyone here know how one can reteive the window id X uses? Any hints on where to look for this info?

Hi André,

I’ve had similar problems in the past, and settled for the following code. The assumption is that you know the name of your window:

Window getWindowIdFromName(Display *dpy, Window level, char *name)
{
    Window *children;
    Window dmy;
    Window root;
    Window id;
    char *window_name;
    int i;
    int no_of_children;

    if (0 == XQueryTree(dpy, level, &dmy, &dmy, &children, &no_of_children))
        return 0;

    for (i=0; i<no_of_children; i++)
    {
        if (XFetchName(dpy,children[i], &window_name))
        {
            if (strcmp (name, window_name))
                XFree(window_name);
            else
            {
                XFree(window_name);
                return children[i];
            }
        }
        else
        {
            if (id = getWindowIdFromName(dpy, children[i], name))
                return id;
        }
    }
    return 0;
}

The level parameter in the initial call to the function is the root window, which you can get using:

 
   Window root = DefaultRootWindow(dpy);
 

I hope this helps.

-Malancha

Thank you! That piece of code works nicely! I just did a minor modification:

 [b]unsigned[/b] int no_of_children;

This makes it compile :slight_smile: Thanks again.