VisUtil.c revision 1ab64890
11ab64890Smrg/* $Xorg: VisUtil.c,v 1.4 2001/02/09 02:03:37 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1986, 1998  The Open Group
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
81ab64890Smrgthe above copyright notice appear in all copies and that both that
91ab64890Smrgcopyright notice and this permission notice appear in supporting
101ab64890Smrgdocumentation.
111ab64890Smrg
121ab64890SmrgThe above copyright notice and this permission notice shall be included in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg*/
271ab64890Smrg
281ab64890Smrg#ifdef HAVE_CONFIG_H
291ab64890Smrg#include <config.h>
301ab64890Smrg#endif
311ab64890Smrg#include "Xlibint.h"
321ab64890Smrg#include "Xutil.h"
331ab64890Smrg#include <stdio.h>
341ab64890Smrg/*
351ab64890Smrg *	This procedure returns a list of visual information structures
361ab64890Smrg *	that match the specified attributes given in the visual information
371ab64890Smrg *	template.
381ab64890Smrg *
391ab64890Smrg *	If no visuals exist that match the specified attributes, a NULL is
401ab64890Smrg *	returned.
411ab64890Smrg *
421ab64890Smrg *	The choices for visual_info_mask are:
431ab64890Smrg *
441ab64890Smrg *		VisualNoMask
451ab64890Smrg *		VisualIDMask
461ab64890Smrg *		VisualScreenMask
471ab64890Smrg *		VisualDepthMask
481ab64890Smrg *		VisualClassMask
491ab64890Smrg *		VisualRedMaskMask
501ab64890Smrg *		VisualGreenMaskMask
511ab64890Smrg *		VisualBlueMaskMask
521ab64890Smrg *		VisualColormapSizeMask
531ab64890Smrg *		VisualBitsPerRGBMask
541ab64890Smrg *		VisualAllMask
551ab64890Smrg */
561ab64890Smrg
571ab64890SmrgXVisualInfo *XGetVisualInfo(
581ab64890Smrg    Display *dpy,
591ab64890Smrg    register long visual_info_mask,
601ab64890Smrg    register XVisualInfo *visual_info_template,
611ab64890Smrg    int *nitems)	/* RETURN */
621ab64890Smrg{
631ab64890Smrg
641ab64890Smrg  register Visual *vp;
651ab64890Smrg  register Depth *dp;
661ab64890Smrg  Screen *sp;
671ab64890Smrg  int ii,screen_s,screen_e,total,count;
681ab64890Smrg  register XVisualInfo *vip,*vip_base;
691ab64890Smrg
701ab64890Smrg  /* NOTE: NO HIGH PERFORMING CODE TO BE FOUND HERE */
711ab64890Smrg
721ab64890Smrg  LockDisplay(dpy);
731ab64890Smrg
741ab64890Smrg  /* ALLOCATE THE ORIGINAL BUFFER; REALLOCED LATER IF OVERFLOW OCCURS;
751ab64890Smrg     FREED AT END IF NO VISUALS ARE FOUND */
761ab64890Smrg
771ab64890Smrg  count = 0;
781ab64890Smrg  total = 10;
791ab64890Smrg  if (! (vip_base = vip = (XVisualInfo *)
801ab64890Smrg	 Xmalloc((unsigned) (sizeof(XVisualInfo) * total)))) {
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 */
1311ab64890Smrg
1321ab64890Smrg              if (count+1 > total)
1331ab64890Smrg                {
1341ab64890Smrg		  XVisualInfo *old_vip_base = vip_base;
1351ab64890Smrg                  total += 10;
1361ab64890Smrg                  if (! (vip_base = (XVisualInfo *)
1371ab64890Smrg			 Xrealloc((char *) vip_base,
1381ab64890Smrg				  (unsigned) (sizeof(XVisualInfo) * total)))) {
1391ab64890Smrg		      Xfree((char *) old_vip_base);
1401ab64890Smrg		      UnlockDisplay(dpy);
1411ab64890Smrg		      return (XVisualInfo *) NULL;
1421ab64890Smrg		  }
1431ab64890Smrg                  vip = &vip_base[count];
1441ab64890Smrg                }
1451ab64890Smrg
1461ab64890Smrg              count++;
1471ab64890Smrg
1481ab64890Smrg              vip->visual = _XVIDtoVisual(dpy, vp->visualid);
1491ab64890Smrg              vip->visualid = vp->visualid;
1501ab64890Smrg              vip->screen = ii;
1511ab64890Smrg              vip->depth = dp->depth;
1521ab64890Smrg              vip->class = vp->class;
1531ab64890Smrg              vip->red_mask = vp->red_mask;
1541ab64890Smrg              vip->green_mask = vp->green_mask;
1551ab64890Smrg              vip->blue_mask = vp->blue_mask;
1561ab64890Smrg              vip->colormap_size = vp->map_entries;
1571ab64890Smrg              vip->bits_per_rgb = vp->bits_per_rgb;
1581ab64890Smrg
1591ab64890Smrg              vip++;
1601ab64890Smrg
1611ab64890Smrg            } /* END OF LOOP ON VISUALS */
1621ab64890Smrg	  } /* END OF IF THERE ARE ANY VISUALS AT THIS DEPTH */
1631ab64890Smrg
1641ab64890Smrg        } /* END OF LOOP ON DEPTHS */
1651ab64890Smrg
1661ab64890Smrg    } /* END OF LOOP ON SCREENS */
1671ab64890Smrg
1681ab64890Smrg  UnlockDisplay(dpy);
1691ab64890Smrg
1701ab64890Smrg  if (count)
1711ab64890Smrg    {
1721ab64890Smrg      *nitems = count;
1731ab64890Smrg      return vip_base;
1741ab64890Smrg    }
1751ab64890Smrg
1761ab64890Smrg  Xfree((char *) vip_base);
1771ab64890Smrg  *nitems = 0;
1781ab64890Smrg  return NULL;
1791ab64890Smrg}
1801ab64890Smrg
1811ab64890Smrg
1821ab64890Smrg/*
1831ab64890Smrg *	This procedure will return the visual information for a visual
1841ab64890Smrg *      that matches the specified depth and class for a screen.  Since
1851ab64890Smrg *	multiple visuals may exist that match the specified depth and
1861ab64890Smrg *	class, which visual chosen is undefined.
1871ab64890Smrg *
1881ab64890Smrg *	If a visual is found, True is returned as the function value,
1891ab64890Smrg *	otherwise False is returned.
1901ab64890Smrg */
1911ab64890Smrg
1921ab64890SmrgStatus XMatchVisualInfo(
1931ab64890Smrg	Display *dpy,
1941ab64890Smrg	int screen,
1951ab64890Smrg	int depth,
1961ab64890Smrg	int class,
1971ab64890Smrg	XVisualInfo *visual_info) /* RETURNED */
1981ab64890Smrg{
1991ab64890Smrg
2001ab64890Smrg  Visual *vp;
2011ab64890Smrg  Depth *dp;
2021ab64890Smrg  Screen *sp;
2031ab64890Smrg  int ii,jj;
2041ab64890Smrg
2051ab64890Smrg  if (screen < 0 || screen >= dpy->nscreens)
2061ab64890Smrg      return False;
2071ab64890Smrg
2081ab64890Smrg  LockDisplay(dpy);
2091ab64890Smrg
2101ab64890Smrg  sp = (Screen *)(&dpy->screens[screen]);
2111ab64890Smrg
2121ab64890Smrg  dp = sp->depths;
2131ab64890Smrg
2141ab64890Smrg  for (ii=0; ii < sp->ndepths; ii++)
2151ab64890Smrg    {
2161ab64890Smrg
2171ab64890Smrg    /* LOOK THROUGH DEPTHS FOR THE WANTED DEPTH */
2181ab64890Smrg
2191ab64890Smrg    if (dp->depth == depth)
2201ab64890Smrg      {
2211ab64890Smrg        vp = dp->visuals;
2221ab64890Smrg
2231ab64890Smrg        /* LOOK THROUGH VISUALS FOR THE WANTED CLASS */
2241ab64890Smrg
2251ab64890Smrg	/* if nvisuals == 0 then vp will be NULL */
2261ab64890Smrg        for (jj=0; jj<dp->nvisuals; jj++)
2271ab64890Smrg          {
2281ab64890Smrg            if (vp->class == class)
2291ab64890Smrg              {
2301ab64890Smrg		visual_info->visual = _XVIDtoVisual(dpy, vp->visualid);
2311ab64890Smrg		visual_info->visualid = vp->visualid;
2321ab64890Smrg		visual_info->screen = screen;
2331ab64890Smrg		visual_info->depth = depth;
2341ab64890Smrg		visual_info->class = vp->class;
2351ab64890Smrg		visual_info->red_mask = vp->red_mask;
2361ab64890Smrg		visual_info->green_mask = vp->green_mask;
2371ab64890Smrg		visual_info->blue_mask = vp->blue_mask;
2381ab64890Smrg		visual_info->colormap_size = vp->map_entries;
2391ab64890Smrg		visual_info->bits_per_rgb = vp->bits_per_rgb;
2401ab64890Smrg                UnlockDisplay(dpy);
2411ab64890Smrg                return True;
2421ab64890Smrg              }
2431ab64890Smrg            vp++;
2441ab64890Smrg          }
2451ab64890Smrg      }
2461ab64890Smrg
2471ab64890Smrg    dp++;
2481ab64890Smrg
2491ab64890Smrg    }
2501ab64890Smrg
2511ab64890Smrg  UnlockDisplay(dpy);
2521ab64890Smrg
2531ab64890Smrg  return False;
2541ab64890Smrg
2551ab64890Smrg}
256