/*
 * xvroot.c - '-root' related code for XV
 *
 *  Author:    John Bradley, University of Pennsylvania
 *                (bradley@cis.upenn.edu)
 *
 *  Contains:
 *            MakeRootPic()
 *            ClearRoot()
 *            SaveRootInfo()
 *            KillOldRootInfo()
 */


/*
 * Copyright 1989, 1990, 1991, 1992 by John Bradley and
 *                       The University of Pennsylvania
 *
 * Permission to use, copy, and distribute for non-commercial purposes,
 * is hereby granted without fee, providing that the above copyright
 * notice appear in all copies and that both the copyright notice and this
 * permission notice appear in supporting documentation.
 *
 * The software may be modified for your own purposes, but modified versions
 * may not be distributed.
 *
 * This software is provided "as is" without any expressed or implied warranty.
 *
 * The author may be contacted via:
 *    US Mail:   John Bradley
 *               GRASP Lab, Room 301C
 *               3401 Walnut St.
 *               Philadelphia, PA  19104
 *
 *    Phone:     (215) 898-8813
 *    EMail:     bradley@cis.upenn.edu
 */


#include "xv.h"
#include "bitmaps.h"

/* local function pre-definitions */
#ifdef __STDC__
static void killRootPix(void);
#else
static void killRootPix();
#endif


/***********************************/
void MakeRootPic()
{
  /* called after 'epic' has been generated (if we're using root).  
     creates the XImage and the pixmap, sets the root to the new
     pixmap, and refreshes the display */

  Pixmap tmpPix;
  int    i, j, k, rpixw, rpixh, rmode;

  killRootPix();

  rmode = rootMode;
  /* if eWIDE,eHIGH == dispWIDE,dispHIGH just use 'normal' mode to save mem */
  if (rmode>=RM_CENTER && eWIDE==dispWIDE && eHIGH==dispHIGH) rmode=RM_NORMAL;

  /* determine how big tmpPix should be based on rootMode */
  switch (rmode) {
  case RM_NORMAL:
  case RM_CENTILE:
  case RM_TILE:    rpixw = eWIDE;    rpixh = eHIGH;    break;
  case RM_MIRROR:
  case RM_IMIRROR: rpixw = 2*eWIDE;  rpixh = 2*eHIGH;  break;
  case RM_CSOLID:
  case RM_CWARP:
  case RM_CBRICK:  rpixw = dispWIDE; rpixh = dispHIGH; break;
  default:         rpixw = eWIDE;    rpixh = eHIGH;    break;
  }
  if (nolimits) { RANGE(rpixw, 1, maxWIDE);  RANGE(rpixh, 1, maxHIGH); }
           else { RANGE(rpixw, 1, dispWIDE);  RANGE(rpixh, 1, dispHIGH); }

  /* create tmpPix */
  xerrcode = 0;
  tmpPix = XCreatePixmap(theDisp, mainW, rpixw, rpixh, dispDEEP);
  XSync(theDisp, False);
  if (xerrcode || !tmpPix) {
    static char *foo[] = { "\nDarn!" };
    char str[128];
    sprintf(str, "Insufficient memory in X server to build root pixmap.");
    PopUp(str, foo, 1);
    return;
  }


  if (rmode == RM_NORMAL || rmode == RM_TILE) {
    XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, 0,0, eWIDE, eHIGH);
  }

  else if (rmode == RM_MIRROR || rmode == RM_IMIRROR) {
    /* quadrant 2 */
    XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, 0,0, eWIDE, eHIGH);
    if (epic == NULL) FatalError("epic == NULL in RM_MIRROR code...\n");

    /* quadrant 1 */
    FlipPic(epic, eWIDE, eHIGH, 0);   /* flip horizontally */
    CreateXImage();
    XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, eWIDE,0, eWIDE, eHIGH);

    /* quadrant 4 */
    FlipPic(epic, eWIDE, eHIGH, 1);   /* flip vertically */
    CreateXImage();
    XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, eWIDE,eHIGH, eWIDE,eHIGH);

    /* quadrant 3 */
    FlipPic(epic, eWIDE, eHIGH, 0);   /* flip horizontally */
    CreateXImage();
    XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, 0,eHIGH, eWIDE,eHIGH);

    FlipPic(epic, eWIDE, eHIGH, 1);   /* flip vertically  (back to orig) */
    CreateXImage();                   /* put back to original state */
  }

  else if (rmode >= RM_CENTER) {
    /* do some stuff to set up the border around the picture */

    if (rmode != RM_CENTILE) {
      XSetForeground(theDisp, theGC, rootbg);
      XFillRectangle(theDisp, tmpPix, theGC, 0,0, dispWIDE, dispHIGH);
    }

    if (rmode == RM_CENTILE) {    /* hagan-style tiling */
      int x, y, w, h, ax, ay, w1, h1, offx, offy;

      w = eWIDE;  h = eHIGH;

      /* compute anchor pt (top-left coords of top-left-most pic) */
      ax = (dispWIDE-w)/2;  ay = (dispHIGH-h)/2;
      while (ax>0) ax = ax - w;
      while (ay>0) ay = ay - h;

      for (i=ay; i < (int) eHIGH; i+=h) {
	for (j=ax; j < (int) eWIDE; j+=w) {
	  /* if image goes off tmpPix, only draw subimage */
	  
	  x = j;  y = i;  w1 = w;  h1 = h;  offx = offy = 0;
	  if (x<0)           { offx = -x;  w1 -= offx;  x = 0; }
	  if (x+w1>eWIDE) { w1 = (eWIDE-x); }

	  if (y<0)           { offy = -y;  h1 -= offy;  y = 0; }
	  if (y+h1>eHIGH)    { h1 = (eHIGH-y); }
	  
	  XPutImage(theDisp, tmpPix, theGC, theImage, offx, offy, 
		    x, y, w1, h1);
	}
      }
    }

    else if (rmode == RM_CSOLID) { }

    else if (rmode == RM_CWARP) {          /* warp effect */
      XSetForeground(theDisp, theGC, rootfg);
      for (i=0; i<=dispWIDE; i+=8) 
	XDrawLine(theDisp, tmpPix, theGC, i, 0, dispWIDE-i, dispHIGH);
      for (i=0; i<=dispHIGH; i+=8)
	XDrawLine(theDisp, tmpPix, theGC, 0, i, dispWIDE, dispHIGH-i);
    }

    else if (rmode == RM_CBRICK) {         /* brick effect */
      XSetForeground(theDisp, theGC, rootfg);
      for (i=k=0; i<dispHIGH; i+=20,k++) {
	XDrawLine(theDisp, tmpPix, theGC, 0, i, dispWIDE, i);
	for (j=(k&1) * 20 + 10; j<dispWIDE; j+=40) 
	  XDrawLine(theDisp, tmpPix, theGC, j,i,j,i+20);
      }
    }


    /* draw the image centered on top of the background */
    if (rmode != RM_CENTILE) 
      XPutImage(theDisp, tmpPix, theGC, theImage, 0,0, 
		(dispWIDE-eWIDE)/2, (dispHIGH-eHIGH)/2, eWIDE, eHIGH);
  }


  XSetWindowBackgroundPixmap(theDisp, mainW, tmpPix);
  XFreePixmap(theDisp, tmpPix);

  XClearWindow(theDisp, mainW);
}



