xstdcmap.c revision 8440cca4
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 318440cca4Smrg#include <stdio.h> 328440cca4Smrg#include <stdlib.h> 338440cca4Smrg#include <X11/Xos.h> 348440cca4Smrg#include <X11/Xlib.h> 358440cca4Smrg#include <X11/Xutil.h> 368440cca4Smrg#include <X11/Xresource.h> 378440cca4Smrg#include <X11/Xatom.h> 388440cca4Smrg#include <X11/Xmu/StdCmap.h> 398440cca4Smrg 408440cca4Smrg#define REPLACE 1 418440cca4Smrg#define DO_NOT_REPLACE 0 428440cca4Smrg#define RETAIN 1 438440cca4Smrg#define DO_NOT_RETAIN 0 448440cca4Smrg 458440cca4Smrgstatic char *display_name = NULL; 468440cca4Smrgstatic char *program_name = NULL; 478440cca4Smrgstatic Bool all = 0; 488440cca4Smrgstatic Bool help = 0; 498440cca4Smrgstatic Bool verbose = 0; 508440cca4Smrgstatic Display *dpy = NULL; 518440cca4Smrg 528440cca4Smrgtypedef struct 538440cca4Smrg{ 548440cca4Smrg Bool create; 558440cca4Smrg Bool delete; 568440cca4Smrg Atom property; 578440cca4Smrg char *name; 588440cca4Smrg char *nickname; 598440cca4Smrg} colormap_property; 608440cca4Smrg 618440cca4Smrgstatic colormap_property propertyTable[]= 628440cca4Smrg{ 638440cca4Smrg{0, 0, XA_RGB_DEFAULT_MAP, "RGB_DEFAULT_MAP", "default"}, 648440cca4Smrg{0, 0, XA_RGB_BEST_MAP, "RGB_BEST_MAP", "best"}, 658440cca4Smrg{0, 0, XA_RGB_GRAY_MAP, "RGB_GRAY_MAP", "gray"}, 668440cca4Smrg{0, 0, XA_RGB_RED_MAP, "RGB_RED_MAP", "red"}, 678440cca4Smrg{0, 0, XA_RGB_GREEN_MAP, "RGB_GREEN_MAP", "green"}, 688440cca4Smrg{0, 0, XA_RGB_BLUE_MAP, "RGB_BLUE_MAP", "blue"}, 698440cca4Smrg}; 708440cca4Smrg#define NPROPERTIES (sizeof propertyTable / sizeof propertyTable[0]) 718440cca4Smrg 728440cca4Smrg#define DEFAULT 0 738440cca4Smrg#define BEST 1 748440cca4Smrg#define GRAY 2 758440cca4Smrg#define RED 3 768440cca4Smrg#define GREEN 4 778440cca4Smrg#define BLUE 5 788440cca4Smrg 798440cca4Smrgstatic char *usage_message[]= 808440cca4Smrg{ 818440cca4Smrg" -all make all standard colormaps for the display", 828440cca4Smrg" -best make the RGB_BEST_MAP", 838440cca4Smrg" -blue make the RGB_BLUE_MAP", 848440cca4Smrg" -default make the RGB_DEFAULT_MAP", 858440cca4Smrg" -delete name remove a standard colormap", 868440cca4Smrg" -display dpy X server to use", 878440cca4Smrg" -gray make the RGB_GRAY_MAP", 888440cca4Smrg" -green make the RGB_GREEN_MAP", 898440cca4Smrg" -red make the RGB_RED_MAP", 908440cca4Smrg" -verbose turn on logging", 918440cca4Smrg"", 928440cca4SmrgNULL }; 938440cca4Smrg 948440cca4Smrgstatic XrmOptionDescRec optionTable[]= 958440cca4Smrg{ 968440cca4Smrg{"-all", ".all", XrmoptionNoArg, (caddr_t) "on"}, 978440cca4Smrg{"-best", ".best", XrmoptionNoArg, (caddr_t) "on"}, 988440cca4Smrg{"-blue", ".blue", XrmoptionNoArg, (caddr_t) "on"}, 998440cca4Smrg{"-default", ".default", XrmoptionNoArg, (caddr_t) "on"}, 1008440cca4Smrg{"-delete", ".delete", XrmoptionSepArg, (caddr_t) NULL}, 1018440cca4Smrg{"-display", ".display", XrmoptionSepArg, (caddr_t) NULL}, 1028440cca4Smrg{"-gray", ".gray", XrmoptionNoArg, (caddr_t) "on"}, 1038440cca4Smrg{"-green", ".green", XrmoptionNoArg, (caddr_t) "on"}, 1048440cca4Smrg{"-help", ".help", XrmoptionNoArg, (caddr_t) "on"}, 1058440cca4Smrg{"-red", ".red", XrmoptionNoArg, (caddr_t) "on"}, 1068440cca4Smrg{"-verbose", ".verbose", XrmoptionNoArg, (caddr_t) "on"}, 1078440cca4Smrg}; 1088440cca4Smrg#define NOPTIONS (sizeof optionTable / sizeof optionTable[0]) 1098440cca4Smrg 1108440cca4Smrgstatic void usage(Status status); 1118440cca4Smrg 1128440cca4Smrgstatic void 1138440cca4Smrgparse(int argc, char **argv) 1148440cca4Smrg{ 1158440cca4Smrg XrmDatabase database = NULL; 1168440cca4Smrg char *type; 1178440cca4Smrg XrmValue value; 1188440cca4Smrg char option[512]; 1198440cca4Smrg 1208440cca4Smrg if (argc == 1) 1218440cca4Smrg usage(0); 1228440cca4Smrg 1238440cca4Smrg XrmInitialize(); 1248440cca4Smrg XrmParseCommand(&database, optionTable, NOPTIONS, program_name, &argc, 1258440cca4Smrg argv); 1268440cca4Smrg if (--argc) 1278440cca4Smrg usage(1); 1288440cca4Smrg 1298440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".all"); 1308440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1318440cca4Smrg all++; 1328440cca4Smrg 1338440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".best"); 1348440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1358440cca4Smrg propertyTable[BEST].create++; 1368440cca4Smrg 1378440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".blue"); 1388440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1398440cca4Smrg propertyTable[BLUE].create++; 1408440cca4Smrg 1418440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".default"); 1428440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1438440cca4Smrg propertyTable[DEFAULT].create++; 1448440cca4Smrg 1458440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".delete"); 1468440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) { 1478440cca4Smrg register int i; 1488440cca4Smrg for (i=0; i < NPROPERTIES; i++) { 1498440cca4Smrg if (strcmp((char *) value.addr, propertyTable[i].nickname) == 0) { 1508440cca4Smrg propertyTable[i].delete++; 1518440cca4Smrg break; 1528440cca4Smrg } 1538440cca4Smrg if (strcmp((char *) value.addr, "all") == 0) 1548440cca4Smrg propertyTable[i].delete++; 1558440cca4Smrg } 1568440cca4Smrg } 1578440cca4Smrg 1588440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".display"); 1598440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1608440cca4Smrg display_name = value.addr; 1618440cca4Smrg 1628440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".gray"); 1638440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1648440cca4Smrg propertyTable[GRAY].create++; 1658440cca4Smrg 1668440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".green"); 1678440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1688440cca4Smrg propertyTable[GREEN].create++; 1698440cca4Smrg 1708440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".help"); 1718440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1728440cca4Smrg help++; 1738440cca4Smrg 1748440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".red"); 1758440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1768440cca4Smrg propertyTable[RED].create++; 1778440cca4Smrg 1788440cca4Smrg (void) sprintf(option, "%s%s", program_name, ".verbose"); 1798440cca4Smrg if (XrmGetResource(database, option, (char *) NULL, &type, &value)) 1808440cca4Smrg verbose++; 1818440cca4Smrg} 1828440cca4Smrg 1838440cca4Smrgstatic void 1848440cca4SmrgExit(Status status) 1858440cca4Smrg{ 1868440cca4Smrg if (dpy) 1878440cca4Smrg XCloseDisplay(dpy); 1888440cca4Smrg exit(status); 1898440cca4Smrg} 1908440cca4Smrg 1918440cca4Smrgstatic void 1928440cca4Smrgusage(Status status) 1938440cca4Smrg{ 1948440cca4Smrg register char **i; 1958440cca4Smrg (void) fprintf(stderr, "usage: %s [-options]\n\n", program_name); 1968440cca4Smrg (void) fprintf(stderr, "where options include:\n"); 1978440cca4Smrg for (i = usage_message; *i != NULL; i++) 1988440cca4Smrg (void) fprintf(stderr, "%s\n", *i); 1998440cca4Smrg Exit(status); 2008440cca4Smrg} 2018440cca4Smrg 2028440cca4Smrg/* Determine the visual of greatest depth in a given visual class. 2038440cca4Smrg * If no such visual exists, return NULL. 2048440cca4Smrg */ 2058440cca4Smrgstatic XVisualInfo * 2068440cca4SmrggetDeepestVisual(int visual_class, /* specifies the desired visual class */ 2078440cca4Smrg XVisualInfo *vinfo, /* specifies all visuals for a screen */ 2088440cca4Smrg int nvisuals) /* specifies number of visuals in the list */ 2098440cca4Smrg{ 2108440cca4Smrg register int i; 2118440cca4Smrg unsigned int maxdepth = 0; 2128440cca4Smrg XVisualInfo *v = NULL; 2138440cca4Smrg 2148440cca4Smrg for (i=0; i < nvisuals; i++, vinfo++) 2158440cca4Smrg if (vinfo->class == visual_class && vinfo->depth > maxdepth) 2168440cca4Smrg { 2178440cca4Smrg maxdepth = vinfo->depth; 2188440cca4Smrg v = vinfo; 2198440cca4Smrg } 2208440cca4Smrg return(v); 2218440cca4Smrg} 2228440cca4Smrg 2238440cca4Smrg/* Determine the ``best'' visual of the screen for a standard colormap 2248440cca4Smrg * property. Return NULL if no visual is appropriate. 2258440cca4Smrg */ 2268440cca4Smrgstatic XVisualInfo * 2278440cca4SmrggetBestVisual(Atom property, /* specifies the standard colormap */ 2288440cca4Smrg XVisualInfo *vinfo, /* specifies all visuals of the screen */ 2298440cca4Smrg int nvisuals) /* specifies number of visuals of screen */ 2308440cca4Smrg{ 2318440cca4Smrg XVisualInfo *v1 = NULL, *v2 = NULL; 2328440cca4Smrg 2338440cca4Smrg if (vinfo == NULL) /* unexpected: a screen with no visuals */ 2348440cca4Smrg return v1; 2358440cca4Smrg v1 = getDeepestVisual(DirectColor, vinfo, nvisuals); 2368440cca4Smrg v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals); 2378440cca4Smrg if (v2 && (!v1 || (v2->colormap_size >= 2388440cca4Smrg ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1)))) 2398440cca4Smrg return v2; 2408440cca4Smrg else if (v1) 2418440cca4Smrg return v1; 2428440cca4Smrg if (property == XA_RGB_BEST_MAP) 2438440cca4Smrg if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL) || 2448440cca4Smrg ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) != NULL)) 2458440cca4Smrg return v1; 2468440cca4Smrg if (property == XA_RGB_GRAY_MAP) 2478440cca4Smrg if (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL) || 2488440cca4Smrg ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) != NULL)) 2498440cca4Smrg return v1; 2508440cca4Smrg if (property == XA_RGB_DEFAULT_MAP) 2518440cca4Smrg for (v1 = vinfo; v1->visual != DefaultVisual(dpy, v1->screen); v1++) 2528440cca4Smrg ; 2538440cca4Smrg return v1; 2548440cca4Smrg 2558440cca4Smrg} 2568440cca4Smrg 2578440cca4Smrgstatic char * 2588440cca4SmrgvisualStringFromClass(int class) 2598440cca4Smrg{ 2608440cca4Smrg switch (class) { 2618440cca4Smrg case PseudoColor: return "PseudoColor"; 2628440cca4Smrg case DirectColor: return "DirectColor"; 2638440cca4Smrg case GrayScale: return "GrayScale"; 2648440cca4Smrg case StaticColor: return "StaticColor"; 2658440cca4Smrg case TrueColor: return "TrueColor"; 2668440cca4Smrg case StaticGray: return "StaticGray"; 2678440cca4Smrg } 2688440cca4Smrg return "unknown visual class"; 2698440cca4Smrg} 2708440cca4Smrg 2718440cca4Smrgstatic int 2728440cca4SmrgdoIndividualColormaps(void) 2738440cca4Smrg{ 2748440cca4Smrg int i, screen, nvisuals; 2758440cca4Smrg Status status = -1; 2768440cca4Smrg XVisualInfo *vinfo = NULL, *v = NULL, template; 2778440cca4Smrg 2788440cca4Smrg screen = DefaultScreen(dpy); 2798440cca4Smrg template.screen = screen; 2808440cca4Smrg vinfo = XGetVisualInfo(dpy, VisualScreenMask, &template, &nvisuals); 2818440cca4Smrg 2828440cca4Smrg /* check for individual standard colormap requests */ 2838440cca4Smrg for (i=0; i < NPROPERTIES; i++) { 2848440cca4Smrg 2858440cca4Smrg if (propertyTable[i].delete) { 2868440cca4Smrg XmuDeleteStandardColormap(dpy, screen, propertyTable[i].property); 2878440cca4Smrg if (verbose) 2888440cca4Smrg fprintf(stderr, "%s: %s was deleted or did not exist.\n", 2898440cca4Smrg program_name, propertyTable[i].name); 2908440cca4Smrg } 2918440cca4Smrg 2928440cca4Smrg if (! propertyTable[i].create) 2938440cca4Smrg continue; 2948440cca4Smrg 2958440cca4Smrg /* which visual is best for this property? */ 2968440cca4Smrg v = getBestVisual(propertyTable[i].property, vinfo, nvisuals); 2978440cca4Smrg if (v == NULL) { 2988440cca4Smrg if (verbose) 2998440cca4Smrg (void) fprintf(stderr, 3008440cca4Smrg "%s: no visual appropriate for %s on screen %d.\n", 3018440cca4Smrg program_name, propertyTable[i].name, screen); 3028440cca4Smrg continue; 3038440cca4Smrg } 3048440cca4Smrg 3058440cca4Smrg 3068440cca4Smrg if (verbose) 3078440cca4Smrg (void) fprintf(stderr, 3088440cca4Smrg "%s: making %s on a %s visual of depth %u.\n", 3098440cca4Smrg program_name, propertyTable[i].name, 3108440cca4Smrg visualStringFromClass(v->class), v->depth); 3118440cca4Smrg 3128440cca4Smrg status = XmuLookupStandardColormap(dpy, screen, v->visualid, 3138440cca4Smrg v->depth, 3148440cca4Smrg propertyTable[i].property, 3158440cca4Smrg DO_NOT_REPLACE, RETAIN); 3168440cca4Smrg if (verbose) 3178440cca4Smrg (void) fprintf(stderr, 3188440cca4Smrg "%s: %s standard colormap %s.\n", program_name, 3198440cca4Smrg propertyTable[i].name, (status) 3208440cca4Smrg ? "was created or already exists" 3218440cca4Smrg : "cannot be defined"); 3228440cca4Smrg if (!status) 3238440cca4Smrg break; 3248440cca4Smrg } 3258440cca4Smrg XFree((char *) vinfo); 3268440cca4Smrg return status; 3278440cca4Smrg} 3288440cca4Smrg 3298440cca4Smrg/* Bare bones standard colormap generation utility */ 3308440cca4Smrgint 3318440cca4Smrgmain(int argc, char *argv[]) 3328440cca4Smrg{ 3338440cca4Smrg Status status = 0; 3348440cca4Smrg 3358440cca4Smrg if ((program_name = strrchr(*argv, '/'))) 3368440cca4Smrg program_name++; 3378440cca4Smrg else 3388440cca4Smrg program_name = *argv; 3398440cca4Smrg 3408440cca4Smrg parse(argc, argv); 3418440cca4Smrg 3428440cca4Smrg if ((dpy = XOpenDisplay(display_name)) == NULL) { 3438440cca4Smrg (void) fprintf(stderr, "%s: cannot open display \"%s\".\n", 3448440cca4Smrg program_name, XDisplayName(display_name)); 3458440cca4Smrg exit(1); 3468440cca4Smrg } 3478440cca4Smrg 3488440cca4Smrg if (help) { 3498440cca4Smrg usage(0); 3508440cca4Smrg Exit(0); 3518440cca4Smrg 3528440cca4Smrg /* Muffle gcc */ 3538440cca4Smrg return 0; 3548440cca4Smrg } 3558440cca4Smrg 3568440cca4Smrg if (all) { 3578440cca4Smrg if (verbose) 3588440cca4Smrg (void) fprintf(stderr, 3598440cca4Smrg "%s: making all appropriate standard colormaps...", 3608440cca4Smrg program_name); 3618440cca4Smrg status = XmuAllStandardColormaps(dpy); 3628440cca4Smrg if (verbose) 3638440cca4Smrg (void) fprintf(stderr, 3648440cca4Smrg "\n%s!\n", (status) ? "success" : "failure"); 3658440cca4Smrg } 3668440cca4Smrg else { 3678440cca4Smrg status = doIndividualColormaps(); 3688440cca4Smrg if (!status && verbose) 3698440cca4Smrg (void) fprintf(stderr, 3708440cca4Smrg "Not all new colormap definitions will be retained.\n"); 3718440cca4Smrg } 3728440cca4Smrg Exit((status == 0) ? 1 : 0); 3738440cca4Smrg /* Muffle compiler */ 3748440cca4Smrg return 0; 3758440cca4Smrg} 376