/* * Window_With_Name: routine to locate a window with a given name on a display. * If no window with the given name is found, 0 is returned. * If more than one window has the given name, the first * one found will be returned. Only top and its subwindows * are looked at. Normally, top should be the RootWindow. */ Window Window_With_Name( Display *disp, Window top, const char *name) { Window *children, dummy; unsigned int nchildren; unsigned int i; Window w=0; char *window_name; if (XFetchName(disp, top, &window_name) && !strcmp(window_name, name)) return(top); if (!XQueryTree(disp, top, &dummy, &dummy, &children, &nchildren)) return(0); for (i=0; i<nchildren; i++) { w = Window_With_Name(disp, children[i], name); if (w) break; } if (children) XFree ((char *)children); return(w); }
/* * Select_Window_Args: a rountine to provide a common interface for * applications that need to allow the user to select one * window on the screen for special consideration. * This routine implements the following command line * arguments: * * -root Selects the root window. * -id <id> Selects window with id <id>. <id> may * be either in decimal or hex. * -name <name> Selects the window with name <name>. * * Call as Select_Window_Args(&argc, argv) in main before * parsing any of your program's command line arguments. * Select_Window_Args will remove its arguments so that * your program does not have to worry about them. * The window returned is the window selected or 0 if * none of the above arguments was present. If 0 is * returned, Select_Window should probably be called after * all command line arguments, and other setup is done. * For examples of usage, see xwininfo, xwd, or xprop. */ Window Select_Window_Args( int *rargc, char **argv) #define ARGC (*rargc) { int nargc=1; int argc; char **nargv; Window w=0; nargv = argv+1; argc = ARGC; #define OPTION argv[0] #define NXTOPTP ++argv, --argc>0 #define NXTOPT if (++argv, --argc==0) usage() #define COPYOPT nargv++[0]=OPTION, nargc++ while (NXTOPTP) { if (!strcmp(OPTION, "-")) { COPYOPT; while (NXTOPTP) COPYOPT; break; } if (!strcmp(OPTION, "-root")) { w=RootWindow(dpy, screen); continue; } if (!strcmp(OPTION, "-name")) { NXTOPT; w = Window_With_Name(dpy, RootWindow(dpy, screen), OPTION); if (!w) Fatal_Error("No window with name %s exists!",OPTION); continue; } if (!strcmp(OPTION, "-id")) { NXTOPT; w=0; sscanf(OPTION, "0x%lx", &w); if (!w) sscanf(OPTION, "%lu", &w); if (!w) Fatal_Error("Invalid window id format: %s.", OPTION); continue; } COPYOPT; } ARGC = nargc; return(w); }
int main(int argc, char *argv[]) { display = XOpenDisplay(0); if (!display) exit(EXIT_FAILURE); Window window_root = DefaultRootWindow(display); if (!window_root) exit(EXIT_FAILURE); Window window_found = Window_With_Name(display, window_root, “put here the name of the window”); if (window_found) { XSetInputFocus(display, window_found, RevertToNone, CurrentTime);
WId windowWithName(const char *name) { return Window_With_Name(tqt_xdisplay(), tqt_xrootwin(), name); }