/*
 * -- $Header: /cec/src/nestor/servers/R3/shXlib/XStColors.c,v 1.1.1.1 90/04/23 10:07:31 spanachi Exp $
 */

#include "copyright.h"

/* $XConsortium: XStColors.c,v 11.12 88/09/06 16:10:35 jim Exp $ */
/* Copyright    Massachusetts Institute of Technology    1986	*/

#include "Xlibint.h"

XStoreColors(dpy, cmap, defs, ncolors)
register Display *dpy;
Colormap cmap;
XColor defs[];
int ncolors;
{
    register int i , 
                 available ,
                 begin , 
                 end ;
    xColorItem citem;
    register xStoreColorsReq *req;

    /*
     * -- Normally , we would not alter this routine, BUT it's the 
     * -- only one that may let the display buffer start with a 
     * -- PARTIAL REQUEST !!!
     * -- We do not allow such nasty things (we would have to store more
     * -- information; remember: we must map the pixels ! )
     */
    
    LockDisplay(dpy);  
    begin  = 0 ;
    end    = ncolors ;
    while( begin < ncolors ) 
    {
	GetReq(StoreColors, req);
	req->cmap = cmap ;
	available = dpy->bufmax - dpy->bufptr ;
	if( ( ( end - begin ) * SIZEOF(xColorItem) ) > available )
	    end     = begin + available / SIZEOF(xColorItem) ;
	else
	    end     = ncolors ;

	/* assume size is 4*n */
	req->length += ( (end - begin) * SIZEOF(xColorItem)) >> 2; 

	for (i = begin ; i < end ; i++) 
	{
	    citem.pixel = defs[ i ].pixel;
	    citem.red = defs[i].red;
	    citem.green = defs[i].green;
	    citem.blue = defs[i].blue;
	    citem.flags = defs[i].flags;

	    /* note that xColorItem doesn't contain all 16-bit quantities, so
	       we can't use Data16 */
	    Data(dpy, (char *)&citem, (long) SIZEOF(xColorItem)); 
	    /* assume size is 4*n */
	}
	begin = end ;
    }
    UnlockDisplay(dpy);
    SyncHandle();
}
