/*
 * -- $Header: /cec/src/nestor/servers/R3/shXlib/XIntAtom.c,v 1.1.1.1
 * 90/04/23 10:00:54 spanachi Exp $
 */
#include "copyright.h"

/* $XConsortium: XIntAtom.c,v 11.12 88/09/06 16:08:47 jim Exp $ */
/* Copyright    Massachusetts Institute of Technology    1986	 */
/* -- MULTIPLEX Extension -- */

#define NEED_REPLIES
#include "Xlibint.h"

/* -- additional includes -- */
#include "utils.h"
#include "map.h"

Atom
XInternAtom (calling_dpy, name, onlyIfExists)
/* -- must rename or else macro GetReq will fail -- */
  register Display *calling_dpy;
  char *name;
  Bool onlyIfExists;
{
  register long nbytes;
  xInternAtomReply rep;
  register xInternAtomReq *req;
  /* -- additional variables -- */
  register Display *dpy;
  register int i, loop;
  register Atom dflt_atom;

  /* -- flush default dpy's queue to keep buffers aligned -- */
  XmuXFlushDefault (calling_dpy);
  loop = True;
  for (i = 0; (dpy = XmuXGetDisplay (calling_dpy, i)) != NULL && loop;
       i++) {
    LockDisplay (dpy);
    GetReq (InternAtom, req);
    nbytes = req->nbytes = name ? strlen (name) : 0;
    req->onlyIfExists = onlyIfExists;
    req->length += (nbytes + 3) >> 2;
    /* -- use single drain routine here -- */
    _XmuXSingleSend (dpy, name, nbytes);

    /*
     * use _XSend instead of Data, since the following _XReply will always
     * flush the buffer anyway
     */
    if (_XReply (dpy, (xReply *) & rep, 0, xTrue) == 0)
      rep.atom = None;
    if (i == 0)
      dflt_atom = rep.atom;
    else

      /*
       * -- insert the two atom values if they differ and application
       */
    if (dflt_atom != rep.atom) {
      XmuXInsertID (dpy, dflt_atom, rep.atom, FromClient);
      XmuXInsertID (dpy, rep.atom, dflt_atom, FromServer);
    }
    UnlockDisplay (dpy);
    SyncHandle ();
    /* -- if application doesn't createthe atom we won't loop -- */
    if (onlyIfExists)
      loop = False;
  }
  return (dflt_atom);
}
