2 /* Copyright (c) Mark J. Kilgard, 1994, 1996, 1997. */
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. */
10 #include <stdio.h> /* SunOS multithreaded assert() needs <stdio.h>. Lame. */
14 #include <X11/Xutil.h>
15 #include <X11/Xatom.h> /* for XA_RGB_DEFAULT_MAP atom */
17 #include <Xmu/StdCmap.h> /* for XmuLookupStandardColormap */
19 #include <X11/Xmu/StdCmap.h> /* for XmuLookupStandardColormap */
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>
28 #define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
32 #include "layerutil.h"
34 GLUTcolormap
*__glutColormapList
= NULL
;
37 __glutAssociateNewColormap(XVisualInfo
* vis
)
40 int transparentPixel
, i
;
41 unsigned long pixels
[255];
43 cmap
= (GLUTcolormap
*) malloc(sizeof(GLUTcolormap
));
45 __glutFatalError("out of memory.");
47 pixels
[0] = 0; /* avoid compilation warnings on win32 */
49 cmap
->size
= 256; /* always assume 256 on Win32 */
51 cmap
->visual
= vis
->visual
;
52 cmap
->size
= vis
->visual
->map_entries
;
55 cmap
->cells
= (GLUTcolorcell
*)
56 malloc(sizeof(GLUTcolorcell
) * cmap
->size
);
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;
65 transparentPixel
= __glutGetTransparentPixel(__glutDisplay
, vis
);
66 if (transparentPixel
== -1 || transparentPixel
>= cmap
->size
) {
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.
74 cmap
->cmap
= XCreateColormap(__glutDisplay
,
75 __glutRoot
, cmap
->visual
, AllocAll
);
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
88 cmap
->cmap
= XCreateColormap(__glutDisplay
,
89 __glutRoot
, 0, AllocNone
);
91 cmap
->cmap
= XCreateColormap(__glutDisplay
,
92 __glutRoot
, vis
->visual
, AllocNone
);
93 XAllocColorCells(__glutDisplay
, cmap
->cmap
, False
, 0, 0,
94 pixels
, cmap
->size
- 1);
97 cmap
->next
= __glutColormapList
;
98 __glutColormapList
= cmap
;
102 static GLUTcolormap
*
103 associateColormap(XVisualInfo
* vis
)
106 GLUTcolormap
*cmap
= __glutColormapList
;
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. */
118 return __glutAssociateNewColormap(vis
);
122 __glutSetupColormap(XVisualInfo
* vi
, GLUTcolormap
** colormap
, Colormap
* cmap
)
125 if (vi
->dwFlags
& PFD_NEED_PALETTE
|| vi
->iPixelType
== PFD_TYPE_COLORINDEX
) {
126 *colormap
= associateColormap(vi
);
127 *cmap
= (*colormap
)->cmap
;
134 XStandardColormap
*standardCmaps
;
136 static Atom hpColorRecoveryAtom
= -1;
137 int isRGB
, visualClass
, rc
;
139 #if defined(__cplusplus) || defined(c_plusplus)
140 visualClass
= vi
->c_class
;
142 visualClass
= vi
->class;
144 switch (visualClass
) {
146 /* Mesa might return a PseudoColor visual for RGB mode. */
147 rc
= glXGetConfig(__glutDisplay
, vi
, GLX_RGBA
, &isRGB
);
148 if (rc
== 0 && isRGB
) {
151 if (MaxCmapsOfScreen(DefaultScreenOfDisplay(__glutDisplay
)) == 1
152 && vi
->visual
== DefaultVisual(__glutDisplay
, __glutScreen
)) {
153 char *privateCmap
= getenv("MESA_PRIVATE_CMAP");
156 /* User doesn't want to share colormaps. */
157 *cmap
= XCreateColormap(__glutDisplay
, __glutRoot
,
158 vi
->visual
, AllocNone
);
160 /* Share the root colormap. */
161 *cmap
= DefaultColormap(__glutDisplay
, __glutScreen
);
164 /* Get our own PseudoColor colormap. */
165 *cmap
= XCreateColormap(__glutDisplay
, __glutRoot
,
166 vi
->visual
, AllocNone
);
169 /* CI mode, real GLX never returns a PseudoColor visual
171 *colormap
= associateColormap(vi
);
172 *cmap
= (*colormap
)->cmap
;
177 *colormap
= NULL
; /* NULL if RGBA */
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. */
187 if (hpColorRecoveryAtom
== -1) {
190 #define VENDOR_HP "Hewlett-Packard"
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
);
198 hpColorRecoveryAtom
= None
;
201 if (hpColorRecoveryAtom
!= None
) {
202 status
= XGetRGBColormaps(__glutDisplay
, __glutRoot
,
203 &standardCmaps
, &numCmaps
, hpColorRecoveryAtom
);
205 for (i
= 0; i
< numCmaps
; i
++) {
206 if (standardCmaps
[i
].visualid
== vi
->visualid
) {
207 *cmap
= standardCmaps
[i
].colormap
;
208 XFree(standardCmaps
);
212 XFree(standardCmaps
);
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
);
231 status
= XGetRGBColormaps(__glutDisplay
, __glutRoot
,
232 &standardCmaps
, &numCmaps
, XA_RGB_DEFAULT_MAP
);
234 for (i
= 0; i
< numCmaps
; i
++) {
235 if (standardCmaps
[i
].visualid
== vi
->visualid
) {
236 *cmap
= standardCmaps
[i
].colormap
;
237 XFree(standardCmaps
);
241 XFree(standardCmaps
);
245 /* If no standard colormap but TrueColor, just make a
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
);
256 /* Mesa supports these visuals */
258 *cmap
= XCreateColormap(__glutDisplay
, __glutRoot
,
259 vi
->visual
, AllocNone
);
263 "could not allocate colormap for visual type: %d.",
272 findColormaps(GLUTwindow
* window
,
273 Window
* winlist
, Colormap
* cmaplist
, int num
, int max
)
278 /* Do not allow more entries that maximum number of
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
;
287 /* Not found on the list; add colormap and window. */
288 winlist
[num
] = window
->win
;
289 cmaplist
[num
] = window
->cmap
;
292 normalColormapAlreadyListed
:
294 /* Repeat above but for the overlay colormap if there one. */
295 if (window
->overlay
) {
298 for (i
= 0; i
< num
; i
++) {
299 if (cmaplist
[i
] == window
->overlay
->cmap
)
300 goto overlayColormapAlreadyListed
;
302 winlist
[num
] = window
->overlay
->win
;
303 cmaplist
[num
] = window
->overlay
->cmap
;
306 overlayColormapAlreadyListed
:
308 /* Recursively search children. */
309 child
= window
->children
;
311 num
= findColormaps(child
, winlist
, cmaplist
, num
, max
);
312 child
= child
->siblings
;
318 __glutEstablishColormapsProperty(GLUTwindow
* window
)
320 /* this routine is strictly X. Win32 doesn't need to do
321 anything of this sort (but has to do other wacky stuff
323 static Atom wmColormapWindows
= None
;
329 assert(!window
->parent
);
330 maxcmaps
= MaxCmapsOfScreen(ScreenOfDisplay(__glutDisplay
,
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
);
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.");
345 XDeleteProperty(__glutDisplay
, window
->win
, wmColormapWindows
);
347 status
= XSetWMColormapWindows(__glutDisplay
, window
->win
,
349 /* XSetWMColormapWindows should always work unless the
350 WM_COLORMAP_WINDOWS property cannot be intern'ed. We
353 __glutFatalError("XSetWMColormapWindows returned False.");
355 /* For portability reasons we don't use alloca for winlist
356 and cmaplist, but we could. */
362 __glutToplevelOf(GLUTwindow
* window
)
364 while (window
->parent
) {
365 window
= window
->parent
;
372 __glutFreeColormap(GLUTcolormap
* cmap
)
374 GLUTcolormap
*cur
, **prev
;
377 if (cmap
->refcnt
== 0) {
378 /* remove from colormap list */
379 cur
= __glutColormapList
;
380 prev
= &__glutColormapList
;
389 /* actually free colormap */
390 XFreeColormap(__glutDisplay
, cmap
->cmap
);