/************************************************************
  fragit.c  By: Stephen Vermeulen

  This program is a demonstration of how one can fragment
  (usually temporarly) CHIP ram.

  To get this to work from CLI you must do the following:

    1 - Open a pair of CLI windows, make one cover the whole
        display and the other cover at least a half of the
        display.

    2 - run Avail or memlist to see what the largest free
        CHIP memory chunk in the chip section is.

    3 - run Fragit, a small window will appear briefly in the
        middle of the workbench, the workbench screen will
        then be covered by a big screen, the little window
        will be closed, then the big screen will be closed.

    4 - run Avail or memlist again and note that while the
        total amount of free CHIP memory remains the same, the
        distribution is different, the one huge chunk has got
        split up!  Nasty!

    5 - move the smaller cli window slightly, or resize it a bit
        and then run Avail or memlist again to see that the
        CHIP chunks change size and the big chunk returns!

    6 - Note that if you try this process when the little
        window that the program opens only overlaps ONE window
        on the workbench nothing untoward happens.  It seems to
        be a problem when windows get stacked THREE deep somewhere.

  compile with Manx 3.4a
************************************************************/

#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <exec/memory.h>
#include <functions.h>

struct IntuitionBase *IntuitionBase;

/***************************************
  This specifies the custom screen that
  our program will open briefly
****************************************/

struct NewScreen my_s =
{
  0, 0, 640, 400, 4,
  0, 1, LACE | HIRES, CUSTOMSCREEN,
  NULL,                    /* force the font to topaz 80 */
  NULL,                       /* screen title */
  NULL,                       /* still no screen gadgets in Amiga */
  NULL                        /* let Intuition give us bitmap */
};

/****************************************
  Temp window is the user friendly prompt
  that causes the memory fragmentation.
*****************************************/

struct NewWindow temp_window =
{
  200, 100, 240, 14,
  -1, -1,
                        /* these are the message events we want to use */
  NULL,

  SMART_REFRESH | NOCAREREFRESH,
  NULL,               /* this is where gadgets go */
  NULL,                 /* default checkmark */
  NULL, /* not title */
  NULL,                 /* on the workbench */
  NULL,
  0, 0, -1, -1,         /* don't care as no sizing is allowed */
  WBENCHSCREEN
};

/************************************************************
  The main routine just opens the various libraries, allocates
  memory, opens the main window.  Then after a 2 second pause
  closes it all.
*************************************************************/

void main(argc, argv)
short argc;
char *argv[];
{
  struct Screen *s;
  struct Window *tw;

  IntuitionBase = (struct IntuitionBase *)
                  OpenLibrary("intuition.library", 0L);
  if (IntuitionBase)
  {
    tw = OpenWindow(&temp_window);
    Delay(60L);
    s = (struct Screen *) OpenScreen(&my_s);
    if (s)
    {
      Delay(60L);
      if (tw) CloseWindow(tw);
      tw = NULL;
      CloseScreen(s);
    }
    if (tw) CloseWindow(tw);
  }
  CloseLibrary(IntuitionBase);
}
