SetRGBCMap.c revision 61b2299d
11ab64890Smrg/* $Xorg: SetRGBCMap.c,v 1.4 2001/02/09 02:03:36 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1989, 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
311ab64890Smrg#ifdef HAVE_CONFIG_H
321ab64890Smrg#include <config.h>
331ab64890Smrg#endif
341ab64890Smrg#include <X11/Xlibint.h>
351ab64890Smrg#include <X11/Xutil.h>
361ab64890Smrg#include "Xatomtype.h"
371ab64890Smrg#include <X11/Xatom.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     */
591ab64890Smrg    if ((count > 1) && ((data = ((xPropStandardColormap *)
601ab64890Smrg				 Xmalloc(count*sizeof(xPropStandardColormap))))
611ab64890Smrg			 != 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));
991ab64890Smrg	Xfree ((char *) data);
1001ab64890Smrg    }
1011ab64890Smrg}
102