#ifdef __VMS
#include <GL/vms_x_fix.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#if !defined(_WIN32)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#if defined(__vms)
#include <Xmu/StdCmap.h>
#else
#include <X11/Xmu/StdCmap.h>
#endif
#endif
#if defined(_SGI_EXTRA_PREDEFINES) && !defined(NO_FAST_ATOMS)
#include <X11/SGIFastAtom.h>
#else
#define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
#endif
#include "glutint.h"
#include "layerutil.h"
GLUTcolormap *__glutColormapList = NULL;
GLUTcolormap *
__glutAssociateNewColormap(XVisualInfo * vis)
{
GLUTcolormap *cmap;
int transparentPixel, i;
unsigned long pixels[255];
cmap = (GLUTcolormap *) malloc(sizeof(GLUTcolormap));
if (!cmap)
__glutFatalError("out of memory.");
#if defined(_WIN32)
pixels[0] = 0;
cmap->visual = 0;
cmap->size = 256;
#else
cmap->visual = vis->visual;
cmap->size = vis->visual->map_entries;
#endif
cmap->refcnt = 1;
cmap->cells = (GLUTcolorcell *)
malloc(sizeof(GLUTcolorcell) * cmap->size);
if (!cmap->cells)
__glutFatalError("out of memory.");
for (i = cmap->size - 1; i >= 0; i--) {
cmap->cells[i].component[GLUT_RED] = -1.0;
cmap->cells[i].component[GLUT_GREEN] = -1.0;
cmap->cells[i].component[GLUT_BLUE] = -1.0;
}
transparentPixel = __glutGetTransparentPixel(__glutDisplay, vis);
if (transparentPixel == -1 || transparentPixel >= cmap->size) {
cmap->cmap = XCreateColormap(__glutDisplay,
__glutRoot, cmap->visual, AllocAll);
} else {
#if defined(_WIN32)
cmap->cmap = XCreateColormap(__glutDisplay,
__glutRoot, 0, AllocNone);
#else
cmap->cmap = XCreateColormap(__glutDisplay,
__glutRoot, vis->visual, AllocNone);
XAllocColorCells(__glutDisplay, cmap->cmap, False, 0, 0,
pixels, cmap->size - 1);
#endif
}
cmap->next = __glutColormapList;
__glutColormapList = cmap;
return cmap;
}
static GLUTcolormap *
associateColormap(XVisualInfo * vis)
{
#if !defined(_WIN32)
GLUTcolormap *cmap = __glutColormapList;
while (cmap != NULL) {
if (cmap->visual->visualid == vis->visual->visualid) {
cmap->refcnt++;
return cmap;
}
cmap = cmap->next;
}
#endif
return __glutAssociateNewColormap(vis);
}
void
__glutSetupColormap(XVisualInfo * vi, GLUTcolormap ** colormap, Colormap * cmap)
{
#if defined(_WIN32)
if (vi->dwFlags & PFD_NEED_PALETTE || vi->iPixelType == PFD_TYPE_COLORINDEX) {
*colormap = associateColormap(vi);
*cmap = (*colormap)->cmap;
} else {
*colormap = NULL;
*cmap = 0;
}
#else
Status status;
XStandardColormap *standardCmaps;
int i, numCmaps;
static Atom hpColorRecoveryAtom = -1;
int isRGB, visualClass, rc;
#if defined(__cplusplus) || defined(c_plusplus)
visualClass = vi->c_class;
#else
visualClass = vi->class;
#endif
switch (visualClass) {
case PseudoColor:
rc = glXGetConfig(__glutDisplay, vi, GLX_RGBA, &isRGB);
if (rc == 0 && isRGB) {
*colormap = NULL;
if (MaxCmapsOfScreen(DefaultScreenOfDisplay(__glutDisplay)) == 1
&& vi->visual == DefaultVisual(__glutDisplay, __glutScreen)) {
char *privateCmap = getenv("MESA_PRIVATE_CMAP");
if (privateCmap) {
*cmap = XCreateColormap(__glutDisplay, __glutRoot,
vi->visual, AllocNone);
} else {
*cmap = DefaultColormap(__glutDisplay, __glutScreen);
}
} else {
*cmap = XCreateColormap(__glutDisplay, __glutRoot,
vi->visual, AllocNone);
}
} else {
*colormap = associateColormap(vi);
*cmap = (*colormap)->cmap;
}
break;
case TrueColor:
*colormap = NULL;
if (hpColorRecoveryAtom == -1) {
char *xvendor;
#define VENDOR_HP "Hewlett-Packard"
xvendor = ServerVendor(__glutDisplay);
if (!strncmp(xvendor, VENDOR_HP, sizeof(VENDOR_HP) - 1)) {
hpColorRecoveryAtom = XInternAtom(__glutDisplay, "_HP_RGB_SMOOTH_MAP_LIST", True);
} else {
hpColorRecoveryAtom = None;
}
}
if (hpColorRecoveryAtom != None) {
status = XGetRGBColormaps(__glutDisplay, __glutRoot,
&standardCmaps, &numCmaps, hpColorRecoveryAtom);
if (status == 1) {
for (i = 0; i < numCmaps; i++) {
if (standardCmaps[i].visualid == vi->visualid) {
*cmap = standardCmaps[i].colormap;
XFree(standardCmaps);
return;
}
}
XFree(standardCmaps);
}
}
#ifndef SOLARIS_2_4_BUG
status = XmuLookupStandardColormap(__glutDisplay,
vi->screen, vi->visualid, vi->depth, XA_RGB_DEFAULT_MAP,
False, True);
if (status == 1) {
status = XGetRGBColormaps(__glutDisplay, __glutRoot,
&standardCmaps, &numCmaps, XA_RGB_DEFAULT_MAP);
if (status == 1) {
for (i = 0; i < numCmaps; i++) {
if (standardCmaps[i].visualid == vi->visualid) {
*cmap = standardCmaps[i].colormap;
XFree(standardCmaps);
return;
}
}
XFree(standardCmaps);
}
}
#endif
*cmap = XCreateColormap(__glutDisplay, __glutRoot,
vi->visual, AllocNone);
break;
case DirectColor:
*colormap = NULL;
*cmap = XCreateColormap(__glutDisplay, __glutRoot,
vi->visual, AllocAll);
if (vi->depth == 24) {
XColor xc[256];
int i;
for (i = 0; i < 256; i++) {
xc[i].pixel = (i << 16) | (i << 8) | i;
xc[i].red = (i << 8) | i;
xc[i].green = (i << 8) | i;
xc[i].blue = (i << 8) | i;
xc[i].flags = DoRed | DoGreen | DoBlue;
}
XStoreColors(__glutDisplay, *cmap, xc, 256);
}
else {
fprintf(stderr, "GLUT Error: DirectColor visuals other than 24-bits "
"not fully supported.\n");
}
break;
case StaticColor:
case StaticGray:
case GrayScale:
*colormap = NULL;
*cmap = XCreateColormap(__glutDisplay, __glutRoot,
vi->visual, AllocNone);
break;
default:
__glutFatalError(
"could not allocate colormap for visual type: %d.",
visualClass);
}
return;
#endif
}
#if !defined(_WIN32)
static int
findColormaps(GLUTwindow * window,
Window * winlist, Colormap * cmaplist, int num, int max)
{
GLUTwindow *child;
int i;
if (num >= max)
return num;
for (i = 0; i < num; i++) {
if (cmaplist[i] == window->cmap)
goto normalColormapAlreadyListed;
}
winlist[num] = window->win;
cmaplist[num] = window->cmap;
num++;
normalColormapAlreadyListed:
if (window->overlay) {
if (num >= max)
return num;
for (i = 0; i < num; i++) {
if (cmaplist[i] == window->overlay->cmap)
goto overlayColormapAlreadyListed;
}
winlist[num] = window->overlay->win;
cmaplist[num] = window->overlay->cmap;
num++;
}
overlayColormapAlreadyListed:
child = window->children;
while (child) {
num = findColormaps(child, winlist, cmaplist, num, max);
child = child->siblings;
}
return num;
}
void
__glutEstablishColormapsProperty(GLUTwindow * window)
{
static Atom wmColormapWindows = None;
Window *winlist;
Colormap *cmaplist;
Status status;
int maxcmaps, num;
assert(!window->parent);
maxcmaps = MaxCmapsOfScreen(ScreenOfDisplay(__glutDisplay,
__glutScreen));
winlist = (Window *) malloc(maxcmaps * sizeof(Window));
cmaplist = (Colormap *) malloc(maxcmaps * sizeof(Colormap));
num = findColormaps(window, winlist, cmaplist, 0, maxcmaps);
if (num < 2) {
wmColormapWindows = XSGIFastInternAtom(__glutDisplay,
"WM_COLORMAP_WINDOWS", SGI_XA_WM_COLORMAP_WINDOWS, False);
if (wmColormapWindows == None) {
__glutWarning("Could not intern X atom for WM_COLORMAP_WINDOWS.");
return;
}
XDeleteProperty(__glutDisplay, window->win, wmColormapWindows);
} else {
status = XSetWMColormapWindows(__glutDisplay, window->win,
winlist, num);
if (status == False)
__glutFatalError("XSetWMColormapWindows returned False.");
}
free(winlist);
free(cmaplist);
}
GLUTwindow *
__glutToplevelOf(GLUTwindow * window)
{
while (window->parent) {
window = window->parent;
}
return window;
}
#endif
void
__glutFreeColormap(GLUTcolormap * cmap)
{
GLUTcolormap *cur, **prev;
cmap->refcnt--;
if (cmap->refcnt == 0) {
cur = __glutColormapList;
prev = &__glutColormapList;
while (cur) {
if (cur == cmap) {
*prev = cmap->next;
break;
}
prev = &(cur->next);
cur = cur->next;
}
XFreeColormap(__glutDisplay, cmap->cmap);
free(cmap->cells);
free(cmap);
}
}