GetRGBCMap.c revision 1ab64890
1/* $Xorg: GetRGBCMap.c,v 1.4 2001/02/09 02:03:33 xorgcvs Exp $ */
2
3/*
4
5Copyright 1987, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included
14in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of The Open Group shall
25not be used in advertising or otherwise to promote the sale, use or
26other dealings in this Software without prior written authorization
27from The Open Group.
28
29*/
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include <X11/Xlibint.h>
35#include <X11/Xutil.h>
36#include "Xatomtype.h"
37#include <X11/Xatom.h>
38
39Status XGetRGBColormaps (
40    Display *dpy,
41    Window w,
42    XStandardColormap **stdcmap,	/* RETURN */
43    int *count,				/* RETURN */
44    Atom property)			/* XA_RGB_BEST_MAP, etc. */
45{
46    register int i;			/* iterator variable */
47    xPropStandardColormap *data = NULL;	 /* data read in from prop */
48    Atom actual_type;			/* how the prop was actually stored */
49    int actual_format;			/* ditto */
50    unsigned long leftover;		/* how much was left over */
51    unsigned long nitems;		/* number of 32bits read */
52    int ncmaps;				/* number of structs this makes */
53    Bool old_style = False;		/* if was too short */
54    VisualID def_visual = None;		/* visual to use if prop too short */
55    XStandardColormap *cmaps;		/* return value */
56
57
58    if (XGetWindowProperty (dpy, w, property, 0L, 1000000L, False,
59			    XA_RGB_COLOR_MAP, &actual_type, &actual_format,
60			    &nitems, &leftover, (unsigned char **)&data)
61	!= Success)
62      return False;
63
64    /* if wrong type or format, or too small for us, then punt */
65    if ((actual_type != XA_RGB_COLOR_MAP) || (actual_format != 32) ||
66	(nitems < OldNumPropStandardColormapElements)) {
67	if (data) Xfree ((char *) data);
68	return False;
69    }
70
71    /*
72     * See how many properties were found; if pre-ICCCM then assume
73     * default visual and a kill id of 1.
74     */
75    if (nitems < NumPropStandardColormapElements) {
76	ncmaps = 1;
77	old_style = True;
78	if (nitems < (NumPropStandardColormapElements - 1)) {
79	    Screen *sp = _XScreenOfWindow (dpy, w);
80
81	    if (!sp) {
82		if (data) Xfree ((char *) data);
83		return False;
84	    }
85	    def_visual = sp->root_visual->visualid;
86	}
87    } else {
88	/*
89	 * make sure we have an integral number of colormaps
90	 */
91	ncmaps = (nitems / NumPropStandardColormapElements);
92	if ((((unsigned long) ncmaps) * NumPropStandardColormapElements) !=
93	    nitems) {
94	    if (data) Xfree ((char *) data);
95	    return False;
96	}
97    }
98
99
100    /*
101     * allocate array
102     */
103    cmaps = (XStandardColormap *) Xmalloc (ncmaps *
104					   sizeof (XStandardColormap));
105    if (!cmaps) {
106	if (data) Xfree ((char *) data);
107	return False;
108    }
109
110
111    /*
112     * and fill it in, handling compatibility with pre-ICCCM short stdcmaps
113     */
114    {
115	register XStandardColormap *map;
116	register xPropStandardColormap *prop;
117
118	for (i = ncmaps, map = cmaps, prop = data; i > 0; i--, map++, prop++) {
119	    map->colormap   = prop->colormap;
120	    map->red_max    = prop->red_max;
121	    map->red_mult   = prop->red_mult;
122	    map->green_max  = prop->green_max;
123	    map->green_mult = prop->green_mult;
124	    map->blue_max   = prop->blue_max;
125	    map->blue_mult  = prop->blue_mult;
126	    map->base_pixel = prop->base_pixel;
127	    map->visualid   = (def_visual ? def_visual : prop->visualid);
128	    map->killid     = (old_style ? None : prop->killid);
129	}
130    }
131    Xfree ((char *) data);
132    *stdcmap = cmaps;
133    *count = ncmaps;
134    return True;
135}
136
137