Imported Upstream version 1.1.1
[platform/upstream/libXmu.git] / src / VisCmap.c
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 <math.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xmu/StdCmap.h>
40
41 /*
42  * To create all of the appropriate standard colormaps for a given visual on
43  * a given screen, use XmuVisualStandardColormaps.
44  *
45  * Define all appropriate standard colormap properties for the given visual.
46  * If replace is true, any previous definition will be removed.
47  * If retain is true, new properties will be retained for the duration of
48  * the server session.  Return 0 on failure, non-zero on success.
49  * On failure, no new properties will be defined, and, old ones may have
50  * been removed if replace was True.
51  *
52  * Not all standard colormaps are meaningful to all visual classes.  This
53  * routine will check and define the following properties for the following
54  * classes, provided that the size of the colormap is not too small.
55  *
56  *      DirectColor and PseudoColor
57  *          RGB_DEFAULT_MAP
58  *          RGB_BEST_MAP
59  *          RGB_RED_MAP
60  *          RGB_GREEN_MAP
61  *          RGB_BLUE_MAP
62  *          RGB_GRAY_MAP
63  *
64  *      TrueColor and StaticColor
65  *          RGB_BEST_MAP
66  *
67  *      GrayScale and StaticGray
68  *          RGB_GRAY_MAP
69  */
70
71 Status
72 XmuVisualStandardColormaps(Display *dpy, int screen, VisualID visualid,
73                            unsigned int depth, Bool replace, Bool retain)
74      /*
75       * dpy                     - specifies server connection
76       * screen                  - specifies screen number
77       * visualid                - specifies the visual
78       * depth                   - specifies the visual
79       * replace specifies       - whether to replace
80       * retain                  - specifies whether to retain
81       */
82 {
83     Status              status;
84     int                 n;
85     long                vinfo_mask;
86     XVisualInfo         vinfo_template, *vinfo;
87
88     status = 0;
89     vinfo_template.screen = screen;
90     vinfo_template.visualid = visualid;
91     vinfo_template.depth = depth;
92     vinfo_mask = VisualScreenMask | VisualIDMask | VisualDepthMask;
93     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
94         return 0;
95
96     if (vinfo->colormap_size <= 2) {
97         /* Monochrome visuals have no standard maps; considered successful */
98         XFree((char *) vinfo);
99         return 1;
100     }
101
102     switch (vinfo->class)
103     {
104       case PseudoColor:
105       case DirectColor:
106         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
107                                            XA_RGB_DEFAULT_MAP, replace,retain);
108         if (!status) break;
109
110         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
111                                            XA_RGB_GRAY_MAP, replace, retain);
112         if (!status) {
113             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
114             break;
115         }
116
117         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
118                                            XA_RGB_RED_MAP, replace, retain);
119         if (!status) {
120             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
121             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
122             break;
123         }
124
125         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
126                                            XA_RGB_GREEN_MAP, replace, retain);
127         if (!status) {
128             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
129             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
130             XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
131             break;
132         }
133
134         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
135                                            XA_RGB_BLUE_MAP, replace, retain);
136         if (!status) {
137             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
138             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
139             XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
140             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
141             break;
142         }
143         /* fall through */
144
145       case StaticColor:
146       case TrueColor:
147
148         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
149                                            XA_RGB_BEST_MAP, replace, retain);
150         if (!status && (vinfo->class == PseudoColor ||
151                         vinfo->class == DirectColor)) {
152             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
153             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
154             XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
155             XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
156             XmuDeleteStandardColormap(dpy, screen, XA_RGB_BLUE_MAP);
157         }
158         break;
159         /* the end for PseudoColor, DirectColor, StaticColor, and TrueColor */
160
161       case GrayScale:
162         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
163                                            XA_RGB_DEFAULT_MAP, replace,
164                                            retain);
165         if (! status) break;
166         /*FALLTHROUGH*/
167
168       case StaticGray:
169
170         status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
171                                            XA_RGB_GRAY_MAP, replace, retain);
172         if (! status && vinfo->class == GrayScale) {
173             XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
174             break;
175         }
176     }
177
178     XFree((char *) vinfo);
179     return status;
180 }