Imported Upstream version 1.6.2
[platform/upstream/libX11.git] / src / GetRGBCMap.c
1
2 /*
3
4 Copyright 1987, 1998  The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of The Open Group shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from 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 <X11/Xatom.h>
37
38 Status XGetRGBColormaps (
39     Display *dpy,
40     Window w,
41     XStandardColormap **stdcmap,        /* RETURN */
42     int *count,                         /* RETURN */
43     Atom property)                      /* XA_RGB_BEST_MAP, etc. */
44 {
45     register int i;                     /* iterator variable */
46     xPropStandardColormap *data = NULL;  /* data read in from prop */
47     Atom actual_type;                   /* how the prop was actually stored */
48     int actual_format;                  /* ditto */
49     unsigned long leftover;             /* how much was left over */
50     unsigned long nitems;               /* number of 32bits read */
51     int ncmaps;                         /* number of structs this makes */
52     Bool old_style = False;             /* if was too short */
53     VisualID def_visual = None;         /* visual to use if prop too short */
54     XStandardColormap *cmaps;           /* return value */
55
56
57     if (XGetWindowProperty (dpy, w, property, 0L, 1000000L, False,
58                             XA_RGB_COLOR_MAP, &actual_type, &actual_format,
59                             &nitems, &leftover, (unsigned char **)&data)
60         != Success)
61       return False;
62
63     /* if wrong type or format, or too small for us, then punt */
64     if ((actual_type != XA_RGB_COLOR_MAP) || (actual_format != 32) ||
65         (nitems < OldNumPropStandardColormapElements)) {
66         if (data) Xfree (data);
67         return False;
68     }
69
70     /*
71      * See how many properties were found; if pre-ICCCM then assume
72      * default visual and a kill id of 1.
73      */
74     if (nitems < NumPropStandardColormapElements) {
75         ncmaps = 1;
76         old_style = True;
77         if (nitems < (NumPropStandardColormapElements - 1)) {
78             Screen *sp = _XScreenOfWindow (dpy, w);
79
80             if (!sp) {
81                 if (data) Xfree (data);
82                 return False;
83             }
84             def_visual = sp->root_visual->visualid;
85         }
86     } else {
87         /*
88          * make sure we have an integral number of colormaps
89          */
90         ncmaps = (nitems / NumPropStandardColormapElements);
91         if ((((unsigned long) ncmaps) * NumPropStandardColormapElements) !=
92             nitems) {
93             if (data) Xfree (data);
94             return False;
95         }
96     }
97
98
99     /*
100      * allocate array
101      */
102     cmaps = Xmalloc (ncmaps * sizeof (XStandardColormap));
103     if (!cmaps) {
104         if (data) Xfree (data);
105         return False;
106     }
107
108
109     /*
110      * and fill it in, handling compatibility with pre-ICCCM short stdcmaps
111      */
112     {
113         register XStandardColormap *map;
114         register xPropStandardColormap *prop;
115
116         for (i = ncmaps, map = cmaps, prop = data; i > 0; i--, map++, prop++) {
117             map->colormap   = prop->colormap;
118             map->red_max    = prop->red_max;
119             map->red_mult   = prop->red_mult;
120             map->green_max  = prop->green_max;
121             map->green_mult = prop->green_mult;
122             map->blue_max   = prop->blue_max;
123             map->blue_mult  = prop->blue_mult;
124             map->base_pixel = prop->base_pixel;
125             map->visualid   = (def_visual ? def_visual : prop->visualid);
126             map->killid     = (old_style ? None : prop->killid);
127         }
128     }
129     Xfree (data);
130     *stdcmap = cmaps;
131     *count = ncmaps;
132     return True;
133 }
134