comparison src/video/Xext/XmuStdCmap/StdCmap.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: StdCmap.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/StdCmap.c,v 1.5 2001/01/17 19:42:56 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
43 #define lowbit(x) ((x) & (~(x) + 1))
44
45 /*
46 * Prototypes
47 */
48 /* argument restrictions */
49 static Status valid_args(XVisualInfo *, unsigned long, unsigned long,
50 unsigned long, Atom);
51
52 /*
53 * To create any one standard colormap, use XmuStandardColormap().
54 *
55 * Create a standard colormap for the given screen, visualid, and visual
56 * depth, with the given red, green, and blue maximum values, with the
57 * given standard property name. Return a pointer to an XStandardColormap
58 * structure which describes the newly created colormap, upon success.
59 * Upon failure, return NULL.
60 *
61 * XmuStandardColormap() calls XmuCreateColormap() to create the map.
62 *
63 * Resources created by this function are not made permanent; that is the
64 * caller's responsibility.
65 */
66
67 XStandardColormap *
68 XmuStandardColormap(Display * dpy, int screen, VisualID visualid,
69 unsigned int depth, Atom property, Colormap cmap,
70 unsigned long red_max, unsigned long green_max,
71 unsigned long blue_max)
72 /*
73 * dpy - specifies X server connection
74 * screen - specifies display screen
75 * visualid - identifies the visual type
76 * depth - identifies the visual type
77 * property - a standard colormap property
78 * cmap - specifies colormap ID or None
79 * red_max, green_max, blue_max - allocations
80 */
81 {
82 XStandardColormap *stdcmap;
83 Status status;
84 XVisualInfo vinfo_template, *vinfo;
85 long vinfo_mask;
86 int n;
87
88 /* Match the required visual information to an actual visual */
89 vinfo_template.visualid = visualid;
90 vinfo_template.screen = screen;
91 vinfo_template.depth = depth;
92 vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
93 if ((vinfo =
94 XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
95 return 0;
96
97 /* Check the validity of the combination of visual characteristics,
98 * allocation, and colormap property. Create an XStandardColormap
99 * structure.
100 */
101
102 if (!valid_args(vinfo, red_max, green_max, blue_max, property)
103 || ((stdcmap = XAllocStandardColormap()) == NULL)) {
104 XFree((char *) vinfo);
105 return 0;
106 }
107
108 /* Fill in the XStandardColormap structure */
109
110 if (cmap == DefaultColormap(dpy, screen)) {
111 /* Allocating out of the default map, cannot use XFreeColormap() */
112 Window win = XCreateWindow(dpy, RootWindow(dpy, screen), 1, 1, 1, 1,
113 0, 0, InputOnly, vinfo->visual,
114 (unsigned long) 0,
115 (XSetWindowAttributes *) NULL);
116 stdcmap->killid = (XID) XCreatePixmap(dpy, win, 1, 1, depth);
117 XDestroyWindow(dpy, win);
118 stdcmap->colormap = cmap;
119 } else {
120 stdcmap->killid = ReleaseByFreeingColormap;
121 stdcmap->colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
122 vinfo->visual, AllocNone);
123 }
124 stdcmap->red_max = red_max;
125 stdcmap->green_max = green_max;
126 stdcmap->blue_max = blue_max;
127 if (property == XA_RGB_GRAY_MAP)
128 stdcmap->red_mult = stdcmap->green_mult = stdcmap->blue_mult = 1;
129 else if (vinfo->class == TrueColor || vinfo->class == DirectColor) {
130 stdcmap->red_mult = lowbit(vinfo->red_mask);
131 stdcmap->green_mult = lowbit(vinfo->green_mask);
132 stdcmap->blue_mult = lowbit(vinfo->blue_mask);
133 } else {
134 stdcmap->red_mult = (red_max > 0)
135 ? (green_max + 1) * (blue_max + 1) : 0;
136 stdcmap->green_mult = (green_max > 0) ? blue_max + 1 : 0;
137 stdcmap->blue_mult = (blue_max > 0) ? 1 : 0;
138 }
139 stdcmap->base_pixel = 0; /* base pixel may change */
140 stdcmap->visualid = vinfo->visualid;
141
142 /* Make the colormap */
143
144 status = XmuCreateColormap(dpy, stdcmap);
145
146 /* Clean up */
147
148 XFree((char *) vinfo);
149 if (!status) {
150
151 /* Free the colormap or the pixmap, if we created one */
152 if (stdcmap->killid == ReleaseByFreeingColormap)
153 XFreeColormap(dpy, stdcmap->colormap);
154 else if (stdcmap->killid != None)
155 XFreePixmap(dpy, stdcmap->killid);
156
157 XFree((char *) stdcmap);
158 return (XStandardColormap *) NULL;
159 }
160 return stdcmap;
161 }
162
163 /****************************************************************************/
164 static Status
165 valid_args(XVisualInfo * vinfo, unsigned long red_max,
166 unsigned long green_max, unsigned long blue_max, Atom property)
167 /*
168 * vinfo - specifies visual
169 * red_max, green_max, blue_max - specifies alloc
170 * property - specifies property name
171 */
172 {
173 unsigned long ncolors; /* number of colors requested */
174
175 /* Determine that the number of colors requested is <= map size */
176
177 if ((vinfo->class == DirectColor) || (vinfo->class == TrueColor)) {
178 unsigned long mask;
179
180 mask = vinfo->red_mask;
181 while (!(mask & 1))
182 mask >>= 1;
183 if (red_max > mask)
184 return 0;
185 mask = vinfo->green_mask;
186 while (!(mask & 1))
187 mask >>= 1;
188 if (green_max > mask)
189 return 0;
190 mask = vinfo->blue_mask;
191 while (!(mask & 1))
192 mask >>= 1;
193 if (blue_max > mask)
194 return 0;
195 } else if (property == XA_RGB_GRAY_MAP) {
196 ncolors = red_max + green_max + blue_max + 1;
197 if (ncolors > vinfo->colormap_size)
198 return 0;
199 } else {
200 ncolors = (red_max + 1) * (green_max + 1) * (blue_max + 1);
201 if (ncolors > vinfo->colormap_size)
202 return 0;
203 }
204
205 /* Determine that the allocation and visual make sense for the property */
206
207 switch (property) {
208 case XA_RGB_DEFAULT_MAP:
209 if (red_max == 0 || green_max == 0 || blue_max == 0)
210 return 0;
211 break;
212 case XA_RGB_RED_MAP:
213 if (red_max == 0)
214 return 0;
215 break;
216 case XA_RGB_GREEN_MAP:
217 if (green_max == 0)
218 return 0;
219 break;
220 case XA_RGB_BLUE_MAP:
221 if (blue_max == 0)
222 return 0;
223 break;
224 case XA_RGB_BEST_MAP:
225 if (red_max == 0 || green_max == 0 || blue_max == 0)
226 return 0;
227 break;
228 case XA_RGB_GRAY_MAP:
229 if (red_max == 0 || blue_max == 0 || green_max == 0)
230 return 0;
231 break;
232 default:
233 return 0;
234 }
235 return 1;
236 }