xstdcmap.c revision 04449c54
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)) { 15104449c54Smrg unsigned int i; 1528440cca4Smrg for (i=0; i < NPROPERTIES; i++) { 1538440cca4Smrg if (strcmp((char *) value.addr, propertyTable[i].nickname) == 0) { 1548440cca4Smrg propertyTable[i].delete++; 1558440cca4Smrg break; 1568440cca4Smrg } 1578440cca4Smrg if (strcmp((char *) value.addr, "all") == 0) 1588440cca4Smrg propertyTable[i].delete++; 1598440cca4Smrg } 1608440cca4Smrg } 1618440cca4Smrg 16220915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".display"); 1638440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1648440cca4Smrg display_name = value.addr; 1658440cca4Smrg 16620915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".gray"); 1678440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1688440cca4Smrg propertyTable[GRAY].create++; 1698440cca4Smrg 17020915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".green"); 1718440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1728440cca4Smrg propertyTable[GREEN].create++; 1738440cca4Smrg 17420915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".help"); 1758440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1768440cca4Smrg help++; 1778440cca4Smrg 17820915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".red"); 1798440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1808440cca4Smrg propertyTable[RED].create++; 1818440cca4Smrg 18220915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".verbose"); 1838440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1848440cca4Smrg verbose++; 18520915927Smrg 18620915927Smrg snprintf(option, sizeof(option), "%s%s", program_name, ".version"); 18720915927Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 18820915927Smrg version++; 1898440cca4Smrg} 1908440cca4Smrg 19120915927Smrgstatic void _X_NORETURN 1928440cca4SmrgExit(Status status) 1938440cca4Smrg{ 1948440cca4Smrg if (dpy) 1958440cca4Smrg XCloseDisplay(dpy); 1968440cca4Smrg exit(status); 1978440cca4Smrg} 1988440cca4Smrg 19920915927Smrgstatic void _X_NORETURN 2008440cca4Smrgusage(Status status) 2018440cca4Smrg{ 2028440cca4Smrg (void) fprintf(stderr, "usage: %s [-options]\n\n", program_name); 20320915927Smrg (void) fprintf(stderr, "where options include:\n%s\n", usage_message); 2048440cca4Smrg Exit(status); 2058440cca4Smrg} 2068440cca4Smrg 2078440cca4Smrg/* Determine the visual of greatest depth in a given visual class. 2088440cca4Smrg * If no such visual exists, return NULL. 2098440cca4Smrg */ 2108440cca4Smrgstatic XVisualInfo * 2118440cca4SmrggetDeepestVisual(int visual_class, /* specifies the desired visual class */ 2128440cca4Smrg XVisualInfo *vinfo, /* specifies all visuals for a screen */ 2138440cca4Smrg int nvisuals) /* specifies number of visuals in the list */ 2148440cca4Smrg{ 2158440cca4Smrg register int i; 21604449c54Smrg int maxdepth = 0; 2178440cca4Smrg XVisualInfo *v = NULL; 2188440cca4Smrg 2198440cca4Smrg for (i=0; i < nvisuals; i++, vinfo++) 2208440cca4Smrg if (vinfo->class == visual_class && vinfo->depth > maxdepth) 2218440cca4Smrg { 2228440cca4Smrg maxdepth = vinfo->depth; 2238440cca4Smrg v = vinfo; 2248440cca4Smrg } 2258440cca4Smrg return(v); 2268440cca4Smrg} 2278440cca4Smrg 2288440cca4Smrg/* Determine the ``best'' visual of the screen for a standard colormap 2298440cca4Smrg * property. Return NULL if no visual is appropriate. 2308440cca4Smrg */ 2318440cca4Smrgstatic XVisualInfo * 2328440cca4SmrggetBestVisual(Atom property, /* specifies the standard colormap */ 2338440cca4Smrg XVisualInfo *vinfo, /* specifies all visuals of the screen */ 2348440cca4Smrg int nvisuals) /* specifies number of visuals of screen */ 2358440cca4Smrg{ 2368440cca4Smrg XVisualInfo *v1 = NULL, *v2 = NULL; 2378440cca4Smrg 2388440cca4Smrg if (vinfo == NULL) /* unexpected: a screen with no visuals */ 2398440cca4Smrg return v1; 2408440cca4Smrg v1 = getDeepestVisual(DirectColor, vinfo, nvisuals); 2418440cca4Smrg v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals); 2428440cca4Smrg if (v2 && (!v1 || (v2->colormap_size >= 2438440cca4Smrg ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1)))) 2448440cca4Smrg return v2; 2458440cca4Smrg else if (v1) 2468440cca4Smrg return v1; 2478440cca4Smrg if (property == XA_RGB_BEST_MAP) 2488440cca4Smrg if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL) || 2498440cca4Smrg ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) != NULL)) 2508440cca4Smrg return v1; 2518440cca4Smrg if (property == XA_RGB_GRAY_MAP) 2528440cca4Smrg if (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL) || 2538440cca4Smrg ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) != NULL)) 2548440cca4Smrg return v1; 2558440cca4Smrg if (property == XA_RGB_DEFAULT_MAP) 2568440cca4Smrg for (v1 = vinfo; v1->visual != DefaultVisual(dpy, v1->screen); v1++) 2578440cca4Smrg ; 2588440cca4Smrg return v1; 2598440cca4Smrg 2608440cca4Smrg} 2618440cca4Smrg 26220915927Smrgstatic const char * 2638440cca4SmrgvisualStringFromClass(int class) 2648440cca4Smrg{ 2658440cca4Smrg switch (class) { 2668440cca4Smrg case PseudoColor: return "PseudoColor"; 2678440cca4Smrg case DirectColor: return "DirectColor"; 2688440cca4Smrg case GrayScale: return "GrayScale"; 2698440cca4Smrg case StaticColor: return "StaticColor"; 2708440cca4Smrg case TrueColor: return "TrueColor"; 2718440cca4Smrg case StaticGray: return "StaticGray"; 2728440cca4Smrg } 2738440cca4Smrg return "unknown visual class"; 2748440cca4Smrg} 2758440cca4Smrg 2768440cca4Smrgstatic int 2778440cca4SmrgdoIndividualColormaps(void) 2788440cca4Smrg{ 27904449c54Smrg unsigned int i; 28004449c54Smrg int screen, nvisuals; 2818440cca4Smrg Status status = -1; 2828440cca4Smrg XVisualInfo *vinfo = NULL, *v = NULL, template; 2838440cca4Smrg 2848440cca4Smrg screen = DefaultScreen(dpy); 2858440cca4Smrg template.screen = screen; 2868440cca4Smrg vinfo = XGetVisualInfo(dpy, VisualScreenMask, &template, &nvisuals); 2878440cca4Smrg 2888440cca4Smrg /* check for individual standard colormap requests */ 2898440cca4Smrg for (i=0; i < NPROPERTIES; i++) { 2908440cca4Smrg 2918440cca4Smrg if (propertyTable[i].delete) { 2928440cca4Smrg XmuDeleteStandardColormap(dpy, screen, propertyTable[i].property); 2938440cca4Smrg if (verbose) 2948440cca4Smrg fprintf(stderr, "%s: %s was deleted or did not exist.\n", 2958440cca4Smrg program_name, propertyTable[i].name); 2968440cca4Smrg } 2978440cca4Smrg 2988440cca4Smrg if (! propertyTable[i].create) 2998440cca4Smrg continue; 3008440cca4Smrg 3018440cca4Smrg /* which visual is best for this property? */ 3028440cca4Smrg v = getBestVisual(propertyTable[i].property, vinfo, nvisuals); 3038440cca4Smrg if (v == NULL) { 3048440cca4Smrg if (verbose) 3058440cca4Smrg (void) fprintf(stderr, 3068440cca4Smrg "%s: no visual appropriate for %s on screen %d.\n", 3078440cca4Smrg program_name, propertyTable[i].name, screen); 3088440cca4Smrg continue; 3098440cca4Smrg } 3108440cca4Smrg 3118440cca4Smrg 3128440cca4Smrg if (verbose) 3138440cca4Smrg (void) fprintf(stderr, 3148440cca4Smrg "%s: making %s on a %s visual of depth %u.\n", 3158440cca4Smrg program_name, propertyTable[i].name, 3168440cca4Smrg visualStringFromClass(v->class), v->depth); 3178440cca4Smrg 3188440cca4Smrg status = XmuLookupStandardColormap(dpy, screen, v->visualid, 3198440cca4Smrg v->depth, 3208440cca4Smrg propertyTable[i].property, 3218440cca4Smrg DO_NOT_REPLACE, RETAIN); 3228440cca4Smrg if (verbose) 3238440cca4Smrg (void) fprintf(stderr, 3248440cca4Smrg "%s: %s standard colormap %s.\n", program_name, 3258440cca4Smrg propertyTable[i].name, (status) 3268440cca4Smrg ? "was created or already exists" 3278440cca4Smrg : "cannot be defined"); 3288440cca4Smrg if (!status) 3298440cca4Smrg break; 3308440cca4Smrg } 3318440cca4Smrg XFree((char *) vinfo); 3328440cca4Smrg return status; 3338440cca4Smrg} 3348440cca4Smrg 3358440cca4Smrg/* Bare bones standard colormap generation utility */ 3368440cca4Smrgint 3378440cca4Smrgmain(int argc, char *argv[]) 3388440cca4Smrg{ 3398440cca4Smrg Status status = 0; 3408440cca4Smrg 3418440cca4Smrg if ((program_name = strrchr(*argv, '/'))) 3428440cca4Smrg program_name++; 3438440cca4Smrg else 3448440cca4Smrg program_name = *argv; 3458440cca4Smrg 3468440cca4Smrg parse(argc, argv); 3478440cca4Smrg 34820915927Smrg if (help) { 34920915927Smrg usage(0); 35020915927Smrg } 35120915927Smrg 35220915927Smrg if (version) { 35320915927Smrg printf("%s\n", PACKAGE_STRING); 35420915927Smrg exit(0); 35520915927Smrg } 35620915927Smrg 3578440cca4Smrg if ((dpy = XOpenDisplay(display_name)) == NULL) { 3588440cca4Smrg (void) fprintf(stderr, "%s: cannot open display \"%s\".\n", 3598440cca4Smrg program_name, XDisplayName(display_name)); 3608440cca4Smrg exit(1); 3618440cca4Smrg } 3628440cca4Smrg 3638440cca4Smrg if (all) { 3648440cca4Smrg if (verbose) 3658440cca4Smrg (void) fprintf(stderr, 3668440cca4Smrg "%s: making all appropriate standard colormaps...", 3678440cca4Smrg program_name); 3688440cca4Smrg status = XmuAllStandardColormaps(dpy); 3698440cca4Smrg if (verbose) 3708440cca4Smrg (void) fprintf(stderr, 3718440cca4Smrg "\n%s!\n", (status) ? "success" : "failure"); 3728440cca4Smrg } 3738440cca4Smrg else { 3748440cca4Smrg status = doIndividualColormaps(); 3758440cca4Smrg if (!status && verbose) 3768440cca4Smrg (void) fprintf(stderr, 3778440cca4Smrg "Not all new colormap definitions will be retained.\n"); 3788440cca4Smrg } 3798440cca4Smrg Exit((status == 0) ? 1 : 0); 3808440cca4Smrg} 381