Home | History | Annotate | Line # | Download | only in src
      1 /*
      2 
      3 Copyright 1989, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in this Software without prior written authorization from The Open Group.
     24 
     25 */
     26 
     27 /*
     28  * Author:  Donna Converse, MIT X Consortium
     29  */
     30 
     31 #ifdef HAVE_CONFIG_H
     32 #include <config.h>
     33 #endif
     34 #include <stdio.h>
     35 #include <X11/Xlib.h>
     36 #include <X11/Xatom.h>
     37 #include <X11/Xutil.h>
     38 #include <X11/Xmu/StdCmap.h>
     39 #include <stdlib.h>
     40 #include "Xmuint.h"
     41 
     42 /*
     43  * Prototypes
     44  */
     45 static Status lookup(Display*, int, VisualID, Atom, XStandardColormap*, Bool);
     46 
     47 /*
     48  * To create a standard colormap if one does not currently exist, or
     49  * replace the currently existing standard colormap, use
     50  * XmuLookupStandardColormap().
     51  *
     52  * Given a screen, a visual, and a property, XmuLookupStandardColormap()
     53  * will determine the best allocation for the property under the specified
     54  * visual, and determine the whether to create a new colormap or to use
     55  * the default colormap of the screen.  It will call XmuStandardColormap()
     56  * to create the standard colormap.
     57  *
     58  * If replace is true, any previous definition of the property will be
     59  * replaced.  If retain is true, the property and the colormap will be
     60  * made permanent for the duration of the server session.  However,
     61  * pre-existing property definitions which are not replaced cannot be made
     62  * permanent by a call to XmuLookupStandardColormap(); a request to retain
     63  * resources pertains to newly created resources.
     64  *
     65  * Returns 0 on failure, non-zero on success.  A request to create a
     66  * standard colormap upon a visual which cannot support such a map is
     67  * considered a failure.  An example of this would be requesting any
     68  * standard colormap property on a monochrome visual, or, requesting an
     69  * RGB_BEST_MAP on a display whose colormap size is 16.
     70  */
     71 
     72 Status
     73 XmuLookupStandardColormap(Display *dpy, int screen, VisualID visualid,
     74 			  unsigned int depth, Atom property,
     75 			  Bool replace, Bool retain)
     76      /*
     77       * dpy		- specifies X server connection
     78       * screen 		- specifies screen of display
     79       * visualid	- specifies the visual type
     80       * depth		- specifies  the visual type
     81       * property	- a standard colormap property
     82       * replace		- specifies whether to replace
     83       * retain		- specifies whether to retain
     84       */
     85 {
     86     Display		*odpy;		/* original display connection */
     87     XStandardColormap	*colormap;
     88     XVisualInfo		vinfo_template, *vinfo;	/* visual */
     89     long		vinfo_mask;
     90     unsigned long	r_max, g_max, b_max;	/* allocation */
     91     int			count;
     92     Colormap		cmap;			/* colormap ID */
     93     Status		status = 0;
     94 
     95 
     96     /* Match the requested visual */
     97 
     98     vinfo_template.visualid = visualid;
     99     vinfo_template.screen = screen;
    100     vinfo_template.depth = depth;
    101     vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
    102     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) ==
    103 	NULL)
    104 	return 0;
    105 
    106     /* Monochrome visuals have no standard maps */
    107 
    108     if (vinfo->colormap_size <= 2) {
    109 	XFree((char *) vinfo);
    110 	return 0;
    111     }
    112 
    113     /* If the requested property already exists on this screen, and,
    114      * if the replace flag has not been set to true, return success.
    115      * lookup() will remove a pre-existing map if replace is true.
    116      */
    117 
    118     if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL,
    119 	       replace) && !replace) {
    120 	XFree((char *) vinfo);
    121 	return 1;
    122     }
    123 
    124     /* Determine the best allocation for this property under the requested
    125      * visualid and depth, and determine whether or not to use the default
    126      * colormap of the screen.
    127      */
    128 
    129     if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
    130 	XFree((char *) vinfo);
    131 	return 0;
    132     }
    133 
    134     cmap = (property == XA_RGB_DEFAULT_MAP &&
    135 	    visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
    136 	? DefaultColormap(dpy, screen) : None;
    137 
    138     /* If retaining resources, open a new connection to the same server */
    139 
    140     if (retain) {
    141 	odpy = dpy;
    142 	if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
    143 	    XFree((char *) vinfo);
    144 	    return 0;
    145 	}
    146     }
    147 
    148     /* Create the standard colormap */
    149 
    150     colormap = XmuStandardColormap(dpy, screen, visualid, depth, property,
    151 				   cmap, r_max, g_max, b_max);
    152 
    153     /* Set the standard colormap property */
    154 
    155     if (colormap) {
    156 	XGrabServer(dpy);
    157 
    158 	if (lookup(dpy, screen, visualid, property, colormap, replace) &&
    159 	    !replace) {
    160 	    /* Someone has defined the property since we last looked.
    161 	     * Since we will not replace it, release our own resources.
    162 	     * If this is the default map, our allocations will be freed
    163 	     * when this connection closes.
    164 	     */
    165 	    if (colormap->killid == ReleaseByFreeingColormap)
    166 		XFreeColormap(dpy, colormap->colormap);
    167 	}
    168 	else if (retain) {
    169 		XSetCloseDownMode(dpy, RetainPermanent);
    170 	}
    171 	XUngrabServer(dpy);
    172 	XFree((char *) colormap);
    173 	status = 1;
    174     }
    175 
    176     if (retain)
    177 	XCloseDisplay(dpy);
    178     XFree((char *) vinfo);
    179     return status;
    180 }
    181 
    182 /***************************************************************************/
    183 
    184 /* Lookup a standard colormap property.  If the property is RGB_DEFAULT_MAP,
    185  * the visualid is used to determine whether the indicated standard colormap
    186  * exists.  If the map exists and replace is true, delete the resources used
    187  * by the map and remove the property.  Return true if the map exists,
    188  * or did exist and was deleted; return false if the map was not found.
    189  *
    190  * Note that this is not the way that a Status return is normally used.
    191  *
    192  * If new is not NULL, new points to an XStandardColormap structure which
    193  * describes a standard colormap of the specified property.  It will be made
    194  * a standard colormap of the screen if none already exists, or if replace
    195  * is true.
    196  */
    197 
    198 static Status
    199 lookup(Display *dpy, int screen, VisualID visualid, Atom property,
    200        XStandardColormap *cnew, Bool replace)
    201      /*
    202       * dpy		- specifies display connection
    203       * screen		- specifies screen number
    204       * visualid	- specifies visualid for std map
    205       * property	- specifies colormap property name
    206       * cnew		- specifies a standard colormap
    207       * replace		- specifies whether to replace
    208       */
    209 {
    210     register int	i;
    211     int			count;
    212     XStandardColormap	*stdcmaps, *s;
    213     Window		win = RootWindow(dpy, screen);
    214 
    215     /* The property does not already exist */
    216 
    217     if (! XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
    218 	if (cnew)
    219 	    XSetRGBColormaps(dpy, win, cnew, 1, property);
    220 	return 0;
    221     }
    222 
    223     /* The property exists and is not describing the RGB_DEFAULT_MAP */
    224 
    225     if (property != XA_RGB_DEFAULT_MAP) {
    226 	if (replace) {
    227 	    XmuDeleteStandardColormap(dpy, screen, property);
    228 	    if (cnew)
    229 		XSetRGBColormaps(dpy, win, cnew, 1, property);
    230 	}
    231 	XFree((char *)stdcmaps);
    232 	return 1;
    233     }
    234 
    235     /* The property exists and is RGB_DEFAULT_MAP */
    236 
    237     for (i=0, s=stdcmaps; (i < count) && (s->visualid != visualid); i++, s++)
    238 	;
    239 
    240     /* No RGB_DEFAULT_MAP property matches the given visualid */
    241 
    242     if (i == count) {
    243 	if (cnew) {
    244 	    XStandardColormap	*m, *maps;
    245 
    246 	    s = Xmumallocarray((count+1), sizeof(XStandardColormap));
    247 
    248 	    for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
    249 		m->colormap   = maps->colormap;
    250 		m->red_max    = maps->red_max;
    251 		m->red_mult   = maps->red_mult;
    252 		m->green_max  = maps->green_max;
    253 		m->green_mult = maps->green_mult;
    254 		m->blue_max   = maps->blue_max;
    255 		m->blue_mult  = maps->blue_mult;
    256 		m->base_pixel = maps->base_pixel;
    257 		m->visualid   = maps->visualid;
    258 		m->killid     = maps->killid;
    259 	    }
    260 	    m->colormap   = cnew->colormap;
    261 	    m->red_max    = cnew->red_max;
    262 	    m->red_mult   = cnew->red_mult;
    263 	    m->green_max  = cnew->green_max;
    264 	    m->green_mult = cnew->green_mult;
    265 	    m->blue_max   = cnew->blue_max;
    266 	    m->blue_mult  = cnew->blue_mult;
    267 	    m->base_pixel = cnew->base_pixel;
    268 	    m->visualid   = cnew->visualid;
    269 	    m->killid     = cnew->killid;
    270 
    271 	    XSetRGBColormaps(dpy, win, s, ++count, property);
    272 	    free(s);
    273 	}
    274 	XFree((char *) stdcmaps);
    275 	return 0;
    276     }
    277 
    278     /* Found an RGB_DEFAULT_MAP property with a matching visualid */
    279 
    280     if (replace) {
    281 	/* Free old resources first - we may need them, particularly in
    282 	 * the default colormap of the screen.  However, because of this,
    283 	 * it is possible that we will destroy the old resource and fail
    284 	 * to create a new one if XmuStandardColormap() fails.
    285 	 */
    286 
    287 	if (count == 1) {
    288 	    XmuDeleteStandardColormap(dpy, screen, property);
    289 	    if (cnew)
    290 		XSetRGBColormaps(dpy, win, cnew, 1, property);
    291 	}
    292 	else {
    293 	    XStandardColormap	*map;
    294 
    295 	    /* s still points to the matching standard colormap */
    296 
    297 	    if (s->killid == ReleaseByFreeingColormap) {
    298 		if ((s->colormap != None) &&
    299 		    (s->colormap != DefaultColormap(dpy, screen)))
    300 		    XFreeColormap(dpy, s->colormap);
    301 	    }
    302 	    else if (s->killid != None)
    303 		XKillClient(dpy, s->killid);
    304 
    305 	    map = (cnew) ? cnew : stdcmaps + --count;
    306 
    307 	    s->colormap   = map->colormap;
    308 	    s->red_max    = map->red_max;
    309 	    s->red_mult   = map->red_mult;
    310 	    s->green_max  = map->green_max;
    311 	    s->green_mult = map->green_mult;
    312 	    s->blue_max   = map->blue_max;
    313 	    s->blue_mult  = map->blue_mult;
    314 	    s->visualid   = map->visualid;
    315 	    s->killid     = map->killid;
    316 
    317 	    XSetRGBColormaps(dpy, win, stdcmaps, count, property);
    318 	}
    319     }
    320     XFree((char *) stdcmaps);
    321     return 1;
    322 }
    323