1/*
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
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 <X11/Xatom.h>
37#include "reallocarray.h"
38
39void XSetRGBColormaps (
40    Display *dpy,
41    Window w,
42    XStandardColormap *cmaps,
43    int count,
44    Atom property)			/* XA_RGB_BEST_MAP, etc. */
45{
46    register int i;			/* iterator variable */
47    register xPropStandardColormap *map;  /* tmp variable, data in prop */
48    register XStandardColormap *cmap;	/* tmp variable, user data */
49    xPropStandardColormap *data, tmpdata;  /* scratch data */
50    int mode = PropModeReplace;		/* for partial writes */
51    Bool alloced_scratch_space;		/* do we need to free? */
52
53
54    if (count < 1) return;
55
56    /*
57     * if doing more than one, allocate scratch space for it
58     */
59    if ((count > 1) &&
60        ((data = (Xmallocarray(count,
61                               sizeof(xPropStandardColormap)))) != NULL)) {
62	alloced_scratch_space = True;
63    } else {
64	data = &tmpdata;
65	alloced_scratch_space = False;
66    }
67
68
69    /*
70     * Do the iteration.  If using temp space put out each part of the prop;
71     * otherwise, wait until the end and blast it all at once.
72     */
73    for (i = count, map = data, cmap = cmaps; i > 0; i--, cmap++) {
74	map->colormap	= cmap->colormap;
75	map->red_max	= cmap->red_max;
76	map->red_mult	= cmap->red_mult;
77	map->green_max	= cmap->green_max;
78	map->green_mult	= cmap->green_mult;
79	map->blue_max	= cmap->blue_max;
80	map->blue_mult	= cmap->blue_mult;
81	map->base_pixel	= cmap->base_pixel;
82	map->visualid	= cmap->visualid;
83	map->killid	= cmap->killid;
84
85	if (alloced_scratch_space) {
86	    map++;
87	} else {
88	    XChangeProperty (dpy, w, property, XA_RGB_COLOR_MAP, 32, mode,
89			     (unsigned char *) data,
90			     NumPropStandardColormapElements);
91	    mode = PropModeAppend;
92	}
93    }
94
95    if (alloced_scratch_space) {
96	XChangeProperty (dpy, w, property, XA_RGB_COLOR_MAP, 32,
97			 PropModeReplace, (unsigned char *) data,
98			 (int) (count * NumPropStandardColormapElements));
99	Xfree (data);
100    }
101}
102