/************************************************************************/
void ClearRoot()
{
  killRootPix();
  XClearWindow(theDisp, vrootW);
  XFlush(theDisp);
}


/************************************************************************/
static void killRootPix()
{
  Pixmap pix, bitmap;
  GC gc;
  XGCValues gc_init;

  if (vrootW == rootW) {
    XSetWindowBackgroundPixmap(theDisp, rootW, None);
  }

  else {
    bitmap = XCreateBitmapFromData(theDisp, vrootW, root_weave_bits,
				   root_weave_width, root_weave_height);

    gc_init.foreground = BlackPixel(theDisp, theScreen);
    gc_init.background = WhitePixel(theDisp, theScreen);
    gc = XCreateGC(theDisp, vrootW, GCForeground|GCBackground, &gc_init);
    pix = XCreatePixmap(theDisp, vrootW, root_weave_width, 
			root_weave_height, 
			(unsigned int) DefaultDepth(theDisp, theScreen));

    XCopyPlane(theDisp, bitmap, pix, gc, 0,0, root_weave_width,
	       root_weave_height, 0,0, (unsigned long) 1);
    XSetWindowBackgroundPixmap(theDisp, vrootW, pix);

    XFreeGC(theDisp, gc);
    XFreePixmap(theDisp, bitmap);
    XFreePixmap(theDisp, pix);
  }
}



/***********************************/
void SaveRootInfo()
{
  /* called when using root window.  stores the pixmap ID used to draw the
     root window in a property.  This will be used later to free all resources
     allocated by this instantiation of xv (ie, the alloc'd colors).  These
     resources are kept alloc'ed after client exits so that rainbow effect
     is avoided */

  Atom          prop;
  static Pixmap riPix = (Pixmap) NULL;

  if ( !(theVisual->class & 1)) return;  /* no colormap to worry about */
  if (riPix) return;                     /* it's already been saved once */

  riPix = XCreatePixmap(theDisp, vrootW, 1, 1, 1);
  if (!riPix) return;   /* unable to save.  thankfully, unlikely to happen */

  prop = XInternAtom(theDisp, "_XSETROOT_ID", False);
  if (!prop) FatalError("couldn't create _XSETROOT_ID atom");

  XChangeProperty(theDisp, vrootW, prop, XA_PIXMAP, 32, PropModeReplace,
		  (unsigned char *) &riPix, 1);

  XSetCloseDownMode(theDisp, RetainPermanent);
}


/***********************************/
void KillOldRootInfo()
{
  /* get the pixmap ID from the _XSETROOT_ID property, and kill it */

  Atom           prop, type;
  int            format;
  unsigned long  length, after;
  unsigned char *data;

  prop = XInternAtom(theDisp, "_XSETROOT_ID", True);
  if (prop == None) return;    /* no old pixmap to kill */

  if (XGetWindowProperty(theDisp, vrootW, prop, 0L, 1L, True, 
			 AnyPropertyType, &type, &format, &length, 
			 &after, &data) == Success) {

    if (type==XA_PIXMAP && format==32 && length==1 && after==0 && data) {
      XKillClient(theDisp, *((Pixmap *)data));
      XDeleteProperty(theDisp, vrootW, prop);
    }

    XFree((char *) data);
  }
}



    
