[Reworded] Deiconifying a window using Win ID and Display

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

3 posts / 0 new
Last post
[Reworded] Deiconifying a window using Win ID and Display

Submitted by Anonymous (not verified) on Fri, 06/15/2012 - 12:07

Hi all,
I'm trying to deiconify a window using the win id and display id. Currently I do the following:
char *display_name = getenv("DISPLAY");
Display *dp = NULL;
Window w = 138412048; //window ID hardcoded
if ((dp = XOpenDisplay(display_name)) == NULL){
printf("UNIX environment DISPLAY variable is not set correctly.\n");
exit(-1);
}
Widget shell = XtWindowToWidget(dp, w);
I then go on to Realize (if not realized), GetValues, SetValues, Map, and Raise the widget.
XtWindowToWidget is returning NULL, which indicates that there is no widget in the current display that corresponds to that window. I'm temporarily hardcoding the win ID that is returned by "xwininfo -int", which displays window info and the integer version of the window ID.
Any ideas why this shouldn't work? I'm at my wits end, and appreciate any ideas. Thanks in advance.
‹ Displaying Japanese in Text Widget (quite newvie) fixed size custom dialog with widgets ›

bolkhov

Wed, 11/17/2004 - 08:23#1

The problem is "the wrong window ID" (+)

Hi!

Try "xwininfo -root -tree", and you'll see that for any given res_name there are TWO windows: one "strange" (don't know where does it come from) and another one is "real".

Since the "strange" one goes first, xwininfo picks that one. Additionally, what xwininfo reports upon clicking on a window is NOT YOURS, but a WINDOW MANAGER'S window.

So, if you want to write a proggie that, for example, deiconifies and raises some-other-program's window, you have to do much more work.

WBR,
Dmitry

bolkhov

Wed, 11/17/2004 - 08:30#2

Code for finding a window by res_name or by title

Below goes a code to find a window by either its res_name or title. It sets an error handler, because some of the windows, listed by XQueryTree, may disappear in between.

That code was written by me, based on knowledge gained from xwininfo, xwd and XmuClientWindow().

<br />
static int may_fail = 0;<br />
static int was_X_error = 0;</p>
<p>static int ErrorHandler(Display *d __attribute__((unused)),<br />
XErrorEvent *e __attribute__((unused)))<br />
{<br />
was_X_error = 1;</p>
<p>// fprintf(stderr, "ERR!!!\n");</p>
<p> return 0;<br />
}</p>
<p>static void ENTER_FAILABLE(void)<br />
{<br />
may_fail++;<br />
was_X_error = 0;</p>
<p> if (may_fail == 1) XSetErrorHandler(ErrorHandler);<br />
}</p>
<p>static void LEAVE_FAILABLE(void)<br />
{<br />
was_X_error = 0;<br />
may_fail--;</p>
<p> if (may_fail == 1) XSetErrorHandler(NULL);<br />
}</p>
<p>static Atom xa_WM_STATE = None;</p>
<p>static Window SearchOneLevel(Display *dpy, Window win,<br />
const char *res_name, const char *title)<br />
{<br />
Window ret;</p>
<p> Window *children;<br />
unsigned int nchildren;<br />
Window dummy;<br />
int i;</p>
<p> XClassHint hints;<br />
char *winname;</p>
<p> Atom type;<br />
int format;<br />
unsigned long nitems, after;<br />
unsigned char *data;</p>
<p> if (!XQueryTree(dpy, win, &dummy, &dummy, &children, &nchildren))<br />
return 0;</p>
<p> for (i = 0, ret = 0; i < nchildren && ret == 0; i++)<br />
{<br />
/* Does this window have a WM_STATE property? */<br />
ENTER_FAILABLE();</p>
<p> type = None;<br />
XGetWindowProperty(dpy, children[i], xa_WM_STATE,<br />
0, 0, False,<br />
AnyPropertyType, &type, &format, &nitems,<br />
&after, &data);</p>
<p> if (was_X_error)<br />
{<br />
}<br />
else if (type)<br />
{<br />
/* Yes, it has WM_STATE! */</p>
<p> XFree(data); // We no longer need obtained data</p>
<p> if (res_name != NULL && res_name[0] != '\0')<br />
{<br />
/* Check window name... */<br />
if (XGetClassHint(dpy, children[i], &hints))<br />
{<br />
if (hints.res_name != NULL &&<br />
strcmp(res_name, hints.res_name) == 0) ret = children[i];</p>
<p> if (hints.res_name != NULL) XFree(hints.res_name);<br />
if (hints.res_class != NULL) XFree(hints.res_class);<br />
}<br />
}<br />
else if (title != NULL && title[0] != '\0')<br />
{<br />
if (XFetchName(dpy, children[i], &winname))<br />
{<br />
if (strcmp(title, winname) == 0) ret = children[i];<br />
XFree(winname);<br />
}<br />
}<br />
}<br />
else<br />
{<br />
/* No, we should recurse deeper */<br />
ret = SearchOneLevel(dpy, children[i], res_name, title);<br />
}</p>
<p> LEAVE_FAILABLE();<br />
}</p>
<p> if (children) XFree(children);</p>
<p> return ret;<br />
}</p>
<p>static Window SearchForWindow(Widget w, const char *res_name, const char *title)<br />
{<br />
Display *dpy = XtDisplay(w);<br />
Window root = RootWindowOfScreen(XtScreen(w));</p>
<p> if (xa_WM_STATE == None)<br />
{<br />
xa_WM_STATE = XInternAtom(dpy, "WM_STATE", True);<br />
if (xa_WM_STATE == None) return 0;<br />
}</p>
<p> return SearchOneLevel(dpy, root, res_name, title);<br />
}<br />