home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*----------------------------------------------------------------
- *
- * cmapview - scans and displays colormaps and installed visuals
- *
- *
- * 06.94 John Magdziarz, Silicon Graphics, Inc.
- * Last edit 07.22.94
- *
- *----------------------------------------------------------------*/
- #include <stdio.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include "XLayerUtil.h"
- #include <X11/keysym.h>
-
-
- char *installNames[] = {
- "No",
- "Yes",
- };
-
- char *vtypeNames[] = {
- "Underlay",
- "Normal",
- "Overlay",
- "Popup",
- };
-
-
- char *vclassNames[] = {
- "StaticGray",
- "GrayScale",
- "StaticColor",
- "PseudoColor",
- "TrueColor",
- "DirectColor",
- };
-
- Display *dpy;
- int screen;
- Window win;
- XWindowAttributes win_attributes;
- XVisualInfo vistemplate, *vinfo;
- int junk;
- Visual *defaultVisual;
- Visual *vis;
- XLayerVisualInfo template;
- XLayerVisualInfo *otherLayerInfo, *defaultLayerInfo;
- GC gc;
- XSetWindowAttributes attr;
-
-
- enum Vtype {underlay, normal, overlay, popup};
-
- struct {
- Colormap cmap;
- int vclass;
- enum Vtype vtype;
- int depth;
- int installed;
- int numRefs;
- int layer;
- Visual *visual;
- GC gc;
- Window win;
- int winIO;
- } allcmaps[1000];
-
- int cmapCnt = 0;
- int cmapsInstalled = 0;
- int winCnt = 0;
- int winChoice = 0;
-
-
- enum Vtype identifyVisual(Visual *);
- void getWindowCmapInfo(Window);
-
-
- usage()
- {
- printf("error in usage\n");
- }
-
-
- Display *OpenDisplay(display_name)
- char *display_name;
- {
- Display *d;
-
- d = XOpenDisplay(display_name);
- if (d == NULL) {
- fprintf (stderr, "unable to open display '%s'\n",
- XDisplayName (display_name));
- usage ();
- /* doesn't return */
- }
-
- return(d);
- }
-
-
- Display_Window_Id(window, newline_wanted)
- Window window;
- Bool newline_wanted;
- {
- char *win_name;
-
- printf("%ld", window); /* print id # in hex/dec */
-
- if (!window) {
- printf(" (none)");
- } else {
- if (window == RootWindow(dpy, screen)) {
- printf(" (the root window)");
- }
- if (!XFetchName(dpy, window, &win_name)) { /* Get window name if any */
- printf(" (has no name)");
- } else if (win_name) {
- printf(" \"%s\"", win_name);
- XFree(win_name);
- } else
- printf(" (has no name)");
- }
-
- if (newline_wanted)
- printf("\n");
-
- return;
- }
-
-
- void
- buildWin(int newWin)
- {
- attr.event_mask = ExposureMask;
- attr.colormap = allcmaps[newWin].cmap;
- attr.background_pixel = None;
- attr.border_pixel = None;
-
- allcmaps[newWin].win = XCreateWindow(dpy, win,
- 0, 0, 256, 256,
- 0, allcmaps[newWin].depth, allcmaps[newWin].winIO,
- allcmaps[newWin].visual,
- CWBorderPixel | CWBackPixel | CWColormap | CWEventMask, &attr);
-
- allcmaps[newWin].gc = XCreateGC(dpy, allcmaps[newWin].win, NULL, NULL);
- }
-
-
- void
- scanXTree(Window window, int recurse)
- {
- walkTree(window, recurse, 0);
- printf("\n\n");
- }
-
-
- walkTree(Window startWin, int recurse, int level)
- {
- int i, j;
- int rel_x, rel_y, abs_x, abs_y;
- unsigned int width, height, border, depth;
- Window root_win, parent_win;
- unsigned int num_children;
- Window *child_list;
- XClassHint classhint;
- int new;
-
- if (!XQueryTree(dpy, startWin, &root_win, &parent_win, &child_list,
- &num_children)) {
- fprintf(stderr, "Can't query window tree.");
- exit(-1);
- }
-
- if (level == 0)
- printf("\nScanning window hierarchy...");
-
- if (level == 1) {
- putc('.', stdout);
- fflush(stdout);
- }
-
- winCnt += num_children;
-
- getWindowCmapInfo(startWin);
-
- for (i = (int)num_children - 1; i >= 0; i--) {
- if (recurse)
- walkTree(child_list[i], 1, level+1);
- }
- if (child_list) XFree((char *)child_list);
- }
-
-
- void
- getWindowCmapInfo(Window startWin)
- {
- int j;
- XWindowAttributes win_attributes;
- XVisualInfo vistemplate;
- XVisualInfo *vinfo;
- int junk;
- int newcmap;
-
-
- if (!XGetWindowAttributes(dpy, startWin, &win_attributes)) {
- fprintf(stderr, "Can't get window attributes.");
- exit(-1);
- }
- vistemplate.visualid = XVisualIDFromVisual(win_attributes.visual);
- vinfo = XGetVisualInfo(dpy, VisualIDMask, &vistemplate, &junk);
-
- newcmap = 1;
- for (j = 0; j < cmapCnt; j++)
- if (win_attributes.colormap == allcmaps[j].cmap) {
- allcmaps[j].numRefs++;
- if (win_attributes.map_installed)
- allcmaps[j].installed = True;
- newcmap = 0;
- break;
- }
-
- if (newcmap && win_attributes.depth != 0) { /* if InputOnly, ignore */
- allcmaps[cmapCnt].numRefs++;
- allcmaps[cmapCnt].cmap = win_attributes.colormap;
- allcmaps[cmapCnt].vclass = vinfo->class;
- allcmaps[cmapCnt].visual = vinfo->visual;
- allcmaps[cmapCnt].vtype = identifyVisual(vinfo->visual);
- allcmaps[cmapCnt].depth = win_attributes.depth;
- allcmaps[cmapCnt].winIO = win_attributes.class;
- if (win_attributes.map_installed)
- cmapsInstalled++;
- else
- allcmaps[cmapCnt].installed = 0;
- buildWin(cmapCnt);
- cmapCnt++;
- }
- }
-
-
- #if 0
-
- for (i = (int)num_children - 1; i >= 0; i--) {
- if (!XGetWindowAttributes(dpy, window, &win_attributes)) {
- fprintf(stderr, "Can't get window attributes.");
- exit(-1);
- }
- vistemplate.visualid = XVisualIDFromVisual(win_attributes.visual);
- vinfo = XGetVisualInfo(dpy, VisualIDMask, &vistemplate, &junk);
-
- new = 1;
- for (j = 0; j < cmapCnt; j++)
- if (win_attributes.colormap == allcmaps[j].cmap) {
- allcmaps[j].numRefs++;
- if (win_attributes.map_installed)
- allcmaps[j].installed = True;
- new = 0;
- break;
- }
-
- if (new && win_attributes.depth != 0) {
- allcmaps[cmapCnt].numRefs++;
- allcmaps[cmapCnt].cmap = win_attributes.colormap;
- allcmaps[cmapCnt].vclass = vinfo->class;
- allcmaps[cmapCnt].visual = vinfo->visual;
- allcmaps[cmapCnt].vtype = identifyVisual(vinfo->visual);
- allcmaps[cmapCnt].depth = win_attributes.depth;
- allcmaps[cmapCnt].winIO = win_attributes.class;
- if (win_attributes.map_installed)
- cmapsInstalled++;
- else
- allcmaps[cmapCnt].installed = 0;
- buildWin(cmapCnt);
- cmapCnt++;
- }
-
- if (recurse)
- walkTree(child_list[i], 1, level+1);
- }
- if (child_list) XFree((char *)child_list);
- }
- #endif
-
- enum Vtype
- identifyVisual(Visual *vis)
- {
- int nVisuals;
- static int defLayer;
- int tmpLayer;
-
- if (!defaultVisual) {
- defaultVisual = DefaultVisual(dpy, screen);
- template.vinfo.visualid = defaultVisual->visualid;
- defaultLayerInfo = XGetLayerVisualInfo(dpy, VisualIDMask, &template, &nVisuals);
- defLayer = defaultLayerInfo->layer;
- }
-
- template.vinfo.visualid = vis->visualid;
- defaultLayerInfo = XGetLayerVisualInfo(dpy,
- VisualIDMask, &template, &nVisuals);
- tmpLayer = defaultLayerInfo->layer;
-
- if (tmpLayer < defLayer && defaultLayerInfo->type != None)
- return(underlay);
- else if (tmpLayer == defLayer)
- return(normal);
- else if (tmpLayer == defLayer + 1 && defaultLayerInfo->type != None)
- return(overlay);
- else if (tmpLayer == defLayer + 2 && defaultLayerInfo->type != None)
- return(popup);
- else {
- fprintf(stderr, "error determining layer\n");
- exit(-1);
- }
- }
-
-
-
- void
- listColormaps()
- {
- int i;
- char tmpstr[80];
-
- sprintf(tmpstr, "%9s%9s%10s%12s%9s%12s", "cmap", "depth", "vclass", "vtype",
- "# refs", "installed");
- /*
- tvclass\t\t# refs\tInstalled\n");
- */
- printf("%s\n", tmpstr);
- printf("---------------------------------------------------------------\n");
-
- for (i = 0; i < cmapCnt; i++) {
- printf("%2d", i);
- printf("%10d", allcmaps[i].cmap);
- printf("%5d", allcmaps[i].depth);
- printf("%15s", vclassNames[allcmaps[i].vclass]);
- printf("%10s", vtypeNames[allcmaps[i].vtype]);
- printf("%7d", allcmaps[i].numRefs);
- printf("%10s", installNames[allcmaps[i].installed]);
- printf("\n");
- };
- printf("\n\nClick MB1 in display window to cycle through colormaps that\n");
- printf("are currently associated with windows on this server.\n\n");
- }
-
-
- void
- displayColormap(int winChoice)
- {
- int i;
- int sqw = 16;
- int sqh = 16;
- int cells;
- int cellx;
- int jump;
- char title[80];
-
- cells = allcmaps[winChoice].visual->map_entries;
- if (cells > 256)
- cells = 256;
- cellx = 16;
-
- XClearWindow(dpy, win);
- XMapWindow(dpy, allcmaps[winChoice].win);
- XClearWindow(dpy, allcmaps[winChoice].win);
- sprintf(title, "%d)%s-%d-%s",
- winChoice,
- vtypeNames[allcmaps[winChoice].vtype],
- allcmaps[winChoice].depth,
- vclassNames[allcmaps[winChoice].vclass]);
-
- XStoreName(dpy, win, title);
- for (i = 0; i < cells; i++) {
- XSetForeground(dpy, allcmaps[winChoice].gc, i);
- if (cells <= 256)
- XFillRectangle(dpy, allcmaps[winChoice].win, allcmaps[winChoice].gc,
- (i % cellx) * sqw, (i / cellx) * sqh, sqw, sqh);
- else
- XDrawPoint(dpy, allcmaps[winChoice].win, allcmaps[winChoice].gc,
- (i % cellx), (i / cellx));
- }
- XFlush(dpy);
- }
-
-
- processEvents()
- {
- XEvent event;
- KeySym keysym;
- char buf[4];
- static int first = 1;
-
- while (XEventsQueued(dpy, QueuedAfterReading)) {
-
- XNextEvent(dpy, &event);
- switch (event.type) {
-
- case Expose:
- if (first) {
- first = 0;
- displayColormap(0);
- }
- else
- displayColormap(winChoice);
- break;
-
-
- case MotionNotify:
- break;
-
- case ButtonPress:
- switch(event.xbutton.button) {
-
- case Button1:
- XUnmapSubwindows(dpy, win);
- winChoice++;
- winChoice = winChoice % cmapCnt;
- displayColormap(winChoice);
- break;
-
- case Button2:
- break;
- }
- break;
- case KeyPress:
- /* save out which unmodified key (i.e. the key was
- * not modified w/something like "Shift", "Ctrl",
- * or "Alt") got pressed for use below.
- */
- XLookupString((XKeyEvent *)&event, buf, 4, &keysym, 0);
- if (keysym == XK_Escape)
- exit();
- break;
- }
- }
- }
-
-
-
- void
- setWMColormaps()
- {
- int i;
- Window tmpwins[100];
-
- for (i = 0; i < cmapCnt; i++)
- tmpwins[i] = allcmaps[i].win;
- XSetWMColormapWindows(dpy, win, tmpwins, cmapCnt);
- }
-
-
-
- main()
- {
- if (!(dpy = XOpenDisplay(NULL))) {
- fprintf (stderr, "unable to open display '%s'\n",
- XDisplayName(NULL));
- usage ();
- }
- screen = DefaultScreen(dpy);
-
- attr.event_mask = KeyPressMask | ButtonPressMask | ExposureMask;
- attr.background_pixel = BlackPixel(dpy, screen);
- attr.border_pixel = None;
-
- win = XCreateWindow(dpy, RootWindow(dpy, screen),
- 0, 0, 256, 256,
- 0, 8, InputOutput, DefaultVisual(dpy, screen),
- CWBorderPixel | CWBackPixel | CWEventMask, &attr);
-
- gc = XCreateGC(dpy, win, NULL, NULL);
- XMapWindow(dpy, win);
- XStoreName(dpy, win, "Dr. cmap\n");
- XFlush(dpy);
- scanXTree(RootWindow(dpy, screen), True);
- setWMColormaps();
- listColormaps();
-
- while (1)
- processEvents();
- }
-