/*------------------------- AvailMem V1.12 ----------------------------*/
/*Written and copyright ©1988-1991 by Dave Schreiber. All rights       */
/*reserved.  This program may not be sold, but reasonable charges for  */
/*media, duplication, shipping/handling, etc. are permitted.	       */
/*---------------------------------------------------------------------*/
/*Keeps a running count of the amount of FAST, CHIP, and total memory  */
/*free.  Updates every .7 seconds. Compiled with SAS/C V5.10.	       */
/*To compile:							       */
/*     1> lc -v -Lcd AvailMem					       */
/*----------------------------- Enjoy ---------------------------------*/
/*Version history:						       */
/* 1.12 - Fixed a problem with non-8pt default fonts under 2.0	       */
/* 1.10 - Added a counter of the largest block free for FAST, CHIP, and*/
/*	  total memory						       */
/* 1.03 - First release to the public.	Included on Fred Fish disk #285*/
/*---------------------------------------------------------------------*/


/*Standard #include files*/
#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>

/*Window and IntuiText definitions*/
#include "AvailMem.h"

struct IntuitionBase *IntuitionBase;   /*Library base pointers*/
struct GfxBase *GfxBase;

void MainLoop();
void MakeString();
void InitIntuiText(UWORD add,UWORD addX);

#define Rp Wdw->RPort

	 /*Using _main() keeps AmigaDOS from opening an extra */
_main()  /*console window when AvailMem is run from Workbench.*/
{
   if((IntuitionBase=(struct IntuitionBase *)   /*Open Intuition*/
	 OpenLibrary("intuition.library",0))==NULL)
      exit(10);
			      /*Open Graphics*/
   if((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
   {
      CloseLibrary(IntuitionBase); /*Like it really matters...*/
      exit(20);
   }

   MainLoop();

   CloseLibrary(GfxBase);     /*Close the libraries*/
   CloseLibrary(IntuitionBase);
   return(0);
}

void MainLoop()   /*The main loop*/
{
   struct Window *Wdw;	/*Window pointer*/
   struct Screen *WBScreen;
   ULONG c,f;
   UWORD add1,add2,addX;

   WBScreen=IntuitionBase->ActiveScreen;

   /*Get font size information*/
   add1 = WBScreen->Font->ta_YSize-8;
   add2 = GfxBase->DefaultFont->tf_YSize-8;
   addX = GfxBase->DefaultFont->tf_XSize-8;

   /*Change window dimensions to fit default fonts*/
   NewWindowStructure1.Height+=add1+5*add2;
   NewWindowStructure1.Width=30*(8+addX);

   InitIntuiText(add2,addX);  /*Initialize the IntuiText structures*/

			      /*Open the window*/
   if((Wdw=(struct Window *)OpenWindow(&NewWindowStructure1))==NULL)
      exit(30);

   Delay(10);

   PrintIText(Rp,&IText1,-6,2+add1);

   /*while the close gadget wasn't pressed...*/
   while( ( GetMsg(Wdw->UserPort) ) == NULL)
      {  /*For each possibility, get the memory count, convert it to text,
	 /*put it into the appropriate intuitext structure,*/

      c=AvailMem(MEMF_LARGEST|MEMF_CHIP);
      f=AvailMem(MEMF_LARGEST|MEMF_FAST);
      MakeString(IText12.IText,(c < f) ? f : c);
      MakeString(IText11.IText,c);
      MakeString(IText10.IText,f);

      c=AvailMem(MEMF_CHIP);
      f=AvailMem(MEMF_FAST);
      MakeString(IText9.IText,f);
      MakeString(IText8.IText,c);
      MakeString(IText7.IText,f+c);

      PrintIText(Rp,&IText7,-6,2+add1); /*and print it*/
      Delay(35);    /*Wait for .7 seconds, then do the whole thing again*/
      }
   CloseWindow(Wdw);    /*Close the window*/
}

void MakeString(string,amt) /*Convert amt to a string, put into 'string'*/
char *string;		    /*and pad out with spaces*/
ULONG amt;
{
   BYTE c;
   stcu_d(string,amt,8);   /*Convert number into text*/
   for(c=strlen(string);c<8;c++)
      string[c]=' ';       /*Pad out with spaces*/

   string[8]=NULL;	   /*And terminate with a NULL*/
}


/*Initializes the IntuiText structures (spaces them according to the */
/*default fonts).*/
void InitIntuiText(UWORD add,UWORD addX)
{
   IText5.TopEdge=(IText6.TopEdge+=add);
   IText1.TopEdge=IText9.TopEdge=(IText10.TopEdge+=(add*2));
   IText2.TopEdge=IText8.TopEdge=(IText11.TopEdge+=(add*3));
   IText3.TopEdge=IText7.TopEdge=(IText12.TopEdge+=(add*4));

   IText5.LeftEdge=IText9.LeftEdge=IText8.LeftEdge=
	 (IText7.LeftEdge+=addX*7);
   IText6.LeftEdge=IText10.LeftEdge=IText11.LeftEdge=
	 (IText12.LeftEdge+=addX*17);
}

