Window xfindwind( Display *dpy, Window win, char *name, int depth ) { char *nr; Window rr, pr, *cl; Window wr; unsigned int nc; register int i; if (depth == 0) /* negative depths search all */ return(None); if (!XQueryTree(dpy, win, &rr, &pr, &cl, &nc) || nc == 0) return(None); wr = None; /* breadth first search */ for (i = 0; wr == None && i < nc; i++) if (XFetchName(dpy, cl[i], &nr)) { if (!strcmp(nr, name)) wr = cl[i]; free(nr); } for (i = 0; wr == None && i < nc; i++) wr = xfindwind(dpy, cl[i], name, depth-1); XFree((char *)cl); return(wr); }
static void setvec( /* set up vector drawing for pick */ int ipt[2] ) { extern Window xfindwind(); XWindowAttributes wa; XColor xc; XGCValues gcv; int rx, ry, wx, wy; Window rw, cw; unsigned int pm; /* compute pointer location */ if (gwind == 0) { register char *wn; for (wn = picture; *wn; wn++); while (wn > picture && wn[-1] != '/') wn--; if ((gwind = xfindwind(theDisplay, rwind, wn, 4)) == 0) { fprintf(stderr, "%s: cannot find display window!\n", progname); exit(1); } } XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm); xoff = wx - ipt[0]; yoff = wy - ipt[1]; /* set graphics context */ if (vecGC == 0) { XGetWindowAttributes(theDisplay, gwind, &wa); xc.red = 65535; xc.green = 0; xc.blue = 0; xc.flags = DoRed|DoGreen|DoBlue; if (XAllocColor(theDisplay, wa.colormap, &xc)) { gcv.foreground = xc.pixel; vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv); } else { gcv.function = GXinvert; vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv); } } }
static void init( /* get view and find window */ char *pname, char *wname ) { extern Window xfindwind(); XWindowAttributes wa; XColor xc; XGCValues gcv; register int i; /* get the viewing parameters */ if (viewfile(pname, &ourview, &pres) <= 0 || setview(&ourview) != NULL) { fprintf(stderr, "%s: cannot get view from \"%s\"\n", progname, pname); exit(1); } /* open the display */ if ((theDisplay = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "%s: cannot open display; DISPLAY variable set?\n", progname); exit(1); } /* find our window */ if (wname == NULL) { /* remove directory prefix from name */ for (i = strlen(pname); i-- > 0; ) if (pname[i] == '/') break; wname = pname+i+1; i = 0; } else i = 1; gwind = xfindwind(theDisplay, rwind, wname, 4); if (gwind == None) { if (i) { fprintf(stderr, "%s: cannot find \"%s\" window\n", progname, wname); exit(2); } /* start ximage */ if (fork() == 0) { execlp(XIM, XIM, "-c", "256", pname, 0); perror(XIM); fprintf(stderr, "%s: cannot start %s\n", progname, XIM); kill(getppid(), SIGPIPE); _exit(1); } do sleep(8); while ((gwind=xfindwind(theDisplay,rwind,wname,4)) == None); } else XMapRaised(theDisplay, gwind); do { XGetWindowAttributes(theDisplay, gwind, &wa); sleep(6); } while (wa.map_state != IsViewable); if (wa.width != scanlen(&pres) || wa.height != numscans(&pres)) { fprintf(stderr, "%s: warning -- window seems to be the wrong size!\n", progname); if (pres.rt & YMAJOR) { pres.xr = wa.width; pres.yr = wa.height; } else { pres.xr = wa.height; pres.yr = wa.width; } } /* set graphics context */ gcv.font = XLoadFont(theDisplay, FONTNAME); if (gcv.font == 0) { fprintf(stderr, "%s: cannot load font \"%s\"\n", progname, FONTNAME); exit(1); } xc.red = col[0] >= 1.0 ? 65535 : (unsigned)(65536*col[0]); xc.green = col[1] >= 1.0 ? 65535 : (unsigned)(65536*col[1]); xc.blue = col[2] >= 1.0 ? 65535 : (unsigned)(65536*col[2]); xc.flags = DoRed|DoGreen|DoBlue; gcv.background = xc.green >= 32768 ? BlackPixel(theDisplay,DefaultScreen(theDisplay)) : WhitePixel(theDisplay,DefaultScreen(theDisplay)) ; if (XAllocColor(theDisplay, wa.colormap, &xc)) { gcv.foreground = xc.pixel; vecGC = XCreateGC(theDisplay,gwind, GCForeground|GCBackground|GCFont,&gcv); strGC = vecGC; } else { gcv.function = GXinvert; vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv); gcv.foreground = xc.green < 32768 ? BlackPixel(theDisplay,DefaultScreen(theDisplay)) : WhitePixel(theDisplay,DefaultScreen(theDisplay)) ; strGC = XCreateGC(theDisplay,gwind, GCForeground|GCBackground|GCFont,&gcv); } }