11ab64890Smrg 21ab64890Smrg/* 31ab64890Smrg 41ab64890SmrgCopyright 1987, 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#ifdef HAVE_CONFIG_H 311ab64890Smrg#include <config.h> 321ab64890Smrg#endif 331ab64890Smrg#include <X11/Xlibint.h> 341ab64890Smrg#include <X11/Xutil.h> 351ab64890Smrg#include "Xatomtype.h" 36258a0ebeSmrg#include "reallocarray.h" 371ab64890Smrg#include <X11/Xatom.h> 381ab64890Smrg 391ab64890SmrgStatus XGetRGBColormaps ( 401ab64890Smrg Display *dpy, 411ab64890Smrg Window w, 421ab64890Smrg XStandardColormap **stdcmap, /* RETURN */ 431ab64890Smrg int *count, /* RETURN */ 441ab64890Smrg Atom property) /* XA_RGB_BEST_MAP, etc. */ 451ab64890Smrg{ 461ab64890Smrg register int i; /* iterator variable */ 471ab64890Smrg xPropStandardColormap *data = NULL; /* data read in from prop */ 481ab64890Smrg Atom actual_type; /* how the prop was actually stored */ 491ab64890Smrg int actual_format; /* ditto */ 501ab64890Smrg unsigned long leftover; /* how much was left over */ 511ab64890Smrg unsigned long nitems; /* number of 32bits read */ 521ab64890Smrg int ncmaps; /* number of structs this makes */ 531ab64890Smrg Bool old_style = False; /* if was too short */ 541ab64890Smrg VisualID def_visual = None; /* visual to use if prop too short */ 551ab64890Smrg XStandardColormap *cmaps; /* return value */ 561ab64890Smrg 571ab64890Smrg 581ab64890Smrg if (XGetWindowProperty (dpy, w, property, 0L, 1000000L, False, 591ab64890Smrg XA_RGB_COLOR_MAP, &actual_type, &actual_format, 601ab64890Smrg &nitems, &leftover, (unsigned char **)&data) 611ab64890Smrg != Success) 621ab64890Smrg return False; 631ab64890Smrg 641ab64890Smrg /* if wrong type or format, or too small for us, then punt */ 651ab64890Smrg if ((actual_type != XA_RGB_COLOR_MAP) || (actual_format != 32) || 661ab64890Smrg (nitems < OldNumPropStandardColormapElements)) { 670f8248bfSmrg Xfree (data); 681ab64890Smrg return False; 691ab64890Smrg } 701ab64890Smrg 711ab64890Smrg /* 7261b2299dSmrg * See how many properties were found; if pre-ICCCM then assume 731ab64890Smrg * default visual and a kill id of 1. 741ab64890Smrg */ 751ab64890Smrg if (nitems < NumPropStandardColormapElements) { 761ab64890Smrg ncmaps = 1; 771ab64890Smrg old_style = True; 781ab64890Smrg if (nitems < (NumPropStandardColormapElements - 1)) { 791ab64890Smrg Screen *sp = _XScreenOfWindow (dpy, w); 801ab64890Smrg 811ab64890Smrg if (!sp) { 820f8248bfSmrg Xfree (data); 831ab64890Smrg return False; 841ab64890Smrg } 851ab64890Smrg def_visual = sp->root_visual->visualid; 861ab64890Smrg } 871ab64890Smrg } else { 881ab64890Smrg /* 8961b2299dSmrg * make sure we have an integral number of colormaps 901ab64890Smrg */ 911ab64890Smrg ncmaps = (nitems / NumPropStandardColormapElements); 921ab64890Smrg if ((((unsigned long) ncmaps) * NumPropStandardColormapElements) != 931ab64890Smrg nitems) { 940f8248bfSmrg Xfree (data); 951ab64890Smrg return False; 961ab64890Smrg } 971ab64890Smrg } 981ab64890Smrg 9961b2299dSmrg 1001ab64890Smrg /* 1011ab64890Smrg * allocate array 1021ab64890Smrg */ 103258a0ebeSmrg cmaps = Xmallocarray (ncmaps, sizeof (XStandardColormap)); 1041ab64890Smrg if (!cmaps) { 1050f8248bfSmrg Xfree (data); 1061ab64890Smrg return False; 1071ab64890Smrg } 1081ab64890Smrg 1091ab64890Smrg 1101ab64890Smrg /* 1111ab64890Smrg * and fill it in, handling compatibility with pre-ICCCM short stdcmaps 1121ab64890Smrg */ 1131ab64890Smrg { 1141ab64890Smrg register XStandardColormap *map; 1151ab64890Smrg register xPropStandardColormap *prop; 1161ab64890Smrg 1171ab64890Smrg for (i = ncmaps, map = cmaps, prop = data; i > 0; i--, map++, prop++) { 1181ab64890Smrg map->colormap = prop->colormap; 1191ab64890Smrg map->red_max = prop->red_max; 1201ab64890Smrg map->red_mult = prop->red_mult; 1211ab64890Smrg map->green_max = prop->green_max; 1221ab64890Smrg map->green_mult = prop->green_mult; 1231ab64890Smrg map->blue_max = prop->blue_max; 1241ab64890Smrg map->blue_mult = prop->blue_mult; 1251ab64890Smrg map->base_pixel = prop->base_pixel; 1261ab64890Smrg map->visualid = (def_visual ? def_visual : prop->visualid); 1271ab64890Smrg map->killid = (old_style ? None : prop->killid); 1281ab64890Smrg } 1291ab64890Smrg } 130818534a1Smrg Xfree (data); 1311ab64890Smrg *stdcmap = cmaps; 1321ab64890Smrg *count = ncmaps; 1331ab64890Smrg return True; 1341ab64890Smrg} 1351ab64890Smrg 136