Tizen 2.1 base
[framework/uifw/xorg/lib/libx11.git] / src / VisUtil.c
1 /*
2
3 Copyright 1986, 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 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include "Xlibint.h"
31 #include "Xutil.h"
32 #include <stdio.h>
33 /*
34  *      This procedure returns a list of visual information structures
35  *      that match the specified attributes given in the visual information
36  *      template.
37  *
38  *      If no visuals exist that match the specified attributes, a NULL is
39  *      returned.
40  *
41  *      The choices for visual_info_mask are:
42  *
43  *              VisualNoMask
44  *              VisualIDMask
45  *              VisualScreenMask
46  *              VisualDepthMask
47  *              VisualClassMask
48  *              VisualRedMaskMask
49  *              VisualGreenMaskMask
50  *              VisualBlueMaskMask
51  *              VisualColormapSizeMask
52  *              VisualBitsPerRGBMask
53  *              VisualAllMask
54  */
55
56 XVisualInfo *XGetVisualInfo(
57     Display *dpy,
58     register long visual_info_mask,
59     register XVisualInfo *visual_info_template,
60     int *nitems)        /* RETURN */
61 {
62
63   register Visual *vp;
64   register Depth *dp;
65   Screen *sp;
66   int ii,screen_s,screen_e,total,count;
67   register XVisualInfo *vip,*vip_base;
68
69   /* NOTE: NO HIGH PERFORMING CODE TO BE FOUND HERE */
70
71   LockDisplay(dpy);
72
73   /* ALLOCATE THE ORIGINAL BUFFER; REALLOCED LATER IF OVERFLOW OCCURS;
74      FREED AT END IF NO VISUALS ARE FOUND */
75
76   count = 0;
77   total = 10;
78   if (! (vip_base = vip = (XVisualInfo *)
79          Xmalloc((unsigned) (sizeof(XVisualInfo) * total)))) {
80       UnlockDisplay(dpy);
81       return (XVisualInfo *) NULL;
82   }
83
84   /* DETERMINE IF WE DO ALL SCREENS OR ONLY ONE */
85
86   screen_s = 0;
87   screen_e = dpy->nscreens;
88   if (visual_info_mask & VisualScreenMask)
89     {
90       screen_s = visual_info_template->screen;
91       if (screen_s < 0 || screen_s >= screen_e)
92           screen_e = screen_s;
93       else
94           screen_e = screen_s + 1;
95     }
96
97   /* LOOP THROUGH SCREENS */
98
99   for (ii=screen_s; ii<screen_e; ii++)
100     {
101       sp = (Screen *)(&dpy->screens[ii]);
102
103       /* LOOP THROUGH DEPTHS */
104
105       for (dp=sp->depths; dp < (sp->depths + sp->ndepths); dp++)
106         {
107           if ((visual_info_mask & VisualDepthMask) &&
108               (dp->depth != visual_info_template->depth)) continue;
109
110           /* LOOP THROUGH VISUALS */
111
112           if (dp->visuals) {
113             for (vp=dp->visuals; vp<(dp->visuals + dp->nvisuals); vp++) {
114               if ((visual_info_mask & VisualIDMask) &&
115                 (vp->visualid != visual_info_template->visualid)) continue;
116               if ((visual_info_mask & VisualClassMask) &&
117                 (vp->class != visual_info_template->class)) continue;
118               if ((visual_info_mask & VisualRedMaskMask) &&
119                 (vp->red_mask != visual_info_template->red_mask)) continue;
120               if ((visual_info_mask & VisualGreenMaskMask) &&
121                 (vp->green_mask != visual_info_template->green_mask)) continue;
122               if ((visual_info_mask & VisualBlueMaskMask) &&
123                 (vp->blue_mask != visual_info_template->blue_mask)) continue;
124               if ((visual_info_mask & VisualColormapSizeMask) &&
125                 (vp->map_entries != visual_info_template->colormap_size)) continue;
126               if ((visual_info_mask & VisualBitsPerRGBMask) &&
127                 (vp->bits_per_rgb != visual_info_template->bits_per_rgb)) continue;
128
129               /* YEA!!! WE FOUND A GOOD ONE */
130
131               if (count+1 > total)
132                 {
133                   XVisualInfo *old_vip_base = vip_base;
134                   total += 10;
135                   if (! (vip_base = (XVisualInfo *)
136                          Xrealloc((char *) vip_base,
137                                   (unsigned) (sizeof(XVisualInfo) * total)))) {
138                       Xfree((char *) old_vip_base);
139                       UnlockDisplay(dpy);
140                       return (XVisualInfo *) NULL;
141                   }
142                   vip = &vip_base[count];
143                 }
144
145               count++;
146
147               vip->visual = _XVIDtoVisual(dpy, vp->visualid);
148               vip->visualid = vp->visualid;
149               vip->screen = ii;
150               vip->depth = dp->depth;
151               vip->class = vp->class;
152               vip->red_mask = vp->red_mask;
153               vip->green_mask = vp->green_mask;
154               vip->blue_mask = vp->blue_mask;
155               vip->colormap_size = vp->map_entries;
156               vip->bits_per_rgb = vp->bits_per_rgb;
157
158               vip++;
159
160             } /* END OF LOOP ON VISUALS */
161           } /* END OF IF THERE ARE ANY VISUALS AT THIS DEPTH */
162
163         } /* END OF LOOP ON DEPTHS */
164
165     } /* END OF LOOP ON SCREENS */
166
167   UnlockDisplay(dpy);
168
169   if (count)
170     {
171       *nitems = count;
172       return vip_base;
173     }
174
175   Xfree((char *) vip_base);
176   *nitems = 0;
177   return NULL;
178 }
179
180 \f
181 /*
182  *      This procedure will return the visual information for a visual
183  *      that matches the specified depth and class for a screen.  Since
184  *      multiple visuals may exist that match the specified depth and
185  *      class, which visual chosen is undefined.
186  *
187  *      If a visual is found, True is returned as the function value,
188  *      otherwise False is returned.
189  */
190
191 Status XMatchVisualInfo(
192         Display *dpy,
193         int screen,
194         int depth,
195         int class,
196         XVisualInfo *visual_info) /* RETURNED */
197 {
198
199   Visual *vp;
200   Depth *dp;
201   Screen *sp;
202   int ii,jj;
203
204   if (screen < 0 || screen >= dpy->nscreens)
205       return False;
206
207   LockDisplay(dpy);
208
209   sp = (Screen *)(&dpy->screens[screen]);
210
211   dp = sp->depths;
212
213   for (ii=0; ii < sp->ndepths; ii++)
214     {
215
216     /* LOOK THROUGH DEPTHS FOR THE WANTED DEPTH */
217
218     if (dp->depth == depth)
219       {
220         vp = dp->visuals;
221
222         /* LOOK THROUGH VISUALS FOR THE WANTED CLASS */
223
224         /* if nvisuals == 0 then vp will be NULL */
225         for (jj=0; jj<dp->nvisuals; jj++)
226           {
227             if (vp->class == class)
228               {
229                 visual_info->visual = _XVIDtoVisual(dpy, vp->visualid);
230                 visual_info->visualid = vp->visualid;
231                 visual_info->screen = screen;
232                 visual_info->depth = depth;
233                 visual_info->class = vp->class;
234                 visual_info->red_mask = vp->red_mask;
235                 visual_info->green_mask = vp->green_mask;
236                 visual_info->blue_mask = vp->blue_mask;
237                 visual_info->colormap_size = vp->map_entries;
238                 visual_info->bits_per_rgb = vp->bits_per_rgb;
239                 UnlockDisplay(dpy);
240                 return True;
241               }
242             vp++;
243           }
244       }
245
246     dp++;
247
248     }
249
250   UnlockDisplay(dpy);
251
252   return False;
253
254 }