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