11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1989, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included
121ab64890Smrgin all copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
171ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
181ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
191ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201ab64890SmrgOTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall
231ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or
241ab64890Smrgother dealings in this Software without prior written authorization
251ab64890Smrgfrom The Open Group.
261ab64890Smrg
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>
37258a0ebeSmrg#include "reallocarray.h"
381ab64890Smrg
391ab64890Smrgvoid XSetRGBColormaps (
401ab64890Smrg    Display *dpy,
411ab64890Smrg    Window w,
421ab64890Smrg    XStandardColormap *cmaps,
431ab64890Smrg    int count,
441ab64890Smrg    Atom property)			/* XA_RGB_BEST_MAP, etc. */
451ab64890Smrg{
461ab64890Smrg    register int i;			/* iterator variable */
471ab64890Smrg    register xPropStandardColormap *map;  /* tmp variable, data in prop */
481ab64890Smrg    register XStandardColormap *cmap;	/* tmp variable, user data */
491ab64890Smrg    xPropStandardColormap *data, tmpdata;  /* scratch data */
501ab64890Smrg    int mode = PropModeReplace;		/* for partial writes */
511ab64890Smrg    Bool alloced_scratch_space;		/* do we need to free? */
5261b2299dSmrg
531ab64890Smrg
541ab64890Smrg    if (count < 1) return;
551ab64890Smrg
561ab64890Smrg    /*
571ab64890Smrg     * if doing more than one, allocate scratch space for it
581ab64890Smrg     */
59258a0ebeSmrg    if ((count > 1) &&
60258a0ebeSmrg        ((data = (Xmallocarray(count,
61258a0ebeSmrg                               sizeof(xPropStandardColormap)))) != NULL)) {
621ab64890Smrg	alloced_scratch_space = True;
631ab64890Smrg    } else {
641ab64890Smrg	data = &tmpdata;
651ab64890Smrg	alloced_scratch_space = False;
661ab64890Smrg    }
671ab64890Smrg
681ab64890Smrg
691ab64890Smrg    /*
701ab64890Smrg     * Do the iteration.  If using temp space put out each part of the prop;
711ab64890Smrg     * otherwise, wait until the end and blast it all at once.
721ab64890Smrg     */
731ab64890Smrg    for (i = count, map = data, cmap = cmaps; i > 0; i--, cmap++) {
741ab64890Smrg	map->colormap	= cmap->colormap;
751ab64890Smrg	map->red_max	= cmap->red_max;
761ab64890Smrg	map->red_mult	= cmap->red_mult;
771ab64890Smrg	map->green_max	= cmap->green_max;
781ab64890Smrg	map->green_mult	= cmap->green_mult;
791ab64890Smrg	map->blue_max	= cmap->blue_max;
801ab64890Smrg	map->blue_mult	= cmap->blue_mult;
811ab64890Smrg	map->base_pixel	= cmap->base_pixel;
821ab64890Smrg	map->visualid	= cmap->visualid;
831ab64890Smrg	map->killid	= cmap->killid;
841ab64890Smrg
851ab64890Smrg	if (alloced_scratch_space) {
861ab64890Smrg	    map++;
871ab64890Smrg	} else {
881ab64890Smrg	    XChangeProperty (dpy, w, property, XA_RGB_COLOR_MAP, 32, mode,
891ab64890Smrg			     (unsigned char *) data,
901ab64890Smrg			     NumPropStandardColormapElements);
911ab64890Smrg	    mode = PropModeAppend;
921ab64890Smrg	}
931ab64890Smrg    }
941ab64890Smrg
951ab64890Smrg    if (alloced_scratch_space) {
961ab64890Smrg	XChangeProperty (dpy, w, property, XA_RGB_COLOR_MAP, 32,
971ab64890Smrg			 PropModeReplace, (unsigned char *) data,
981ab64890Smrg			 (int) (count * NumPropStandardColormapElements));
99818534a1Smrg	Xfree (data);
1001ab64890Smrg    }
1011ab64890Smrg}
102