home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xstdcmap / xstdcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-24  |  10.7 KB  |  361 lines

  1. /*
  2.  * $XConsortium: xstdcmap.c,v 1.6 89/07/24 11:06:26 jim Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Donna Converse, MIT X Consortium
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <X11/Xos.h>
  28. #include <X11/Xlib.h>
  29. #include <X11/Xutil.h>
  30. #include <X11/Xresource.h>
  31. #include <X11/Xatom.h>
  32. #include <X11/Xmu/StdCmap.h>
  33.  
  34. extern void exit();
  35.  
  36. #define REPLACE        1
  37. #define DO_NOT_REPLACE  0
  38. #define RETAIN        1
  39. #define DO_NOT_RETAIN    0
  40.  
  41. static char        *display_name = NULL;
  42. static char        *program_name = NULL;
  43. static Bool        all = 0;
  44. static Bool        help = 0;
  45. static Bool         verbose = 0;
  46. static Display        *dpy = NULL;
  47.  
  48. typedef struct
  49. {
  50.     Bool    create;
  51.     Bool    delete;
  52.     Atom    property;
  53.     char    *name;
  54.     char    *nickname;
  55. } colormap_property;
  56.  
  57. static colormap_property propertyTable[]=
  58. {
  59. {0,    0,    XA_RGB_DEFAULT_MAP,    "RGB_DEFAULT_MAP",    "default"},
  60. {0,    0,    XA_RGB_BEST_MAP,    "RGB_BEST_MAP",        "best"},
  61. {0,    0,    XA_RGB_GRAY_MAP,    "RGB_GRAY_MAP",        "gray"},
  62. {0,    0,    XA_RGB_RED_MAP,        "RGB_RED_MAP",        "red"},
  63. {0,    0,    XA_RGB_GREEN_MAP,    "RGB_GREEN_MAP",    "green"},
  64. {0,    0,    XA_RGB_BLUE_MAP,    "RGB_BLUE_MAP",        "blue"},
  65. };
  66. #define NPROPERTIES (sizeof propertyTable / sizeof propertyTable[0])
  67.  
  68. #define DEFAULT    0
  69. #define BEST    1
  70. #define GRAY    2
  71. #define RED    3
  72. #define GREEN    4
  73. #define BLUE    5
  74.  
  75. static char    *usage_message[]=
  76. {
  77. "    -all               make all standard colormaps for the display",
  78. "    -best              make the RGB_BEST_MAP",
  79. "    -blue              make the RGB_BLUE_MAP",
  80. "    -default           make the RGB_DEFAULT_MAP",
  81. "    -delete name       remove a standard colormap",
  82. "    -display dpy       X server to use",
  83. "    -gray              make the RGB_GRAY_MAP",
  84. "    -green             make the RGB_GREEN_MAP",
  85. "    -red               make the RGB_RED_MAP",
  86. "    -verbose           turn on logging",
  87. "",
  88. NULL };
  89.  
  90. static XrmOptionDescRec optionTable[]=
  91. {
  92. {"-all",    ".all",        XrmoptionNoArg,        (caddr_t) "on"},
  93. {"-best",    ".best",    XrmoptionNoArg,        (caddr_t) "on"},
  94. {"-blue",    ".blue",    XrmoptionNoArg,        (caddr_t) "on"},
  95. {"-default",    ".default",    XrmoptionNoArg,        (caddr_t) "on"},
  96. {"-delete",    ".delete",    XrmoptionSepArg,    (caddr_t) NULL},
  97. {"-display",    ".display",     XrmoptionSepArg,    (caddr_t) NULL},
  98. {"-gray",    ".gray",    XrmoptionNoArg,        (caddr_t) "on"},
  99. {"-green",    ".green",    XrmoptionNoArg,        (caddr_t) "on"},
  100. {"-help",    ".help",        XrmoptionNoArg,        (caddr_t) "on"},
  101. {"-red",    ".red",        XrmoptionNoArg,        (caddr_t) "on"},
  102. {"-verbose",    ".verbose",    XrmoptionNoArg,        (caddr_t) "on"},
  103. };
  104. #define NOPTIONS (sizeof optionTable / sizeof optionTable[0])
  105.  
  106. static void parse(argc, argv)
  107.     int        argc;
  108.     char    **argv;
  109. {
  110.     XrmDatabase        database = NULL;
  111.     char        *type;
  112.     XrmValue        value;
  113.     char        option[512];
  114.  
  115.     if (argc == 1)
  116.     usage(0);
  117.  
  118.     XrmInitialize();
  119.     XrmParseCommand(&database, optionTable, NOPTIONS, program_name, &argc,
  120.             argv);
  121.     if (--argc)
  122.     usage(1);
  123.  
  124.     (void) sprintf(option, "%s%s", program_name, ".all");
  125.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  126.         all++;
  127.  
  128.     (void) sprintf(option, "%s%s", program_name, ".best");
  129.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  130.         propertyTable[BEST].create++;
  131.  
  132.     (void) sprintf(option, "%s%s", program_name, ".blue");
  133.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  134.     propertyTable[BLUE].create++;
  135.  
  136.     (void) sprintf(option, "%s%s", program_name, ".default");
  137.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  138.     propertyTable[DEFAULT].create++;
  139.  
  140.     (void) sprintf(option, "%s%s", program_name, ".delete");
  141.     if (XrmGetResource(database, option, (char *) NULL, &type, &value)) {
  142.     register int i;
  143.     for (i=0; i < NPROPERTIES; i++) 
  144.         if (strncmp((char *) value.addr, propertyTable[i].nickname,
  145.             (int) value.size) == 0) {
  146.         propertyTable[i].delete++;
  147.         break;
  148.         }
  149.     }
  150.         
  151.     (void) sprintf(option, "%s%s", program_name, ".display");
  152.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  153.     display_name = value.addr;
  154.  
  155.     (void) sprintf(option, "%s%s", program_name, ".gray");
  156.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  157.     propertyTable[GRAY].create++;
  158.  
  159.     (void) sprintf(option, "%s%s", program_name, ".green");
  160.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  161.     propertyTable[GREEN].create++;
  162.  
  163.     (void) sprintf(option, "%s%s", program_name, ".help");
  164.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  165.     help++;
  166.  
  167.     (void) sprintf(option, "%s%s", program_name, ".red");
  168.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  169.     propertyTable[RED].create++;
  170.  
  171.     (void) sprintf(option, "%s%s", program_name, ".verbose");
  172.     if (XrmGetResource(database, option, (char *) NULL, &type, &value))
  173.     verbose++;
  174. }
  175.  
  176. Exit(status)
  177.     Status    status;
  178. {
  179.     if (dpy)
  180.     XCloseDisplay(dpy);
  181.     exit(status);
  182. }
  183.  
  184. usage(status)
  185.     Status        status;
  186. {
  187.     register char    **i;
  188.     (void) fprintf(stderr, "usage:  %s [-options]\n\n", program_name);
  189.     (void) fprintf(stderr, "where options include:\n");
  190.     for (i = usage_message; *i != NULL; i++)
  191.     (void) fprintf(stderr, "%s\n", *i);
  192.     Exit(status);
  193. }
  194.  
  195. /* Determine the visual of greatest depth in a given visual class.
  196.  * If no such visual exists, return NULL.  
  197.  */
  198. static XVisualInfo *getDeepestVisual(visual_class, vinfo, nvisuals)
  199.     int        visual_class;    /* specifies the desired visual class */
  200.     XVisualInfo    *vinfo;        /* specifies all visuals for a screen */
  201.     int        nvisuals;    /* specifies number of visuals in the list */
  202. {
  203.     register int    i;
  204.     unsigned int    maxdepth = 0;
  205.     XVisualInfo        *v = NULL;
  206.     
  207.     for (i=0; i < nvisuals; i++, vinfo++)
  208.     if (vinfo->class == visual_class && vinfo->depth > maxdepth)
  209.     {
  210.         maxdepth = vinfo->depth;
  211.         v = vinfo;
  212.     }
  213.     return(v);
  214. }
  215.  
  216. /* Determine the ``best'' visual of the screen for a standard colormap
  217.  * property.  Return NULL if no visual is appropriate.
  218.  */
  219. static XVisualInfo *getBestVisual(property, vinfo, nvisuals)
  220.     Atom    property;    /* specifies the standard colormap */
  221.     XVisualInfo *vinfo;        /* specifies all visuals of the screen */
  222.     int        nvisuals;    /* specifies number of visuals of screen */
  223. {    
  224.     XVisualInfo    *v1 = NULL, *v2 = NULL;
  225.  
  226.     if (vinfo == NULL)         /* unexpected: a screen with no visuals */
  227.     return v1;
  228.     v1 = getDeepestVisual(DirectColor, vinfo, nvisuals);
  229.     v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals);
  230.     if (v2 && (!v1 || (v2->colormap_size >=
  231.                ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1))))
  232.     return v2;
  233.     else if (v1)
  234.     return v1;
  235.     if (property == XA_RGB_BEST_MAP)
  236.     if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL) ||
  237.         ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) != NULL))
  238.         return v1;
  239.     if (property == XA_RGB_GRAY_MAP)
  240.     if (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL) ||
  241.         ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) != NULL))
  242.         return v1;
  243.     return v1;
  244.  
  245. }
  246.  
  247. static char *visualStringFromClass(class)
  248.     int    class;
  249. {
  250.     switch (class) {
  251.       case PseudoColor: return "PseudoColor";
  252.       case DirectColor: return "DirectColor";
  253.       case GrayScale: return "GrayScale";
  254.       case StaticColor: return "StaticColor";
  255.       case TrueColor: return "TrueColor";
  256.       case StaticGray: return "StaticGray";
  257.     }
  258.     return "unknown visual class";
  259. }
  260.  
  261. static int doIndividualColormaps()
  262. {
  263.     int            i, screen, nvisuals;
  264.     Status        status;
  265.     XVisualInfo        *vinfo = NULL, *v = NULL, template;
  266.     
  267.     screen = DefaultScreen(dpy);
  268.     template.screen = screen;
  269.     vinfo = XGetVisualInfo(dpy, VisualScreenMask, &template, &nvisuals);
  270.  
  271.     /* check for individual standard colormap requests */
  272.     for (i=0; i < NPROPERTIES; i++) {
  273.  
  274.     if (propertyTable[i].delete) {
  275.         XmuDeleteStandardColormap(dpy, screen, propertyTable[i].property);
  276.         if (verbose)
  277.         fprintf(stderr, "%s: %s was deleted or did not exist.\n",
  278.             program_name, propertyTable[i].name);
  279.     }
  280.  
  281.     if (! propertyTable[i].create)    
  282.         continue;
  283.     
  284.     /* which visual is best for this property? */
  285.     v = getBestVisual(propertyTable[i].property, vinfo, nvisuals);
  286.     if (v == NULL) {
  287.         if (verbose)
  288.         (void) fprintf(stderr,
  289.                "%s: no visual appropriate for %s on screen %d.\n",
  290.             program_name, propertyTable[i].name, screen);
  291.         continue;
  292.     }
  293.  
  294.  
  295.     if (verbose)
  296.         (void) fprintf(stderr,
  297.                "%s: making %s on a %s visual of depth %u.\n",
  298.                program_name, propertyTable[i].name,
  299.                visualStringFromClass(v->class), v->depth);
  300.     
  301.     status = XmuLookupStandardColormap(dpy, screen, v->visualid,
  302.                        v->depth,
  303.                        propertyTable[i].property,
  304.                        DO_NOT_REPLACE, RETAIN);
  305.     if (verbose)
  306.         (void) fprintf(stderr,
  307.                "%s: %s standard colormap %s.\n", program_name,
  308.                propertyTable[i].name, (status)
  309.                ? "was created or already exists"
  310.                : "cannot be defined");
  311.     if (!status)
  312.         break;
  313.     }
  314.     XFree((char *) vinfo);
  315.     return status;
  316. }
  317.  
  318. /* Bare bones standard colormap generation utility */
  319. main(argc, argv)
  320.     int        argc;
  321.     char    **argv;
  322. {
  323.     Status    status = 0;
  324.  
  325.     if (program_name = rindex(*argv, '/'))
  326.     program_name++;
  327.     else
  328.     program_name = *argv;
  329.  
  330.     parse(argc, argv);
  331.  
  332.     if ((dpy = XOpenDisplay(display_name)) == NULL) {
  333.     (void) fprintf(stderr, "%s: cannot open display \"%s\".\n",
  334.                program_name, XDisplayName(display_name));
  335.     exit(1);
  336.     }
  337.  
  338.     if (help) {
  339.     usage(0);
  340.     Exit(0);
  341.     }
  342.  
  343.     if (all) {
  344.     if (verbose)
  345.         (void) fprintf(stderr,
  346.                "%s: making all appropriate standard colormaps...",
  347.                program_name);
  348.     status = XmuAllStandardColormaps(dpy);
  349.     if (verbose)
  350.         (void) fprintf(stderr,
  351.                "\n%s!\n", (status) ? "success" : "failure");
  352.     }
  353.     else {
  354.     status = doIndividualColormaps();
  355.     if (!status && verbose)
  356.         (void) fprintf(stderr, 
  357.             "Not all new colormap definitions will be retained.\n");
  358.     }
  359.     Exit((status == 0) ? 1 : 0);
  360. }
  361.