18440cca4Smrg/*
28440cca4Smrg * $Xorg: xstdcmap.c,v 1.5 2001/02/09 02:06:01 xorgcvs Exp $
38440cca4Smrg *
48440cca4Smrg *
58440cca4SmrgCopyright 1989, 1998  The Open Group
68440cca4Smrg
78440cca4SmrgPermission to use, copy, modify, distribute, and sell this software and its
88440cca4Smrgdocumentation for any purpose is hereby granted without fee, provided that
98440cca4Smrgthe above copyright notice appear in all copies and that both that
108440cca4Smrgcopyright notice and this permission notice appear in supporting
118440cca4Smrgdocumentation.
128440cca4Smrg
138440cca4SmrgThe above copyright notice and this permission notice shall be included in
148440cca4Smrgall copies or substantial portions of the Software.
158440cca4Smrg
168440cca4SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178440cca4SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188440cca4SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
198440cca4SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
208440cca4SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
218440cca4SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
228440cca4Smrg
238440cca4SmrgExcept as contained in this notice, the name of The Open Group shall not be
248440cca4Smrgused in advertising or otherwise to promote the sale, use or other dealings
258440cca4Smrgin this Software without prior written authorization from The Open Group.
268440cca4Smrg * *
278440cca4Smrg * Author:  Donna Converse, MIT X Consortium
288440cca4Smrg */
298440cca4Smrg/* $XFree86: xc/programs/xstdcmap/xstdcmap.c,v 1.8tsi Exp $ */
308440cca4Smrg
3120915927Smrg#ifdef HAVE_CONFIG_H
3220915927Smrg# include "config.h"
3320915927Smrg#endif
3420915927Smrg
358440cca4Smrg#include <stdio.h>
368440cca4Smrg#include <stdlib.h>
378440cca4Smrg#include <X11/Xos.h>
388440cca4Smrg#include <X11/Xlib.h>
398440cca4Smrg#include <X11/Xutil.h>
408440cca4Smrg#include <X11/Xresource.h>
418440cca4Smrg#include <X11/Xatom.h>
428440cca4Smrg#include <X11/Xmu/StdCmap.h>
438440cca4Smrg
448440cca4Smrg#define REPLACE		1
458440cca4Smrg#define DO_NOT_REPLACE  0
468440cca4Smrg#define RETAIN		1
478440cca4Smrg#define DO_NOT_RETAIN	0
488440cca4Smrg
498440cca4Smrgstatic char		*display_name = NULL;
508440cca4Smrgstatic char		*program_name = NULL;
518440cca4Smrgstatic Bool		all = 0;
528440cca4Smrgstatic Bool		help = 0;
538440cca4Smrgstatic Bool 		verbose = 0;
5420915927Smrgstatic Bool 		version = 0;
558440cca4Smrgstatic Display		*dpy = NULL;
568440cca4Smrg
578440cca4Smrgtypedef struct
588440cca4Smrg{
598440cca4Smrg    Bool	create;
608440cca4Smrg    Bool	delete;
618440cca4Smrg    Atom	property;
6220915927Smrg    const char	*name;
6320915927Smrg    const char	*nickname;
648440cca4Smrg} colormap_property;
658440cca4Smrg
668440cca4Smrgstatic colormap_property propertyTable[]=
678440cca4Smrg{
688440cca4Smrg{0,	0,	XA_RGB_DEFAULT_MAP,	"RGB_DEFAULT_MAP",	"default"},
698440cca4Smrg{0,	0,	XA_RGB_BEST_MAP,	"RGB_BEST_MAP",		"best"},
708440cca4Smrg{0,	0,	XA_RGB_GRAY_MAP,	"RGB_GRAY_MAP",		"gray"},
718440cca4Smrg{0,	0,	XA_RGB_RED_MAP,		"RGB_RED_MAP",		"red"},
728440cca4Smrg{0,	0,	XA_RGB_GREEN_MAP,	"RGB_GREEN_MAP",	"green"},
738440cca4Smrg{0,	0,	XA_RGB_BLUE_MAP,	"RGB_BLUE_MAP",		"blue"},
748440cca4Smrg};
758440cca4Smrg#define NPROPERTIES (sizeof propertyTable / sizeof propertyTable[0])
768440cca4Smrg
778440cca4Smrg#define DEFAULT	0
788440cca4Smrg#define BEST	1
798440cca4Smrg#define GRAY	2
808440cca4Smrg#define RED	3
818440cca4Smrg#define GREEN	4
828440cca4Smrg#define BLUE	5
838440cca4Smrg
8420915927Smrgstatic const char	*usage_message =
8520915927Smrg    "    -all               make all standard colormaps for the display\n"
8620915927Smrg    "    -best              make the RGB_BEST_MAP\n"
8720915927Smrg    "    -blue              make the RGB_BLUE_MAP\n"
8820915927Smrg    "    -default           make the RGB_DEFAULT_MAP\n"
8904449c54Smrg    "    -delete map        remove a standard colormap\n"
9020915927Smrg    "    -display dpy       X server to use\n"
9120915927Smrg    "    -gray              make the RGB_GRAY_MAP\n"
9220915927Smrg    "    -green             make the RGB_GREEN_MAP\n"
9320915927Smrg    "    -red               make the RGB_RED_MAP\n"
9420915927Smrg    "    -verbose           turn on logging\n"
9520915927Smrg    "    -version           print version info\n";
968440cca4Smrg
978440cca4Smrgstatic XrmOptionDescRec optionTable[]=
988440cca4Smrg{
998440cca4Smrg{"-all",	".all",		XrmoptionNoArg,		(caddr_t) "on"},
1008440cca4Smrg{"-best",	".best",	XrmoptionNoArg,		(caddr_t) "on"},
1018440cca4Smrg{"-blue",	".blue",	XrmoptionNoArg,		(caddr_t) "on"},
1028440cca4Smrg{"-default",	".default",	XrmoptionNoArg,		(caddr_t) "on"},
1038440cca4Smrg{"-delete",	".delete",	XrmoptionSepArg,	(caddr_t) NULL},
1048440cca4Smrg{"-display",	".display", 	XrmoptionSepArg,	(caddr_t) NULL},
1058440cca4Smrg{"-gray",	".gray",	XrmoptionNoArg,		(caddr_t) "on"},
1068440cca4Smrg{"-green",	".green",	XrmoptionNoArg,		(caddr_t) "on"},
1078440cca4Smrg{"-help",	".help",        XrmoptionNoArg,		(caddr_t) "on"},
1088440cca4Smrg{"-red",	".red",		XrmoptionNoArg,		(caddr_t) "on"},
1098440cca4Smrg{"-verbose",	".verbose",	XrmoptionNoArg,		(caddr_t) "on"},
11020915927Smrg{"-version",	".version",	XrmoptionNoArg,		(caddr_t) "on"},
1118440cca4Smrg};
1128440cca4Smrg#define NOPTIONS (sizeof optionTable / sizeof optionTable[0])
1138440cca4Smrg
1148440cca4Smrgstatic void usage(Status status);
1158440cca4Smrg
1168440cca4Smrgstatic void
1178440cca4Smrgparse(int argc, char **argv)
1188440cca4Smrg{
1198440cca4Smrg    XrmDatabase		database = NULL;
1208440cca4Smrg    char		*type;
1218440cca4Smrg    XrmValue		value;
1228440cca4Smrg    char		option[512];
1238440cca4Smrg
1248440cca4Smrg    if (argc == 1)
1258440cca4Smrg	usage(0);
1268440cca4Smrg
1278440cca4Smrg    XrmInitialize();
1288440cca4Smrg    XrmParseCommand(&database, optionTable, NOPTIONS, program_name, &argc,
1298440cca4Smrg		    argv);
1308440cca4Smrg    if (--argc)
1318440cca4Smrg	usage(1);
1328440cca4Smrg
13320915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".all");
1348440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1358440cca4Smrg    	all++;
1368440cca4Smrg
13720915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".best");
1388440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1398440cca4Smrg    	propertyTable[BEST].create++;
1408440cca4Smrg
14120915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".blue");
1428440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1438440cca4Smrg	propertyTable[BLUE].create++;
1448440cca4Smrg
14520915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".default");
1468440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1478440cca4Smrg	propertyTable[DEFAULT].create++;
1488440cca4Smrg
14920915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".delete");
1508440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value)) {
15194c9ab7bSmrg	for (unsigned int i = 0; i < NPROPERTIES; i++) {
1528440cca4Smrg	    if (strcmp((char *) value.addr, propertyTable[i].nickname) == 0) {
1538440cca4Smrg		propertyTable[i].delete++;
1548440cca4Smrg		break;
1558440cca4Smrg	    }
1568440cca4Smrg	    if (strcmp((char *) value.addr, "all") == 0)
1578440cca4Smrg		propertyTable[i].delete++;
1588440cca4Smrg	}
1598440cca4Smrg    }
1608440cca4Smrg
16120915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".display");
1628440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1638440cca4Smrg	display_name = value.addr;
1648440cca4Smrg
16520915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".gray");
1668440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1678440cca4Smrg	propertyTable[GRAY].create++;
1688440cca4Smrg
16920915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".green");
1708440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1718440cca4Smrg	propertyTable[GREEN].create++;
1728440cca4Smrg
17320915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".help");
1748440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1758440cca4Smrg	help++;
1768440cca4Smrg
17720915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".red");
1788440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1798440cca4Smrg	propertyTable[RED].create++;
1808440cca4Smrg
18120915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".verbose");
1828440cca4Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
1838440cca4Smrg	verbose++;
18420915927Smrg
18520915927Smrg    snprintf(option, sizeof(option), "%s%s", program_name, ".version");
18620915927Smrg    if (XrmGetResource(database, option, (char *) NULL, &type, &value))
18720915927Smrg	version++;
1888440cca4Smrg}
1898440cca4Smrg
19020915927Smrgstatic void _X_NORETURN
1918440cca4SmrgExit(Status status)
1928440cca4Smrg{
1938440cca4Smrg    if (dpy)
1948440cca4Smrg	XCloseDisplay(dpy);
1958440cca4Smrg    exit(status);
1968440cca4Smrg}
1978440cca4Smrg
19820915927Smrgstatic void  _X_NORETURN
1998440cca4Smrgusage(Status status)
2008440cca4Smrg{
2018440cca4Smrg    (void) fprintf(stderr, "usage:  %s [-options]\n\n", program_name);
20220915927Smrg    (void) fprintf(stderr, "where options include:\n%s\n", usage_message);
2038440cca4Smrg    Exit(status);
2048440cca4Smrg}
2058440cca4Smrg
2068440cca4Smrg/* Determine the visual of greatest depth in a given visual class.
2078440cca4Smrg * If no such visual exists, return NULL.
2088440cca4Smrg */
2098440cca4Smrgstatic XVisualInfo *
2108440cca4SmrggetDeepestVisual(int visual_class,   /* specifies the desired visual class */
2118440cca4Smrg		 XVisualInfo *vinfo, /* specifies all visuals for a screen */
2128440cca4Smrg		 int nvisuals)	/* specifies number of visuals in the list */
2138440cca4Smrg{
21404449c54Smrg    int			maxdepth = 0;
2158440cca4Smrg    XVisualInfo		*v = NULL;
2168440cca4Smrg
21794c9ab7bSmrg    for (int i = 0; i < nvisuals; i++, vinfo++) {
2188440cca4Smrg	if (vinfo->class == visual_class && vinfo->depth > maxdepth)
2198440cca4Smrg	{
2208440cca4Smrg	    maxdepth = vinfo->depth;
2218440cca4Smrg	    v = vinfo;
2228440cca4Smrg	}
22394c9ab7bSmrg    }
2248440cca4Smrg    return(v);
2258440cca4Smrg}
2268440cca4Smrg
2278440cca4Smrg/* Determine the ``best'' visual of the screen for a standard colormap
2288440cca4Smrg * property.  Return NULL if no visual is appropriate.
2298440cca4Smrg */
2308440cca4Smrgstatic XVisualInfo *
2318440cca4SmrggetBestVisual(Atom property,	/* specifies the standard colormap */
2328440cca4Smrg	      XVisualInfo *vinfo, /* specifies all visuals of the screen */
2338440cca4Smrg	      int nvisuals)	/* specifies number of visuals of screen */
2348440cca4Smrg{
23594c9ab7bSmrg    XVisualInfo	*v1, *v2;
2368440cca4Smrg
2378440cca4Smrg    if (vinfo == NULL)		 /* unexpected: a screen with no visuals */
23894c9ab7bSmrg	return NULL;
2398440cca4Smrg    v1 = getDeepestVisual(DirectColor, vinfo, nvisuals);
2408440cca4Smrg    v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals);
2418440cca4Smrg    if (v2 && (!v1 || (v2->colormap_size >=
2428440cca4Smrg		       ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1))))
2438440cca4Smrg	return v2;
2448440cca4Smrg    else if (v1)
2458440cca4Smrg	return v1;
2468440cca4Smrg    if (property == XA_RGB_BEST_MAP)
2478440cca4Smrg	if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL) ||
2488440cca4Smrg	    ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) != NULL))
2498440cca4Smrg	    return v1;
2508440cca4Smrg    if (property == XA_RGB_GRAY_MAP)
2518440cca4Smrg	if (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL) ||
2528440cca4Smrg	    ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) != NULL))
2538440cca4Smrg	    return v1;
2548440cca4Smrg    if (property == XA_RGB_DEFAULT_MAP)
2558440cca4Smrg	for (v1 = vinfo; v1->visual != DefaultVisual(dpy, v1->screen); v1++)
2568440cca4Smrg	    ;
2578440cca4Smrg    return v1;
2588440cca4Smrg
2598440cca4Smrg}
2608440cca4Smrg
26120915927Smrgstatic const char *
2628440cca4SmrgvisualStringFromClass(int class)
2638440cca4Smrg{
2648440cca4Smrg    switch (class) {
2658440cca4Smrg      case PseudoColor: return "PseudoColor";
2668440cca4Smrg      case DirectColor: return "DirectColor";
2678440cca4Smrg      case GrayScale: return "GrayScale";
2688440cca4Smrg      case StaticColor: return "StaticColor";
2698440cca4Smrg      case TrueColor: return "TrueColor";
2708440cca4Smrg      case StaticGray: return "StaticGray";
2718440cca4Smrg    }
2728440cca4Smrg    return "unknown visual class";
2738440cca4Smrg}
2748440cca4Smrg
2758440cca4Smrgstatic int
2768440cca4SmrgdoIndividualColormaps(void)
2778440cca4Smrg{
27804449c54Smrg    int			screen, nvisuals;
2798440cca4Smrg    Status		status = -1;
28094c9ab7bSmrg    XVisualInfo		*vinfo, *v = NULL, template;
2818440cca4Smrg
2828440cca4Smrg    screen = DefaultScreen(dpy);
2838440cca4Smrg    template.screen = screen;
2848440cca4Smrg    vinfo = XGetVisualInfo(dpy, VisualScreenMask, &template, &nvisuals);
2858440cca4Smrg
2868440cca4Smrg    /* check for individual standard colormap requests */
28794c9ab7bSmrg    for (unsigned int i = 0; i < NPROPERTIES; i++) {
2888440cca4Smrg
2898440cca4Smrg	if (propertyTable[i].delete) {
2908440cca4Smrg	    XmuDeleteStandardColormap(dpy, screen, propertyTable[i].property);
2918440cca4Smrg	    if (verbose)
2928440cca4Smrg		fprintf(stderr, "%s: %s was deleted or did not exist.\n",
2938440cca4Smrg			program_name, propertyTable[i].name);
2948440cca4Smrg	}
2958440cca4Smrg
2968440cca4Smrg	if (! propertyTable[i].create)
2978440cca4Smrg	    continue;
2988440cca4Smrg
2998440cca4Smrg	/* which visual is best for this property? */
3008440cca4Smrg	v = getBestVisual(propertyTable[i].property, vinfo, nvisuals);
3018440cca4Smrg	if (v == NULL) {
3028440cca4Smrg	    if (verbose)
3038440cca4Smrg		(void) fprintf(stderr,
3048440cca4Smrg		       "%s: no visual appropriate for %s on screen %d.\n",
3058440cca4Smrg			program_name, propertyTable[i].name, screen);
3068440cca4Smrg	    continue;
3078440cca4Smrg	}
3088440cca4Smrg
3098440cca4Smrg
3108440cca4Smrg	if (verbose)
3118440cca4Smrg	    (void) fprintf(stderr,
3128440cca4Smrg			   "%s: making %s on a %s visual of depth %u.\n",
3138440cca4Smrg			   program_name, propertyTable[i].name,
3148440cca4Smrg			   visualStringFromClass(v->class), v->depth);
3158440cca4Smrg
3168440cca4Smrg	status = XmuLookupStandardColormap(dpy, screen, v->visualid,
3178440cca4Smrg					   v->depth,
3188440cca4Smrg					   propertyTable[i].property,
3198440cca4Smrg					   DO_NOT_REPLACE, RETAIN);
3208440cca4Smrg	if (verbose)
3218440cca4Smrg	    (void) fprintf(stderr,
3228440cca4Smrg			   "%s: %s standard colormap %s.\n", program_name,
3238440cca4Smrg			   propertyTable[i].name, (status)
3248440cca4Smrg			   ? "was created or already exists"
3258440cca4Smrg			   : "cannot be defined");
3268440cca4Smrg	if (!status)
3278440cca4Smrg	    break;
3288440cca4Smrg    }
32994c9ab7bSmrg    XFree(vinfo);
3308440cca4Smrg    return status;
3318440cca4Smrg}
3328440cca4Smrg
3338440cca4Smrg/* Bare bones standard colormap generation utility */
3348440cca4Smrgint
3358440cca4Smrgmain(int argc, char *argv[])
3368440cca4Smrg{
3378440cca4Smrg    Status	status = 0;
3388440cca4Smrg
3398440cca4Smrg    if ((program_name = strrchr(*argv, '/')))
3408440cca4Smrg	program_name++;
3418440cca4Smrg    else
3428440cca4Smrg	program_name = *argv;
3438440cca4Smrg
3448440cca4Smrg    parse(argc, argv);
3458440cca4Smrg
34620915927Smrg    if (help) {
34720915927Smrg	usage(0);
34820915927Smrg    }
34920915927Smrg
35020915927Smrg    if (version) {
35120915927Smrg        printf("%s\n", PACKAGE_STRING);
35220915927Smrg        exit(0);
35320915927Smrg    }
35420915927Smrg
3558440cca4Smrg    if ((dpy = XOpenDisplay(display_name)) == NULL) {
3568440cca4Smrg	(void) fprintf(stderr, "%s: cannot open display \"%s\".\n",
3578440cca4Smrg		       program_name, XDisplayName(display_name));
3588440cca4Smrg	exit(1);
3598440cca4Smrg    }
3608440cca4Smrg
3618440cca4Smrg    if (all) {
3628440cca4Smrg	if (verbose)
3638440cca4Smrg	    (void) fprintf(stderr,
3648440cca4Smrg			   "%s: making all appropriate standard colormaps...",
3658440cca4Smrg			   program_name);
3668440cca4Smrg	status = XmuAllStandardColormaps(dpy);
3678440cca4Smrg	if (verbose)
3688440cca4Smrg	    (void) fprintf(stderr,
3698440cca4Smrg			   "\n%s!\n", (status) ? "success" : "failure");
3708440cca4Smrg    }
3718440cca4Smrg    else {
3728440cca4Smrg	status = doIndividualColormaps();
3738440cca4Smrg	if (!status && verbose)
3748440cca4Smrg	    (void) fprintf(stderr,
3758440cca4Smrg		    "Not all new colormap definitions will be retained.\n");
3768440cca4Smrg    }
3778440cca4Smrg    Exit((status == 0) ? 1 : 0);
3788440cca4Smrg}
379