LookupCmap.c

Go to the documentation of this file.
00001 /* $Xorg: LookupCmap.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
00002 
00003 /* 
00004  
00005 Copyright 1989, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/LookupCmap.c,v 1.7 2001/07/25 15:04:50 dawes Exp $ */
00029 
00030 /*
00031  * Author:  Donna Converse, MIT X Consortium
00032  */
00033 
00034 #include <stdio.h>
00035 #include <X11/Xlib.h>
00036 #include <X11/Xatom.h>
00037 #include <X11/Xutil.h>
00038 #include <X11/Xmu/StdCmap.h>
00039 #include <stdlib.h>
00040 
00041 /*
00042  * Prototypes
00043  */
00044 static Status lookup(Display*, int, VisualID, Atom, XStandardColormap*, Bool);
00045 
00046 /*
00047  * To create a standard colormap if one does not currently exist, or
00048  * replace the currently existing standard colormap, use 
00049  * XmuLookupStandardColormap().
00050  *
00051  * Given a screen, a visual, and a property, XmuLookupStandardColormap()
00052  * will determine the best allocation for the property under the specified
00053  * visual, and determine the whether to create a new colormap or to use
00054  * the default colormap of the screen.  It will call XmuStandardColormap()
00055  * to create the standard colormap.
00056  *
00057  * If replace is true, any previous definition of the property will be 
00058  * replaced.  If retain is true, the property and the colormap will be
00059  * made permanent for the duration of the server session.  However,
00060  * pre-existing property definitions which are not replaced cannot be made
00061  * permanent by a call to XmuLookupStandardColormap(); a request to retain 
00062  * resources pertains to newly created resources.
00063  *
00064  * Returns 0 on failure, non-zero on success.  A request to create a 
00065  * standard colormap upon a visual which cannot support such a map is
00066  * considered a failure.  An example of this would be requesting any
00067  * standard colormap property on a monochrome visual, or, requesting an
00068  * RGB_BEST_MAP on a display whose colormap size is 16.
00069  */
00070 
00071 Status
00072 XmuLookupStandardColormap(Display *dpy, int screen, VisualID visualid,
00073                           unsigned int depth, Atom property,
00074                           Bool replace, Bool retain)
00075      /*
00076       * dpy             - specifies X server connection
00077       * screen          - specifies screen of display
00078       * visualid        - specifies the visual type
00079       * depth           - specifies  the visual type
00080       * property        - a standard colormap property
00081       * replace         - specifies whether to replace
00082       * retain          - specifies whether to retain
00083       */
00084 {
00085     Display             *odpy;          /* original display connection */
00086     XStandardColormap   *colormap;      
00087     XVisualInfo         vinfo_template, *vinfo; /* visual */
00088     long                vinfo_mask;
00089     unsigned long       r_max, g_max, b_max;    /* allocation */
00090     int                 count;  
00091     Colormap            cmap;                   /* colormap ID */
00092     Status              status = 0;
00093 
00094 
00095     /* Match the requested visual */
00096 
00097     vinfo_template.visualid = visualid; 
00098     vinfo_template.screen = screen;
00099     vinfo_template.depth = depth;
00100     vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
00101     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &count)) ==
00102         NULL)
00103         return 0;
00104 
00105     /* Monochrome visuals have no standard maps */
00106 
00107     if (vinfo->colormap_size <= 2) {
00108         XFree((char *) vinfo);
00109         return 0;       
00110     }
00111 
00112     /* If the requested property already exists on this screen, and, 
00113      * if the replace flag has not been set to true, return success.
00114      * lookup() will remove a pre-existing map if replace is true.
00115      */
00116 
00117     if (lookup(dpy, screen, visualid, property, (XStandardColormap *) NULL,
00118                replace) && !replace) {
00119         XFree((char *) vinfo);
00120         return 1;
00121     }
00122 
00123     /* Determine the best allocation for this property under the requested
00124      * visualid and depth, and determine whether or not to use the default
00125      * colormap of the screen.
00126      */
00127 
00128     if (!XmuGetColormapAllocation(vinfo, property, &r_max, &g_max, &b_max)) {
00129         XFree((char *) vinfo);
00130         return 0;
00131     }
00132 
00133     cmap = (property == XA_RGB_DEFAULT_MAP &&
00134             visualid == XVisualIDFromVisual(DefaultVisual(dpy, screen)))
00135         ? DefaultColormap(dpy, screen) : None;
00136 
00137     /* If retaining resources, open a new connection to the same server */
00138 
00139     if (retain) {
00140         odpy = dpy;
00141         if ((dpy = XOpenDisplay(XDisplayString(odpy))) == NULL) {
00142             XFree((char *) vinfo);
00143             return 0;
00144         }
00145     }
00146 
00147     /* Create the standard colormap */
00148 
00149     colormap = XmuStandardColormap(dpy, screen, visualid, depth, property,
00150                                    cmap, r_max, g_max, b_max);
00151 
00152     /* Set the standard colormap property */
00153 
00154     if (colormap) {
00155         XGrabServer(dpy);
00156 
00157         if (lookup(dpy, screen, visualid, property, colormap, replace) &&
00158             !replace) {
00159             /* Someone has defined the property since we last looked.
00160              * Since we will not replace it, release our own resources.
00161              * If this is the default map, our allocations will be freed 
00162              * when this connection closes.
00163              */
00164             if (colormap->killid == ReleaseByFreeingColormap)
00165                 XFreeColormap(dpy, colormap->colormap);
00166         }
00167         else if (retain) {
00168                 XSetCloseDownMode(dpy, RetainPermanent);
00169         }
00170         XUngrabServer(dpy);
00171         XFree((char *) colormap);
00172         status = 1;
00173     }
00174 
00175     if (retain)
00176         XCloseDisplay(dpy);
00177     XFree((char *) vinfo);
00178     return status;
00179 }
00180 
00181 /***************************************************************************/
00182 
00183 /* Lookup a standard colormap property.  If the property is RGB_DEFAULT_MAP,
00184  * the visualid is used to determine whether the indicated standard colormap
00185  * exists.  If the map exists and replace is true, delete the resources used
00186  * by the map and remove the property.  Return true if the map exists,
00187  * or did exist and was deleted; return false if the map was not found.
00188  *
00189  * Note that this is not the way that a Status return is normally used.
00190  *
00191  * If new is not NULL, new points to an XStandardColormap structure which
00192  * describes a standard colormap of the specified property.  It will be made
00193  * a standard colormap of the screen if none already exists, or if replace 
00194  * is true.
00195  */
00196 
00197 static Status
00198 lookup(Display *dpy, int screen, VisualID visualid, Atom property,
00199        XStandardColormap *cnew, Bool replace)
00200      /*
00201       * dpy             - specifies display connection
00202       * screen          - specifies screen number
00203       * visualid        - specifies visualid for std map
00204       * property        - specifies colormap property name
00205       * cnew            - specifies a standard colormap
00206       * replace         - specifies whether to replace
00207       */
00208 {
00209     register int        i;
00210     int                 count;
00211     XStandardColormap   *stdcmaps, *s;
00212     Window              win = RootWindow(dpy, screen);
00213 
00214     /* The property does not already exist */
00215 
00216     if (! XGetRGBColormaps(dpy, win, &stdcmaps, &count, property)) {
00217         if (cnew)
00218             XSetRGBColormaps(dpy, win, cnew, 1, property);
00219         return 0;
00220     }
00221 
00222     /* The property exists and is not describing the RGB_DEFAULT_MAP */
00223 
00224     if (property != XA_RGB_DEFAULT_MAP) {
00225         if (replace) {
00226             XmuDeleteStandardColormap(dpy, screen, property);
00227             if (cnew)
00228                 XSetRGBColormaps(dpy, win, cnew, 1, property);
00229         }
00230         XFree((char *)stdcmaps);
00231         return 1;
00232     }
00233 
00234     /* The property exists and is RGB_DEFAULT_MAP */
00235     
00236     for (i=0, s=stdcmaps; (i < count) && (s->visualid != visualid); i++, s++)
00237         ;
00238 
00239     /* No RGB_DEFAULT_MAP property matches the given visualid */
00240 
00241     if (i == count) {
00242         if (cnew) {
00243             XStandardColormap   *m, *maps;
00244 
00245             s = (XStandardColormap *) malloc((unsigned) ((count+1) * sizeof
00246                                               (XStandardColormap)));
00247 
00248             for (i = 0, m = s, maps = stdcmaps; i < count; i++, m++, maps++) {
00249                 m->colormap   = maps->colormap;
00250                 m->red_max    = maps->red_max;
00251                 m->red_mult   = maps->red_mult;
00252                 m->green_max  = maps->green_max;
00253                 m->green_mult = maps->green_mult;
00254                 m->blue_max   = maps->blue_max;
00255                 m->blue_mult  = maps->blue_mult;
00256                 m->base_pixel = maps->base_pixel;
00257                 m->visualid   = maps->visualid;
00258                 m->killid     = maps->killid;
00259             }
00260             m->colormap   = cnew->colormap;
00261             m->red_max    = cnew->red_max;
00262             m->red_mult   = cnew->red_mult;
00263             m->green_max  = cnew->green_max;
00264             m->green_mult = cnew->green_mult;
00265             m->blue_max   = cnew->blue_max;
00266             m->blue_mult  = cnew->blue_mult;
00267             m->base_pixel = cnew->base_pixel;
00268             m->visualid   = cnew->visualid;
00269             m->killid     = cnew->killid;
00270 
00271             XSetRGBColormaps(dpy, win, s, ++count, property);
00272             free((char *) s);
00273         }
00274         XFree((char *) stdcmaps);
00275         return 0;
00276     }
00277 
00278     /* Found an RGB_DEFAULT_MAP property with a matching visualid */
00279 
00280     if (replace) {
00281         /* Free old resources first - we may need them, particularly in 
00282          * the default colormap of the screen.  However, because of this,
00283          * it is possible that we will destroy the old resource and fail 
00284          * to create a new one if XmuStandardColormap() fails.
00285          */
00286 
00287         if (count == 1) {
00288             XmuDeleteStandardColormap(dpy, screen, property);
00289             if (cnew)
00290                 XSetRGBColormaps(dpy, win, cnew, 1, property);
00291         }
00292         else {
00293             XStandardColormap   *map;
00294 
00295             /* s still points to the matching standard colormap */
00296 
00297             if (s->killid == ReleaseByFreeingColormap) {
00298                 if ((s->colormap != None) &&
00299                     (s->colormap != DefaultColormap(dpy, screen)))
00300                     XFreeColormap(dpy, s->colormap);
00301             }
00302             else if (s->killid != None)
00303                 XKillClient(dpy, s->killid);
00304 
00305             map = (cnew) ? cnew : stdcmaps + --count;
00306 
00307             s->colormap   = map->colormap;
00308             s->red_max    = map->red_max;
00309             s->red_mult   = map->red_mult;
00310             s->green_max  = map->green_max;
00311             s->green_mult = map->green_mult;
00312             s->blue_max   = map->blue_max;
00313             s->blue_mult  = map->blue_mult;
00314             s->visualid   = map->visualid;
00315             s->killid     = map->killid;
00316 
00317             XSetRGBColormaps(dpy, win, stdcmaps, count, property);
00318         }
00319     }
00320     XFree((char *) stdcmaps);
00321     return 1;
00322 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1