3 Copyright 1989, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
28 * Author: Donna Converse, MIT X Consortium
36 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
38 #include <X11/Xmu/StdCmap.h>
44 static Status lookup(Display*, int, VisualID, Atom, XStandardColormap*, Bool);
47 * To create a standard colormap if one does not currently exist, or
48 * replace the currently existing standard colormap, use
49 * XmuLookupStandardColormap().
51 * Given a screen, a visual, and a property, XmuLookupStandardColormap()
52 * will determine the best allocation for the property under the specified
53 * visual, and determine the whether to create a new colormap or to use
54 * the default colormap of the screen. It will call XmuStandardColormap()
55 * to create the standard colormap.
57 * If replace is true, any previous definition of the property will be
58 * replaced. If retain is true, the property and the colormap will be
59 * made permanent for the duration of the server session. However,
60 * pre-existing property definitions which are not replaced cannot be made
61 * permanent by a call to XmuLookupStandardColormap(); a request to retain
62 * resources pertains to newly created resources.
64 * Returns 0 on failure, non-zero on success. A request to create a
65 * standard colormap upon a visual which cannot support such a map is
66 * considered a failure. An example of this would be requesting any
67 * standard colormap property on a monochrome visual, or, requesting an
68 * RGB_BEST_MAP on a display whose colormap size is 16.
72 XmuLookupStandardColormap(Display *dpy, int screen, VisualID visualid,
73 unsigned int depth, Atom property,
74 Bool replace, Bool retain)
76 * dpy - specifies X server connection
77 * screen - specifies screen of display
78 * visualid - specifies the visual type
79 * depth - specifies the visual type
80 * property - a standard colormap property
81 * replace - specifies whether to replace
82 * retain - specifies whether to retain
85 Display *odpy; /* original display connection */
86 XStandardColormap *colormap;
87 XVisualInfo vinfo_template, *vinfo; /* visual */
89 unsigned long r_max, g_max, b_max; /* allocation */
91 Colormap cmap; /* colormap ID */
95 /* Match the requested visual */
97 vinfo_template.visualid = visualid;
98 vinfo_template.screen = screen;
99 vinfo_template.depth = depth;
100 vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
101 if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) ==
105 /* Monochrome visuals have no standard maps */
107 if (vinfo->colormap_size <= 2) {
108 XFree((char *) vinfo);
112 /* If the requested property already exists on this screen, and,
113 * if the replace flag has not been set to true, return success.
114 * lookup() will remove a pre-existing map if replace is true.
117 if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL,
118 replace) && !replace) {
119 XFree((char *) vinfo);
123 /* Determine the best allocation for this property under the requested
124 * visualid and depth, and determine whether or not to use the default
125 * colormap of the screen.
128 if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
129 XFree((char *) vinfo);
133 cmap = (property == XA_RGB_DEFAULT_MAP &&
134 visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
135 ? DefaultColormap(dpy, screen) : None;
137 /* If retaining resources, open a new connection to the same server */
141 if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
142 XFree((char *) vinfo);
147 /* Create the standard colormap */
149 colormap = XmuStandardColormap(dpy, screen, visualid, depth, property,
150 cmap, r_max, g_max, b_max);
152 /* Set the standard colormap property */
157 if (lookup(dpy, screen, visualid, property, colormap, replace) &&
159 /* Someone has defined the property since we last looked.
160 * Since we will not replace it, release our own resources.
161 * If this is the default map, our allocations will be freed
162 * when this connection closes.
164 if (colormap->killid == ReleaseByFreeingColormap)
165 XFreeColormap(dpy, colormap->colormap);
168 XSetCloseDownMode(dpy, RetainPermanent);
171 XFree((char *) colormap);
177 XFree((char *) vinfo);
181 /***************************************************************************/
183 /* Lookup a standard colormap property. If the property is RGB_DEFAULT_MAP,
184 * the visualid is used to determine whether the indicated standard colormap
185 * exists. If the map exists and replace is true, delete the resources used
186 * by the map and remove the property. Return true if the map exists,
187 * or did exist and was deleted; return false if the map was not found.
189 * Note that this is not the way that a Status return is normally used.
191 * If new is not NULL, new points to an XStandardColormap structure which
192 * describes a standard colormap of the specified property. It will be made
193 * a standard colormap of the screen if none already exists, or if replace
198 lookup(Display *dpy, int screen, VisualID visualid, Atom property,
199 XStandardColormap *cnew, Bool replace)
201 * dpy - specifies display connection
202 * screen - specifies screen number
203 * visualid - specifies visualid for std map
204 * property - specifies colormap property name
205 * cnew - specifies a standard colormap
206 * replace - specifies whether to replace
211 XStandardColormap *stdcmaps, *s;
212 Window win = RootWindow(dpy, screen);
214 /* The property does not already exist */
216 if (! XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
218 XSetRGBColormaps(dpy, win, cnew, 1, property);
222 /* The property exists and is not describing the RGB_DEFAULT_MAP */
224 if (property != XA_RGB_DEFAULT_MAP) {
226 XmuDeleteStandardColormap(dpy, screen, property);
228 XSetRGBColormaps(dpy, win, cnew, 1, property);
230 XFree((char *)stdcmaps);
234 /* The property exists and is RGB_DEFAULT_MAP */
236 for (i=0, s=stdcmaps; (i < count) && (s->visualid != visualid); i++, s++)
239 /* No RGB_DEFAULT_MAP property matches the given visualid */
243 XStandardColormap *m, *maps;
245 s = (XStandardColormap *) malloc((unsigned) ((count+1) * sizeof
246 (XStandardColormap)));
248 for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
249 m->colormap = maps->colormap;
250 m->red_max = maps->red_max;
251 m->red_mult = maps->red_mult;
252 m->green_max = maps->green_max;
253 m->green_mult = maps->green_mult;
254 m->blue_max = maps->blue_max;
255 m->blue_mult = maps->blue_mult;
256 m->base_pixel = maps->base_pixel;
257 m->visualid = maps->visualid;
258 m->killid = maps->killid;
260 m->colormap = cnew->colormap;
261 m->red_max = cnew->red_max;
262 m->red_mult = cnew->red_mult;
263 m->green_max = cnew->green_max;
264 m->green_mult = cnew->green_mult;
265 m->blue_max = cnew->blue_max;
266 m->blue_mult = cnew->blue_mult;
267 m->base_pixel = cnew->base_pixel;
268 m->visualid = cnew->visualid;
269 m->killid = cnew->killid;
271 XSetRGBColormaps(dpy, win, s, ++count, property);
274 XFree((char *) stdcmaps);
278 /* Found an RGB_DEFAULT_MAP property with a matching visualid */
281 /* Free old resources first - we may need them, particularly in
282 * the default colormap of the screen. However, because of this,
283 * it is possible that we will destroy the old resource and fail
284 * to create a new one if XmuStandardColormap() fails.
288 XmuDeleteStandardColormap(dpy, screen, property);
290 XSetRGBColormaps(dpy, win, cnew, 1, property);
293 XStandardColormap *map;
295 /* s still points to the matching standard colormap */
297 if (s->killid == ReleaseByFreeingColormap) {
298 if ((s->colormap != None) &&
299 (s->colormap != DefaultColormap(dpy, screen)))
300 XFreeColormap(dpy, s->colormap);
302 else if (s->killid != None)
303 XKillClient(dpy, s->killid);
305 map = (cnew) ? cnew : stdcmaps + --count;
307 s->colormap = map->colormap;
308 s->red_max = map->red_max;
309 s->red_mult = map->red_mult;
310 s->green_max = map->green_max;
311 s->green_mult = map->green_mult;
312 s->blue_max = map->blue_max;
313 s->blue_mult = map->blue_mult;
314 s->visualid = map->visualid;
315 s->killid = map->killid;
317 XSetRGBColormaps(dpy, win, stdcmaps, count, property);
320 XFree((char *) stdcmaps);