Left Half / Right Half Maximize
[wmaker-crm.git] / wrlib / LookupCmap.c
blobe66dfd083b32aac38adc71b4d31a9ae7a481073c
1 /* $XConsortium: LookupCmap.c,v 1.10 94/04/17 20:16:11 rws Exp $ */
3 /*
5 Copyright (c) 1989 X Consortium
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:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
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.
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.
31 * Author: Donna Converse, MIT X Consortium
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xutil.h>
39 #include "StdCmap.h"
41 static Status lookup();
44 * To create a standard colormap if one does not currently exist, or
45 * replace the currently existing standard colormap, use
46 * XmuLookupStandardColormap().
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.
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.
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.
68 Status XmuLookupStandardColormap(dpy, screen, visualid, depth, property, replace, retain)
69 Display *dpy; /* specifies X server connection */
70 int screen; /* specifies screen of display */
71 VisualID visualid; /* specifies the visual type */
72 unsigned int depth; /* specifies the visual type */
73 Atom property; /* a standard colormap property */
74 Bool replace; /* specifies whether to replace */
75 Bool retain; /* specifies whether to retain */
77 Display *odpy; /* original display connection */
78 XStandardColormap *colormap;
79 XVisualInfo vinfo_template, *vinfo; /* visual */
80 long vinfo_mask;
81 unsigned long r_max, g_max, b_max; /* allocation */
82 int count;
83 Colormap cmap; /* colormap ID */
84 Status status = 0;
86 /* Match the requested visual */
88 vinfo_template.visualid = visualid;
89 vinfo_template.screen = screen;
90 vinfo_template.depth = depth;
91 vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
92 if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) == NULL)
93 return 0;
95 /* Monochrome visuals have no standard maps */
97 if (vinfo->colormap_size <= 2) {
98 XFree((char *)vinfo);
99 return 0;
102 /* If the requested property already exists on this screen, and,
103 * if the replace flag has not been set to true, return success.
104 * lookup() will remove a pre-existing map if replace is true.
107 if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL, replace) && !replace) {
108 XFree((char *)vinfo);
109 return 1;
112 /* Determine the best allocation for this property under the requested
113 * visualid and depth, and determine whether or not to use the default
114 * colormap of the screen.
117 if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
118 XFree((char *)vinfo);
119 return 0;
122 cmap = (property == XA_RGB_DEFAULT_MAP && visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
123 ? DefaultColormap(dpy, screen) : None;
125 /* If retaining resources, open a new connection to the same server */
127 if (retain) {
128 odpy = dpy;
129 if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
130 XFree((char *)vinfo);
131 return 0;
135 /* Create the standard colormap */
137 colormap = XmuStandardColormap(dpy, screen, visualid, depth, property, cmap, r_max, g_max, b_max);
139 /* Set the standard colormap property */
141 if (colormap) {
142 XGrabServer(dpy);
144 if (lookup(dpy, screen, visualid, property, colormap, replace) && !replace) {
145 /* Someone has defined the property since we last looked.
146 * Since we will not replace it, release our own resources.
147 * If this is the default map, our allocations will be freed
148 * when this connection closes.
150 if (colormap->killid == ReleaseByFreeingColormap)
151 XFreeColormap(dpy, colormap->colormap);
152 } else if (retain) {
153 XSetCloseDownMode(dpy, RetainPermanent);
155 XUngrabServer(dpy);
156 XFree((char *)colormap);
157 status = 1;
160 if (retain)
161 XCloseDisplay(dpy);
162 XFree((char *)vinfo);
163 return status;
166 /***************************************************************************/
168 /* Lookup a standard colormap property. If the property is RGB_DEFAULT_MAP,
169 * the visualid is used to determine whether the indicated standard colormap
170 * exists. If the map exists and replace is true, delete the resources used
171 * by the map and remove the property. Return true if the map exists,
172 * or did exist and was deleted; return false if the map was not found.
174 * Note that this is not the way that a Status return is normally used.
176 * If new is not NULL, new points to an XStandardColormap structure which
177 * describes a standard colormap of the specified property. It will be made
178 * a standard colormap of the screen if none already exists, or if replace
179 * is true.
182 static Status lookup(dpy, screen, visualid, property, new, replace)
183 Display *dpy; /* specifies display connection */
184 int screen; /* specifies screen number */
185 VisualID visualid; /* specifies visualid for std map */
186 Atom property; /* specifies colormap property name */
187 XStandardColormap *new; /* specifies a standard colormap */
188 Bool replace; /* specifies whether to replace */
190 register int i;
191 int count;
192 XStandardColormap *stdcmaps, *s;
193 Window win = RootWindow(dpy, screen);
195 /* The property does not already exist */
197 if (!XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
198 if (new)
199 XSetRGBColormaps(dpy, win, new, 1, property);
200 return 0;
203 /* The property exists and is not describing the RGB_DEFAULT_MAP */
205 if (property != XA_RGB_DEFAULT_MAP) {
206 if (replace) {
207 XmuDeleteStandardColormap(dpy, screen, property);
208 if (new)
209 XSetRGBColormaps(dpy, win, new, 1, property);
211 XFree((char *)stdcmaps);
212 return 1;
215 /* The property exists and is RGB_DEFAULT_MAP */
217 for (i = 0, s = stdcmaps; (i < count) && (s->visualid != visualid); i++, s++) ;
219 /* No RGB_DEFAULT_MAP property matches the given visualid */
221 if (i == count) {
222 if (new) {
223 XStandardColormap *m, *maps;
225 s = (XStandardColormap *) malloc((unsigned)((count + 1) * sizeof(XStandardColormap)));
227 for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
228 m->colormap = maps->colormap;
229 m->red_max = maps->red_max;
230 m->red_mult = maps->red_mult;
231 m->green_max = maps->green_max;
232 m->green_mult = maps->green_mult;
233 m->blue_max = maps->blue_max;
234 m->blue_mult = maps->blue_mult;
235 m->base_pixel = maps->base_pixel;
236 m->visualid = maps->visualid;
237 m->killid = maps->killid;
239 m->colormap = new->colormap;
240 m->red_max = new->red_max;
241 m->red_mult = new->red_mult;
242 m->green_max = new->green_max;
243 m->green_mult = new->green_mult;
244 m->blue_max = new->blue_max;
245 m->blue_mult = new->blue_mult;
246 m->base_pixel = new->base_pixel;
247 m->visualid = new->visualid;
248 m->killid = new->killid;
250 XSetRGBColormaps(dpy, win, s, ++count, property);
251 free((char *)s);
253 XFree((char *)stdcmaps);
254 return 0;
257 /* Found an RGB_DEFAULT_MAP property with a matching visualid */
259 if (replace) {
260 /* Free old resources first - we may need them, particularly in
261 * the default colormap of the screen. However, because of this,
262 * it is possible that we will destroy the old resource and fail
263 * to create a new one if XmuStandardColormap() fails.
266 if (count == 1) {
267 XmuDeleteStandardColormap(dpy, screen, property);
268 if (new)
269 XSetRGBColormaps(dpy, win, new, 1, property);
270 } else {
271 XStandardColormap *map;
273 /* s still points to the matching standard colormap */
275 if (s->killid == ReleaseByFreeingColormap) {
276 if ((s->colormap != None) && (s->colormap != DefaultColormap(dpy, screen)))
277 XFreeColormap(dpy, s->colormap);
278 } else if (s->killid != None)
279 XKillClient(dpy, s->killid);
281 map = (new) ? new : stdcmaps + --count;
283 s->colormap = map->colormap;
284 s->red_max = map->red_max;
285 s->red_mult = map->red_mult;
286 s->green_max = map->green_max;
287 s->green_mult = map->green_mult;
288 s->blue_max = map->blue_max;
289 s->blue_mult = map->blue_mult;
290 s->visualid = map->visualid;
291 s->killid = map->killid;
293 XSetRGBColormaps(dpy, win, stdcmaps, count, property);
296 XFree((char *)stdcmaps);
297 return 1;