AllCmap.c revision 9dedec0c
1/*
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <stdio.h>
31#include <X11/Xlib.h>
32#include <X11/Xatom.h>
33#include <X11/Xutil.h>
34#include <X11/Xmu/StdCmap.h>
35
36static XVisualInfo *getDeepestVisual(int, XVisualInfo*, int);
37
38/*
39 * To create all of the appropriate standard colormaps for every visual of
40 * every screen on a given display, use XmuAllStandardColormaps.
41 *
42 * Define and retain as permanent resources all standard colormaps which are
43 * meaningful for the visuals of each screen of the display.  Return 0 on
44 * failure, non-zero on success.  If the property of any standard colormap
45 * is already defined, redefine it.
46 *
47 * This interface is intended to be used by window managers or a client
48 * upon start-up of a session.
49 *
50 * The standard colormaps of a screen are defined by properties associated
51 * with the screen's root window.  Each screen has exactly one root window.
52 * The property names of standard colormaps are predefined, and each property
53 * name may describe at most one colormap.
54 *
55 * The standard colormaps are
56 *		RGB_BEST_MAP
57 *		RGB_RED_MAP
58 *		RGB_GREEN_MAP
59 *		RGB_BLUE_MAP
60 *		RGB_DEFAULT_MAP
61 *		RGB_GRAY_MAP
62 *
63 * Therefore a screen may have at most 6 standard colormap properties defined.
64 *
65 * A standard colormap is associated with a particular visual of the screen.
66 * A screen may have multiple visuals defined, including visuals of the same
67 * class at different depths.  Note that a visual id might be repeated for
68 * more than one depth, so the visual id and the depth of a visual identify
69 * the visual.  The characteristics of the visual will determine which
70 * standard colormaps are meaningful under that visual, and will determine
71 * how the standard colormap is defined.  Because a standard colormap is
72 * associated with a specific visual, there must be a method of determining
73 * which visuals take precedence in defining standard colormaps.
74 *
75 * The method used here is: for the visual of greatest depth, define all
76 * standard colormaps meaningful to that visual class, according to this
77 * order of (descending) precedence:
78 *	1. DirectColor
79 *	2. PseudoColor
80 *	3. TrueColor and GrayScale
81 *	4. StaticColor and StaticGray
82 *
83 * Allows partial success by screenful.  For example, if a map on screen 1
84 * fails, the maps on screen 0, created earlier, will remain.  However,
85 * none on screen 1 will remain.  If a map on 0 fails, none will remain.
86 *
87 * See the comments under XmuVisualStandardColormaps() for notes on which
88 * standard colormaps are meaningful under these classes of visuals.
89 */
90
91Status
92XmuAllStandardColormaps(Display *dpy)
93{
94    int 	nvisuals, scr;
95    Status	status;
96    long	vinfo_mask;
97    XVisualInfo	template, *vinfo, *v1, *v2;
98
99    status = 0;
100    /* for each screen, determine all visuals of this server */
101    for (scr=0; scr < ScreenCount(dpy); scr++)
102    {
103	template.screen = scr;
104	vinfo_mask = VisualScreenMask;
105	vinfo = XGetVisualInfo(dpy, vinfo_mask, &template, &nvisuals);
106	if (vinfo == NULL) /* unexpected: a screen with no visuals */
107	    continue;
108
109	v1 = getDeepestVisual(DirectColor, vinfo, nvisuals);
110	v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals);
111
112	if (v2 &&
113	    (!v1 || (v2->colormap_size >= (long)
114		     ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1))))
115	    status = XmuVisualStandardColormaps(dpy, scr, v2->visualid,
116						(unsigned) v2->depth, 1, 1);
117	else if (v1)
118	    status = XmuVisualStandardColormaps(dpy, scr, v1->visualid,
119						(unsigned) v1->depth, 1, 1);
120
121	else {
122	    if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL)
123		|| ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) !=
124		NULL))
125		status = XmuVisualStandardColormaps(dpy, scr, v1->visualid,
126						   (unsigned) v1->depth, 1, 1);
127	    if (status &&
128	       (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL)
129		|| ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) !=
130		    NULL)))
131		status = XmuVisualStandardColormaps(dpy, scr, v1->visualid,
132						   (unsigned) v1->depth, 1, 1);
133	}
134	XFree ((char *) vinfo);
135	if (!status) break;
136    }
137    return status;
138}
139
140static XVisualInfo *
141getDeepestVisual(int visual_class, XVisualInfo *vinfo, int nvisuals)
142{
143    register int	i;
144    register int	maxdepth = 0;
145    XVisualInfo		*v = NULL;
146
147    for (i=0; i < nvisuals; i++, vinfo++)
148	if (vinfo->class == visual_class && vinfo->depth > maxdepth)
149	{
150	    maxdepth = vinfo->depth;
151	    v = vinfo;
152	}
153    return(v);
154}
155
156