/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

/* $Header: gc.c,v 1.78 87/06/23 21:38:00 toddb Exp $ */

#include "X.h"
#include "Xmd.h"
#include "Xproto.h"
#include "misc.h"
#include "resource.h"
#include "gcstruct.h"
#include "pixmapstr.h"
#include "dixfontstr.h"
#include "scrnintstr.h"
#include "region.h"

#include "dix.h"
#include "opaque.h"

/* written by drewry august 1986 */

void
ValidateGC(pDraw, pGC)
    DrawablePtr	pDraw;
    GC		*pGC;
{
    GCInterestPtr	pQ, pQInit;

    pQ = pGC->pNextGCInterest;
    pQInit = (GCInterestPtr) &pGC->pNextGCInterest;
    if (pGC->serialNumber != pDraw->serialNumber)
        pGC->stateChanges |= GC←CALL←VALIDATE←BIT;
    do
    {
	if (pQ->ValInterestMask & pGC->stateChanges)
	    (* pQ->ValidateGC) (pGC, pQ, pGC->stateChanges, pDraw);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
    pGC->stateChanges = 0;
    pGC->serialNumber = pDraw->serialNumber;
}



/* ChangeGC(pGC, mask, pval)
   mask is a set of bits indicating which values to change.
   pval contains an appropriate value for each mask.
   if there is an error, the value is marked as changed 
anyway, which is probably wrong, but infrequent.

NOTE:
	all values sent over the protocol for ChangeGC requests are
32 bits long
*/

int
ChangeGC(pGC, mask, pval)
    register GC 	*pGC;
    register BITS32	mask;
    CARD32		*pval;
{
    register int 	index;
    register int 	error = 0;
    PixmapPtr 		pPixmap;
    BITS32		maskQ;
    GCInterestPtr	pQ, pQInit;

    pGC->serialNumber |= GC←CHANGE←SERIAL←BIT;

    maskQ = mask;	/* save these for when we walk the GCque */
    while (mask && !error) 
    {
	index = ffs((long)mask) - 1;
	mask &= ~(index = (1 << index));
	pGC->stateChanges |= index;
	switch (index)
	{
	    case GCFunction:
		if (((CARD8)*pval >= GXclear) && ((CARD8)*pval <= GXset))
		    pGC->alu = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCPlaneMask:
		pGC->planemask = *pval++;
		break;
	    case GCForeground:
		pGC->fgPixel = *pval++;
		break;
	    case GCBackground:
		pGC->bgPixel = *pval++;
		break;
	    case GCLineWidth:		/* ??? line width is a CARD16 */
		pGC->lineWidth = (CARD16)*pval;
                pval++;
		break;
	    case GCLineStyle:
		if (((CARD8)*pval >= LineSolid) 
		    && ((CARD8)*pval <= LineDoubleDash))
		    pGC->lineStyle = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCCapStyle:
		if (((CARD8)*pval >= CapNotLast) 
		    && ((CARD8)*pval <= CapProjecting))
		    pGC->capStyle = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCJoinStyle:
		if (((CARD8)*pval >= JoinMiter) && ((CARD8)*pval <= JoinBevel))
		    pGC->joinStyle = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCFillStyle:
		if (((CARD8)*pval >= FillSolid) 
		    && ((CARD8)*pval <= FillOpaqueStippled))
		    pGC->fillStyle = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCFillRule:
		if (((CARD8)*pval >= EvenOddRule) && 
		    ((CARD8)*pval <= WindingRule))
		    pGC->fillRule = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCTile:
		pPixmap = (PixmapPtr)LookupID((CARD32)*pval, 
					      RT←PIXMAP, RC←CORE);
		pval++;
		if (pPixmap)
		{
		    if ((pPixmap->drawable.depth != pGC->depth) ||
			(pPixmap->drawable.pScreen != pGC->pScreen))
		    {
			error = BadMatch;
		    }
		    else
		    {
			(* pGC->pScreen->DestroyPixmap)(pGC->tile);
			pGC->tile = pPixmap;
			pPixmap->refcnt++;
		    }
		}
		else
		    error = BadPixmap;
		break;
	    case GCStipple:
		pPixmap = (PixmapPtr)LookupID((CARD32)*pval, 
					      RT←PIXMAP, RC←CORE);
		pval++;
		if (pPixmap)
		{
		    if ((pPixmap->drawable.depth != 1) ||
			(pPixmap->drawable.pScreen != pGC->pScreen))
		    {
			error = BadMatch;
		    }
		    else
		    {
			(* pGC->pScreen->DestroyPixmap)(pGC->stipple);
			pGC->stipple = pPixmap;
			pPixmap->refcnt++;
		    }
		}
		else
		    error = BadPixmap;
		break;
	    case GCTileStipXOrigin:
		pGC->patOrg.x = (INT16)*pval;
                pval++;
		break;
	    case GCTileStipYOrigin:
		pGC->patOrg.y = (INT16)*pval;
		pval++;
		break;
	    case GCFont:
              {
		CARD32 fid;
		FontPtr	pFont;

		fid = * pval++;

		pFont = (FontPtr)LookupID(fid, RT←FONT, RC←CORE);

		if (pFont)
		{
		    CloseFont( pGC->font,(long)0);
		    pGC->font = pFont;
		    pGC->font->refcnt++;
		 }
		else
		    error = BadFont;
		break;
	      }
	    case GCSubwindowMode:
		if ((*pval == ClipByChildren) ||
		    (*pval == IncludeInferiors))
		    pGC->subWindowMode = (CARD8)(*pval);
		else
		    error = BadValue;
		pval++;
		break;
	    case GCGraphicsExposures:
		if ((Bool)*pval == xFalse)
		    pGC->graphicsExposures = FALSE;
		else if ((Bool)*pval == xTrue)
		    pGC->graphicsExposures = TRUE;
		else
		    error = BadValue;
		pval++;
		break;
	    case GCClipXOrigin:
		pGC->clipOrg.x = (INT16)(*pval);
		pval++;
		break;
	    case GCClipYOrigin:
		pGC->clipOrg.y = (INT16)(*pval);
		pval++;
		break;
	    case GCClipMask:
	      {
		Pixmap pid;
		int	clipType;

		pid = (Pixmap) *pval++;
		if (pid == None)
		{
		    clipType = CT←NONE;
		}
		else
		{
		    pPixmap = (PixmapPtr)LookupID(pid, RT←PIXMAP, RC←CORE);
		    if (pPixmap)
		    {
		        clipType = CT←PIXMAP;
			pPixmap->refcnt++;
		    }
		    else
			error = BadPixmap;
		}
		if(error == Success)
		{
		    (*pGC->ChangeClip)(pGC, clipType, pPixmap, 0);
		}
		break;
	      }
	    case GCDashOffset:
		pGC->dashOffset = (CARD16)*pval;
		pval++;
		break;
	    case GCDashList:
		Xfree(pGC->dash);
		pGC->numInDashList = 2;
		pGC->dash = (unsigned char *)Xalloc(2 * sizeof(unsigned char));
		pGC->dash[0] = (CARD8)(*pval);
		pGC->dash[1] = (CARD8)(*pval++);
		break;
	    case GCArcMode:
		if (((CARD8)*pval >= ArcChord) 
		    && ((CARD8)*pval <= ArcPieSlice))
		    pGC->arcMode = (CARD8)*pval;
		else
		    error = BadValue;
		pval++;
		break;
	    default:
		break;
	}
    }
    pQ = pGC->pNextGCInterest;
    pQInit = (GCInterestPtr) &pGC->pNextGCInterest;
    do
    {
	/* I assume that if you've set a change interest mask, you've set a
	 * changeGC function */
	if(pQ->ChangeInterestMask & maskQ)
	    (*pQ->ChangeGC)(pGC, pQ, maskQ);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
    return error;
}

/* CreateGC(pDrawable, mask, pval, pStatus)
   creates a default GC for the given drawable, using mask to fill
   in any non-default values.
   Returns a pointer to the new GC on success, NULL otherwise.
   returns status of non-default fields in pStatus
BUG:
   should check for failure to create default tile and stipple

*/

GC *
CreateGC(pDrawable, mask, pval, pStatus)
    DrawablePtr	pDrawable;
    BITS32	mask;
    long	*pval;
    BITS32	*pStatus;
{
    register GC *pGC;
    extern FontPtr defaultFont;
    CARD32	tmpval[3];
    PixmapPtr pStip, pTile;
    GCPtr pgcScratch;	/* for drawing into default tile and stipple */
    xRectangle rect;
#ifdef DEBUG
    void 	(**j)();
#endif /* DEBUG */

    pGC = (GC *)Xalloc(sizeof(GC));
    if (!pGC)
	return (GC *)NULL;

#ifdef DEBUG
    for(j = &pGC->FillSpans;
        j < &pGC->PushPixels;
        j++ )
        *j = (void (*) ())NotImplemented;
#endif /* DEBUG */

    pGC->pScreen = pDrawable->pScreen;
    pGC->depth = pDrawable->depth;
    pGC->alu = GXcopy; /* dst <- src */
    pGC->planemask = ~0;
    pGC->serialNumber = 0;

    pGC->fgPixel = 0;
    pGC->bgPixel = 1;
    pGC->lineWidth = 0;
    pGC->lineStyle = LineSolid;
    pGC->capStyle = CapButt;
    pGC->joinStyle = JoinMiter;
    pGC->fillStyle = FillSolid;
    pGC->fillRule = EvenOddRule;
    pGC->arcMode = ArcPieSlice;
    pGC->font = defaultFont;
    if ( pGC->font)  /* necessary, because open of default font could fail */
	pGC->font->refcnt++;
    pGC->tile = NullPixmap;

    /* build a stipple and fill it with 1 */
    pStip = (PixmapPtr)
	    (*pGC->pScreen->CreatePixmap)(pDrawable->pScreen,
					  1, 1, 1);
    tmpval[0] = GXcopy; tmpval[1] = 1; tmpval[2] = FillSolid;
    pgcScratch = GetScratchGC(1, pGC->pScreen);
    ChangeGC(pgcScratch, GCFunction | GCForeground | GCFillStyle, tmpval);
    ValidateGC((DrawablePtr)pStip, pgcScratch);
    rect.x = 0;
    rect.y = 0;
    rect.width = 0;
    rect.height = 0;
    (*pgcScratch->PolyFillRect)(pStip, pgcScratch, 1, &rect);

    pGC->stipple = pStip;
    pGC->patOrg.x = 0;
    pGC->patOrg.y = 0;
    pGC->subWindowMode = ClipByChildren;
    pGC->graphicsExposures = TRUE;
    pGC->clipOrg.x = 0;
    pGC->clipOrg.y = 0;
    pGC->numInDashList = 2;
    pGC->dash = (unsigned char *)Xalloc(2 * sizeof(unsigned char));
    pGC->dash[0] = 4;
    pGC->dash[1] = 4;
    pGC->dashOffset = 0;

    pGC->stateChanges = (1 << GCLastBit+1) - 1;
    (*pGC->pScreen->CreateGC)(pGC);
    if(mask)
        *pStatus = ChangeGC(pGC, mask, pval);
    else
	*pStatus = Success;


    /* if the client hasn't provided a tile, build one and fill it with
       the foreground pixel
    */
    if (!pGC->tile)
    {
	pTile = (PixmapPtr)
		(*pGC->pScreen->CreatePixmap)(pDrawable->pScreen,
					      1, 1, pGC->depth);
	tmpval[1] = pGC->fgPixel;
	pgcScratch = GetScratchGC(pGC->depth, pGC->pScreen);
	ChangeGC(pgcScratch, GCFunction | GCForeground | GCFillStyle, 
		 tmpval);
	ValidateGC((DrawablePtr)pTile, pgcScratch);
	(*pgcScratch->PolyFillRect)(pTile, pgcScratch,
					 1, &rect);
	pGC->tile = pTile;
    }
    return (pGC);
}


void
CopyGC(pgcSrc, pgcDst, mask)
    register GC		*pgcSrc;
    register GC		*pgcDst;
    register Mask	mask;
{
    register int	index;
    Mask		maskQ;
    GCInterestPtr	pQ, pQInit;
    int i;

    pgcDst->stateChanges |= mask;
    maskQ = mask;
    while (mask)
    {
	index = ffs(mask) - 1;
	mask &= ~(index = (1 << index));
	switch (index)
	{
	    case GCFunction:
		pgcDst->alu = pgcSrc->alu;
		break;
	    case GCPlaneMask:
		pgcDst->planemask = pgcSrc->planemask;
		break;
	    case GCForeground:
		pgcDst->fgPixel = pgcSrc->fgPixel;
		break;
	    case GCBackground:
		pgcDst->bgPixel = pgcSrc->bgPixel;
		break;
	    case GCLineWidth:
		pgcDst->lineWidth = pgcSrc->lineWidth;
		break;
	    case GCLineStyle:
		pgcDst->lineStyle = pgcSrc->lineStyle;
		break;
	    case GCCapStyle:
		pgcDst->capStyle = pgcSrc->capStyle;
		break;
	    case GCJoinStyle:
		pgcDst->joinStyle = pgcSrc->joinStyle;
		break;
	    case GCFillStyle:
		pgcDst->fillStyle = pgcSrc->fillStyle;
		break;
	    case GCFillRule:
		pgcDst->fillRule = pgcSrc->fillRule;
		break;
	    case GCTile:
		{
		    (* pgcDst->pScreen->DestroyPixmap)(pgcDst->tile);
		    pgcDst->tile = pgcSrc->tile;
		    if (IS←VALID←PIXMAP(pgcDst->tile))
		       pgcDst->tile->refcnt ++;
		    break;
		}
	    case GCStipple:
		{
		    (* pgcDst->pScreen->DestroyPixmap)(pgcDst->stipple);
		    pgcDst->stipple = pgcSrc->stipple;
		    if (IS←VALID←PIXMAP(pgcDst->stipple))
    		        pgcDst->stipple->refcnt ++;
		    break;
		}
	    case GCTileStipXOrigin:
		pgcDst->patOrg.x = pgcSrc->patOrg.x;
		break;
	    case GCTileStipYOrigin:
		pgcDst->patOrg.y = pgcSrc->patOrg.y;
		break;
	    case GCFont:
		pgcDst->font = pgcSrc->font;
		break;
	    case GCSubwindowMode:
		pgcDst->subWindowMode = pgcSrc->subWindowMode;
		break;
	    case GCGraphicsExposures:
		pgcDst->graphicsExposures = pgcSrc->graphicsExposures;
		break;
	    case GCClipXOrigin:
		pgcDst->clipOrg.x = pgcSrc->clipOrg.x;
		break;
	    case GCClipYOrigin:
		pgcDst->clipOrg.y = pgcSrc->clipOrg.y;
		break;
	    case GCClipMask:
		(*pgcDst->DestroyClip)(pgcDst);
		pgcDst->clientClip = pgcSrc->clientClip;
		pgcDst->clientClipType = pgcSrc->clientClipType;
		break;
	    case GCDashOffset:
		pgcDst->dashOffset = pgcSrc->dashOffset;
		break;
	    case GCDashList:
		Xfree(pgcDst->dash);
		pgcDst->dash = (unsigned char *)
				Xalloc(2 * sizeof(unsigned char));
		pgcDst->numInDashList = pgcSrc->numInDashList;
		for (i=0; i<pgcSrc->numInDashList; i++)
		    pgcDst->dash[i] = pgcSrc->dash[i];
		break;
	    case GCArcMode:
		pgcDst->arcMode = pgcSrc->arcMode;
		break;
	    default:
		break;
	}
    }
    pQ = pgcSrc->pNextGCInterest;
    pQInit = (GCInterestPtr) &pgcSrc->pNextGCInterest;
    do
    {
	if(pQ->CopyGCSource)
	    (*pQ->CopyGCSource)(pgcSrc, pQ, maskQ, pgcDst);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
    pQ = pgcDst->pNextGCInterest;
    pQInit = (GCInterestPtr) &pgcDst->pNextGCInterest;
    do
    {
	if(pQ->CopyGCDest)
	    (*pQ->CopyGCDest)(pgcDst, pQ, maskQ, pgcSrc);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
}

/*****************
 * FreeGC 
 *   does the diX part of freeing the characteristics in the GC 
 ***************/

int
FreeGC(pGC, gid)
    GC *pGC;
    long gid;
{
    GCInterestPtr	pQ, pQInit, pQnext;

    CloseFont(pGC->font, (long)0);
    (* pGC->DestroyClip)(pGC);

    (* pGC->pScreen->DestroyPixmap)(pGC->tile);
    (* pGC->pScreen->DestroyPixmap)(pGC->stipple);

    pQ = pGC->pNextGCInterest;
    pQInit = (GCInterestPtr) &pGC->pNextGCInterest;
    do
    {
        pQnext = pQ->pNextGCInterest;
	if(pQ->DestroyGC)
	    (*pQ->DestroyGC) (pGC, pQ);
	pQ = pQnext;
    }
    while(pQ != pQInit);
    Xfree(pGC);
}

void
SetGCMask(pGC, selectMask, newDataMask)
    GCPtr pGC;
    Mask selectMask;
    Mask newDataMask;
{
    pGC->stateChanges = (~selectMask & pGC->stateChanges) |
		        (selectMask & newDataMask);
    if (selectMask & newDataMask)
        pGC->serialNumber |= GC←CHANGE←SERIAL←BIT;        
}



/* CreateScratchGC(pScreen, depth)
    like CreateGC, but doesn't do the default tile or stipple,
since we can't create them without already having a GC.  any code
using the tile or stipple has to set them explicitly anyway,
since the state of the scratch gc is unknown.  This is OK
because ChangeGC() has to be able to deal with NULL tiles and
stipples anyway (in case the CreateGC() call has provided a 
value for them -- we can't set the default tile until the
client-supplied attributes are installed, since the fgPixel
is what fills the default tile.  (maybe this comment should
go with CreateGC() or ChangeGC().)
*/

static /* cling */
GC *
CreateScratchGC(pScreen, depth)
    ScreenPtr pScreen;
    int depth;
{
    register GC *pGC;
    extern FontPtr defaultFont;
#ifdef DEBUG
    void 	(**j)();
#endif /* DEBUG */

    pGC = (GC *)Xalloc(sizeof(GC));
    if (!pGC)
	return (GC *)NULL;

#ifdef DEBUG
    for(j = &pGC->FillSpans;
        j < &pGC->PushPixels;
        j++ )
        *j = (void (*) ())NotImplemented;
#endif /* DEBUG */

    pGC->pScreen = pScreen;
    pGC->depth = depth;
    pGC->alu = GXcopy; /* dst <- src */
    pGC->planemask = ~0;
    pGC->serialNumber = 0;

    pGC->fgPixel = 0;
    pGC->bgPixel = 1;
    pGC->lineWidth = 0;
    pGC->lineStyle = LineSolid;
    pGC->capStyle = CapButt;
    pGC->joinStyle = JoinMiter;
    pGC->fillStyle = FillSolid;
    pGC->fillRule = EvenOddRule;
    pGC->arcMode = ArcPieSlice;
    pGC->font = defaultFont;
    if ( pGC->font)  /* necessary, because open of default font could fail */
	pGC->font->refcnt++;
    pGC->tile = NullPixmap;
    pGC->stipple = NullPixmap;
    pGC->patOrg.x = 0;
    pGC->patOrg.y = 0;
    pGC->subWindowMode = ClipByChildren;
    pGC->graphicsExposures = TRUE;
    pGC->clipOrg.x = 0;
    pGC->clipOrg.y = 0;
    pGC->numInDashList = 2;
    pGC->dash = (unsigned char *)Xalloc(2 * sizeof(unsigned char));
    pGC->dash[0] = 4;
    pGC->dash[1] = 4;
    pGC->dashOffset = 0;

    pGC->stateChanges = (1 << GCLastBit+1) - 1;
    (*pScreen->CreateGC)(pGC);
    return pGC;
}


FreeGCperDepth(screenNum)
    int screenNum;
{
    register int i;
    register ScreenPtr pScreen;
    GCPtr *ppGC;

    pScreen = &screenInfo.screen[screenNum];
    ppGC = (GCPtr *) pScreen->GCperDepth;

    for (i = 0; i < pScreen-> numDepths; i++)
    {
	FreeGC(ppGC[i], (long)0);
	ppGC[i] = (GC *) NULL;
    }
}


CreateGCperDepthArray(screenNum)
    int screenNum;
{
    register int i;
    register ScreenPtr pScreen;
    DepthPtr pDepth;
    int p;

    pScreen = &screenInfo.screen[screenNum];
    pScreen->rgf = 0;
    /* do depth 1 seperately because it's not included in list */
    pScreen->GCperDepth[0] = CreateScratchGC(pScreen, 1);
    (pScreen->GCperDepth[0])->graphicsExposures = FALSE;

    pDepth = pScreen->allowedDepths;
    for (i=0; i<pScreen->numDepths; i++, pDepth++)
    {
	pScreen->GCperDepth[i+1] = CreateScratchGC(pScreen,
						   pDepth->depth);
	(pScreen->GCperDepth[i+1])->graphicsExposures = FALSE;
    }
}

SetDashes(pGC, offset, ndash, pdash)
register GCPtr pGC;
int offset;
register int ndash;
register unsigned char *pdash;
{
    register int i;
    register unsigned char *p;
    GCInterestPtr	pQ, pQInit;
    long maskQ = 0;

    i = ndash;
    p = pdash;
    while (i--)
    {
	if (!*p++)
	{
	    /* dash segment must be > 0 */
	    return BadValue;
	}
    }

    if (offset != pGC->dashOffset)
    {
	pGC->dashOffset = offset;
	pGC->stateChanges |= GCDashOffset;
	maskQ |= GCDashOffset;
    }

    p = (unsigned char *)Xalloc(ndash * sizeof(unsigned char));
    if (!p)
    {
	return BadAlloc;
    }
    Xfree(pGC->dash);
    pGC->dash = p;
    pGC->numInDashList = ndash;
    while(ndash--)
	*p++ = *pdash++;
    pGC->stateChanges |= GCDashList;
    maskQ |= GCDashList;

    pQ = pGC->pNextGCInterest;
    pQInit = (GCInterestPtr) &pGC->pNextGCInterest;
    do
    {
	if(pQ->ChangeInterestMask & maskQ)
	    (*pQ->ChangeGC)(pGC, pQ, maskQ);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
    return Success;
}


int
SetClipRects(pGC, nrects, prects, ordering)
    GCPtr		pGC;
    int			nrects;
    xRectangle		*prects;
    int			ordering;
{
    register xRectangle	*prectP, *prectN;
    register int	i;
    int			newct, size, type;
    xRectangle 		*prectsNew;
    GCInterestPtr	pQ, pQInit;

    switch(ordering)
    {
      case Unsorted:
	  newct = CT←UNSORTED;
	  break;
      case YSorted:
	  if(i > 0)
	  {
	      prectP = prects;
	      prectN = prects + 1;
	      for(i = 1; i < nrects; i++, prectN++, prectP++)
		  if(prectN->y < prectP->y)
		      return(BadMatch);
	  }
	  newct = CT←YSORTED;
	  break;
      case YXSorted:
	  if(i > 0)
	  {
	      for(i = 1; i < nrects; i++, prectN++, prectP++)
		  if((prectN->y < prectP->y) ||
		      ( (prectN->y == prectP->y) &&
		        (prectN->x < prectP->x) ) )
		      return(BadMatch);
	  }
	  newct = CT←YXSORTED;
	  break;
      case YXBanded:
	  if(i > 0)
	  {
	      for(i = 1; i < nrects; i++, prectN++, prectP++)
		  if((prectN->y < prectP->y) ||
		      ( (prectN->y == prectP->y) &&
		        (  (prectN->x < prectP->x)   ||
		           (prectN->height != prectP->height) ) ) )
		      return(BadMatch);
	  }
	  newct = CT←YXBANDED;
	  break;
    }

    size = nrects * sizeof(xRectangle);
    prectsNew = (xRectangle *) Xalloc(size);
    bcopy(prects, prectsNew, size);
    (*pGC->ChangeClip)(pGC, newct, prectsNew, nrects);
    pQ = pGC->pNextGCInterest;
    pQInit = (GCInterestPtr) &pGC->pNextGCInterest;
    do
    {
	if(pQ->ChangeInterestMask & GCClipMask)
	    (*pQ->ChangeGC)(pGC, pQ, GCClipMask);
	pQ = pQ->pNextGCInterest;
    }
    while(pQ != pQInit);
    return Success;

}


/*
   sets reasonable defaults 
   if we can get a pre-allocated one, use it and mark it as used.
   if we can't, create one out of whole cloth (The Velveteen GC -- if
   you use it often enough it will become real.)
*/
GCPtr
GetScratchGC(depth, pScreen)
    register int depth;
    register ScreenPtr pScreen;
{
    register int i;
    register GCPtr pGC = (GCPtr)NULL;;

    for (i=0; i<=pScreen->numDepths; i++)
        if ( pScreen->GCperDepth[i] &&
	     pScreen->GCperDepth[i]->depth == depth &&
	     !(pScreen->rgf & 1 << (i+1))
	   )
	{
	    pScreen->rgf |= 1 << (i+1);
            pGC = (pScreen->GCperDepth[i]);

	    pGC->alu = GXcopy;
	    pGC->planemask = ~0;
	    pGC->serialNumber = 0;
	    pGC->fgPixel = 0;
	    pGC->bgPixel = 1;
	    pGC->lineWidth = 0;
	    pGC->lineStyle = LineSolid;
	    pGC->capStyle = CapButt;
	    pGC->joinStyle = JoinMiter;
	    pGC->fillStyle = FillSolid;
	    pGC->fillRule = EvenOddRule;
	    pGC->arcMode = ArcChord;
	    pGC->patOrg.x = 0;
	    pGC->patOrg.y = 0;
	    pGC->subWindowMode = ClipByChildren;
	    pGC->graphicsExposures = FALSE;
	    pGC->clipOrg.x = 0;
	    pGC->clipOrg.y = 0;
	    pGC->stateChanges = (1 << GCLastBit+1) - 1;
	    return pGC;
	}
    /* if we make it this far, need to roll our own */
    pGC =  CreateScratchGC(pScreen, depth);
    pGC->graphicsExposures = FALSE;
    return pGC;
}

/*
   if the gc to free is in the table of pre-existing ones,
mark it as available.
   if not, free it for real
*/
void
FreeScratchGC(pGC)
    register GCPtr pGC;
{
    register ScreenPtr pScreen = pGC->pScreen;
    register int depth = pGC->depth;
    register int i;

    for (i=0; i<=pScreen->numDepths; i++)
    {
        if ( pScreen->GCperDepth[i] == pGC)
	{
	    pScreen->rgf &= ~(1<<i+1);
	    return;
	}
    }
    FreeGC(pGC, (long)0);
}