11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1986, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include "Xlibint.h"
311ab64890Smrg#include "Xutil.h"
321ab64890Smrg#include <stdio.h>
33258a0ebeSmrg#include "reallocarray.h"
34258a0ebeSmrg
351ab64890Smrg/*
361ab64890Smrg *	This procedure returns a list of visual information structures
3761b2299dSmrg *	that match the specified attributes given in the visual information
381ab64890Smrg *	template.
391ab64890Smrg *
401ab64890Smrg *	If no visuals exist that match the specified attributes, a NULL is
411ab64890Smrg *	returned.
421ab64890Smrg *
431ab64890Smrg *	The choices for visual_info_mask are:
441ab64890Smrg *
451ab64890Smrg *		VisualNoMask
461ab64890Smrg *		VisualIDMask
471ab64890Smrg *		VisualScreenMask
481ab64890Smrg *		VisualDepthMask
491ab64890Smrg *		VisualClassMask
501ab64890Smrg *		VisualRedMaskMask
511ab64890Smrg *		VisualGreenMaskMask
521ab64890Smrg *		VisualBlueMaskMask
531ab64890Smrg *		VisualColormapSizeMask
541ab64890Smrg *		VisualBitsPerRGBMask
551ab64890Smrg *		VisualAllMask
561ab64890Smrg */
571ab64890Smrg
581ab64890SmrgXVisualInfo *XGetVisualInfo(
591ab64890Smrg    Display *dpy,
601ab64890Smrg    register long visual_info_mask,
611ab64890Smrg    register XVisualInfo *visual_info_template,
621ab64890Smrg    int *nitems)	/* RETURN */
631ab64890Smrg{
641ab64890Smrg
651ab64890Smrg  register Visual *vp;
661ab64890Smrg  register Depth *dp;
671ab64890Smrg  Screen *sp;
681ab64890Smrg  int ii,screen_s,screen_e,total,count;
691ab64890Smrg  register XVisualInfo *vip,*vip_base;
701ab64890Smrg
711ab64890Smrg  /* NOTE: NO HIGH PERFORMING CODE TO BE FOUND HERE */
721ab64890Smrg
731ab64890Smrg  LockDisplay(dpy);
741ab64890Smrg
751ab64890Smrg  /* ALLOCATE THE ORIGINAL BUFFER; REALLOCED LATER IF OVERFLOW OCCURS;
761ab64890Smrg     FREED AT END IF NO VISUALS ARE FOUND */
771ab64890Smrg
781ab64890Smrg  count = 0;
791ab64890Smrg  total = 10;
80258a0ebeSmrg  if (! (vip_base = vip = Xmallocarray(total, sizeof(XVisualInfo)))) {
811ab64890Smrg      UnlockDisplay(dpy);
821ab64890Smrg      return (XVisualInfo *) NULL;
831ab64890Smrg  }
841ab64890Smrg
851ab64890Smrg  /* DETERMINE IF WE DO ALL SCREENS OR ONLY ONE */
861ab64890Smrg
871ab64890Smrg  screen_s = 0;
881ab64890Smrg  screen_e = dpy->nscreens;
891ab64890Smrg  if (visual_info_mask & VisualScreenMask)
901ab64890Smrg    {
911ab64890Smrg      screen_s = visual_info_template->screen;
921ab64890Smrg      if (screen_s < 0 || screen_s >= screen_e)
931ab64890Smrg	  screen_e = screen_s;
941ab64890Smrg      else
951ab64890Smrg	  screen_e = screen_s + 1;
961ab64890Smrg    }
971ab64890Smrg
981ab64890Smrg  /* LOOP THROUGH SCREENS */
991ab64890Smrg
1001ab64890Smrg  for (ii=screen_s; ii<screen_e; ii++)
1011ab64890Smrg    {
1021ab64890Smrg      sp = (Screen *)(&dpy->screens[ii]);
1031ab64890Smrg
1041ab64890Smrg      /* LOOP THROUGH DEPTHS */
1051ab64890Smrg
1061ab64890Smrg      for (dp=sp->depths; dp < (sp->depths + sp->ndepths); dp++)
1071ab64890Smrg        {
1081ab64890Smrg          if ((visual_info_mask & VisualDepthMask) &&
1091ab64890Smrg	      (dp->depth != visual_info_template->depth)) continue;
1101ab64890Smrg
1111ab64890Smrg          /* LOOP THROUGH VISUALS */
1121ab64890Smrg
1131ab64890Smrg	  if (dp->visuals) {
1141ab64890Smrg	    for (vp=dp->visuals; vp<(dp->visuals + dp->nvisuals); vp++) {
1151ab64890Smrg              if ((visual_info_mask & VisualIDMask) &&
1161ab64890Smrg                (vp->visualid != visual_info_template->visualid)) continue;
1171ab64890Smrg              if ((visual_info_mask & VisualClassMask) &&
1181ab64890Smrg                (vp->class != visual_info_template->class)) continue;
1191ab64890Smrg              if ((visual_info_mask & VisualRedMaskMask) &&
1201ab64890Smrg                (vp->red_mask != visual_info_template->red_mask)) continue;
1211ab64890Smrg              if ((visual_info_mask & VisualGreenMaskMask) &&
1221ab64890Smrg                (vp->green_mask != visual_info_template->green_mask)) continue;
1231ab64890Smrg              if ((visual_info_mask & VisualBlueMaskMask) &&
1241ab64890Smrg                (vp->blue_mask != visual_info_template->blue_mask)) continue;
1251ab64890Smrg              if ((visual_info_mask & VisualColormapSizeMask) &&
1261ab64890Smrg                (vp->map_entries != visual_info_template->colormap_size)) continue;
1271ab64890Smrg              if ((visual_info_mask & VisualBitsPerRGBMask) &&
1281ab64890Smrg                (vp->bits_per_rgb != visual_info_template->bits_per_rgb)) continue;
1291ab64890Smrg
1301ab64890Smrg              /* YEA!!! WE FOUND A GOOD ONE */
13161b2299dSmrg
1321ab64890Smrg              if (count+1 > total)
1331ab64890Smrg                {
1341ab64890Smrg		  XVisualInfo *old_vip_base = vip_base;
1351ab64890Smrg                  total += 10;
136258a0ebeSmrg                  if (! (vip_base = Xreallocarray(vip_base, total,
137258a0ebeSmrg                                                  sizeof(XVisualInfo)))) {
138818534a1Smrg		      Xfree(old_vip_base);
1391ab64890Smrg		      UnlockDisplay(dpy);
1401ab64890Smrg		      return (XVisualInfo *) NULL;
1411ab64890Smrg		  }
1421ab64890Smrg                  vip = &vip_base[count];
1431ab64890Smrg                }
1441ab64890Smrg
1451ab64890Smrg              count++;
1461ab64890Smrg
1471ab64890Smrg              vip->visual = _XVIDtoVisual(dpy, vp->visualid);
1481ab64890Smrg              vip->visualid = vp->visualid;
1491ab64890Smrg              vip->screen = ii;
1501ab64890Smrg              vip->depth = dp->depth;
1511ab64890Smrg              vip->class = vp->class;
1521ab64890Smrg              vip->red_mask = vp->red_mask;
1531ab64890Smrg              vip->green_mask = vp->green_mask;
1541ab64890Smrg              vip->blue_mask = vp->blue_mask;
1551ab64890Smrg              vip->colormap_size = vp->map_entries;
1561ab64890Smrg              vip->bits_per_rgb = vp->bits_per_rgb;
1571ab64890Smrg
1581ab64890Smrg              vip++;
1591ab64890Smrg
1601ab64890Smrg            } /* END OF LOOP ON VISUALS */
1611ab64890Smrg	  } /* END OF IF THERE ARE ANY VISUALS AT THIS DEPTH */
16261b2299dSmrg
1631ab64890Smrg        } /* END OF LOOP ON DEPTHS */
1641ab64890Smrg
1651ab64890Smrg    } /* END OF LOOP ON SCREENS */
1661ab64890Smrg
1671ab64890Smrg  UnlockDisplay(dpy);
1681ab64890Smrg
1691ab64890Smrg  if (count)
1701ab64890Smrg    {
1711ab64890Smrg      *nitems = count;
1721ab64890Smrg      return vip_base;
1731ab64890Smrg    }
1741ab64890Smrg
175818534a1Smrg  Xfree(vip_base);
1761ab64890Smrg  *nitems = 0;
1771ab64890Smrg  return NULL;
1781ab64890Smrg}
1791ab64890Smrg
1801ab64890Smrg
1811ab64890Smrg/*
18261b2299dSmrg *	This procedure will return the visual information for a visual
18361b2299dSmrg *      that matches the specified depth and class for a screen.  Since
18461b2299dSmrg *	multiple visuals may exist that match the specified depth and
1851ab64890Smrg *	class, which visual chosen is undefined.
1861ab64890Smrg *
1871ab64890Smrg *	If a visual is found, True is returned as the function value,
1881ab64890Smrg *	otherwise False is returned.
1891ab64890Smrg */
1901ab64890Smrg
1911ab64890SmrgStatus XMatchVisualInfo(
1921ab64890Smrg	Display *dpy,
1931ab64890Smrg	int screen,
1941ab64890Smrg	int depth,
1951ab64890Smrg	int class,
1961ab64890Smrg	XVisualInfo *visual_info) /* RETURNED */
1971ab64890Smrg{
1981ab64890Smrg
1991ab64890Smrg  Visual *vp;
2001ab64890Smrg  Depth *dp;
2011ab64890Smrg  Screen *sp;
2021ab64890Smrg  int ii,jj;
2031ab64890Smrg
2041ab64890Smrg  if (screen < 0 || screen >= dpy->nscreens)
2051ab64890Smrg      return False;
2061ab64890Smrg
2071ab64890Smrg  LockDisplay(dpy);
2081ab64890Smrg
2091ab64890Smrg  sp = (Screen *)(&dpy->screens[screen]);
2101ab64890Smrg
2111ab64890Smrg  dp = sp->depths;
2121ab64890Smrg
2131ab64890Smrg  for (ii=0; ii < sp->ndepths; ii++)
2141ab64890Smrg    {
2151ab64890Smrg
2161ab64890Smrg    /* LOOK THROUGH DEPTHS FOR THE WANTED DEPTH */
2171ab64890Smrg
2181ab64890Smrg    if (dp->depth == depth)
2191ab64890Smrg      {
2201ab64890Smrg        vp = dp->visuals;
2211ab64890Smrg
2221ab64890Smrg        /* LOOK THROUGH VISUALS FOR THE WANTED CLASS */
2231ab64890Smrg
2241ab64890Smrg	/* if nvisuals == 0 then vp will be NULL */
2251ab64890Smrg        for (jj=0; jj<dp->nvisuals; jj++)
2261ab64890Smrg          {
22761b2299dSmrg            if (vp->class == class)
2281ab64890Smrg              {
2291ab64890Smrg		visual_info->visual = _XVIDtoVisual(dpy, vp->visualid);
2301ab64890Smrg		visual_info->visualid = vp->visualid;
2311ab64890Smrg		visual_info->screen = screen;
2321ab64890Smrg		visual_info->depth = depth;
2331ab64890Smrg		visual_info->class = vp->class;
2341ab64890Smrg		visual_info->red_mask = vp->red_mask;
2351ab64890Smrg		visual_info->green_mask = vp->green_mask;
2361ab64890Smrg		visual_info->blue_mask = vp->blue_mask;
2371ab64890Smrg		visual_info->colormap_size = vp->map_entries;
2381ab64890Smrg		visual_info->bits_per_rgb = vp->bits_per_rgb;
2391ab64890Smrg                UnlockDisplay(dpy);
2401ab64890Smrg                return True;
2411ab64890Smrg              }
2421ab64890Smrg            vp++;
2431ab64890Smrg          }
2441ab64890Smrg      }
2451ab64890Smrg
2461ab64890Smrg    dp++;
2471ab64890Smrg
2481ab64890Smrg    }
2491ab64890Smrg
2501ab64890Smrg  UnlockDisplay(dpy);
2511ab64890Smrg
2521ab64890Smrg  return False;
2531ab64890Smrg
2541ab64890Smrg}
255