/*
 * -- $Header: /cec/src/nestor/servers/R3/shXlib/XGetWAttrs.c,v 1.1.1.1
 * 90/04/23 09:59:46 spanachi Exp $
 */
#include "copyright.h"

/* $XConsortium: XGetWAttrs.c,v 11.21 88/09/06 16:11:44 jim Exp $ */
/* Copyright    Massachusetts Institute of Technology    1986	 */
/* -- MULTIPLEX Extension -- */

#define NEED_REPLIES
#include "Xlibint.h"

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

Status
XGetWindowAttributes (dflt_dpy, w, att)
  register Display *dflt_dpy;
  Window w;
  XWindowAttributes *att;

{
  xGetWindowAttributesReply rep;
  xGetGeometryReply rep2;
  register Display *dpy;
  register xResourceReq *req1;
  register xResourceReq *req2;
  register int i;
  register Screen *sp;
  int depth;

  /* -- flush default dpy's queue to keep buffers aligned -- */
  XmuXFlushDefault (dflt_dpy);
  /* -- switch to the chalk holder display -- */
  dpy = XmuXChalkDisplay (dflt_dpy);

  LockDisplay (dpy);
  /* -- must map ID -- */
  w = XmuXMapID (dpy, w, FromClient);
  GetResReq (GetWindowAttributes, w, req1);
  if (!_XReply (dpy, (xReply *) & rep,
      (SIZEOF (xGetWindowAttributesReply) - SIZEOF (xReply)) >> 2, xTrue)) {
    UnlockDisplay (dpy);
    SyncHandle ();
    return (0);
  }
  att->class = rep.class;
  att->bit_gravity = rep.bitGravity;
  att->win_gravity = rep.winGravity;
  att->backing_store = rep.backingStore;
  att->backing_planes = rep.backingBitPlanes;
  att->backing_pixel = rep.backingPixel;
  att->save_under = rep.saveUnder;
  /* -- must map ! -- */
  att->colormap = XmuXMapID (dpy, rep.colormap, FromServer);
  att->map_installed = rep.mapInstalled;
  att->map_state = rep.mapState;
  att->all_event_masks = rep.allEventMasks;
  att->your_event_mask = rep.yourEventMask;
  att->do_not_propagate_mask = rep.doNotPropagateMask;
  att->override_redirect = rep.override;
  /* -- must map ! -- */
  att->visual = _XVIDtoVisual (dpy,
			       XmuXMapID (dpy, rep.visualID, FromServer));

  GetResReq (GetGeometry, w, req2);

  if (!_XReply (dpy, (xReply *) & rep2, 0, xTrue)) {
    UnlockDisplay (dpy);
    SyncHandle ();
    return (0);
  }
  att->x = cvtINT16toInt (rep2.x);
  att->y = cvtINT16toInt (rep2.y);
  att->width = rep2.width;
  att->height = rep2.height;
  att->border_width = rep2.borderWidth;
  /* -- map it -- */
  att->root = XmuXMapID (dpy, rep2.root, FromServer);

  /* find correct screen so that applications find it easier.... */
  /* -- it's not so easy : we must find the default's correct screen -- */
  /* -- depth's may differ !!! -- */
  if (XmuXDepthFromVisualID (dpy, att->visual->visualid, &depth))
    att->depth = depth;
  else
    att->depth = rep2.depth;
  for (i = 0; i < dflt_dpy->nscreens; i++) {
    sp = &dflt_dpy->screens[i];
    if (sp->root == att->root) {
      att->screen = sp;
      break;
    }
  }
  UnlockDisplay (dpy);
  SyncHandle ();
  return (1);
}
