Fixed off-by-one error in compute_shine_table(). Minor code clean-up
[mesa.git] / src / glut / glx / glut_cmap.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994, 1996, 1997. */
3
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h> /* SunOS multithreaded assert() needs <stdio.h>. Lame. */
11 #include <assert.h>
12 #if !defined(_WIN32)
13 #include <X11/Xlib.h>
14 #include <X11/Xutil.h>
15 #include <X11/Xatom.h> /* for XA_RGB_DEFAULT_MAP atom */
16 #if defined(__vms)
17 #include <Xmu/StdCmap.h> /* for XmuLookupStandardColormap */
18 #else
19 #include <X11/Xmu/StdCmap.h> /* for XmuLookupStandardColormap */
20 #endif
21 #endif
22
23 /* SGI optimization introduced in IRIX 6.3 to avoid X server
24 round trips for interning common X atoms. */
25 #if defined(_SGI_EXTRA_PREDEFINES) && !defined(NO_FAST_ATOMS)
26 #include <X11/SGIFastAtom.h>
27 #else
28 #define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
29 #endif
30
31 #include "glutint.h"
32 #include "layerutil.h"
33
34 GLUTcolormap *__glutColormapList = NULL;
35
36 GLUTcolormap *
37 __glutAssociateNewColormap(XVisualInfo * vis)
38 {
39 GLUTcolormap *cmap;
40 int transparentPixel, i;
41 unsigned long pixels[255];
42
43 cmap = (GLUTcolormap *) malloc(sizeof(GLUTcolormap));
44 if (!cmap)
45 __glutFatalError("out of memory.");
46 #if defined(_WIN32)
47 pixels[0] = 0; /* avoid compilation warnings on win32 */
48 cmap->visual = 0;
49 cmap->size = 256; /* always assume 256 on Win32 */
50 #else
51 cmap->visual = vis->visual;
52 cmap->size = vis->visual->map_entries;
53 #endif
54 cmap->refcnt = 1;
55 cmap->cells = (GLUTcolorcell *)
56 malloc(sizeof(GLUTcolorcell) * cmap->size);
57 if (!cmap->cells)
58 __glutFatalError("out of memory.");
59 /* make all color cell entries be invalid */
60 for (i = cmap->size - 1; i >= 0; i--) {
61 cmap->cells[i].component[GLUT_RED] = -1.0;
62 cmap->cells[i].component[GLUT_GREEN] = -1.0;
63 cmap->cells[i].component[GLUT_BLUE] = -1.0;
64 }
65 transparentPixel = __glutGetTransparentPixel(__glutDisplay, vis);
66 if (transparentPixel == -1 || transparentPixel >= cmap->size) {
67
68 /* If there is no transparent pixel or if the transparent
69 pixel is outside the range of valid colormap cells (HP
70 can implement their overlays this smart way since their
71 transparent pixel is 255), we can AllocAll the colormap.
72 See note below. */
73
74 cmap->cmap = XCreateColormap(__glutDisplay,
75 __glutRoot, cmap->visual, AllocAll);
76 } else {
77
78 /* On machines where zero (or some other value in the range
79 of 0 through map_entries-1), BadAlloc may be generated
80 when an AllocAll overlay colormap is allocated since the
81 transparent pixel precludes all the cells in the colormap
82 being allocated (the transparent pixel is pre-allocated).
83 So in this case, use XAllocColorCells to allocate
84 map_entries-1 pixels (that is, all but the transparent
85 pixel. */
86
87 #if defined(_WIN32)
88 cmap->cmap = XCreateColormap(__glutDisplay,
89 __glutRoot, 0, AllocNone);
90 #else
91 cmap->cmap = XCreateColormap(__glutDisplay,
92 __glutRoot, vis->visual, AllocNone);
93 XAllocColorCells(__glutDisplay, cmap->cmap, False, 0, 0,
94 pixels, cmap->size - 1);
95 #endif
96 }
97 cmap->next = __glutColormapList;
98 __glutColormapList = cmap;
99 return cmap;
100 }
101
102 static GLUTcolormap *
103 associateColormap(XVisualInfo * vis)
104 {
105 #if !defined(_WIN32)
106 GLUTcolormap *cmap = __glutColormapList;
107
108 while (cmap != NULL) {
109 /* Play safe: compare visual IDs, not Visual*'s. */
110 if (cmap->visual->visualid == vis->visual->visualid) {
111 /* Already have created colormap for the visual. */
112 cmap->refcnt++;
113 return cmap;
114 }
115 cmap = cmap->next;
116 }
117 #endif
118 return __glutAssociateNewColormap(vis);
119 }
120
121 void
122 __glutSetupColormap(XVisualInfo * vi, GLUTcolormap ** colormap, Colormap * cmap)
123 {
124 #if defined(_WIN32)
125 if (vi->dwFlags & PFD_NEED_PALETTE || vi->iPixelType == PFD_TYPE_COLORINDEX) {
126 *colormap = associateColormap(vi);
127 *cmap = (*colormap)->cmap;
128 } else {
129 *colormap = NULL;
130 *cmap = 0;
131 }
132 #else
133 Status status;
134 XStandardColormap *standardCmaps;
135 int i, numCmaps;
136 static Atom hpColorRecoveryAtom = -1;
137 int isRGB, visualClass, rc;
138
139 #if defined(__cplusplus) || defined(c_plusplus)
140 visualClass = vi->c_class;
141 #else
142 visualClass = vi->class;
143 #endif
144 switch (visualClass) {
145 case PseudoColor:
146 /* Mesa might return a PseudoColor visual for RGB mode. */
147 rc = glXGetConfig(__glutDisplay, vi, GLX_RGBA, &isRGB);
148 if (rc == 0 && isRGB) {
149 /* Must be Mesa. */
150 *colormap = NULL;
151 if (MaxCmapsOfScreen(DefaultScreenOfDisplay(__glutDisplay)) == 1
152 && vi->visual == DefaultVisual(__glutDisplay, __glutScreen)) {
153 char *privateCmap = getenv("MESA_PRIVATE_CMAP");
154
155 if (privateCmap) {
156 /* User doesn't want to share colormaps. */
157 *cmap = XCreateColormap(__glutDisplay, __glutRoot,
158 vi->visual, AllocNone);
159 } else {
160 /* Share the root colormap. */
161 *cmap = DefaultColormap(__glutDisplay, __glutScreen);
162 }
163 } else {
164 /* Get our own PseudoColor colormap. */
165 *cmap = XCreateColormap(__glutDisplay, __glutRoot,
166 vi->visual, AllocNone);
167 }
168 } else {
169 /* CI mode, real GLX never returns a PseudoColor visual
170 for RGB mode. */
171 *colormap = associateColormap(vi);
172 *cmap = (*colormap)->cmap;
173 }
174 break;
175 case TrueColor:
176 case DirectColor:
177 *colormap = NULL; /* NULL if RGBA */
178
179 /* Hewlett-Packard supports a feature called "HP Color
180 Recovery". Mesa has code to use HP Color Recovery. For
181 Mesa to use this feature, the atom
182 _HP_RGB_SMOOTH_MAP_LIST must be defined on the root
183 window AND the colormap obtainable by XGetRGBColormaps
184 for that atom must be set on the window. If that
185 colormap is not set, the output will look stripy. */
186
187 if (hpColorRecoveryAtom == -1) {
188 char *xvendor;
189
190 #define VENDOR_HP "Hewlett-Packard"
191
192 /* Only makes sense to make XInternAtom round-trip if we
193 know that we are connected to an HP X server. */
194 xvendor = ServerVendor(__glutDisplay);
195 if (!strncmp(xvendor, VENDOR_HP, sizeof(VENDOR_HP) - 1)) {
196 hpColorRecoveryAtom = XInternAtom(__glutDisplay, "_HP_RGB_SMOOTH_MAP_LIST", True);
197 } else {
198 hpColorRecoveryAtom = None;
199 }
200 }
201 if (hpColorRecoveryAtom != None) {
202 status = XGetRGBColormaps(__glutDisplay, __glutRoot,
203 &standardCmaps, &numCmaps, hpColorRecoveryAtom);
204 if (status == 1) {
205 for (i = 0; i < numCmaps; i++) {
206 if (standardCmaps[i].visualid == vi->visualid) {
207 *cmap = standardCmaps[i].colormap;
208 XFree(standardCmaps);
209 return;
210 }
211 }
212 XFree(standardCmaps);
213 }
214 }
215 #ifndef SOLARIS_2_4_BUG
216 /* Solaris 2.4 and 2.5 have a bug in their
217 XmuLookupStandardColormap implementations. Please
218 compile your Solaris 2.4 or 2.5 version of GLUT with
219 -DSOLARIS_2_4_BUG to work around this bug. The symptom
220 of the bug is that programs will get a BadMatch error
221 from X_CreateWindow when creating a GLUT window because
222 Solaris 2.4 and 2.5 create a corrupted RGB_DEFAULT_MAP
223 property. Note that this workaround prevents Colormap
224 sharing between applications, perhaps leading
225 unnecessary colormap installations or colormap flashing.
226 Sun fixed this bug in Solaris 2.6. */
227 status = XmuLookupStandardColormap(__glutDisplay,
228 vi->screen, vi->visualid, vi->depth, XA_RGB_DEFAULT_MAP,
229 /* replace */ False, /* retain */ True);
230 if (status == 1) {
231 status = XGetRGBColormaps(__glutDisplay, __glutRoot,
232 &standardCmaps, &numCmaps, XA_RGB_DEFAULT_MAP);
233 if (status == 1) {
234 for (i = 0; i < numCmaps; i++) {
235 if (standardCmaps[i].visualid == vi->visualid) {
236 *cmap = standardCmaps[i].colormap;
237 XFree(standardCmaps);
238 return;
239 }
240 }
241 XFree(standardCmaps);
242 }
243 }
244 #endif
245 /* If no standard colormap but TrueColor, just make a
246 private one. */
247 /* XXX Should do a better job of internal sharing for
248 privately allocated TrueColor colormaps. */
249 /* XXX DirectColor probably needs ramps hand initialized! */
250 *cmap = XCreateColormap(__glutDisplay, __glutRoot,
251 vi->visual, AllocNone);
252 break;
253 case StaticColor:
254 case StaticGray:
255 case GrayScale:
256 /* Mesa supports these visuals */
257 *colormap = NULL;
258 *cmap = XCreateColormap(__glutDisplay, __glutRoot,
259 vi->visual, AllocNone);
260 break;
261 default:
262 __glutFatalError(
263 "could not allocate colormap for visual type: %d.",
264 visualClass);
265 }
266 return;
267 #endif
268 }
269
270 #if !defined(_WIN32)
271 static int
272 findColormaps(GLUTwindow * window,
273 Window * winlist, Colormap * cmaplist, int num, int max)
274 {
275 GLUTwindow *child;
276 int i;
277
278 /* Do not allow more entries that maximum number of
279 colormaps! */
280 if (num >= max)
281 return num;
282 /* Is cmap for this window already on the list? */
283 for (i = 0; i < num; i++) {
284 if (cmaplist[i] == window->cmap)
285 goto normalColormapAlreadyListed;
286 }
287 /* Not found on the list; add colormap and window. */
288 winlist[num] = window->win;
289 cmaplist[num] = window->cmap;
290 num++;
291
292 normalColormapAlreadyListed:
293
294 /* Repeat above but for the overlay colormap if there one. */
295 if (window->overlay) {
296 if (num >= max)
297 return num;
298 for (i = 0; i < num; i++) {
299 if (cmaplist[i] == window->overlay->cmap)
300 goto overlayColormapAlreadyListed;
301 }
302 winlist[num] = window->overlay->win;
303 cmaplist[num] = window->overlay->cmap;
304 num++;
305 }
306 overlayColormapAlreadyListed:
307
308 /* Recursively search children. */
309 child = window->children;
310 while (child) {
311 num = findColormaps(child, winlist, cmaplist, num, max);
312 child = child->siblings;
313 }
314 return num;
315 }
316
317 void
318 __glutEstablishColormapsProperty(GLUTwindow * window)
319 {
320 /* this routine is strictly X. Win32 doesn't need to do
321 anything of this sort (but has to do other wacky stuff
322 later). */
323 static Atom wmColormapWindows = None;
324 Window *winlist;
325 Colormap *cmaplist;
326 Status status;
327 int maxcmaps, num;
328
329 assert(!window->parent);
330 maxcmaps = MaxCmapsOfScreen(ScreenOfDisplay(__glutDisplay,
331 __glutScreen));
332 /* For portability reasons we don't use alloca for winlist
333 and cmaplist, but we could. */
334 winlist = (Window *) malloc(maxcmaps * sizeof(Window));
335 cmaplist = (Colormap *) malloc(maxcmaps * sizeof(Colormap));
336 num = findColormaps(window, winlist, cmaplist, 0, maxcmaps);
337 if (num < 2) {
338 /* Property no longer needed; remove it. */
339 wmColormapWindows = XSGIFastInternAtom(__glutDisplay,
340 "WM_COLORMAP_WINDOWS", SGI_XA_WM_COLORMAP_WINDOWS, False);
341 if (wmColormapWindows == None) {
342 __glutWarning("Could not intern X atom for WM_COLORMAP_WINDOWS.");
343 return;
344 }
345 XDeleteProperty(__glutDisplay, window->win, wmColormapWindows);
346 } else {
347 status = XSetWMColormapWindows(__glutDisplay, window->win,
348 winlist, num);
349 /* XSetWMColormapWindows should always work unless the
350 WM_COLORMAP_WINDOWS property cannot be intern'ed. We
351 check to be safe. */
352 if (status == False)
353 __glutFatalError("XSetWMColormapWindows returned False.");
354 }
355 /* For portability reasons we don't use alloca for winlist
356 and cmaplist, but we could. */
357 free(winlist);
358 free(cmaplist);
359 }
360
361 GLUTwindow *
362 __glutToplevelOf(GLUTwindow * window)
363 {
364 while (window->parent) {
365 window = window->parent;
366 }
367 return window;
368 }
369 #endif
370
371 void
372 __glutFreeColormap(GLUTcolormap * cmap)
373 {
374 GLUTcolormap *cur, **prev;
375
376 cmap->refcnt--;
377 if (cmap->refcnt == 0) {
378 /* remove from colormap list */
379 cur = __glutColormapList;
380 prev = &__glutColormapList;
381 while (cur) {
382 if (cur == cmap) {
383 *prev = cmap->next;
384 break;
385 }
386 prev = &(cur->next);
387 cur = cur->next;
388 }
389 /* actually free colormap */
390 XFreeColormap(__glutDisplay, cmap->cmap);
391 free(cmap->cells);
392 free(cmap);
393 }
394 }
395