/*----------------------------------------------------------------------*/ /* Locate and print the current netscape URL */ /* */ /* compile with: cc url.c -o url -L/usr/X11R6/lib -lXt -lXext -lX11 */ /*----------------------------------------------------------------------*/ #include #include #include #include #include #include #include /*-----------------------------------------------------------------------*/ unsigned char *retdata; /*-----------------------------------------------------------------------*/ int querytreerecurse(Display *dpy, Window top, Atom urlatom, Boolean matchname) { Window root_ret, parent, nwin, *children; int result, i, j, num_children; char *wname; Atom type; int format; unsigned long nitems, left; Boolean matched = True; XQueryTree(dpy, top, &root_ret, &parent, &children, &num_children); /* if (XFetchName(dpy, top, &wname)) printf("window \"%s\" (%04x) (parent %04x) has %d children\n", wname, top, parent, num_children); else printf("window (unnamed) (%04x) (parent %04x) has %d children\n", top, parent, num_children); */ if (matchname == True) { matched = False; if (XFetchName(dpy, top, &wname)) if (strstr(wname, "Netscape:") != NULL) { /* printf("window \"%s\" (%04x) (parent %04x) has %d children\n", wname, top, parent, num_children); */ if (strstr(wname, "Print") != NULL) { matched = True; /* printf("exact match!\n"); */ } } } if (matched == True) { result = XGetWindowProperty(dpy, top, urlatom, 0L, 256L, FALSE, AnyPropertyType, &type, &format, &nitems, &left, &retdata); if ((result == Success) && (retdata != NULL)) return Success; } /* Give preference to the Netscape "Print" window */ for (i = 0; i < num_children; i++) { nwin = *(children + i); if (querytreerecurse(dpy, nwin, urlatom, matchname) == Success) return Success; } return -1; } /*-----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { Widget toplevel = XtInitialize(argv[0], "Get_URL", NULL, 0, &argc, argv); Display *dpy = XtDisplay(toplevel); Atom URLATOM = XInternAtom(dpy, "_MOZILLA_URL", True); int result; if (URLATOM == None) { URLATOM = XInternAtom(dpy, "_NETSCAPE_URL", True); } if (URLATOM == None) { fprintf(stderr, "Atoms _MOZILLA_URL and _NETSCAPE_URL are not registered\n" "with the X server. Is Netscape running? Check xlsatoms. . .\n"); exit(1); } /* printf("urlatom = %d\n", (int)urlatom); */ /* Find the netscape window which responds to a query for Atom */ /* "_MOZILLA_URL". Search entire window tree recursively. */ result = querytreerecurse(dpy, DefaultRootWindow(dpy), URLATOM, True); if (result == Success) { fprintf(stdout, "%s\n", retdata); /* fprintf(stderr, "URL returned value is %s\n", retdata); */ exit (0); } else { result = querytreerecurse(dpy, DefaultRootWindow(dpy), URLATOM, False); if (result == Success) { fprintf(stdout, "%s\n", retdata); exit (0); } else { fprintf(stderr, "No success on get URL for any window\n"); exit (1); } } } /*----------------------------------------------------------------------*/