1 /* $XConsortium: LookupCmap.c,v 1.10 94/04/17 20:16:11 rws Exp $ */
2
3 /*
4
5 Copyright (c) 1989 X Consortium
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
27
28 */
29
30 /*
31 * Author: Donna Converse, MIT X Consortium
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xmu/StdCmap.h>
40
41 static Status lookup();
42
43 /*
44 * To create a standard colormap if one does not currently exist, or
45 * replace the currently existing standard colormap, use
46 * XmuLookupStandardColormap().
47 *
48 * Given a screen, a visual, and a property, XmuLookupStandardColormap()
49 * will determine the best allocation for the property under the specified
50 * visual, and determine the whether to create a new colormap or to use
51 * the default colormap of the screen. It will call XmuStandardColormap()
52 * to create the standard colormap.
53 *
54 * If replace is true, any previous definition of the property will be
55 * replaced. If retain is true, the property and the colormap will be
56 * made permanent for the duration of the server session. However,
57 * pre-existing property definitions which are not replaced cannot be made
58 * permanent by a call to XmuLookupStandardColormap(); a request to retain
59 * resources pertains to newly created resources.
60 *
61 * Returns 0 on failure, non-zero on success. A request to create a
62 * standard colormap upon a visual which cannot support such a map is
63 * considered a failure. An example of this would be requesting any
64 * standard colormap property on a monochrome visual, or, requesting an
65 * RGB_BEST_MAP on a display whose colormap size is 16.
66 */
67
XmuLookupStandardColormap(dpy,screen,visualid,depth,property,replace,retain)68 Status XmuLookupStandardColormap(dpy, screen, visualid, depth, property,
69 replace, retain)
70 Display *dpy; /* specifies X server connection */
71 int screen; /* specifies screen of display */
72 VisualID visualid; /* specifies the visual type */
73 unsigned int depth; /* specifies the visual type */
74 Atom property; /* a standard colormap property */
75 Bool replace; /* specifies whether to replace */
76 Bool retain; /* specifies whether to retain */
77 {
78 Display *odpy; /* original display connection */
79 XStandardColormap *colormap;
80 XVisualInfo vinfo_template, *vinfo; /* visual */
81 long vinfo_mask;
82 unsigned long r_max, g_max, b_max; /* allocation */
83 int count;
84 Colormap cmap; /* colormap ID */
85 Status status = 0;
86
87
88 /* Match the requested visual */
89
90 vinfo_template.visualid = visualid;
91 vinfo_template.screen = screen;
92 vinfo_template.depth = depth;
93 vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
94 if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) ==
95 NULL)
96 return 0;
97
98 /* Monochrome visuals have no standard maps */
99
100 if (vinfo->colormap_size <= 2) {
101 XFree((char *) vinfo);
102 return 0;
103 }
104
105 /* If the requested property already exists on this screen, and,
106 * if the replace flag has not been set to true, return success.
107 * lookup() will remove a pre-existing map if replace is true.
108 */
109
110 if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL,
111 replace) && !replace) {
112 XFree((char *) vinfo);
113 return 1;
114 }
115
116 /* Determine the best allocation for this property under the requested
117 * visualid and depth, and determine whether or not to use the default
118 * colormap of the screen.
119 */
120
121 if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
122 XFree((char *) vinfo);
123 return 0;
124 }
125
126 cmap = (property == XA_RGB_DEFAULT_MAP &&
127 visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
128 ? DefaultColormap(dpy, screen) : None;
129
130 /* If retaining resources, open a new connection to the same server */
131
132 if (retain) {
133 odpy = dpy;
134 if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
135 XFree((char *) vinfo);
136 return 0;
137 }
138 }
139
140 /* Create the standard colormap */
141
142 colormap = XmuStandardColormap(dpy, screen, visualid, depth, property,
143 cmap, r_max, g_max, b_max);
144
145 /* Set the standard colormap property */
146
147 if (colormap) {
148 XGrabServer(dpy);
149
150 if (lookup(dpy, screen, visualid, property, colormap, replace) &&
151 !replace) {
152 /* Someone has defined the property since we last looked.
153 * Since we will not replace it, release our own resources.
154 * If this is the default map, our allocations will be freed
155 * when this connection closes.
156 */
157 if (colormap->killid == ReleaseByFreeingColormap)
158 XFreeColormap(dpy, colormap->colormap);
159 }
160 else if (retain) {
161 XSetCloseDownMode(dpy, RetainPermanent);
162 }
163 XUngrabServer(dpy);
164 XFree((char *) colormap);
165 status = 1;
166 }
167
168 if (retain)
169 XCloseDisplay(dpy);
170 XFree((char *) vinfo);
171 return status;
172 }
173
174 /***************************************************************************/
175
176 /* Lookup a standard colormap property. If the property is RGB_DEFAULT_MAP,
177 * the visualid is used to determine whether the indicated standard colormap
178 * exists. If the map exists and replace is true, delete the resources used
179 * by the map and remove the property. Return true if the map exists,
180 * or did exist and was deleted; return false if the map was not found.
181 *
182 * Note that this is not the way that a Status return is normally used.
183 *
184 * If new is not NULL, new points to an XStandardColormap structure which
185 * describes a standard colormap of the specified property. It will be made
186 * a standard colormap of the screen if none already exists, or if replace
187 * is true.
188 */
189
lookup(dpy,screen,visualid,property,new,replace)190 static Status lookup(dpy, screen, visualid, property, new, replace)
191 Display *dpy; /* specifies display connection */
192 int screen; /* specifies screen number */
193 VisualID visualid; /* specifies visualid for std map */
194 Atom property; /* specifies colormap property name */
195 XStandardColormap *new; /* specifies a standard colormap */
196 Bool replace; /* specifies whether to replace */
197 {
198 register int i;
199 int count;
200 XStandardColormap *stdcmaps, *s;
201 Window win = RootWindow(dpy, screen);
202
203 /* The property does not already exist */
204
205 if (! XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
206 if (new)
207 XSetRGBColormaps(dpy, win, new, 1, property);
208 return 0;
209 }
210
211 /* The property exists and is not describing the RGB_DEFAULT_MAP */
212
213 if (property != XA_RGB_DEFAULT_MAP) {
214 if (replace) {
215 XmuDeleteStandardColormap(dpy, screen, property);
216 if (new)
217 XSetRGBColormaps(dpy, win, new, 1, property);
218 }
219 XFree((char *)stdcmaps);
220 return 1;
221 }
222
223 /* The property exists and is RGB_DEFAULT_MAP */
224
225 for (i=0, s=stdcmaps; (i < count) && (s->visualid != visualid); i++, s++)
226 ;
227
228 /* No RGB_DEFAULT_MAP property matches the given visualid */
229
230 if (i == count) {
231 if (new) {
232 XStandardColormap *m, *maps;
233
234 s = (XStandardColormap *) malloc((unsigned) ((count+1) * sizeof
235 (XStandardColormap)));
236
237 for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
238 m->colormap = maps->colormap;
239 m->red_max = maps->red_max;
240 m->red_mult = maps->red_mult;
241 m->green_max = maps->green_max;
242 m->green_mult = maps->green_mult;
243 m->blue_max = maps->blue_max;
244 m->blue_mult = maps->blue_mult;
245 m->base_pixel = maps->base_pixel;
246 m->visualid = maps->visualid;
247 m->killid = maps->killid;
248 }
249 m->colormap = new->colormap;
250 m->red_max = new->red_max;
251 m->red_mult = new->red_mult;
252 m->green_max = new->green_max;
253 m->green_mult = new->green_mult;
254 m->blue_max = new->blue_max;
255 m->blue_mult = new->blue_mult;
256 m->base_pixel = new->base_pixel;
257 m->visualid = new->visualid;
258 m->killid = new->killid;
259
260 XSetRGBColormaps(dpy, win, s, ++count, property);
261 free((char *) s);
262 }
263 XFree((char *) stdcmaps);
264 return 0;
265 }
266
267 /* Found an RGB_DEFAULT_MAP property with a matching visualid */
268
269 if (replace) {
270 /* Free old resources first - we may need them, particularly in
271 * the default colormap of the screen. However, because of this,
272 * it is possible that we will destroy the old resource and fail
273 * to create a new one if XmuStandardColormap() fails.
274 */
275
276 if (count == 1) {
277 XmuDeleteStandardColormap(dpy, screen, property);
278 if (new)
279 XSetRGBColormaps(dpy, win, new, 1, property);
280 }
281 else {
282 XStandardColormap *map;
283
284 /* s still points to the matching standard colormap */
285
286 if (s->killid == ReleaseByFreeingColormap) {
287 if ((s->colormap != None) &&
288 (s->colormap != DefaultColormap(dpy, screen)))
289 XFreeColormap(dpy, s->colormap);
290 }
291 else if (s->killid != None)
292 XKillClient(dpy, s->killid);
293
294 map = (new) ? new : stdcmaps + --count;
295
296 s->colormap = map->colormap;
297 s->red_max = map->red_max;
298 s->red_mult = map->red_mult;
299 s->green_max = map->green_max;
300 s->green_mult = map->green_mult;
301 s->blue_max = map->blue_max;
302 s->blue_mult = map->blue_mult;
303 s->visualid = map->visualid;
304 s->killid = map->killid;
305
306 XSetRGBColormaps(dpy, win, stdcmaps, count, property);
307 }
308 }
309 XFree((char *) stdcmaps);
310 return 1;
311 }
312