SetRGBCMap.c revision 1ab64890
1/* $Xorg: SetRGBCMap.c,v 1.4 2001/02/09 02:03:36 xorgcvs Exp $ */
2/*
3
4Copyright 1989, 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
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
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) && ((data = ((xPropStandardColormap *)
60				 Xmalloc(count*sizeof(xPropStandardColormap))))
61			 != 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 ((char *) data);
100    }
101}
102