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
authorBob Pendleton <bob@pendleton.com>
Thu, 12 Jul 2007 20:00:50 +0000
changeset 21852032348afed1
parent 2184 8f8516e79a13
child 2186 6fe43f7efd94
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.
configure.in
src/file/SDL_rwops.c
src/video/Xext/XmuStdCmap/AllCmap.c
src/video/Xext/XmuStdCmap/CmapAlloc.c
src/video/Xext/XmuStdCmap/CrCmap.c
src/video/Xext/XmuStdCmap/DelCmap.c
src/video/Xext/XmuStdCmap/Distinct.c
src/video/Xext/XmuStdCmap/LookupCmap.c
src/video/Xext/XmuStdCmap/StdCmap.c
src/video/Xext/XmuStdCmap/VisCmap.c
src/video/Xext/extensions/StdCmap.h
src/video/x11/SDL_x11sym.h
src/video/x11/SDL_x11window.c
test/common.c
test/testwm.c
     1.1 --- a/configure.in	Thu Jul 12 08:07:30 2007 +0000
     1.2 +++ b/configure.in	Thu Jul 12 20:00:50 2007 +0000
     1.3 @@ -945,6 +945,7 @@
     1.4  
     1.5              AC_DEFINE(SDL_VIDEO_DRIVER_X11)
     1.6              SOURCES="$SOURCES $srcdir/src/video/x11/*.c"
     1.7 +            SOURCES="$SOURCES $srcdir/src/video/Xext/XmuStdCmap/*.c"
     1.8              EXTRA_CFLAGS="$EXTRA_CFLAGS $X_CFLAGS"
     1.9  
    1.10              if test x$enable_x11_shared = xmaybe; then
     2.1 --- a/src/file/SDL_rwops.c	Thu Jul 12 08:07:30 2007 +0000
     2.2 +++ b/src/file/SDL_rwops.c	Thu Jul 12 20:00:50 2007 +0000
     2.3 @@ -191,7 +191,8 @@
     2.4          void *data = (char *) context->hidden.win32io.buffer.data +
     2.5              context->hidden.win32io.buffer.size -
     2.6              context->hidden.win32io.buffer.left;
     2.7 -        read_ahead = SDL_min(total_need, (size_t)context->hidden.win32io.buffer.left);
     2.8 +        read_ahead =
     2.9 +            SDL_min(total_need, (size_t) context->hidden.win32io.buffer.left);
    2.10          SDL_memcpy(ptr, data, read_ahead);
    2.11          context->hidden.win32io.buffer.left -= read_ahead;
    2.12  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/Xext/XmuStdCmap/AllCmap.c	Thu Jul 12 20:00:50 2007 +0000
     3.3 @@ -0,0 +1,159 @@
     3.4 +/* $Xorg: AllCmap.c,v 1.4 2001/02/09 02:03:51 xorgcvs Exp $ */
     3.5 +
     3.6 +/* 
     3.7 + 
     3.8 +Copyright 1989, 1998  The Open Group
     3.9 +
    3.10 +Permission to use, copy, modify, distribute, and sell this software and its
    3.11 +documentation for any purpose is hereby granted without fee, provided that
    3.12 +the above copyright notice appear in all copies and that both that
    3.13 +copyright notice and this permission notice appear in supporting
    3.14 +documentation.
    3.15 +
    3.16 +The above copyright notice and this permission notice shall be included in
    3.17 +all copies or substantial portions of the Software.
    3.18 +
    3.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    3.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    3.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    3.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    3.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    3.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    3.25 +
    3.26 +Except as contained in this notice, the name of The Open Group shall not be
    3.27 +used in advertising or otherwise to promote the sale, use or other dealings
    3.28 +in this Software without prior written authorization from The Open Group.
    3.29 +
    3.30 +*/
    3.31 +/* $XFree86: xc/lib/Xmu/AllCmap.c,v 1.7 2001/01/17 19:42:53 dawes Exp $ */
    3.32 +
    3.33 +#ifdef HAVE_CONFIG_H
    3.34 +#include <config.h>
    3.35 +#endif
    3.36 +#include <stdio.h>
    3.37 +#include <X11/Xlib.h>
    3.38 +#include <X11/Xatom.h>
    3.39 +#include <X11/Xutil.h>
    3.40 +#include "../extensions/StdCmap.h"
    3.41 +
    3.42 +static XVisualInfo *getDeepestVisual(int, XVisualInfo *, int);
    3.43 +
    3.44 +/*
    3.45 + * To create all of the appropriate standard colormaps for every visual of
    3.46 + * every screen on a given display, use XmuAllStandardColormaps.
    3.47 + *
    3.48 + * Define and retain as permanent resources all standard colormaps which are
    3.49 + * meaningful for the visuals of each screen of the display.  Return 0 on
    3.50 + * failure, non-zero on success.  If the property of any standard colormap 
    3.51 + * is already defined, redefine it.
    3.52 + *
    3.53 + * This interface is intended to be used by window managers or a client
    3.54 + * upon start-up of a session.
    3.55 + *
    3.56 + * The standard colormaps of a screen are defined by properties associated
    3.57 + * with the screen's root window.  Each screen has exactly one root window.
    3.58 + * The property names of standard colormaps are predefined, and each property
    3.59 + * name may describe at most one colormap.
    3.60 + * 
    3.61 + * The standard colormaps are
    3.62 + *		RGB_BEST_MAP
    3.63 + *		RGB_RED_MAP
    3.64 + *		RGB_GREEN_MAP
    3.65 + *		RGB_BLUE_MAP
    3.66 + *		RGB_DEFAULT_MAP
    3.67 + *		RGB_GRAY_MAP
    3.68 + *
    3.69 + * Therefore a screen may have at most 6 standard colormap properties defined.
    3.70 + *
    3.71 + * A standard colormap is associated with a particular visual of the screen.
    3.72 + * A screen may have multiple visuals defined, including visuals of the same
    3.73 + * class at different depths.  Note that a visual id might be repeated for
    3.74 + * more than one depth, so the visual id and the depth of a visual identify
    3.75 + * the visual.  The characteristics of the visual will determine which
    3.76 + * standard colormaps are meaningful under that visual, and will determine
    3.77 + * how the standard colormap is defined.  Because a standard colormap is
    3.78 + * associated with a specific visual, there must be a method of determining
    3.79 + * which visuals take precedence in defining standard colormaps.
    3.80 + * 
    3.81 + * The method used here is: for the visual of greatest depth, define all 
    3.82 + * standard colormaps meaningful to that visual class, according to this
    3.83 + * order of (descending) precedence:
    3.84 + *	1. DirectColor
    3.85 + *	2. PseudoColor
    3.86 + *	3. TrueColor and GrayScale
    3.87 + *	4. StaticColor and StaticGray
    3.88 + *
    3.89 + * Allows partial success by screenful.  For example, if a map on screen 1
    3.90 + * fails, the maps on screen 0, created earlier, will remain.  However,
    3.91 + * none on screen 1 will remain.  If a map on 0 fails, none will remain.
    3.92 + *
    3.93 + * See the comments under XmuVisualStandardColormaps() for notes on which
    3.94 + * standard colormaps are meaningful under these classes of visuals.
    3.95 + */
    3.96 +
    3.97 +Status
    3.98 +XmuAllStandardColormaps(Display * dpy)
    3.99 +{
   3.100 +    int nvisuals, scr;
   3.101 +    Status status;
   3.102 +    long vinfo_mask;
   3.103 +    XVisualInfo template, *vinfo, *v1, *v2;
   3.104 +
   3.105 +    status = 0;
   3.106 +    /* for each screen, determine all visuals of this server */
   3.107 +    for (scr = 0; scr < ScreenCount(dpy); scr++) {
   3.108 +        template.screen = scr;
   3.109 +        vinfo_mask = VisualScreenMask;
   3.110 +        vinfo = XGetVisualInfo(dpy, vinfo_mask, &template, &nvisuals);
   3.111 +        if (vinfo == NULL)      /* unexpected: a screen with no visuals */
   3.112 +            continue;
   3.113 +
   3.114 +        v1 = getDeepestVisual(DirectColor, vinfo, nvisuals);
   3.115 +        v2 = getDeepestVisual(PseudoColor, vinfo, nvisuals);
   3.116 +
   3.117 +        if (v2 &&
   3.118 +            (!v1 || (v2->colormap_size >=
   3.119 +                     ((v1->red_mask | v1->green_mask | v1->blue_mask) + 1))))
   3.120 +            status = XmuVisualStandardColormaps(dpy, scr, v2->visualid,
   3.121 +                                                (unsigned) v2->depth, 1, 1);
   3.122 +        else if (v1)
   3.123 +            status = XmuVisualStandardColormaps(dpy, scr, v1->visualid,
   3.124 +                                                (unsigned) v1->depth, 1, 1);
   3.125 +
   3.126 +        else {
   3.127 +            if (((v1 = getDeepestVisual(TrueColor, vinfo, nvisuals)) != NULL)
   3.128 +                || ((v1 = getDeepestVisual(StaticColor, vinfo, nvisuals)) !=
   3.129 +                    NULL))
   3.130 +                status = XmuVisualStandardColormaps(dpy, scr, v1->visualid,
   3.131 +                                                    (unsigned) v1->depth, 1,
   3.132 +                                                    1);
   3.133 +            if (status
   3.134 +                &&
   3.135 +                (((v1 = getDeepestVisual(GrayScale, vinfo, nvisuals)) != NULL)
   3.136 +                 || ((v1 = getDeepestVisual(StaticGray, vinfo, nvisuals)) !=
   3.137 +                     NULL)))
   3.138 +                status =
   3.139 +                    XmuVisualStandardColormaps(dpy, scr, v1->visualid,
   3.140 +                                               (unsigned) v1->depth, 1, 1);
   3.141 +        }
   3.142 +        XFree((char *) vinfo);
   3.143 +        if (!status)
   3.144 +            break;
   3.145 +    }
   3.146 +    return status;
   3.147 +}
   3.148 +
   3.149 +static XVisualInfo *
   3.150 +getDeepestVisual(int visual_class, XVisualInfo * vinfo, int nvisuals)
   3.151 +{
   3.152 +    register int i;
   3.153 +    register int maxdepth = 0;
   3.154 +    XVisualInfo *v = NULL;
   3.155 +
   3.156 +    for (i = 0; i < nvisuals; i++, vinfo++)
   3.157 +        if (vinfo->class == visual_class && vinfo->depth > maxdepth) {
   3.158 +            maxdepth = vinfo->depth;
   3.159 +            v = vinfo;
   3.160 +        }
   3.161 +    return (v);
   3.162 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/Xext/XmuStdCmap/CmapAlloc.c	Thu Jul 12 20:00:50 2007 +0000
     4.3 @@ -0,0 +1,337 @@
     4.4 +/* $Xorg: CmapAlloc.c,v 1.4 2001/02/09 02:03:51 xorgcvs Exp $ */
     4.5 +
     4.6 +/* 
     4.7 +
     4.8 +Copyright 1989, 1994, 1998  The Open Group
     4.9 +
    4.10 +Permission to use, copy, modify, distribute, and sell this software and its
    4.11 +documentation for any purpose is hereby granted without fee, provided that
    4.12 +the above copyright notice appear in all copies and that both that
    4.13 +copyright notice and this permission notice appear in supporting
    4.14 +documentation.
    4.15 +
    4.16 +The above copyright notice and this permission notice shall be included in
    4.17 +all copies or substantial portions of the Software.
    4.18 +
    4.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    4.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    4.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    4.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    4.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    4.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    4.25 +
    4.26 +Except as contained in this notice, the name of The Open Group shall not be
    4.27 +used in advertising or otherwise to promote the sale, use or other dealings
    4.28 +in this Software without prior written authorization from The Open Group.
    4.29 +
    4.30 +*/
    4.31 +/* $XFree86: xc/lib/Xmu/CmapAlloc.c,v 1.6 2001/01/17 19:42:53 dawes Exp $ */
    4.32 +
    4.33 +/*
    4.34 + * Author:  Donna Converse, MIT X Consortium
    4.35 + */
    4.36 +
    4.37 +#ifdef HAVE_CONFIG_H
    4.38 +#include <config.h>
    4.39 +#endif
    4.40 +#include <X11/Xlib.h>
    4.41 +#include <X11/Xatom.h>
    4.42 +#include <X11/Xutil.h>
    4.43 +#include "../extensions/StdCmap.h"
    4.44 +#include <stdio.h>
    4.45 +
    4.46 +#define lowbit(x) ((x) & (~(x) + 1))
    4.47 +
    4.48 +/*
    4.49 + * Prototypes
    4.50 + */
    4.51 +static void best_allocation(XVisualInfo *, unsigned long *, unsigned long *,
    4.52 +                            unsigned long *);
    4.53 +static int default_allocation(XVisualInfo *, unsigned long *,
    4.54 +                              unsigned long *, unsigned long *);
    4.55 +static void gray_allocation(int, unsigned long *, unsigned long *,
    4.56 +                            unsigned long *);
    4.57 +static int icbrt(int);
    4.58 +static int icbrt_with_bits(int, int);
    4.59 +static int icbrt_with_guess(int, int);
    4.60 +
    4.61 +/* To determine the best allocation of reds, greens, and blues in a 
    4.62 + * standard colormap, use XmuGetColormapAllocation.
    4.63 + * 	vinfo		specifies visual information for a chosen visual
    4.64 + *	property	specifies one of the standard colormap property names
    4.65 + * 	red_max		returns maximum red value 
    4.66 + *      green_max	returns maximum green value
    4.67 + * 	blue_max	returns maximum blue value
    4.68 + *
    4.69 + * XmuGetColormapAllocation returns 0 on failure, non-zero on success.
    4.70 + * It is assumed that the visual is appropriate for the colormap property.
    4.71 + */
    4.72 +
    4.73 +Status
    4.74 +XmuGetColormapAllocation(XVisualInfo * vinfo, Atom property,
    4.75 +                         unsigned long *red_max,
    4.76 +                         unsigned long *green_max, unsigned long *blue_max)
    4.77 +{
    4.78 +    Status status = 1;
    4.79 +
    4.80 +    if (vinfo->colormap_size <= 2)
    4.81 +        return 0;
    4.82 +
    4.83 +    switch (property) {
    4.84 +    case XA_RGB_DEFAULT_MAP:
    4.85 +        status = default_allocation(vinfo, red_max, green_max, blue_max);
    4.86 +        break;
    4.87 +    case XA_RGB_BEST_MAP:
    4.88 +        best_allocation(vinfo, red_max, green_max, blue_max);
    4.89 +        break;
    4.90 +    case XA_RGB_GRAY_MAP:
    4.91 +        gray_allocation(vinfo->colormap_size, red_max, green_max, blue_max);
    4.92 +        break;
    4.93 +    case XA_RGB_RED_MAP:
    4.94 +        *red_max = vinfo->colormap_size - 1;
    4.95 +        *green_max = *blue_max = 0;
    4.96 +        break;
    4.97 +    case XA_RGB_GREEN_MAP:
    4.98 +        *green_max = vinfo->colormap_size - 1;
    4.99 +        *red_max = *blue_max = 0;
   4.100 +        break;
   4.101 +    case XA_RGB_BLUE_MAP:
   4.102 +        *blue_max = vinfo->colormap_size - 1;
   4.103 +        *red_max = *green_max = 0;
   4.104 +        break;
   4.105 +    default:
   4.106 +        status = 0;
   4.107 +    }
   4.108 +    return status;
   4.109 +}
   4.110 +
   4.111 +/****************************************************************************/
   4.112 +/* Determine the appropriate color allocations of a gray scale.
   4.113 + *
   4.114 + * Keith Packard, MIT X Consortium
   4.115 + */
   4.116 +
   4.117 +static void
   4.118 +gray_allocation(int n, unsigned long *red_max, unsigned long *green_max,
   4.119 +                unsigned long *blue_max)
   4.120 +{
   4.121 +    *red_max = (n * 30) / 100;
   4.122 +    *green_max = (n * 59) / 100;
   4.123 +    *blue_max = (n * 11) / 100;
   4.124 +    *green_max += ((n - 1) - (*red_max + *green_max + *blue_max));
   4.125 +}
   4.126 +
   4.127 +/****************************************************************************/
   4.128 +/* Determine an appropriate color allocation for the RGB_DEFAULT_MAP.
   4.129 + * If a map has less than a minimum number of definable entries, we do not
   4.130 + * produce an allocation for an RGB_DEFAULT_MAP.  
   4.131 + *
   4.132 + * For 16 planes, the default colormap will have 27 each RGB; for 12 planes,
   4.133 + * 12 each.  For 8 planes, let n = the number of colormap entries, which may
   4.134 + * be 256 or 254.  Then, maximum red value = floor(cube_root(n - 125)) - 1.
   4.135 + * Maximum green and maximum blue values are identical to maximum red.
   4.136 + * This leaves at least 125 cells which clients can allocate.
   4.137 + *
   4.138 + * Return 0 if an allocation has been determined, non-zero otherwise.
   4.139 + */
   4.140 +
   4.141 +static int
   4.142 +default_allocation(XVisualInfo * vinfo, unsigned long *red,
   4.143 +                   unsigned long *green, unsigned long *blue)
   4.144 +{
   4.145 +    int ngrays;                 /* number of gray cells */
   4.146 +
   4.147 +    switch (vinfo->class) {
   4.148 +    case PseudoColor:
   4.149 +
   4.150 +        if (vinfo->colormap_size > 65000)
   4.151 +            /* intended for displays with 16 planes */
   4.152 +            *red = *green = *blue = (unsigned long) 27;
   4.153 +        else if (vinfo->colormap_size > 4000)
   4.154 +            /* intended for displays with 12 planes */
   4.155 +            *red = *green = *blue = (unsigned long) 12;
   4.156 +        else if (vinfo->colormap_size < 250)
   4.157 +            return 0;
   4.158 +        else
   4.159 +            /* intended for displays with 8 planes */
   4.160 +            *red = *green = *blue = (unsigned long)
   4.161 +                (icbrt(vinfo->colormap_size - 125) - 1);
   4.162 +        break;
   4.163 +
   4.164 +    case DirectColor:
   4.165 +
   4.166 +        if (vinfo->colormap_size < 10)
   4.167 +            return 0;
   4.168 +        *red = *green = *blue = vinfo->colormap_size / 2 - 1;
   4.169 +        break;
   4.170 +
   4.171 +    case TrueColor:
   4.172 +
   4.173 +        *red = vinfo->red_mask / lowbit(vinfo->red_mask);
   4.174 +        *green = vinfo->green_mask / lowbit(vinfo->green_mask);
   4.175 +        *blue = vinfo->blue_mask / lowbit(vinfo->blue_mask);
   4.176 +        break;
   4.177 +
   4.178 +    case GrayScale:
   4.179 +
   4.180 +        if (vinfo->colormap_size > 65000)
   4.181 +            ngrays = 4096;
   4.182 +        else if (vinfo->colormap_size > 4000)
   4.183 +            ngrays = 512;
   4.184 +        else if (vinfo->colormap_size < 250)
   4.185 +            return 0;
   4.186 +        else
   4.187 +            ngrays = 12;
   4.188 +        gray_allocation(ngrays, red, green, blue);
   4.189 +        break;
   4.190 +
   4.191 +    default:
   4.192 +        return 0;
   4.193 +    }
   4.194 +    return 1;
   4.195 +}
   4.196 +
   4.197 +/****************************************************************************/
   4.198 +/* Determine an appropriate color allocation for the RGB_BEST_MAP.
   4.199 + *
   4.200 + * For a DirectColor or TrueColor visual, the allocation is determined
   4.201 + * by the red_mask, green_mask, and blue_mask members of the visual info.
   4.202 + *
   4.203 + * Otherwise, if the colormap size is an integral power of 2, determine
   4.204 + * the allocation according to the number of bits given to each color,
   4.205 + * with green getting more than red, and red more than blue, if there
   4.206 + * are to be inequities in the distribution.  If the colormap size is
   4.207 + * not an integral power of 2, let n = the number of colormap entries.
   4.208 + * Then maximum red value = floor(cube_root(n)) - 1;
   4.209 + * 	maximum blue value = floor(cube_root(n)) - 1;
   4.210 + *	maximum green value = n / ((# red values) * (# blue values)) - 1;
   4.211 + * Which, on a GPX, allows for 252 entries in the best map, out of 254
   4.212 + * defineable colormap entries.
   4.213 + */
   4.214 +
   4.215 +static void
   4.216 +best_allocation(XVisualInfo * vinfo, unsigned long *red, unsigned long *green,
   4.217 +                unsigned long *blue)
   4.218 +{
   4.219 +
   4.220 +    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
   4.221 +        *red = vinfo->red_mask;
   4.222 +        while ((*red & 01) == 0)
   4.223 +            *red >>= 1;
   4.224 +        *green = vinfo->green_mask;
   4.225 +        while ((*green & 01) == 0)
   4.226 +            *green >>= 1;
   4.227 +        *blue = vinfo->blue_mask;
   4.228 +        while ((*blue & 01) == 0)
   4.229 +            *blue >>= 1;
   4.230 +    } else {
   4.231 +        register int bits, n;
   4.232 +
   4.233 +        /* Determine n such that n is the least integral power of 2 which is
   4.234 +         * greater than or equal to the number of entries in the colormap.
   4.235 +         */
   4.236 +        n = 1;
   4.237 +        bits = 0;
   4.238 +        while (vinfo->colormap_size > n) {
   4.239 +            n = n << 1;
   4.240 +            bits++;
   4.241 +        }
   4.242 +
   4.243 +        /* If the number of entries in the colormap is a power of 2, determine
   4.244 +         * the allocation by "dealing" the bits, first to green, then red, then
   4.245 +         * blue.  If not, find the maximum integral red, green, and blue values
   4.246 +         * which, when multiplied together, do not exceed the number of 
   4.247 +
   4.248 +         * colormap entries.
   4.249 +         */
   4.250 +        if (n == vinfo->colormap_size) {
   4.251 +            register int r, g, b;
   4.252 +            b = bits / 3;
   4.253 +            g = b + ((bits % 3) ? 1 : 0);
   4.254 +            r = b + (((bits % 3) == 2) ? 1 : 0);
   4.255 +            *red = 1 << r;
   4.256 +            *green = 1 << g;
   4.257 +            *blue = 1 << b;
   4.258 +        } else {
   4.259 +            *red = icbrt_with_bits(vinfo->colormap_size, bits);
   4.260 +            *blue = *red;
   4.261 +            *green = (vinfo->colormap_size / ((*red) * (*blue)));
   4.262 +        }
   4.263 +        (*red)--;
   4.264 +        (*green)--;
   4.265 +        (*blue)--;
   4.266 +    }
   4.267 +    return;
   4.268 +}
   4.269 +
   4.270 +/*
   4.271 + * integer cube roots by Newton's method
   4.272 + *
   4.273 + * Stephen Gildea, MIT X Consortium, July 1991
   4.274 + */
   4.275 +
   4.276 +static int
   4.277 +icbrt(int a)
   4.278 +{
   4.279 +    register int bits = 0;
   4.280 +    register unsigned n = a;
   4.281 +
   4.282 +    while (n) {
   4.283 +        bits++;
   4.284 +        n >>= 1;
   4.285 +    }
   4.286 +    return icbrt_with_bits(a, bits);
   4.287 +}
   4.288 +
   4.289 +
   4.290 +static int
   4.291 +icbrt_with_bits(int a, int bits)
   4.292 +     /* bits - log 2 of a */
   4.293 +{
   4.294 +    return icbrt_with_guess(a, a >> 2 * bits / 3);
   4.295 +}
   4.296 +
   4.297 +#ifdef _X_ROOT_STATS
   4.298 +int icbrt_loopcount;
   4.299 +#endif
   4.300 +
   4.301 +/* Newton's Method:  x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
   4.302 +
   4.303 +/* for cube roots, x^3 - a = 0,  x_new = x - 1/3 (x - a/x^2) */
   4.304 +
   4.305 +/*
   4.306 + * Quick and dirty cube roots.  Nothing fancy here, just Newton's method.
   4.307 + * Only works for positive integers (since that's all we need).
   4.308 + * We actually return floor(cbrt(a)) because that's what we need here, too.
   4.309 + */
   4.310 +
   4.311 +static int
   4.312 +icbrt_with_guess(int a, int guess)
   4.313 +{
   4.314 +    register int delta;
   4.315 +
   4.316 +#ifdef _X_ROOT_STATS
   4.317 +    icbrt_loopcount = 0;
   4.318 +#endif
   4.319 +    if (a <= 0)
   4.320 +        return 0;
   4.321 +    if (guess < 1)
   4.322 +        guess = 1;
   4.323 +
   4.324 +    do {
   4.325 +#ifdef _X_ROOT_STATS
   4.326 +        icbrt_loopcount++;
   4.327 +#endif
   4.328 +        delta = (guess - a / (guess * guess)) / 3;
   4.329 +#ifdef DEBUG
   4.330 +        printf("pass %d: guess=%d, delta=%d\n", icbrt_loopcount, guess,
   4.331 +               delta);
   4.332 +#endif
   4.333 +        guess -= delta;
   4.334 +    } while (delta != 0);
   4.335 +
   4.336 +    if (guess * guess * guess > a)
   4.337 +        guess--;
   4.338 +
   4.339 +    return guess;
   4.340 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/video/Xext/XmuStdCmap/CrCmap.c	Thu Jul 12 20:00:50 2007 +0000
     5.3 @@ -0,0 +1,531 @@
     5.4 +/* $Xorg: CrCmap.c,v 1.4 2001/02/09 02:03:51 xorgcvs Exp $ */
     5.5 +
     5.6 +/* 
     5.7 +
     5.8 +Copyright 1989, 1998  The Open Group
     5.9 +
    5.10 +Permission to use, copy, modify, distribute, and sell this software and its
    5.11 +documentation for any purpose is hereby granted without fee, provided that
    5.12 +the above copyright notice appear in all copies and that both that
    5.13 +copyright notice and this permission notice appear in supporting
    5.14 +documentation.
    5.15 +
    5.16 +The above copyright notice and this permission notice shall be included in
    5.17 +all copies or substantial portions of the Software.
    5.18 +
    5.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    5.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    5.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    5.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    5.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    5.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    5.25 +
    5.26 +Except as contained in this notice, the name of The Open Group shall not be
    5.27 +used in advertising or otherwise to promote the sale, use or other dealings
    5.28 +in this Software without prior written authorization from The Open Group.
    5.29 +
    5.30 +*/
    5.31 +/* $XFree86: xc/lib/Xmu/CrCmap.c,v 3.6 2001/01/17 19:42:53 dawes Exp $ */
    5.32 +
    5.33 +/*
    5.34 + * Author:  Donna Converse, MIT X Consortium
    5.35 + */
    5.36 +
    5.37 +/*
    5.38 + * CreateCmap.c - given a standard colormap description, make the map.
    5.39 + */
    5.40 +
    5.41 +#ifdef HAVE_CONFIG_H
    5.42 +#include <config.h>
    5.43 +#endif
    5.44 +#include <stdio.h>
    5.45 +#include <stdlib.h>
    5.46 +#include <X11/Xlib.h>
    5.47 +#include <X11/Xutil.h>
    5.48 +#include "../extensions/StdCmap.h"
    5.49 +
    5.50 +/*
    5.51 + * Prototypes
    5.52 + */
    5.53 +/* allocate entire map Read Only */
    5.54 +static int ROmap(Display *, Colormap, unsigned long[], int, int);
    5.55 +
    5.56 +/* allocate a cell, prefer Read Only */
    5.57 +static Status ROorRWcell(Display *, Colormap, unsigned long[], int,
    5.58 +                         XColor *, unsigned long);
    5.59 +
    5.60 +/* allocate a cell Read Write */
    5.61 +static Status RWcell(Display *, Colormap, XColor *, XColor *,
    5.62 +                     unsigned long *);
    5.63 +
    5.64 +/* for quicksort */
    5.65 +static int compare(_Xconst void *, _Xconst void *);
    5.66 +
    5.67 +/* find contiguous sequence of cells */
    5.68 +static Status contiguous(unsigned long[], int, int, unsigned long, int *,
    5.69 +                         int *);
    5.70 +
    5.71 +/* frees resources before quitting */
    5.72 +static void free_cells(Display *, Colormap, unsigned long[], int, int);
    5.73 +
    5.74 +/* create a map in a RO visual type */
    5.75 +static Status readonly_map(Display *, XVisualInfo *, XStandardColormap *);
    5.76 +
    5.77 +/* create a map in a RW visual type */
    5.78 +static Status readwrite_map(Display *, XVisualInfo *, XStandardColormap *);
    5.79 +
    5.80 +#define lowbit(x) ((x) & (~(x) + 1))
    5.81 +#define TRUEMATCH(mult,max,mask) \
    5.82 +    (colormap->max * colormap->mult <= vinfo->mask && \
    5.83 +     lowbit(vinfo->mask) == colormap->mult)
    5.84 +
    5.85 +/*
    5.86 + * To create any one colormap which is described by an XStandardColormap
    5.87 + * structure, use XmuCreateColormap().
    5.88 + *
    5.89 + * Return 0 on failure, non-zero on success.
    5.90 + * Resources created by this function are not made permanent.
    5.91 + * No argument error checking is provided.  Use at your own risk.
    5.92 + *
    5.93 + * All colormaps are created with read only allocations, with the exception
    5.94 + * of read only allocations of colors in the default map or otherwise
    5.95 + * which fail to return the expected pixel value, and these are individually 
    5.96 + * defined as read/write allocations.  This is done so that all the cells
    5.97 + * defined in the default map are contiguous, for use in image processing.
    5.98 + * This typically happens with White and Black in the default map.
    5.99 + *
   5.100 + * Colormaps of static visuals are considered to be successfully created if
   5.101 + * the map of the static visual matches the definition given in the
   5.102 + * standard colormap structure.
   5.103 + */
   5.104 +
   5.105 +Status
   5.106 +XmuCreateColormap(Display * dpy, XStandardColormap * colormap)
   5.107 +     /* dpy      - specifies the connection under which the map is created
   5.108 +      * colormap - specifies the map to be created, and returns, particularly
   5.109 +      *            if the map is created as a subset of the default colormap
   5.110 +      *            of the screen, the base_pixel of the map.
   5.111 +      */
   5.112 +{
   5.113 +    XVisualInfo vinfo_template; /* template visual information */
   5.114 +    XVisualInfo *vinfo;         /* matching visual information */
   5.115 +    XVisualInfo *vpointer;      /* for freeing the entire list */
   5.116 +    long vinfo_mask;            /* specifies the visual mask value */
   5.117 +    int n;                      /* number of matching visuals */
   5.118 +    int status;
   5.119 +
   5.120 +    vinfo_template.visualid = colormap->visualid;
   5.121 +    vinfo_mask = VisualIDMask;
   5.122 +    if ((vinfo =
   5.123 +         XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
   5.124 +        return 0;
   5.125 +
   5.126 +    /* A visual id may be valid on multiple screens.  Also, there may 
   5.127 +     * be multiple visuals with identical visual ids at different depths.  
   5.128 +     * If the colormap is the Default Colormap, use the Default Visual.
   5.129 +     * Otherwise, arbitrarily, use the deepest visual.
   5.130 +     */
   5.131 +    vpointer = vinfo;
   5.132 +    if (n > 1) {
   5.133 +        register int i;
   5.134 +        register int screen_number;
   5.135 +        Bool def_cmap;
   5.136 +
   5.137 +        def_cmap = False;
   5.138 +        for (screen_number = ScreenCount(dpy); --screen_number >= 0;)
   5.139 +            if (colormap->colormap == DefaultColormap(dpy, screen_number)) {
   5.140 +                def_cmap = True;
   5.141 +                break;
   5.142 +            }
   5.143 +
   5.144 +        if (def_cmap) {
   5.145 +            for (i = 0; i < n; i++, vinfo++) {
   5.146 +                if (vinfo->visual == DefaultVisual(dpy, screen_number))
   5.147 +                    break;
   5.148 +            }
   5.149 +        } else {
   5.150 +            int maxdepth = 0;
   5.151 +            XVisualInfo *v = NULL;
   5.152 +
   5.153 +            for (i = 0; i < n; i++, vinfo++)
   5.154 +                if (vinfo->depth > maxdepth) {
   5.155 +                    maxdepth = vinfo->depth;
   5.156 +                    v = vinfo;
   5.157 +                }
   5.158 +            vinfo = v;
   5.159 +        }
   5.160 +    }
   5.161 +
   5.162 +    if (vinfo->class == PseudoColor || vinfo->class == DirectColor ||
   5.163 +        vinfo->class == GrayScale)
   5.164 +        status = readwrite_map(dpy, vinfo, colormap);
   5.165 +    else if (vinfo->class == TrueColor)
   5.166 +        status = TRUEMATCH(red_mult, red_max, red_mask) &&
   5.167 +            TRUEMATCH(green_mult, green_max, green_mask) &&
   5.168 +            TRUEMATCH(blue_mult, blue_max, blue_mask);
   5.169 +    else
   5.170 +        status = readonly_map(dpy, vinfo, colormap);
   5.171 +
   5.172 +    XFree((char *) vpointer);
   5.173 +    return status;
   5.174 +}
   5.175 +
   5.176 +/****************************************************************************/
   5.177 +static Status
   5.178 +readwrite_map(Display * dpy, XVisualInfo * vinfo,
   5.179 +              XStandardColormap * colormap)
   5.180 +{
   5.181 +    register unsigned long i, n;        /* index counters */
   5.182 +    unsigned long ncolors;      /* number of colors to be defined */
   5.183 +    int npixels;                /* number of pixels allocated R/W */
   5.184 +    int first_index;            /* first index of pixels to use */
   5.185 +    int remainder;              /* first index of remainder */
   5.186 +    XColor color;               /* the definition of a color */
   5.187 +    unsigned long *pixels;      /* array of colormap pixels */
   5.188 +    unsigned long delta;
   5.189 +
   5.190 +
   5.191 +    /* Determine ncolors, the number of colors to be defined.
   5.192 +     * Insure that 1 < ncolors <= the colormap size.
   5.193 +     */
   5.194 +    if (vinfo->class == DirectColor) {
   5.195 +        ncolors = colormap->red_max;
   5.196 +        if (colormap->green_max > ncolors)
   5.197 +            ncolors = colormap->green_max;
   5.198 +        if (colormap->blue_max > ncolors)
   5.199 +            ncolors = colormap->blue_max;
   5.200 +        ncolors++;
   5.201 +        delta = lowbit(vinfo->red_mask) +
   5.202 +            lowbit(vinfo->green_mask) + lowbit(vinfo->blue_mask);
   5.203 +    } else {
   5.204 +        ncolors = colormap->red_max * colormap->red_mult +
   5.205 +            colormap->green_max * colormap->green_mult +
   5.206 +            colormap->blue_max * colormap->blue_mult + 1;
   5.207 +        delta = 1;
   5.208 +    }
   5.209 +    if (ncolors <= 1 || (int) ncolors > vinfo->colormap_size)
   5.210 +        return 0;
   5.211 +
   5.212 +    /* Allocate Read/Write as much of the colormap as we can possibly get.
   5.213 +     * Then insure that the pixels we were allocated are given in 
   5.214 +     * monotonically increasing order, using a quicksort.  Next, insure
   5.215 +     * that our allocation includes a subset of contiguous pixels at least
   5.216 +     * as long as the number of colors to be defined.  Now we know that 
   5.217 +     * these conditions are met:
   5.218 +     *  1) There are no free cells in the colormap.
   5.219 +     *  2) We have a contiguous sequence of pixels, monotonically 
   5.220 +     *     increasing, of length >= the number of colors requested.
   5.221 +     *
   5.222 +     * One cell at a time, we will free, compute the next color value, 
   5.223 +     * then allocate read only.  This takes a long time.
   5.224 +     * This is done to insure that cells are allocated read only in the
   5.225 +     * contiguous order which we prefer.  If the server has a choice of
   5.226 +     * cells to grant to an allocation request, the server may give us any
   5.227 +     * cell, so that is why we do these slow gymnastics.
   5.228 +     */
   5.229 +
   5.230 +    if ((pixels = (unsigned long *) calloc((unsigned) vinfo->colormap_size,
   5.231 +                                           sizeof(unsigned long))) == NULL)
   5.232 +        return 0;
   5.233 +
   5.234 +    if ((npixels = ROmap(dpy, colormap->colormap, pixels,
   5.235 +                         vinfo->colormap_size, ncolors)) == 0) {
   5.236 +        free((char *) pixels);
   5.237 +        return 0;
   5.238 +    }
   5.239 +
   5.240 +    qsort((char *) pixels, npixels, sizeof(unsigned long), compare);
   5.241 +
   5.242 +    if (!contiguous
   5.243 +        (pixels, npixels, ncolors, delta, &first_index, &remainder)) {
   5.244 +        /* can't find enough contiguous cells, give up */
   5.245 +        XFreeColors(dpy, colormap->colormap, pixels, npixels,
   5.246 +                    (unsigned long) 0);
   5.247 +        free((char *) pixels);
   5.248 +        return 0;
   5.249 +    }
   5.250 +    colormap->base_pixel = pixels[first_index];
   5.251 +
   5.252 +    /* construct a gray map */
   5.253 +    if (colormap->red_mult == 1 && colormap->green_mult == 1 &&
   5.254 +        colormap->blue_mult == 1)
   5.255 +        for (n = colormap->base_pixel, i = 0; i < ncolors; i++, n += delta) {
   5.256 +            color.pixel = n;
   5.257 +            color.blue = color.green = color.red =
   5.258 +                (unsigned short) ((i * 65535) / (colormap->red_max +
   5.259 +                                                 colormap->green_max +
   5.260 +                                                 colormap->blue_max));
   5.261 +
   5.262 +            if (!ROorRWcell(dpy, colormap->colormap, pixels, npixels, &color,
   5.263 +                            first_index + i))
   5.264 +                return 0;
   5.265 +        }
   5.266 +
   5.267 +    /* construct a red ramp map */
   5.268 +    else if (colormap->green_max == 0 && colormap->blue_max == 0)
   5.269 +        for (n = colormap->base_pixel, i = 0; i < ncolors; i++, n += delta) {
   5.270 +            color.pixel = n;
   5.271 +            color.red = (unsigned short) ((i * 65535) / colormap->red_max);
   5.272 +            color.green = color.blue = 0;
   5.273 +
   5.274 +            if (!ROorRWcell(dpy, colormap->colormap, pixels, npixels, &color,
   5.275 +                            first_index + i))
   5.276 +                return 0;
   5.277 +        }
   5.278 +
   5.279 +    /* construct a green ramp map */
   5.280 +    else if (colormap->red_max == 0 && colormap->blue_max == 0)
   5.281 +        for (n = colormap->base_pixel, i = 0; i < ncolors; i++, n += delta) {
   5.282 +            color.pixel = n;
   5.283 +            color.green =
   5.284 +                (unsigned short) ((i * 65535) / colormap->green_max);
   5.285 +            color.red = color.blue = 0;
   5.286 +
   5.287 +            if (!ROorRWcell(dpy, colormap->colormap, pixels, npixels, &color,
   5.288 +                            first_index + i))
   5.289 +                return 0;
   5.290 +        }
   5.291 +
   5.292 +    /* construct a blue ramp map */
   5.293 +    else if (colormap->red_max == 0 && colormap->green_max == 0)
   5.294 +        for (n = colormap->base_pixel, i = 0; i < ncolors; i++, n += delta) {
   5.295 +            color.pixel = n;
   5.296 +            color.blue = (unsigned short) ((i * 65535) / colormap->blue_max);
   5.297 +            color.red = color.green = 0;
   5.298 +
   5.299 +            if (!ROorRWcell(dpy, colormap->colormap, pixels, npixels, &color,
   5.300 +                            first_index + i))
   5.301 +                return 0;
   5.302 +        }
   5.303 +
   5.304 +    /* construct a standard red green blue cube map */
   5.305 +    else {
   5.306 +#define calc(max,mult) (((n / colormap->mult) % \
   5.307 +			 (colormap->max + 1)) * 65535) / colormap->max
   5.308 +
   5.309 +        for (n = 0, i = 0; i < ncolors; i++, n += delta) {
   5.310 +            color.pixel = n + colormap->base_pixel;
   5.311 +            color.red = calc(red_max, red_mult);
   5.312 +            color.green = calc(green_max, green_mult);
   5.313 +            color.blue = calc(blue_max, blue_mult);
   5.314 +            if (!ROorRWcell(dpy, colormap->colormap, pixels, npixels, &color,
   5.315 +                            first_index + i))
   5.316 +                return 0;
   5.317 +        }
   5.318 +#undef calc
   5.319 +    }
   5.320 +    /* We have a read-only map defined.  Now free unused cells,
   5.321 +     * first those occuring before the contiguous sequence begins,
   5.322 +     * then any following the contiguous sequence.
   5.323 +     */
   5.324 +
   5.325 +    if (first_index)
   5.326 +        XFreeColors(dpy, colormap->colormap, pixels, first_index,
   5.327 +                    (unsigned long) 0);
   5.328 +    if (remainder)
   5.329 +        XFreeColors(dpy, colormap->colormap,
   5.330 +                    &(pixels[first_index + ncolors]), remainder,
   5.331 +                    (unsigned long) 0);
   5.332 +
   5.333 +    free((char *) pixels);
   5.334 +    return 1;
   5.335 +}
   5.336 +
   5.337 +
   5.338 +/****************************************************************************/
   5.339 +static int
   5.340 +ROmap(Display * dpy, Colormap cmap, unsigned long pixels[], int m, int n)
   5.341 +     /*
   5.342 +      * dpy     - the X server connection
   5.343 +      * cmap    - specifies colormap ID
   5.344 +      * pixels  - returns pixel allocations
   5.345 +      * m       - specifies colormap size
   5.346 +      * n       - specifies number of colors
   5.347 +      */
   5.348 +{
   5.349 +    register int p;
   5.350 +
   5.351 +    /* first try to allocate the entire colormap */
   5.352 +    if (XAllocColorCells(dpy, cmap, 1, (unsigned long *) NULL,
   5.353 +                         (unsigned) 0, pixels, (unsigned) m))
   5.354 +        return m;
   5.355 +
   5.356 +    /* Allocate all available cells in the colormap, using a binary
   5.357 +     * algorithm to discover how many cells we can allocate in the colormap.
   5.358 +     */
   5.359 +    m--;
   5.360 +    while (n <= m) {
   5.361 +        p = n + ((m - n + 1) / 2);
   5.362 +        if (XAllocColorCells(dpy, cmap, 1, (unsigned long *) NULL,
   5.363 +                             (unsigned) 0, pixels, (unsigned) p)) {
   5.364 +            if (p == m)
   5.365 +                return p;
   5.366 +            else {
   5.367 +                XFreeColors(dpy, cmap, pixels, p, (unsigned long) 0);
   5.368 +                n = p;
   5.369 +            }
   5.370 +        } else
   5.371 +            m = p - 1;
   5.372 +    }
   5.373 +    return 0;
   5.374 +}
   5.375 +
   5.376 +
   5.377 +/****************************************************************************/
   5.378 +static Status
   5.379 +contiguous(unsigned long pixels[], int npixels, int ncolors,
   5.380 +           unsigned long delta, int *first, int *rem)
   5.381 +     /* pixels  - specifies allocated pixels
   5.382 +      * npixels - specifies count of alloc'd pixels
   5.383 +      * ncolors - specifies needed sequence length
   5.384 +      * delta   - between pixels
   5.385 +      * first   - returns first index of sequence
   5.386 +      * rem     - returns first index after sequence, or 0, if none follow
   5.387 +      */
   5.388 +{
   5.389 +    register int i = 1;         /* walking index into the pixel array */
   5.390 +    register int count = 1;     /* length of sequence discovered so far */
   5.391 +
   5.392 +    *first = 0;
   5.393 +    if (npixels == ncolors) {
   5.394 +        *rem = 0;
   5.395 +        return 1;
   5.396 +    }
   5.397 +    *rem = npixels - 1;
   5.398 +    while (count < ncolors && ncolors - count <= *rem) {
   5.399 +        if (pixels[i - 1] + delta == pixels[i])
   5.400 +            count++;
   5.401 +        else {
   5.402 +            count = 1;
   5.403 +            *first = i;
   5.404 +        }
   5.405 +        i++;
   5.406 +        (*rem)--;
   5.407 +    }
   5.408 +    if (count != ncolors)
   5.409 +        return 0;
   5.410 +    return 1;
   5.411 +}
   5.412 +
   5.413 +
   5.414 +/****************************************************************************/
   5.415 +static Status
   5.416 +ROorRWcell(Display * dpy, Colormap cmap, unsigned long pixels[],
   5.417 +           int npixels, XColor * color, unsigned long p)
   5.418 +{
   5.419 +    unsigned long pixel;
   5.420 +    XColor request;
   5.421 +
   5.422 +    /* Free the read/write allocation of one cell in the colormap.
   5.423 +     * Request a read only allocation of one cell in the colormap.
   5.424 +     * If the read only allocation cannot be granted, give up, because
   5.425 +     * there must be no free cells in the colormap.
   5.426 +     * If the read only allocation is granted, but gives us a cell which
   5.427 +     * is not the one that we just freed, it is probably the case that
   5.428 +     * we are trying allocate White or Black or some other color which
   5.429 +     * already has a read-only allocation in the map.  So we try to 
   5.430 +     * allocate the previously freed cell with a read/write allocation,
   5.431 +     * because we want contiguous cells for image processing algorithms.
   5.432 +     */
   5.433 +
   5.434 +    pixel = color->pixel;
   5.435 +    request.red = color->red;
   5.436 +    request.green = color->green;
   5.437 +    request.blue = color->blue;
   5.438 +
   5.439 +    XFreeColors(dpy, cmap, &pixel, 1, (unsigned long) 0);
   5.440 +    if (!XAllocColor(dpy, cmap, color)
   5.441 +        || (color->pixel != pixel &&
   5.442 +            (!RWcell(dpy, cmap, color, &request, &pixel)))) {
   5.443 +        free_cells(dpy, cmap, pixels, npixels, (int) p);
   5.444 +        return 0;
   5.445 +    }
   5.446 +    return 1;
   5.447 +}
   5.448 +
   5.449 +
   5.450 +/****************************************************************************/
   5.451 +static void
   5.452 +free_cells(Display * dpy, Colormap cmap, unsigned long pixels[],
   5.453 +           int npixels, int p)
   5.454 +     /*
   5.455 +      * pixels  - to be freed
   5.456 +      * npixels - original number allocated
   5.457 +      */
   5.458 +{
   5.459 +    /* One of the npixels allocated has already been freed.
   5.460 +     * p is the index of the freed pixel.
   5.461 +     * First free the pixels preceeding p, and there are p of them;
   5.462 +     * then free the pixels following p, there are npixels - p - 1 of them.
   5.463 +     */
   5.464 +    XFreeColors(dpy, cmap, pixels, p, (unsigned long) 0);
   5.465 +    XFreeColors(dpy, cmap, &(pixels[p + 1]), npixels - p - 1,
   5.466 +                (unsigned long) 0);
   5.467 +    free((char *) pixels);
   5.468 +}
   5.469 +
   5.470 +
   5.471 +/****************************************************************************/
   5.472 +static Status
   5.473 +RWcell(Display * dpy, Colormap cmap, XColor * color, XColor * request,
   5.474 +       unsigned long *pixel)
   5.475 +{
   5.476 +    unsigned long n = *pixel;
   5.477 +
   5.478 +    XFreeColors(dpy, cmap, &(color->pixel), 1, (unsigned long) 0);
   5.479 +    if (!XAllocColorCells(dpy, cmap, (Bool) 0, (unsigned long *) NULL,
   5.480 +                          (unsigned) 0, pixel, (unsigned) 1))
   5.481 +        return 0;
   5.482 +    if (*pixel != n) {
   5.483 +        XFreeColors(dpy, cmap, pixel, 1, (unsigned long) 0);
   5.484 +        return 0;
   5.485 +    }
   5.486 +    color->pixel = *pixel;
   5.487 +    color->flags = DoRed | DoGreen | DoBlue;
   5.488 +    color->red = request->red;
   5.489 +    color->green = request->green;
   5.490 +    color->blue = request->blue;
   5.491 +    XStoreColors(dpy, cmap, color, 1);
   5.492 +    return 1;
   5.493 +}
   5.494 +
   5.495 +
   5.496 +/****************************************************************************/
   5.497 +static int
   5.498 +compare(_Xconst void *e1, _Xconst void *e2)
   5.499 +{
   5.500 +    return ((int) (*(long *) e1 - *(long *) e2));
   5.501 +}
   5.502 +
   5.503 +
   5.504 +/****************************************************************************/
   5.505 +static Status
   5.506 +readonly_map(Display * dpy, XVisualInfo * vinfo, XStandardColormap * colormap)
   5.507 +{
   5.508 +    int i, last_pixel;
   5.509 +    XColor color;
   5.510 +
   5.511 +    last_pixel = (colormap->red_max + 1) * (colormap->green_max + 1) *
   5.512 +        (colormap->blue_max + 1) + colormap->base_pixel - 1;
   5.513 +
   5.514 +    for (i = colormap->base_pixel; i <= last_pixel; i++) {
   5.515 +
   5.516 +        color.pixel = (unsigned long) i;
   5.517 +        color.red = (unsigned short)
   5.518 +            (((i / colormap->red_mult) * 65535) / colormap->red_max);
   5.519 +
   5.520 +        if (vinfo->class == StaticColor) {
   5.521 +            color.green = (unsigned short)
   5.522 +                ((((i / colormap->green_mult) % (colormap->green_max + 1)) *
   5.523 +                  65535) / colormap->green_max);
   5.524 +            color.blue = (unsigned short)
   5.525 +                (((i % colormap->green_mult) * 65535) / colormap->blue_max);
   5.526 +        } else                  /* vinfo->class == GrayScale, old style allocation XXX */
   5.527 +            color.green = color.blue = color.red;
   5.528 +
   5.529 +        XAllocColor(dpy, colormap->colormap, &color);
   5.530 +        if (color.pixel != (unsigned long) i)
   5.531 +            return 0;
   5.532 +    }
   5.533 +    return 1;
   5.534 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/video/Xext/XmuStdCmap/DelCmap.c	Thu Jul 12 20:00:50 2007 +0000
     6.3 @@ -0,0 +1,71 @@
     6.4 +/* $Xorg: DelCmap.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
     6.5 +
     6.6 +/* 
     6.7 + 
     6.8 +Copyright 1989, 1998  The Open Group
     6.9 +
    6.10 +Permission to use, copy, modify, distribute, and sell this software and its
    6.11 +documentation for any purpose is hereby granted without fee, provided that
    6.12 +the above copyright notice appear in all copies and that both that
    6.13 +copyright notice and this permission notice appear in supporting
    6.14 +documentation.
    6.15 +
    6.16 +The above copyright notice and this permission notice shall be included in
    6.17 +all copies or substantial portions of the Software.
    6.18 +
    6.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    6.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    6.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    6.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    6.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    6.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    6.25 +
    6.26 +Except as contained in this notice, the name of The Open Group shall not be
    6.27 +used in advertising or otherwise to promote the sale, use or other dealings
    6.28 +in this Software without prior written authorization from The Open Group.
    6.29 +
    6.30 +*/
    6.31 +/* $XFree86: xc/lib/Xmu/DelCmap.c,v 1.6 2001/01/17 19:42:54 dawes Exp $ */
    6.32 +
    6.33 +/*
    6.34 + * Author:  Donna Converse, MIT X Consortium
    6.35 + */
    6.36 +
    6.37 +#ifdef HAVE_CONFIG_H
    6.38 +#include <config.h>
    6.39 +#endif
    6.40 +#include <X11/Xlib.h>
    6.41 +#include <X11/Xutil.h>
    6.42 +#include "../extensions/StdCmap.h"
    6.43 +
    6.44 +/* To remove any standard colormap property, use XmuDeleteStandardColormap().
    6.45 + * XmuDeleteStandardColormap() will remove the specified property from the
    6.46 + * specified screen, releasing any resources used by the colormap(s) of the
    6.47 + * property if possible.
    6.48 + */
    6.49 +
    6.50 +void
    6.51 +XmuDeleteStandardColormap(Display * dpy, int screen, Atom property)
    6.52 +     /* dpy;            - specifies the X server to connect to
    6.53 +      * screen          - specifies the screen of the display
    6.54 +      * property        - specifies the standard colormap property
    6.55 +      */
    6.56 +{
    6.57 +    XStandardColormap *stdcmaps, *s;
    6.58 +    int count = 0;
    6.59 +
    6.60 +    if (XGetRGBColormaps(dpy, RootWindow(dpy, screen), &stdcmaps, &count,
    6.61 +                         property)) {
    6.62 +        for (s = stdcmaps; count > 0; count--, s++) {
    6.63 +            if ((s->killid == ReleaseByFreeingColormap) &&
    6.64 +                (s->colormap != None) &&
    6.65 +                (s->colormap != DefaultColormap(dpy, screen)))
    6.66 +                XFreeColormap(dpy, s->colormap);
    6.67 +            else if (s->killid != None)
    6.68 +                XKillClient(dpy, s->killid);
    6.69 +        }
    6.70 +        XDeleteProperty(dpy, RootWindow(dpy, screen), property);
    6.71 +        XFree((char *) stdcmaps);
    6.72 +        XSync(dpy, False);
    6.73 +    }
    6.74 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/video/Xext/XmuStdCmap/Distinct.c	Thu Jul 12 20:00:50 2007 +0000
     7.3 @@ -0,0 +1,90 @@
     7.4 +/* $Xorg: Distinct.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
     7.5 +
     7.6 +/*
     7.7 +
     7.8 +Copyright 1990, 1998  The Open Group
     7.9 +
    7.10 +Permission to use, copy, modify, distribute, and sell this software and its
    7.11 +documentation for any purpose is hereby granted without fee, provided that
    7.12 +the above copyright notice appear in all copies and that both that
    7.13 +copyright notice and this permission notice appear in supporting
    7.14 +documentation.
    7.15 +
    7.16 +The above copyright notice and this permission notice shall be included in
    7.17 +all copies or substantial portions of the Software.
    7.18 +
    7.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    7.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    7.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    7.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    7.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    7.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    7.25 +
    7.26 +Except as contained in this notice, the name of The Open Group shall not be
    7.27 +used in advertising or otherwise to promote the sale, use or other dealings
    7.28 +in this Software without prior written authorization from The Open Group.
    7.29 +
    7.30 +*/
    7.31 +/* $XFree86: xc/lib/Xmu/Distinct.c,v 3.5 2001/07/25 15:04:50 dawes Exp $ */
    7.32 +
    7.33 +/*
    7.34 + * Author:  Keith Packard, MIT X Consortium
    7.35 + */
    7.36 +
    7.37 +#ifdef HAVE_CONFIG_H
    7.38 +#include <config.h>
    7.39 +#endif
    7.40 +#include <X11/Xlib.h>
    7.41 +#include <stdlib.h>
    7.42 +#include <X11/Xutil.h>
    7.43 +#include "../extensions/StdCmap.h"
    7.44 +
    7.45 +/*
    7.46 + * Distinguishable colors routine.  Determines if two colors are
    7.47 + * distinguishable or not.  Somewhat arbitrary meaning.
    7.48 + */
    7.49 +
    7.50 +#define MIN_DISTINGUISH	10000.0
    7.51 +
    7.52 +Bool
    7.53 +XmuDistinguishableColors(XColor * colors, int count)
    7.54 +{
    7.55 +    double deltaRed, deltaGreen, deltaBlue;
    7.56 +    double dist;
    7.57 +    int i, j;
    7.58 +
    7.59 +    for (i = 0; i < count - 1; i++)
    7.60 +        for (j = i + 1; j < count; j++) {
    7.61 +            deltaRed = (double) colors[i].red - (double) colors[j].red;
    7.62 +            deltaGreen = (double) colors[i].green - (double) colors[j].green;
    7.63 +            deltaBlue = (double) colors[i].blue - (double) colors[j].blue;
    7.64 +            dist = deltaRed * deltaRed +
    7.65 +                deltaGreen * deltaGreen + deltaBlue * deltaBlue;
    7.66 +            if (dist <= MIN_DISTINGUISH * MIN_DISTINGUISH)
    7.67 +                return False;
    7.68 +        }
    7.69 +    return True;
    7.70 +}
    7.71 +
    7.72 +Bool
    7.73 +XmuDistinguishablePixels(Display * dpy, Colormap cmap,
    7.74 +                         unsigned long *pixels, int count)
    7.75 +{
    7.76 +    XColor *defs;
    7.77 +    int i, j;
    7.78 +    Bool ret;
    7.79 +
    7.80 +    for (i = 0; i < count - 1; i++)
    7.81 +        for (j = i + 1; j < count; j++)
    7.82 +            if (pixels[i] == pixels[j])
    7.83 +                return False;
    7.84 +    defs = (XColor *) malloc(count * sizeof(XColor));
    7.85 +    if (!defs)
    7.86 +        return False;
    7.87 +    for (i = 0; i < count; i++)
    7.88 +        defs[i].pixel = pixels[i];
    7.89 +    XQueryColors(dpy, cmap, defs, count);
    7.90 +    ret = XmuDistinguishableColors(defs, count);
    7.91 +    free((char *) defs);
    7.92 +    return ret;
    7.93 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/video/Xext/XmuStdCmap/LookupCmap.c	Thu Jul 12 20:00:50 2007 +0000
     8.3 @@ -0,0 +1,323 @@
     8.4 +/* $Xorg: LookupCmap.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
     8.5 +
     8.6 +/* 
     8.7 + 
     8.8 +Copyright 1989, 1998  The Open Group
     8.9 +
    8.10 +Permission to use, copy, modify, distribute, and sell this software and its
    8.11 +documentation for any purpose is hereby granted without fee, provided that
    8.12 +the above copyright notice appear in all copies and that both that
    8.13 +copyright notice and this permission notice appear in supporting
    8.14 +documentation.
    8.15 +
    8.16 +The above copyright notice and this permission notice shall be included in
    8.17 +all copies or substantial portions of the Software.
    8.18 +
    8.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    8.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    8.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    8.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    8.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    8.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    8.25 +
    8.26 +Except as contained in this notice, the name of The Open Group shall not be
    8.27 +used in advertising or otherwise to promote the sale, use or other dealings
    8.28 +in this Software without prior written authorization from The Open Group.
    8.29 +
    8.30 +*/
    8.31 +/* $XFree86: xc/lib/Xmu/LookupCmap.c,v 1.7 2001/07/25 15:04:50 dawes Exp $ */
    8.32 +
    8.33 +/*
    8.34 + * Author:  Donna Converse, MIT X Consortium
    8.35 + */
    8.36 +
    8.37 +#ifdef HAVE_CONFIG_H
    8.38 +#include <config.h>
    8.39 +#endif
    8.40 +#include <stdio.h>
    8.41 +#include <X11/Xlib.h>
    8.42 +#include <X11/Xatom.h>
    8.43 +#include <X11/Xutil.h>
    8.44 +#include "../extensions/StdCmap.h"
    8.45 +#include <stdlib.h>
    8.46 +
    8.47 +/*
    8.48 + * Prototypes
    8.49 + */
    8.50 +static Status lookup(Display *, int, VisualID, Atom, XStandardColormap *,
    8.51 +                     Bool);
    8.52 +
    8.53 +/*
    8.54 + * To create a standard colormap if one does not currently exist, or
    8.55 + * replace the currently existing standard colormap, use 
    8.56 + * XmuLookupStandardColormap().
    8.57 + *
    8.58 + * Given a screen, a visual, and a property, XmuLookupStandardColormap()
    8.59 + * will determine the best allocation for the property under the specified
    8.60 + * visual, and determine the whether to create a new colormap or to use
    8.61 + * the default colormap of the screen.  It will call XmuStandardColormap()
    8.62 + * to create the standard colormap.
    8.63 + *
    8.64 + * If replace is true, any previous definition of the property will be 
    8.65 + * replaced.  If retain is true, the property and the colormap will be
    8.66 + * made permanent for the duration of the server session.  However,
    8.67 + * pre-existing property definitions which are not replaced cannot be made
    8.68 + * permanent by a call to XmuLookupStandardColormap(); a request to retain 
    8.69 + * resources pertains to newly created resources.
    8.70 + *
    8.71 + * Returns 0 on failure, non-zero on success.  A request to create a 
    8.72 + * standard colormap upon a visual which cannot support such a map is
    8.73 + * considered a failure.  An example of this would be requesting any
    8.74 + * standard colormap property on a monochrome visual, or, requesting an
    8.75 + * RGB_BEST_MAP on a display whose colormap size is 16.
    8.76 + */
    8.77 +
    8.78 +Status
    8.79 +XmuLookupStandardColormap(Display * dpy, int screen, VisualID visualid,
    8.80 +                          unsigned int depth, Atom property,
    8.81 +                          Bool replace, Bool retain)
    8.82 +     /*
    8.83 +      * dpy             - specifies X server connection
    8.84 +      * screen          - specifies screen of display
    8.85 +      * visualid        - specifies the visual type
    8.86 +      * depth           - specifies  the visual type
    8.87 +      * property        - a standard colormap property
    8.88 +      * replace         - specifies whether to replace
    8.89 +      * retain          - specifies whether to retain
    8.90 +      */
    8.91 +{
    8.92 +    Display *odpy;              /* original display connection */
    8.93 +    XStandardColormap *colormap;
    8.94 +    XVisualInfo vinfo_template, *vinfo; /* visual */
    8.95 +    long vinfo_mask;
    8.96 +    unsigned long r_max, g_max, b_max;  /* allocation */
    8.97 +    int count;
    8.98 +    Colormap cmap;              /* colormap ID */
    8.99 +    Status status = 0;
   8.100 +
   8.101 +
   8.102 +    /* Match the requested visual */
   8.103 +
   8.104 +    vinfo_template.visualid = visualid;
   8.105 +    vinfo_template.screen = screen;
   8.106 +    vinfo_template.depth = depth;
   8.107 +    vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
   8.108 +    if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) ==
   8.109 +        NULL)
   8.110 +        return 0;
   8.111 +
   8.112 +    /* Monochrome visuals have no standard maps */
   8.113 +
   8.114 +    if (vinfo->colormap_size <= 2) {
   8.115 +        XFree((char *) vinfo);
   8.116 +        return 0;
   8.117 +    }
   8.118 +
   8.119 +    /* If the requested property already exists on this screen, and, 
   8.120 +     * if the replace flag has not been set to true, return success.
   8.121 +     * lookup() will remove a pre-existing map if replace is true.
   8.122 +     */
   8.123 +
   8.124 +    if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL,
   8.125 +               replace) && !replace) {
   8.126 +        XFree((char *) vinfo);
   8.127 +        return 1;
   8.128 +    }
   8.129 +
   8.130 +    /* Determine the best allocation for this property under the requested
   8.131 +     * visualid and depth, and determine whether or not to use the default
   8.132 +     * colormap of the screen.
   8.133 +     */
   8.134 +
   8.135 +    if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
   8.136 +        XFree((char *) vinfo);
   8.137 +        return 0;
   8.138 +    }
   8.139 +
   8.140 +    cmap = (property == XA_RGB_DEFAULT_MAP &&
   8.141 +            visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
   8.142 +        ? DefaultColormap(dpy, screen) : None;
   8.143 +
   8.144 +    /* If retaining resources, open a new connection to the same server */
   8.145 +
   8.146 +    if (retain) {
   8.147 +        odpy = dpy;
   8.148 +        if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
   8.149 +            XFree((char *) vinfo);
   8.150 +            return 0;
   8.151 +        }
   8.152 +    }
   8.153 +
   8.154 +    /* Create the standard colormap */
   8.155 +
   8.156 +    colormap = XmuStandardColormap(dpy, screen, visualid, depth, property,
   8.157 +                                   cmap, r_max, g_max, b_max);
   8.158 +
   8.159 +    /* Set the standard colormap property */
   8.160 +
   8.161 +    if (colormap) {
   8.162 +        XGrabServer(dpy);
   8.163 +
   8.164 +        if (lookup(dpy, screen, visualid, property, colormap, replace) &&
   8.165 +            !replace) {
   8.166 +            /* Someone has defined the property since we last looked.
   8.167 +             * Since we will not replace it, release our own resources.
   8.168 +             * If this is the default map, our allocations will be freed 
   8.169 +             * when this connection closes.
   8.170 +             */
   8.171 +            if (colormap->killid == ReleaseByFreeingColormap)
   8.172 +                XFreeColormap(dpy, colormap->colormap);
   8.173 +        } else if (retain) {
   8.174 +            XSetCloseDownMode(dpy, RetainPermanent);
   8.175 +        }
   8.176 +        XUngrabServer(dpy);
   8.177 +        XFree((char *) colormap);
   8.178 +        status = 1;
   8.179 +    }
   8.180 +
   8.181 +    if (retain)
   8.182 +        XCloseDisplay(dpy);
   8.183 +    XFree((char *) vinfo);
   8.184 +    return status;
   8.185 +}
   8.186 +
   8.187 +/***************************************************************************/
   8.188 +
   8.189 +/* Lookup a standard colormap property.  If the property is RGB_DEFAULT_MAP,
   8.190 + * the visualid is used to determine whether the indicated standard colormap
   8.191 + * exists.  If the map exists and replace is true, delete the resources used
   8.192 + * by the map and remove the property.  Return true if the map exists,
   8.193 + * or did exist and was deleted; return false if the map was not found.
   8.194 + *
   8.195 + * Note that this is not the way that a Status return is normally used.
   8.196 + *
   8.197 + * If new is not NULL, new points to an XStandardColormap structure which
   8.198 + * describes a standard colormap of the specified property.  It will be made
   8.199 + * a standard colormap of the screen if none already exists, or if replace 
   8.200 + * is true.
   8.201 + */
   8.202 +
   8.203 +static Status
   8.204 +lookup(Display * dpy, int screen, VisualID visualid, Atom property,
   8.205 +       XStandardColormap * cnew, Bool replace)
   8.206 +     /*
   8.207 +      * dpy             - specifies display connection
   8.208 +      * screen          - specifies screen number
   8.209 +      * visualid        - specifies visualid for std map
   8.210 +      * property        - specifies colormap property name
   8.211 +      * cnew            - specifies a standard colormap
   8.212 +      * replace         - specifies whether to replace
   8.213 +      */
   8.214 +{
   8.215 +    register int i;
   8.216 +    int count;
   8.217 +    XStandardColormap *stdcmaps, *s;
   8.218 +    Window win = RootWindow(dpy, screen);
   8.219 +
   8.220 +    /* The property does not already exist */
   8.221 +
   8.222 +    if (!XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
   8.223 +        if (cnew)
   8.224 +            XSetRGBColormaps(dpy, win, cnew, 1, property);
   8.225 +        return 0;
   8.226 +    }
   8.227 +
   8.228 +    /* The property exists and is not describing the RGB_DEFAULT_MAP */
   8.229 +
   8.230 +    if (property != XA_RGB_DEFAULT_MAP) {
   8.231 +        if (replace) {
   8.232 +            XmuDeleteStandardColormap(dpy, screen, property);
   8.233 +            if (cnew)
   8.234 +                XSetRGBColormaps(dpy, win, cnew, 1, property);
   8.235 +        }
   8.236 +        XFree((char *) stdcmaps);
   8.237 +        return 1;
   8.238 +    }
   8.239 +
   8.240 +    /* The property exists and is RGB_DEFAULT_MAP */
   8.241 +
   8.242 +    for (i = 0, s = stdcmaps; (i < count) && (s->visualid != visualid);
   8.243 +         i++, s++);
   8.244 +
   8.245 +    /* No RGB_DEFAULT_MAP property matches the given visualid */
   8.246 +
   8.247 +    if (i == count) {
   8.248 +        if (cnew) {
   8.249 +            XStandardColormap *m, *maps;
   8.250 +
   8.251 +            s = (XStandardColormap *) malloc((unsigned) ((count + 1) * sizeof
   8.252 +                                                         (XStandardColormap)));
   8.253 +
   8.254 +            for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
   8.255 +                m->colormap = maps->colormap;
   8.256 +                m->red_max = maps->red_max;
   8.257 +                m->red_mult = maps->red_mult;
   8.258 +                m->green_max = maps->green_max;
   8.259 +                m->green_mult = maps->green_mult;
   8.260 +                m->blue_max = maps->blue_max;
   8.261 +                m->blue_mult = maps->blue_mult;
   8.262 +                m->base_pixel = maps->base_pixel;
   8.263 +                m->visualid = maps->visualid;
   8.264 +                m->killid = maps->killid;
   8.265 +            }
   8.266 +            m->colormap = cnew->colormap;
   8.267 +            m->red_max = cnew->red_max;
   8.268 +            m->red_mult = cnew->red_mult;
   8.269 +            m->green_max = cnew->green_max;
   8.270 +            m->green_mult = cnew->green_mult;
   8.271 +            m->blue_max = cnew->blue_max;
   8.272 +            m->blue_mult = cnew->blue_mult;
   8.273 +            m->base_pixel = cnew->base_pixel;
   8.274 +            m->visualid = cnew->visualid;
   8.275 +            m->killid = cnew->killid;
   8.276 +
   8.277 +            XSetRGBColormaps(dpy, win, s, ++count, property);
   8.278 +            free((char *) s);
   8.279 +        }
   8.280 +        XFree((char *) stdcmaps);
   8.281 +        return 0;
   8.282 +    }
   8.283 +
   8.284 +    /* Found an RGB_DEFAULT_MAP property with a matching visualid */
   8.285 +
   8.286 +    if (replace) {
   8.287 +        /* Free old resources first - we may need them, particularly in 
   8.288 +         * the default colormap of the screen.  However, because of this,
   8.289 +         * it is possible that we will destroy the old resource and fail 
   8.290 +         * to create a new one if XmuStandardColormap() fails.
   8.291 +         */
   8.292 +
   8.293 +        if (count == 1) {
   8.294 +            XmuDeleteStandardColormap(dpy, screen, property);
   8.295 +            if (cnew)
   8.296 +                XSetRGBColormaps(dpy, win, cnew, 1, property);
   8.297 +        } else {
   8.298 +            XStandardColormap *map;
   8.299 +
   8.300 +            /* s still points to the matching standard colormap */
   8.301 +
   8.302 +            if (s->killid == ReleaseByFreeingColormap) {
   8.303 +                if ((s->colormap != None) &&
   8.304 +                    (s->colormap != DefaultColormap(dpy, screen)))
   8.305 +                    XFreeColormap(dpy, s->colormap);
   8.306 +            } else if (s->killid != None)
   8.307 +                XKillClient(dpy, s->killid);
   8.308 +
   8.309 +            map = (cnew) ? cnew : stdcmaps + --count;
   8.310 +
   8.311 +            s->colormap = map->colormap;
   8.312 +            s->red_max = map->red_max;
   8.313 +            s->red_mult = map->red_mult;
   8.314 +            s->green_max = map->green_max;
   8.315 +            s->green_mult = map->green_mult;
   8.316 +            s->blue_max = map->blue_max;
   8.317 +            s->blue_mult = map->blue_mult;
   8.318 +            s->visualid = map->visualid;
   8.319 +            s->killid = map->killid;
   8.320 +
   8.321 +            XSetRGBColormaps(dpy, win, stdcmaps, count, property);
   8.322 +        }
   8.323 +    }
   8.324 +    XFree((char *) stdcmaps);
   8.325 +    return 1;
   8.326 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/video/Xext/XmuStdCmap/StdCmap.c	Thu Jul 12 20:00:50 2007 +0000
     9.3 @@ -0,0 +1,236 @@
     9.4 +/* $Xorg: StdCmap.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
     9.5 +
     9.6 +/* 
     9.7 +
     9.8 +Copyright 1989, 1998  The Open Group
     9.9 +
    9.10 +Permission to use, copy, modify, distribute, and sell this software and its
    9.11 +documentation for any purpose is hereby granted without fee, provided that
    9.12 +the above copyright notice appear in all copies and that both that
    9.13 +copyright notice and this permission notice appear in supporting
    9.14 +documentation.
    9.15 +
    9.16 +The above copyright notice and this permission notice shall be included in
    9.17 +all copies or substantial portions of the Software.
    9.18 +
    9.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    9.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    9.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    9.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    9.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    9.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    9.25 +
    9.26 +Except as contained in this notice, the name of The Open Group shall not be
    9.27 +used in advertising or otherwise to promote the sale, use or other dealings
    9.28 +in this Software without prior written authorization from The Open Group.
    9.29 +
    9.30 +*/
    9.31 +/* $XFree86: xc/lib/Xmu/StdCmap.c,v 1.5 2001/01/17 19:42:56 dawes Exp $ */
    9.32 +
    9.33 +/*
    9.34 + * Author:  Donna Converse, MIT X Consortium
    9.35 + */
    9.36 +
    9.37 +#ifdef HAVE_CONFIG_H
    9.38 +#include <config.h>
    9.39 +#endif
    9.40 +#include <stdio.h>
    9.41 +#include <X11/Xlib.h>
    9.42 +#include <X11/Xatom.h>
    9.43 +#include <X11/Xutil.h>
    9.44 +#include "../extensions/StdCmap.h"
    9.45 +
    9.46 +#define lowbit(x) ((x) & (~(x) + 1))
    9.47 +
    9.48 +/*
    9.49 + * Prototypes
    9.50 + */
    9.51 +/* argument restrictions */
    9.52 +static Status valid_args(XVisualInfo *, unsigned long, unsigned long,
    9.53 +                         unsigned long, Atom);
    9.54 +
    9.55 +/*
    9.56 + * To create any one standard colormap, use XmuStandardColormap().
    9.57 + *
    9.58 + * Create a standard colormap for the given screen, visualid, and visual
    9.59 + * depth, with the given red, green, and blue maximum values, with the
    9.60 + * given standard property name.  Return a pointer to an XStandardColormap
    9.61 + * structure which describes the newly created colormap, upon success.
    9.62 + * Upon failure, return NULL.
    9.63 + * 
    9.64 + * XmuStandardColormap() calls XmuCreateColormap() to create the map.
    9.65 + *
    9.66 + * Resources created by this function are not made permanent; that is the
    9.67 + * caller's responsibility.
    9.68 + */
    9.69 +
    9.70 +XStandardColormap *
    9.71 +XmuStandardColormap(Display * dpy, int screen, VisualID visualid,
    9.72 +                    unsigned int depth, Atom property, Colormap cmap,
    9.73 +                    unsigned long red_max, unsigned long green_max,
    9.74 +                    unsigned long blue_max)
    9.75 +     /*
    9.76 +      * dpy                             - specifies X server connection
    9.77 +      * screen                          - specifies display screen
    9.78 +      * visualid                        - identifies the visual type
    9.79 +      * depth                           - identifies the visual type
    9.80 +      * property                        - a standard colormap property
    9.81 +      * cmap                            - specifies colormap ID or None
    9.82 +      * red_max, green_max, blue_max    - allocations
    9.83 +      */
    9.84 +{
    9.85 +    XStandardColormap *stdcmap;
    9.86 +    Status status;
    9.87 +    XVisualInfo vinfo_template, *vinfo;
    9.88 +    long vinfo_mask;
    9.89 +    int n;
    9.90 +
    9.91 +    /* Match the required visual information to an actual visual */
    9.92 +    vinfo_template.visualid = visualid;
    9.93 +    vinfo_template.screen = screen;
    9.94 +    vinfo_template.depth = depth;
    9.95 +    vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
    9.96 +    if ((vinfo =
    9.97 +         XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
    9.98 +        return 0;
    9.99 +
   9.100 +    /* Check the validity of the combination of visual characteristics,
   9.101 +     * allocation, and colormap property.  Create an XStandardColormap
   9.102 +     * structure.
   9.103 +     */
   9.104 +
   9.105 +    if (!valid_args(vinfo, red_max, green_max, blue_max, property)
   9.106 +        || ((stdcmap = XAllocStandardColormap()) == NULL)) {
   9.107 +        XFree((char *) vinfo);
   9.108 +        return 0;
   9.109 +    }
   9.110 +
   9.111 +    /* Fill in the XStandardColormap structure */
   9.112 +
   9.113 +    if (cmap == DefaultColormap(dpy, screen)) {
   9.114 +        /* Allocating out of the default map, cannot use XFreeColormap() */
   9.115 +        Window win = XCreateWindow(dpy, RootWindow(dpy, screen), 1, 1, 1, 1,
   9.116 +                                   0, 0, InputOnly, vinfo->visual,
   9.117 +                                   (unsigned long) 0,
   9.118 +                                   (XSetWindowAttributes *) NULL);
   9.119 +        stdcmap->killid = (XID) XCreatePixmap(dpy, win, 1, 1, depth);
   9.120 +        XDestroyWindow(dpy, win);
   9.121 +        stdcmap->colormap = cmap;
   9.122 +    } else {
   9.123 +        stdcmap->killid = ReleaseByFreeingColormap;
   9.124 +        stdcmap->colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
   9.125 +                                            vinfo->visual, AllocNone);
   9.126 +    }
   9.127 +    stdcmap->red_max = red_max;
   9.128 +    stdcmap->green_max = green_max;
   9.129 +    stdcmap->blue_max = blue_max;
   9.130 +    if (property == XA_RGB_GRAY_MAP)
   9.131 +        stdcmap->red_mult = stdcmap->green_mult = stdcmap->blue_mult = 1;
   9.132 +    else if (vinfo->class == TrueColor || vinfo->class == DirectColor) {
   9.133 +        stdcmap->red_mult = lowbit(vinfo->red_mask);
   9.134 +        stdcmap->green_mult = lowbit(vinfo->green_mask);
   9.135 +        stdcmap->blue_mult = lowbit(vinfo->blue_mask);
   9.136 +    } else {
   9.137 +        stdcmap->red_mult = (red_max > 0)
   9.138 +            ? (green_max + 1) * (blue_max + 1) : 0;
   9.139 +        stdcmap->green_mult = (green_max > 0) ? blue_max + 1 : 0;
   9.140 +        stdcmap->blue_mult = (blue_max > 0) ? 1 : 0;
   9.141 +    }
   9.142 +    stdcmap->base_pixel = 0;    /* base pixel may change */
   9.143 +    stdcmap->visualid = vinfo->visualid;
   9.144 +
   9.145 +    /* Make the colormap */
   9.146 +
   9.147 +    status = XmuCreateColormap(dpy, stdcmap);
   9.148 +
   9.149 +    /* Clean up */
   9.150 +
   9.151 +    XFree((char *) vinfo);
   9.152 +    if (!status) {
   9.153 +
   9.154 +        /* Free the colormap or the pixmap, if we created one */
   9.155 +        if (stdcmap->killid == ReleaseByFreeingColormap)
   9.156 +            XFreeColormap(dpy, stdcmap->colormap);
   9.157 +        else if (stdcmap->killid != None)
   9.158 +            XFreePixmap(dpy, stdcmap->killid);
   9.159 +
   9.160 +        XFree((char *) stdcmap);
   9.161 +        return (XStandardColormap *) NULL;
   9.162 +    }
   9.163 +    return stdcmap;
   9.164 +}
   9.165 +
   9.166 +/****************************************************************************/
   9.167 +static Status
   9.168 +valid_args(XVisualInfo * vinfo, unsigned long red_max,
   9.169 +           unsigned long green_max, unsigned long blue_max, Atom property)
   9.170 +     /*
   9.171 +      * vinfo                           - specifies visual
   9.172 +      * red_max, green_max, blue_max    - specifies alloc
   9.173 +      * property                        - specifies property name
   9.174 +      */
   9.175 +{
   9.176 +    unsigned long ncolors;      /* number of colors requested */
   9.177 +
   9.178 +    /* Determine that the number of colors requested is <= map size */
   9.179 +
   9.180 +    if ((vinfo->class == DirectColor) || (vinfo->class == TrueColor)) {
   9.181 +        unsigned long mask;
   9.182 +
   9.183 +        mask = vinfo->red_mask;
   9.184 +        while (!(mask & 1))
   9.185 +            mask >>= 1;
   9.186 +        if (red_max > mask)
   9.187 +            return 0;
   9.188 +        mask = vinfo->green_mask;
   9.189 +        while (!(mask & 1))
   9.190 +            mask >>= 1;
   9.191 +        if (green_max > mask)
   9.192 +            return 0;
   9.193 +        mask = vinfo->blue_mask;
   9.194 +        while (!(mask & 1))
   9.195 +            mask >>= 1;
   9.196 +        if (blue_max > mask)
   9.197 +            return 0;
   9.198 +    } else if (property == XA_RGB_GRAY_MAP) {
   9.199 +        ncolors = red_max + green_max + blue_max + 1;
   9.200 +        if (ncolors > vinfo->colormap_size)
   9.201 +            return 0;
   9.202 +    } else {
   9.203 +        ncolors = (red_max + 1) * (green_max + 1) * (blue_max + 1);
   9.204 +        if (ncolors > vinfo->colormap_size)
   9.205 +            return 0;
   9.206 +    }
   9.207 +
   9.208 +    /* Determine that the allocation and visual make sense for the property */
   9.209 +
   9.210 +    switch (property) {
   9.211 +    case XA_RGB_DEFAULT_MAP:
   9.212 +        if (red_max == 0 || green_max == 0 || blue_max == 0)
   9.213 +            return 0;
   9.214 +        break;
   9.215 +    case XA_RGB_RED_MAP:
   9.216 +        if (red_max == 0)
   9.217 +            return 0;
   9.218 +        break;
   9.219 +    case XA_RGB_GREEN_MAP:
   9.220 +        if (green_max == 0)
   9.221 +            return 0;
   9.222 +        break;
   9.223 +    case XA_RGB_BLUE_MAP:
   9.224 +        if (blue_max == 0)
   9.225 +            return 0;
   9.226 +        break;
   9.227 +    case XA_RGB_BEST_MAP:
   9.228 +        if (red_max == 0 || green_max == 0 || blue_max == 0)
   9.229 +            return 0;
   9.230 +        break;
   9.231 +    case XA_RGB_GRAY_MAP:
   9.232 +        if (red_max == 0 || blue_max == 0 || green_max == 0)
   9.233 +            return 0;
   9.234 +        break;
   9.235 +    default:
   9.236 +        return 0;
   9.237 +    }
   9.238 +    return 1;
   9.239 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/video/Xext/XmuStdCmap/VisCmap.c	Thu Jul 12 20:00:50 2007 +0000
    10.3 @@ -0,0 +1,184 @@
    10.4 +/* $Xorg: VisCmap.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
    10.5 +
    10.6 +/* 
    10.7 +
    10.8 +Copyright 1989, 1998  The Open Group
    10.9 +
   10.10 +Permission to use, copy, modify, distribute, and sell this software and its
   10.11 +documentation for any purpose is hereby granted without fee, provided that
   10.12 +the above copyright notice appear in all copies and that both that
   10.13 +copyright notice and this permission notice appear in supporting
   10.14 +documentation.
   10.15 +
   10.16 +The above copyright notice and this permission notice shall be included in
   10.17 +all copies or substantial portions of the Software.
   10.18 +
   10.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   10.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   10.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
   10.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   10.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   10.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   10.25 +
   10.26 +Except as contained in this notice, the name of The Open Group shall not be
   10.27 +used in advertising or otherwise to promote the sale, use or other dealings
   10.28 +in this Software without prior written authorization from The Open Group.
   10.29 +
   10.30 +*/
   10.31 +/* $XFree86: xc/lib/Xmu/VisCmap.c,v 1.6 2001/01/17 19:42:57 dawes Exp $ */
   10.32 +
   10.33 +/*
   10.34 + * Author:  Donna Converse, MIT X Consortium
   10.35 + */
   10.36 +
   10.37 +#ifdef HAVE_CONFIG_H
   10.38 +#include <config.h>
   10.39 +#endif
   10.40 +#include <stdio.h>
   10.41 +#include <math.h>
   10.42 +#include <X11/Xlib.h>
   10.43 +#include <X11/Xatom.h>
   10.44 +#include <X11/Xutil.h>
   10.45 +#include "../extensions/StdCmap.h"
   10.46 +
   10.47 +/*
   10.48 + * To create all of the appropriate standard colormaps for a given visual on
   10.49 + * a given screen, use XmuVisualStandardColormaps.
   10.50 + * 
   10.51 + * Define all appropriate standard colormap properties for the given visual.
   10.52 + * If replace is true, any previous definition will be removed.
   10.53 + * If retain is true, new properties will be retained for the duration of
   10.54 + * the server session.  Return 0 on failure, non-zero on success.
   10.55 + * On failure, no new properties will be defined, and, old ones may have
   10.56 + * been removed if replace was True.
   10.57 + *
   10.58 + * Not all standard colormaps are meaningful to all visual classes.  This
   10.59 + * routine will check and define the following properties for the following
   10.60 + * classes, provided that the size of the colormap is not too small.
   10.61 + *
   10.62 + *	DirectColor and PseudoColor
   10.63 + *	    RGB_DEFAULT_MAP
   10.64 + *	    RGB_BEST_MAP
   10.65 + *	    RGB_RED_MAP
   10.66 + *	    RGB_GREEN_MAP
   10.67 + * 	    RGB_BLUE_MAP
   10.68 + *          RGB_GRAY_MAP
   10.69 + *
   10.70 + *	TrueColor and StaticColor
   10.71 + *	    RGB_BEST_MAP
   10.72 + *
   10.73 + *	GrayScale and StaticGray
   10.74 + *	    RGB_GRAY_MAP
   10.75 + */
   10.76 +
   10.77 +Status
   10.78 +XmuVisualStandardColormaps(Display * dpy, int screen, VisualID visualid,
   10.79 +                           unsigned int depth, Bool replace, Bool retain)
   10.80 +     /*
   10.81 +      * dpy                     - specifies server connection
   10.82 +      * screen                  - specifies screen number
   10.83 +      * visualid                - specifies the visual
   10.84 +      * depth                   - specifies the visual
   10.85 +      * replace specifies       - whether to replace
   10.86 +      * retain                  - specifies whether to retain
   10.87 +      */
   10.88 +{
   10.89 +    Status status;
   10.90 +    int n;
   10.91 +    long vinfo_mask;
   10.92 +    XVisualInfo vinfo_template, *vinfo;
   10.93 +
   10.94 +    status = 0;
   10.95 +    vinfo_template.screen = screen;
   10.96 +    vinfo_template.visualid = visualid;
   10.97 +    vinfo_template.depth = depth;
   10.98 +    vinfo_mask = VisualScreenMask | VisualIDMask | VisualDepthMask;
   10.99 +    if ((vinfo =
  10.100 +         XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
  10.101 +        return 0;
  10.102 +
  10.103 +    if (vinfo->colormap_size <= 2) {
  10.104 +        /* Monochrome visuals have no standard maps; considered successful */
  10.105 +        XFree((char *) vinfo);
  10.106 +        return 1;
  10.107 +    }
  10.108 +
  10.109 +    switch (vinfo->class) {
  10.110 +    case PseudoColor:
  10.111 +    case DirectColor:
  10.112 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.113 +                                           XA_RGB_DEFAULT_MAP, replace,
  10.114 +                                           retain);
  10.115 +        if (!status)
  10.116 +            break;
  10.117 +
  10.118 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.119 +                                           XA_RGB_GRAY_MAP, replace, retain);
  10.120 +        if (!status) {
  10.121 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.122 +            break;
  10.123 +        }
  10.124 +
  10.125 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.126 +                                           XA_RGB_RED_MAP, replace, retain);
  10.127 +        if (!status) {
  10.128 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.129 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  10.130 +            break;
  10.131 +        }
  10.132 +
  10.133 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.134 +                                           XA_RGB_GREEN_MAP, replace, retain);
  10.135 +        if (!status) {
  10.136 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.137 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  10.138 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  10.139 +            break;
  10.140 +        }
  10.141 +
  10.142 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.143 +                                           XA_RGB_BLUE_MAP, replace, retain);
  10.144 +        if (!status) {
  10.145 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.146 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  10.147 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  10.148 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
  10.149 +            break;
  10.150 +        }
  10.151 +        /* fall through */
  10.152 +
  10.153 +    case StaticColor:
  10.154 +    case TrueColor:
  10.155 +
  10.156 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.157 +                                           XA_RGB_BEST_MAP, replace, retain);
  10.158 +        if (!status && (vinfo->class == PseudoColor ||
  10.159 +                        vinfo->class == DirectColor)) {
  10.160 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.161 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  10.162 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  10.163 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
  10.164 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_BLUE_MAP);
  10.165 +        }
  10.166 +        break;
  10.167 +        /* the end for PseudoColor, DirectColor, StaticColor, and TrueColor */
  10.168 +
  10.169 +    case GrayScale:
  10.170 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.171 +                                           XA_RGB_DEFAULT_MAP, replace,
  10.172 +                                           retain);
  10.173 +        if (!status)
  10.174 +            break;
  10.175 +     /*FALLTHROUGH*/ case StaticGray:
  10.176 +
  10.177 +        status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  10.178 +                                           XA_RGB_GRAY_MAP, replace, retain);
  10.179 +        if (!status && vinfo->class == GrayScale) {
  10.180 +            XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  10.181 +            break;
  10.182 +        }
  10.183 +    }
  10.184 +
  10.185 +    XFree((char *) vinfo);
  10.186 +    return status;
  10.187 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/video/Xext/extensions/StdCmap.h	Thu Jul 12 20:00:50 2007 +0000
    11.3 @@ -0,0 +1,80 @@
    11.4 +/* $Xorg: StdCmap.h,v 1.5 2001/02/09 02:03:53 xorgcvs Exp $ */
    11.5 +
    11.6 +/*
    11.7 + 
    11.8 +Copyright 1988, 1998  The Open Group
    11.9 +
   11.10 +Permission to use, copy, modify, distribute, and sell this software and its
   11.11 +documentation for any purpose is hereby granted without fee, provided that
   11.12 +the above copyright notice appear in all copies and that both that
   11.13 +copyright notice and this permission notice appear in supporting
   11.14 +documentation.
   11.15 +
   11.16 +The above copyright notice and this permission notice shall be included in
   11.17 +all copies or substantial portions of the Software.
   11.18 +
   11.19 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   11.20 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   11.21 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
   11.22 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   11.23 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   11.24 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   11.25 +
   11.26 +Except as contained in this notice, the name of The Open Group shall not be
   11.27 +used in advertising or otherwise to promote the sale, use or other dealings
   11.28 +in this Software without prior written authorization from The Open Group.
   11.29 +
   11.30 +*/
   11.31 +/* $XFree86: xc/lib/Xmu/StdCmap.h,v 1.8 2001/01/23 17:38:14 keithp Exp $ */
   11.32 +
   11.33 +/*
   11.34 + * The interfaces described by this header file are for miscellaneous utilities
   11.35 + * and are not part of the Xlib standard.
   11.36 + */
   11.37 +
   11.38 +#ifndef _XMU_STDCMAP_H_
   11.39 +#define _XMU_STDCMAP_H_
   11.40 +
   11.41 +#include <X11/Xfuncproto.h>
   11.42 +#include "SDL_name.h"
   11.43 +
   11.44 +_XFUNCPROTOBEGIN extern Status XmuAllStandardColormaps(Display * dpy);
   11.45 +
   11.46 +extern Status XmuCreateColormap(Display * dpy, XStandardColormap * colormap);
   11.47 +
   11.48 +extern void XmuDeleteStandardColormap
   11.49 +    (Display * dpy, int screen, Atom property);
   11.50 +
   11.51 +extern Status XmuGetColormapAllocation
   11.52 +    (XVisualInfo * vinfo,
   11.53 +     Atom property,
   11.54 +     unsigned long *red_max_return,
   11.55 +     unsigned long *green_max_return, unsigned long *blue_max_return);
   11.56 +
   11.57 +extern Status XmuLookupStandardColormap
   11.58 +    (Display * dpy,
   11.59 +     int screen,
   11.60 +     VisualID visualid,
   11.61 +     unsigned int depth, Atom property, Bool replace, Bool retain);
   11.62 +
   11.63 +extern XStandardColormap *XmuStandardColormap
   11.64 +    (Display * dpy,
   11.65 +     int screen,
   11.66 +     VisualID visualid,
   11.67 +     unsigned int depth,
   11.68 +     Atom property,
   11.69 +     Colormap cmap,
   11.70 +     unsigned long red_max, unsigned long green_max, unsigned long blue_max);
   11.71 +
   11.72 +extern Status XmuVisualStandardColormaps
   11.73 +    (Display * dpy,
   11.74 +     int screen,
   11.75 +     VisualID visualid, unsigned int depth, Bool replace, Bool retain);
   11.76 +
   11.77 +extern Bool XmuDistinguishableColors(XColor * colors, int count);
   11.78 +
   11.79 +extern Bool XmuDistinguishablePixels
   11.80 +    (Display * dpy, Colormap cmap, unsigned long *pixels, int count);
   11.81 +
   11.82 +_XFUNCPROTOEND
   11.83 +#endif /* _XMU_STDCMAP_H_ */
    12.1 --- a/src/video/x11/SDL_x11sym.h	Thu Jul 12 08:07:30 2007 +0000
    12.2 +++ b/src/video/x11/SDL_x11sym.h	Thu Jul 12 20:00:50 2007 +0000
    12.3 @@ -25,7 +25,9 @@
    12.4  SDL_X11_MODULE(BASEXLIB)
    12.5  SDL_X11_SYM(XClassHint*,XAllocClassHint,(void),(),return)
    12.6  SDL_X11_SYM(Status,XAllocColor,(Display* a,Colormap b,XColor* c),(a,b,c),return)
    12.7 +SDL_X11_SYM(Status,XAllocColorCells,(Display *a,Colormap b,Bool c,unsigned long d[],unsigned int e,unsigned long f[],unsigned int g),(a,b,c,d,e,f,g),return)
    12.8  SDL_X11_SYM(XSizeHints*,XAllocSizeHints,(void),(),return)
    12.9 +SDL_X11_SYM(XStandardColormap *,XAllocStandardColormap,(void),(),return)
   12.10  SDL_X11_SYM(XWMHints*,XAllocWMHints,(void),(),return)
   12.11  SDL_X11_SYM(int,XChangePointerControl,(Display* a,Bool b,Bool c,int d,int e,int f),(a,b,c,d,e,f),return)
   12.12  SDL_X11_SYM(int,XChangeProperty,(Display* a,Window b,Atom c,Atom d,int e,int f,_Xconst unsigned char* g,int h),(a,b,c,d,e,f,g,h),return)
   12.13 @@ -46,7 +48,7 @@
   12.14  SDL_X11_SYM(int,XDestroyWindow,(Display* a,Window b),(a,b),return)
   12.15  SDL_X11_SYM(char*,XDisplayName,(_Xconst char* a),(a),return)
   12.16  SDL_X11_SYM(int,XEventsQueued,(Display* a,int b),(a,b),return)
   12.17 -SDL_X11_SYM(Bool,XFilterEvent,(XEvent *event, Window w),(event,w),return)
   12.18 +SDL_X11_SYM(Bool,XFilterEvent,(XEvent *event,Window w),(event,w),return)
   12.19  SDL_X11_SYM(int,XFlush,(Display* a),(a),return)
   12.20  SDL_X11_SYM(int,XFree,(void*a),(a),return)
   12.21  SDL_X11_SYM(int,XFreeColormap,(Display* a,Colormap b),(a,b),return)
   12.22 @@ -58,15 +60,18 @@
   12.23  SDL_X11_SYM(int,XGetErrorDatabaseText,(Display* a,_Xconst char* b,_Xconst char* c,_Xconst char* d,char* e,int f),(a,b,c,d,e,f),return)
   12.24  SDL_X11_SYM(XModifierKeymap*,XGetModifierMapping,(Display* a),(a),return)
   12.25  SDL_X11_SYM(int,XGetPointerControl,(Display* a,int* b,int* c,int* d),(a,b,c,d),return)
   12.26 -SDL_X11_SYM(int,XGetScreenSaver,(Display* a,int* b,int* c,int* d, int* e),(a,b,c,d,e),return)
   12.27 +SDL_X11_SYM(int,XGetRGBColormaps,(Display* a,Window b,XStandardColormap **c,int *d,Atom e),(a,b,c,d,e),return)
   12.28 +SDL_X11_SYM(int,XGetScreenSaver,(Display* a,int* b,int* c,int* d,int* e),(a,b,c,d,e),return)
   12.29  SDL_X11_SYM(XVisualInfo*,XGetVisualInfo,(Display* a,long b,XVisualInfo* c,int* d),(a,b,c,d),return)
   12.30  SDL_X11_SYM(XWMHints*,XGetWMHints,(Display* a,Window b),(a,b),return)
   12.31  SDL_X11_SYM(Status,XGetWindowAttributes,(Display* a,Window b,XWindowAttributes* c),(a,b,c),return)
   12.32  SDL_X11_SYM(int,XGrabKeyboard,(Display* a,Window b,Bool c,int d,int e,Time f),(a,b,c,d,e,f),return)
   12.33  SDL_X11_SYM(int,XGrabPointer,(Display* a,Window b,Bool c,unsigned int d,int e,int f,Window g,Cursor h,Time i),(a,b,c,d,e,f,g,h,i),return)
   12.34 +SDL_X11_SYM(int,XGrabServer,(Display* a),(a),return)
   12.35  SDL_X11_SYM(Status,XIconifyWindow,(Display* a,Window b,int c),(a,b,c),return)
   12.36  SDL_X11_SYM(int,XInstallColormap,(Display* a,Colormap b),(a,b),return)
   12.37  SDL_X11_SYM(KeyCode,XKeysymToKeycode,(Display* a,KeySym b),(a,b),return)
   12.38 +SDL_X11_SYM(int,XKillClient,(Display* a,XID b),(a,b),return)
   12.39  SDL_X11_SYM(Atom,XInternAtom,(Display* a,_Xconst char* b,Bool c),(a,b,c),return)
   12.40  SDL_X11_SYM(XPixmapFormatValues*,XListPixmapFormats,(Display* a,int* b),(a,b),return)
   12.41  SDL_X11_SYM(int,XLookupString,(XKeyEvent* a,char* b,int c,KeySym* d,XComposeStatus* e),(a,b,c,d,e),return)
   12.42 @@ -91,8 +96,10 @@
   12.43  SDL_X11_SYM(int,XSelectInput,(Display* a,Window b,long c),(a,b,c),return)
   12.44  SDL_X11_SYM(Status,XSendEvent,(Display* a,Window b,Bool c,long d,XEvent* e),(a,b,c,d,e),return)
   12.45  SDL_X11_SYM(int,XSetClassHint,(Display* a,Window b,XClassHint* c),(a,b,c),return)
   12.46 +SDL_X11_SYM(int,XSetCloseDownMode,(Display *a, int b),(a,b),return)
   12.47  SDL_X11_SYM(XErrorHandler,XSetErrorHandler,(XErrorHandler a),(a),return)
   12.48  SDL_X11_SYM(XIOErrorHandler,XSetIOErrorHandler,(XIOErrorHandler a),(a),return)
   12.49 +SDL_X11_SYM(void,XSetRGBColormaps,( Display *a,Window b,XStandardColormap *c,int d,Atom e),(a,b,c,d,e),return)
   12.50  SDL_X11_SYM(int,XSetScreenSaver,(Display* a,int b,int c,int d,int e),(a,b,c,d,e),return)
   12.51  SDL_X11_SYM(int,XSetTransientForHint,(Display* a,Window b,Window c),(a,b,c),return)
   12.52  SDL_X11_SYM(int,XSetWMHints,(Display* a,Window b,XWMHints* c),(a,b,c),return)
   12.53 @@ -107,8 +114,9 @@
   12.54  SDL_X11_SYM(int,XSync,(Display* a,Bool b),(a,b),return)
   12.55  SDL_X11_SYM(int,XUngrabKeyboard,(Display* a,Time b),(a,b),return)
   12.56  SDL_X11_SYM(int,XUngrabPointer,(Display* a,Time b),(a,b),return)
   12.57 +SDL_X11_SYM(int,XUngrabServer,(Display* a),(a),return)
   12.58  SDL_X11_SYM(int,XUnmapWindow,(Display* a,Window b),(a,b),return)
   12.59 -SDL_X11_SYM(int,XWarpPointer,(Display* a,Window b,Window c,int d,int e,unsigned int f,unsigned int g,int h, int i),(a,b,c,d,e,f,g,h,i),return)
   12.60 +SDL_X11_SYM(int,XWarpPointer,(Display* a,Window b,Window c,int d,int e,unsigned int f,unsigned int g,int h,int i),(a,b,c,d,e,f,g,h,i),return)
   12.61  SDL_X11_SYM(VisualID,XVisualIDFromVisual,(Visual* a),(a),return)
   12.62  SDL_X11_SYM(XExtDisplayInfo*,XextAddDisplay,(XExtensionInfo* a,Display* b,char* c,XExtensionHooks* d,int e,XPointer f),(a,b,c,d,e,f),return)
   12.63  SDL_X11_SYM(XExtensionInfo*,XextCreateExtension,(void),(),return)
   12.64 @@ -174,9 +182,9 @@
   12.65  #if defined(__osf__)
   12.66  SDL_X11_MODULE(OSF_ENTRY_POINTS)
   12.67  SDL_X11_SYM(void,_SmtBufferOverflow,(Display *dpy,register smtDisplayPtr p),(dpy,p),)
   12.68 -SDL_X11_SYM(void,_SmtIpError,(Display *dpy,register smtDisplayPtr p, int i),(dpy,p,i),)
   12.69 -SDL_X11_SYM(int,ipAllocateData,(ChannelPtr a, IPCard b, IPDataPtr * c),(a,b,c),return)
   12.70 -SDL_X11_SYM(int,ipUnallocateAndSendData,(ChannelPtr a, IPCard b),(a,b),return)
   12.71 +SDL_X11_SYM(void,_SmtIpError,(Display *dpy,register smtDisplayPtr p,int i),(dpy,p,i),)
   12.72 +SDL_X11_SYM(int,ipAllocateData,(ChannelPtr a,IPCard b,IPDataPtr * c),(a,b,c),return)
   12.73 +SDL_X11_SYM(int,ipUnallocateAndSendData,(ChannelPtr a,IPCard b),(a,b),return)
   12.74  #endif
   12.75  
   12.76  /* Xrandr support. */
   12.77 @@ -185,8 +193,8 @@
   12.78  SDL_X11_SYM(Status,XRRQueryVersion,(Display *dpy,int *major_versionp,int *minor_versionp),(dpy,major_versionp,minor_versionp),return)
   12.79  SDL_X11_SYM(XRRScreenConfiguration *,XRRGetScreenInfo,(Display *dpy,Drawable draw),(dpy,draw),return)
   12.80  SDL_X11_SYM(SizeID,XRRConfigCurrentConfiguration,(XRRScreenConfiguration *config,Rotation *rotation),(config,rotation),return)
   12.81 -SDL_X11_SYM(XRRScreenSize *,XRRConfigSizes,(XRRScreenConfiguration *config, int *nsizes),(config,nsizes),return)
   12.82 -SDL_X11_SYM(Status,XRRSetScreenConfig,(Display *dpy, XRRScreenConfiguration *config, Drawable draw, int size_index, Rotation rotation, Time timestamp),(dpy,config,draw,size_index,rotation,timestamp),return)
   12.83 +SDL_X11_SYM(XRRScreenSize *,XRRConfigSizes,(XRRScreenConfiguration *config,int *nsizes),(config,nsizes),return)
   12.84 +SDL_X11_SYM(Status,XRRSetScreenConfig,(Display *dpy,XRRScreenConfiguration *config,Drawable draw,int size_index,Rotation rotation,Time timestamp),(dpy,config,draw,size_index,rotation,timestamp),return)
   12.85  SDL_X11_SYM(void,XRRFreeScreenConfigInfo,(XRRScreenConfiguration *config),(config),)
   12.86  #endif
   12.87  
    13.1 --- a/src/video/x11/SDL_x11window.c	Thu Jul 12 08:07:30 2007 +0000
    13.2 +++ b/src/video/x11/SDL_x11window.c	Thu Jul 12 20:00:50 2007 +0000
    13.3 @@ -26,7 +26,7 @@
    13.4  #include "../../events/SDL_keyboard_c.h"
    13.5  
    13.6  #include "SDL_x11video.h"
    13.7 -
    13.8 +#include "../Xext/extensions/StdCmap.h"
    13.9  
   13.10  static int
   13.11  SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
   13.12 @@ -181,11 +181,40 @@
   13.13      }
   13.14      xattr.background_pixel = 0;
   13.15      xattr.border_pixel = 0;
   13.16 -    if (visual->class == PseudoColor || visual->class == DirectColor) {
   13.17 -        xattr.colormap =
   13.18 -            XCreateColormap(data->display,
   13.19 -                            RootWindow(data->display, displaydata->screen),
   13.20 -                            visual, AllocAll);
   13.21 +    if (visual->class == DirectColor || visual->class == TrueColor) {
   13.22 +        int nmaps;
   13.23 +        XStandardColormap *stdmaps;
   13.24 +        int i;
   13.25 +        Bool found = False;
   13.26 +
   13.27 +        if (0 != XGetRGBColormaps(data->display,
   13.28 +                                  RootWindow(data->display,
   13.29 +                                             displaydata->screen), &stdmaps,
   13.30 +                                  &nmaps, XA_RGB_BEST_MAP)) {
   13.31 +            for (i = 0; i < nmaps; i++) {
   13.32 +                if (stdmaps[i].visualid == visual->visualid) {
   13.33 +                    xattr.colormap = stdmaps[i].colormap;
   13.34 +                    found = True;
   13.35 +                    break;
   13.36 +                }
   13.37 +            }
   13.38 +            XFree(stdmaps);
   13.39 +        }
   13.40 +        if (!found) {
   13.41 +            int max = visual->map_entries - 1;
   13.42 +            XStandardColormap *cmap =
   13.43 +                XmuStandardColormap(data->display, displaydata->screen,
   13.44 +                                    visual->visualid, depth,
   13.45 +                                    XA_RGB_BEST_MAP, None,
   13.46 +                                    max, max, max);
   13.47 +            if (cmap->visualid = visual->visualid) {
   13.48 +                xattr.colormap = cmap->colormap;
   13.49 +            } else {
   13.50 +                SDL_SetError
   13.51 +                    ("Couldn't create window:XA_RGB_BEST_MAP not found");
   13.52 +                return -1;
   13.53 +            }
   13.54 +        }
   13.55      } else {
   13.56          xattr.colormap =
   13.57              XCreateColormap(data->display,
    14.1 --- a/test/common.c	Thu Jul 12 08:07:30 2007 +0000
    14.2 +++ b/test/common.c	Thu Jul 12 20:00:50 2007 +0000
    14.3 @@ -245,7 +245,7 @@
    14.4          if (!argv[index]) {
    14.5              return -1;
    14.6          }
    14.7 -        state->audiospec.channels = (Uint8)SDL_atoi(argv[index]);
    14.8 +        state->audiospec.channels = (Uint8) SDL_atoi(argv[index]);
    14.9          return 2;
   14.10      }
   14.11      if (SDL_strcasecmp(argv[index], "--samples") == 0) {
   14.12 @@ -253,7 +253,7 @@
   14.13          if (!argv[index]) {
   14.14              return -1;
   14.15          }
   14.16 -        state->audiospec.samples = (Uint16)SDL_atoi(argv[index]);
   14.17 +        state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
   14.18          return 2;
   14.19      }
   14.20      if ((SDL_strcasecmp(argv[index], "-h") == 0)
   14.21 @@ -498,7 +498,7 @@
   14.22      fprintf(stderr, ")\n");
   14.23  
   14.24      fprintf(stderr, "    Texture formats (%d): ", info->num_texture_formats);
   14.25 -    for (i = 0; i < (int)info->num_texture_formats; ++i) {
   14.26 +    for (i = 0; i < (int) info->num_texture_formats; ++i) {
   14.27          if (i > 0) {
   14.28              fprintf(stderr, ", ");
   14.29          }
    15.1 --- a/test/testwm.c	Thu Jul 12 08:07:30 2007 +0000
    15.2 +++ b/test/testwm.c	Thu Jul 12 20:00:50 2007 +0000
    15.3 @@ -175,7 +175,7 @@
    15.4      SDL_PushEvent(&event);
    15.5  }
    15.6  
    15.7 -static int (SDLCALL *old_filterfunc) (void *, SDL_Event *);
    15.8 +static int (SDLCALL * old_filterfunc) (void *, SDL_Event *);
    15.9  static void *old_filterdata;
   15.10  
   15.11  int SDLCALL