comparison src/video/Xext/XmuStdCmap/LookupCmap.c @ 2185:2032348afed1

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