/* ScreenZap.c 
*  Clears away dead screens to regain chip ram and to clear the display.
*  Written by Lars Clausen april 1988, placed in the Public Domain	*/

#include <intuition/intuitionbase.h>

void *OpenWindow();
void *GetMsg();
void *OpenLibrary();

struct Window *w;
struct IntuitionBase *IntuitionBase;

struct NewWindow nw =
{
   50,50,
   295,50,
   0,1,
   CLOSEWINDOW | MOUSEBUTTONS,
   WINDOWCLOSE | ACTIVATE | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | NOCAREREFRESH,
   NULL,NULL,(UBYTE *)"Click In Window To Proceed",
   NULL, NULL,295,50,295,50,WBENCHSCREEN
};

OpenIt()
{
   IntuitionBase = OpenLibrary("intuition.library",0L);
   if (IntuitionBase == 0)
   {
      printf("Not enough mem to open Intuition. Get some.\n");
      exit(1);
   }
   w = OpenWindow(&nw);
   if (w == 0)
   {
      printf("No space for opening window. Clean up your memory.\n");
      exit(2);
   }
}

Closer()
{
   struct Screen *Curs,*Nexts;
   struct Window *Curw,*Nextw;
   int x,y;

   Curs = w->WScreen;
   Curs = Curs->NextScreen;
   printf("FIRE!\n");
   for (x=0; Curs!=NULL; x++)
   {
      if (Curs->FirstWindow!=NULL)
      {
         for (Curw=Curs->FirstWindow;Curw->NextWindow!=NULL;Curw=Curs->FirstWindow)
         {
            for (;Curw->NextWindow!=NULL; Curw=Curw->NextWindow);
            CloseWindow(Curw);
            printf("Bang!  ");
         }
         CloseWindow(Curw);
         printf("Bang!  All windows in screen %d dead.\n",x);
      }
      Nexts = Curs->NextScreen;
      CloseScreen(Curs);
      printf("BUMMM...\n");
      Curs = Nexts;
   }
   return(x);
}

main(argc,argv)
int argc; char **argv;
{
   struct IntuiMessage *msg;

   OpenIt();
   WaitPort(w->UserPort);
   msg = GetMsg(w->UserPort);
   if (msg->Class == MOUSEBUTTONS)
   {
      printf("Reporting: %d screens killed\n",Closer());
	  if (argc == 0) /* WB startup, let the user read the kill # */ {
	     printf("Press return to exit.\n");
	     gets();
	  }
   }
   CloseWindow(w);
   CloseLibrary(IntuitionBase);
}
