1
2/*
3
4Copyright 1987, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27
28*/
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <X11/Xlibint.h>
34#include <X11/Xutil.h>
35#include "Xatomtype.h"
36#include "reallocarray.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	Xfree (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		Xfree (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	    Xfree (data);
95	    return False;
96	}
97    }
98
99
100    /*
101     * allocate array
102     */
103    cmaps = Xmallocarray (ncmaps, sizeof (XStandardColormap));
104    if (!cmaps) {
105	Xfree (data);
106	return False;
107    }
108
109
110    /*
111     * and fill it in, handling compatibility with pre-ICCCM short stdcmaps
112     */
113    {
114	register XStandardColormap *map;
115	register xPropStandardColormap *prop;
116
117	for (i = ncmaps, map = cmaps, prop = data; i > 0; i--, map++, prop++) {
118	    map->colormap   = prop->colormap;
119	    map->red_max    = prop->red_max;
120	    map->red_mult   = prop->red_mult;
121	    map->green_max  = prop->green_max;
122	    map->green_mult = prop->green_mult;
123	    map->blue_max   = prop->blue_max;
124	    map->blue_mult  = prop->blue_mult;
125	    map->base_pixel = prop->base_pixel;
126	    map->visualid   = (def_visual ? def_visual : prop->visualid);
127	    map->killid     = (old_style ? None : prop->killid);
128	}
129    }
130    Xfree (data);
131    *stdcmap = cmaps;
132    *count = ncmaps;
133    return True;
134}
135
136