#include "copyright.h"

/* $XConsortium: XGetHColor.c,v 11.10 88/09/06 16:07:50 jim Exp $ */
/* Copyright    Massachusetts Institute of Technology    1986	 */

/* -- MULTIPLEX Extension -- */

#define NEED_REPLIES
#define NEED_EVENTS
#include "Xlibint.h"

/* -- additional includes -- */
#include "XmuXlibint.h"
#include "resources.h"
#include "map.h"

Status
XAllocColor (calling_dpy, cmap, def)
/* -- must rename or else macro GetReq will fail -- */
  register Display *calling_dpy;
  Colormap cmap;
  XColor *def;
{
  register Display *dpy;
  register int i;
  Status status;
  xAllocColorReply rep;
  register xAllocColorReq *req;

  /*
   * -- Flush the default display's output queue -- to keep things in order
   */
  XmuXFlushDefault (calling_dpy);

  /*
   * -- must loop over all associated displays
   */

  status = 1;
  for (i = 0;
       (dpy = XmuXGetDisplay (calling_dpy, i)) != NULL && status; i++) {
    LockDisplay (dpy);
    GetReq (AllocColor, req);

    if (i != 0)
      req->cmap = XmuXMapID (dpy, cmap, FromClient);
    else
      req->cmap = cmap;

    req->red = def->red;
    req->green = def->green;
    req->blue = def->blue;

    status = _XReply (dpy, (xReply *) & rep, 0, xTrue);
    if (status) {
      if (i == 0) {
	def->pixel = rep.pixel;
	def->red = rep.red;
	def->green = rep.green;
	def->blue = rep.blue;
	if (XmuXStoreResources (dpy)) {
	  unsigned long pixel = rep.pixel;
	  XmuXUpdateColormapInfo (dpy, cmap, &pixel, 1, NULL,
				  0, True);
	}
      }
      else
	XmuXInsertSharedCell (dpy, req->cmap,
			      def->pixel, rep.pixel);
    }
    UnlockDisplay (dpy);
    SyncHandle ();
  }
  return (status);
}
