GetRGBCMap.c revision 61b2299d
11ab64890Smrg/* $Xorg: GetRGBCMap.c,v 1.4 2001/02/09 02:03:33 xorgcvs Exp $ */
21ab64890Smrg
31ab64890Smrg/*
41ab64890Smrg
51ab64890SmrgCopyright 1987, 1998  The Open Group
61ab64890Smrg
71ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
81ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
91ab64890Smrgthe above copyright notice appear in all copies and that both that
101ab64890Smrgcopyright notice and this permission notice appear in supporting
111ab64890Smrgdocumentation.
121ab64890Smrg
131ab64890SmrgThe above copyright notice and this permission notice shall be included
141ab64890Smrgin all copies or substantial portions of the Software.
151ab64890Smrg
161ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
171ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
181ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
191ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
201ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
211ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
221ab64890SmrgOTHER DEALINGS IN THE SOFTWARE.
231ab64890Smrg
241ab64890SmrgExcept as contained in this notice, the name of The Open Group shall
251ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or
261ab64890Smrgother dealings in this Software without prior written authorization
271ab64890Smrgfrom The Open Group.
281ab64890Smrg
291ab64890Smrg*/
301ab64890Smrg
311ab64890Smrg#ifdef HAVE_CONFIG_H
321ab64890Smrg#include <config.h>
331ab64890Smrg#endif
341ab64890Smrg#include <X11/Xlibint.h>
351ab64890Smrg#include <X11/Xutil.h>
361ab64890Smrg#include "Xatomtype.h"
371ab64890Smrg#include <X11/Xatom.h>
381ab64890Smrg
391ab64890SmrgStatus XGetRGBColormaps (
401ab64890Smrg    Display *dpy,
411ab64890Smrg    Window w,
421ab64890Smrg    XStandardColormap **stdcmap,	/* RETURN */
431ab64890Smrg    int *count,				/* RETURN */
441ab64890Smrg    Atom property)			/* XA_RGB_BEST_MAP, etc. */
451ab64890Smrg{
461ab64890Smrg    register int i;			/* iterator variable */
471ab64890Smrg    xPropStandardColormap *data = NULL;	 /* data read in from prop */
481ab64890Smrg    Atom actual_type;			/* how the prop was actually stored */
491ab64890Smrg    int actual_format;			/* ditto */
501ab64890Smrg    unsigned long leftover;		/* how much was left over */
511ab64890Smrg    unsigned long nitems;		/* number of 32bits read */
521ab64890Smrg    int ncmaps;				/* number of structs this makes */
531ab64890Smrg    Bool old_style = False;		/* if was too short */
541ab64890Smrg    VisualID def_visual = None;		/* visual to use if prop too short */
551ab64890Smrg    XStandardColormap *cmaps;		/* return value */
561ab64890Smrg
571ab64890Smrg
581ab64890Smrg    if (XGetWindowProperty (dpy, w, property, 0L, 1000000L, False,
591ab64890Smrg			    XA_RGB_COLOR_MAP, &actual_type, &actual_format,
601ab64890Smrg			    &nitems, &leftover, (unsigned char **)&data)
611ab64890Smrg	!= Success)
621ab64890Smrg      return False;
631ab64890Smrg
641ab64890Smrg    /* if wrong type or format, or too small for us, then punt */
651ab64890Smrg    if ((actual_type != XA_RGB_COLOR_MAP) || (actual_format != 32) ||
661ab64890Smrg	(nitems < OldNumPropStandardColormapElements)) {
671ab64890Smrg	if (data) Xfree ((char *) data);
681ab64890Smrg	return False;
691ab64890Smrg    }
701ab64890Smrg
711ab64890Smrg    /*
7261b2299dSmrg     * See how many properties were found; if pre-ICCCM then assume
731ab64890Smrg     * default visual and a kill id of 1.
741ab64890Smrg     */
751ab64890Smrg    if (nitems < NumPropStandardColormapElements) {
761ab64890Smrg	ncmaps = 1;
771ab64890Smrg	old_style = True;
781ab64890Smrg	if (nitems < (NumPropStandardColormapElements - 1)) {
791ab64890Smrg	    Screen *sp = _XScreenOfWindow (dpy, w);
801ab64890Smrg
811ab64890Smrg	    if (!sp) {
821ab64890Smrg		if (data) Xfree ((char *) data);
831ab64890Smrg		return False;
841ab64890Smrg	    }
851ab64890Smrg	    def_visual = sp->root_visual->visualid;
861ab64890Smrg	}
871ab64890Smrg    } else {
881ab64890Smrg	/*
8961b2299dSmrg	 * make sure we have an integral number of colormaps
901ab64890Smrg	 */
911ab64890Smrg	ncmaps = (nitems / NumPropStandardColormapElements);
921ab64890Smrg	if ((((unsigned long) ncmaps) * NumPropStandardColormapElements) !=
931ab64890Smrg	    nitems) {
941ab64890Smrg	    if (data) Xfree ((char *) data);
951ab64890Smrg	    return False;
961ab64890Smrg	}
971ab64890Smrg    }
981ab64890Smrg
9961b2299dSmrg
1001ab64890Smrg    /*
1011ab64890Smrg     * allocate array
1021ab64890Smrg     */
1031ab64890Smrg    cmaps = (XStandardColormap *) Xmalloc (ncmaps *
1041ab64890Smrg					   sizeof (XStandardColormap));
1051ab64890Smrg    if (!cmaps) {
1061ab64890Smrg	if (data) Xfree ((char *) data);
1071ab64890Smrg	return False;
1081ab64890Smrg    }
1091ab64890Smrg
1101ab64890Smrg
1111ab64890Smrg    /*
1121ab64890Smrg     * and fill it in, handling compatibility with pre-ICCCM short stdcmaps
1131ab64890Smrg     */
1141ab64890Smrg    {
1151ab64890Smrg	register XStandardColormap *map;
1161ab64890Smrg	register xPropStandardColormap *prop;
1171ab64890Smrg
1181ab64890Smrg	for (i = ncmaps, map = cmaps, prop = data; i > 0; i--, map++, prop++) {
1191ab64890Smrg	    map->colormap   = prop->colormap;
1201ab64890Smrg	    map->red_max    = prop->red_max;
1211ab64890Smrg	    map->red_mult   = prop->red_mult;
1221ab64890Smrg	    map->green_max  = prop->green_max;
1231ab64890Smrg	    map->green_mult = prop->green_mult;
1241ab64890Smrg	    map->blue_max   = prop->blue_max;
1251ab64890Smrg	    map->blue_mult  = prop->blue_mult;
1261ab64890Smrg	    map->base_pixel = prop->base_pixel;
1271ab64890Smrg	    map->visualid   = (def_visual ? def_visual : prop->visualid);
1281ab64890Smrg	    map->killid     = (old_style ? None : prop->killid);
1291ab64890Smrg	}
1301ab64890Smrg    }
1311ab64890Smrg    Xfree ((char *) data);
1321ab64890Smrg    *stdcmap = cmaps;
1331ab64890Smrg    *count = ncmaps;
1341ab64890Smrg    return True;
1351ab64890Smrg}
1361ab64890Smrg
137