/*------------------------- AvailMem V1.03 ----------------------------*/
/*Written by and copyright ©1988, 1989 David 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 .5 seconds.  With a stack size of 4K, it takes  */
/*up about 12K of RAM.  Compiled under Lattice C V5.00                 */
/*----------------------------- Enjoy ---------------------------------*/

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

/*PowerWindows V2.0 window definition*/
struct NewWindow NewWindowStructure1 = {
   383,17,   /* window XY origin relative to TopLeft of screen */
   210,50,   /* window width and height */
   0,1,   /* detail and block pens */
   CLOSEWINDOW,   /* IDCMP flags */
   WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE,   /* other window flags */
   NULL,   /* first gadget in gadget list */
   NULL,   /* custom CHECKMARK imagery */
   "AvailMem V1.03",   /* window title */
   NULL,   /* custom screen pointer */
   NULL,   /* custom bitmap */
   5,5,   /* minimum width and height */
   640,200,   /* maximum width and height */
   WBENCHSCREEN   /* destination screen type */
};

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

#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)
      exit(20);

   MainLoop();

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

MainLoop()   /*The main loop*/
{
   unsigned int Mem[3]; /*Holds levels of free memory (CHIP,FAST, & total)*/
   char MemString[3][20];  /*Holds string conversions of above*/
   unsigned char c;     /*Counter variable*/
   struct Window *Wdw;  /*Window pointer*/

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

   SetAPen(Rp,1);       /*Write initial text into window*/
   Move(Rp,15,18);
   Text(Rp,"Available memory:",17);
   Move(Rp,15,27);
   Text(Rp,"Fast:",5);
   Move(Rp,15,36);
   Text(Rp,"Chip:",5);
   Move(Rp,15,45);
   Text(Rp,"Total:",6);

   /*while the close gadget wasn't pressed...*/
   while( ( GetMsg(Wdw->UserPort) ) == NULL)
      {
      /*Mem[0] == FAST RAM free*/
      /*Mem[1] == CHIP RAM free*/
      /*Mem[2] == Total RAM free*/
      Mem[2]=(Mem[1]=AvailMem(MEMF_CHIP))+(Mem[0]=AvailMem(MEMF_FAST));

               /*Clear out strings*/
      MemString[0][0]=MemString[1][0]=MemString[2][0]=NULL;
      for(c=0;c<=2;c++)
         {
         stcu_d(MemString[c],Mem[c],10); /*Convert numbers into strings*/
         strcat(MemString[c]," bytes "); /*Tack " bytes " onto string's end*/
         Move(Rp,86,27+(9*c));   /*Position the cursor*/
         Text(Rp,MemString[c],strlen(MemString[c])); /*and print the text*/
         }
      Delay(25);    /*Wait for .5 seconds, then do the whole thing again*/
      }
   CloseWindow(Wdw);    /*Close the window*/
}

