[comp.windows.x] How do I get ID of the window that contains the pointer ?

subodh@mindcraft.com (Subodh Nijsure) (04/12/91)

	How do I get ID of the window that holds the pointer ? 

In most cases this window would belong to other application.  
Which rules out XQueryPointer() ( NO, I don't have any destructive 
thoughts in my mind ).

Also, I want to get this window ID without any user input.


Any pointers ??

Thanks in advance.

Subodh Nijsure ( subodh@mindcraft.com )

fgreco@govt.shearson.COM (Frank Greco) (04/12/91)

> 	How do I get ID of the window that holds the pointer ? 
> 
> In most cases this window would belong to other application.  
> Which rules out XQueryPointer() ( NO, I don't have any destructive 
> thoughts in my mind ).

	Why does this rule out XQueryPointer()?  It should do
	exactly what you want: gets the ID of the window under
	the pointer.  Unless you mean something else by
	"holds the pointer" that I'm missing.

> Any pointers ??

	Yes, usually one.... ;->

	Frank G.

klee@wsl.dec.com (Ken Lee) (04/12/91)

In article <671411930.29062@mindcraft.com>, subodh@mindcraft.com (Subodh Nijsure) writes:
|> In most cases this window would belong to other application.  
|> Which rules out XQueryPointer()

No, it does not rule out XQueryPointer.

-- 
Ken Lee
DEC Western Software Laboratory, Palo Alto, Calif.
Internet: klee@wsl.dec.com
uucp: uunet!decwrl!klee

mouse@lightning.mcrcim.mcgill.EDU (der Mouse) (04/13/91)

> How do I get ID of the window that holds the pointer ?

Which window that holds the pointer?  There's a whole chain of windows,
all the way from the root down to the smallest application window, all
of which could be said to hold the pointer.

Two plausible things you might want are (1) the "top-level" window
containing the pointer (ie, the highest window that was created as a
child of the root) or (2) the lowest (ie, innermost) window containing
the pointer.

> In most cases this window would belong to other application.  Which
> rules out XQueryPointer()

I don't see why.  You need to keep doing XQueryPointer calls, tracing
your way down through the window tree until you find the window of
interest, and unless you grab the server and the pointer there are race
conditions, but with those caveats it should work fine.

> Also, I want to get this window ID without any user input.

The user need not do anything in the above scenario; in fact, the
reason I said you need to grab the pointer is to freeze it, so that
user input doesn't cause the mouse to change windows while you're
chasing down the window tree.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

marbru@auto-trol.com (Martin Brunecky) (04/16/91)

In article <671411930.29062@mindcraft.com> subodh@mindcraft.com (Subodh Nijsure) writes:
>
>	How do I get ID of the window that holds the pointer ? 
>
      This is a fragment od the code used to send an event to a window
      containing a pointer. You may find a better solution, of course ...
      The target window may be owned by any application, whowever, this
      code only looks for windows interested in a particular event ..

    ......
    /* Find the pointer, if not on our screen, don't send anything */
    if (XQueryPointer( dpy, root, &root, &child, &rx, &ry, &wx, &wy, &mods ) )
    {
        XButtonEvent    bev;
        Window          wins[MAX_WINDOW_STACK];
        int             x[MAX_WINDOW_STACK];
        int             y[MAX_WINDOW_STACK];
        unsigned int    mask =  ButtonPressMask; 
        register int    iw = 0;
        int             ic = 0;         /* index of constraint window */

        /* save root window on our window stack */
        wins[iw] = root;  x[iw] = rx;  y[iw++] = ry;

        /* find the topmost window containing the pointer */
        while ( child != None )
        {
           win = child;
           if ( win == XtWindow(w) ) ic = iw; /* check for constraining win */
           XTranslateCoordinates ( dpy, root, win, rx, ry, &wx, &wy, &child );
           wins[iw] = win;  x[iw] = wx;  y[iw++] = wy;
           if (iw > MAX_WINDOW_STACK) return (FALSE);
        }
        wins[iw] = None;    /* make sure last "next' window is null */

        /* find the window which wants our event, give up on don't propagate */
        for ( --iw; iw >= 0; --iw)
        {
           XWindowAttributes attr;
           if ( XGetWindowAttributes ( dpy, wins[iw], &attr ))
           {
                if (attr.all_event_masks & mask       ) break;
                if (attr.map_state != IsViewable      ) continue;
                if (attr.do_not_propagate_mask & mask ) return(FALSE);
           }
        }

        .....

-- 
=*= Opinions presented here are solely of my own and not those of Auto-trol =*=
Martin Brunecky                           {...}sunpeaks!auto-trol!marbru
(303) 252-2499                        (sometimes also:  marbru@auto-trol.COM )
Auto-trol Technology Corp. 12500 North Washington St., Denver, CO 80241-2404