What is wrong with my X11 code?

I am trying to get an X Window at a specific location on the screen. When I asked people to execute this function, they said that you would just call XQueryTree recursively.

This is a piece of code that I think is somehow wrong. When I debug it, it works fine. The only problem is that the conclusion he gives seems a little strange. When I do XQueryTree in the root window, I get hundreds of children when I have only five or so open. In addition, it seems that there is a top-level window somewhere where it simply does not exist, and returns it as a result. Regardless of how I move my actual windows, XQueryTree seems to indicate that there is another window on top of my windows (not covering the whole screen.) When I look where it says that this window is in some then an arbitrary point on my desktop.

If this helps: The display is in XOpenDisplay (NULL), and the root window that I originally passed is XDefaultRootWindow (display). I am running gnome under debian with metacity.

point getwindowatloc(Display * display, Window root, jint x, jint y) { Window returnedroot; Window returnedparent; Window * children; unsigned int numchildren; XQueryTree(display,root,&returnedroot,&returnedparent,&children, &numchildren); XWindowAttributes w; int i; for(i=numchildren-1; i>=0; i--) { XGetWindowAttributes(display,children[i],&w); if(x>=wx && x<=w.x+w.width && y>=wy && y <= w.y+w.height) { point result={wx,wy}; XFree(children); return result; } else { point result=getwindowatloc(display,children[i],xw.x,yw.y); if(result.x!=INT_MAX) { result.x+=wx; result.y+=wy; XFree(children); return result; } } } if(children) { XFree(children); } return notfound; } 

Thanks!

EDIT: for those looking for similar information: I ended up searching for xwininfo source. The key function is Find_Client in dsimple.c, which somehow ignores window managers to get the window that you are really looking for. If you want to look into the subwindows, this is the code I added to Select_Window in dsimple.c, which will look recursively inside the subwindows using XTranslateCoordinates.

 Window child; do { XTranslateCoordinates(dpy,target_temp,target_win,x,y,&x,&y,&child); target_temp=target_win; target_win=child; } while(target_win); return target_temp; 
+6
c x11 xlib xorg
source share
2 answers

Your code looks right (I have not tested it), and the results you describe do not look strange at all. Metacity (and other X window managers) will create many windows around the windows in the application to display the window name, borders and other decorations.

Try the test with some simpler window manager like TVM (or not at all). TVM should create far fewer windows than current window managers. This should facilitate understanding.

Usually, however, it is a bad idea to deal with a window manager. Can't you solve your problem at a higher level, since you need to use xlib directly?

+3
source share

I think you want to query the _NET_CLIENT_LIST root window. This will create a list of window identifiers for all client windows, with the exception of all "virtual" windows created by the window manager. Most window managers seem to support _NET_CLIENT_LIST , but you can also ask if any given function is supported.

+3
source share

All Articles