/***************************************************************************/
/*									   */
/* This magnificient piece of software is real PD, so you do not need to   */
/* send any money to me, but I would like to receive some good sourcecodes */
/* especially for OS2.0 [ amiga, of course ;-) ]			   */
/*									   */
/***************************************************************************/
/*									   */
/* This program is intended to open a productivity screen instead of	   */
/* an interlaced screen, to reduce flicker and to avoid  resyncronisation  */
/* of some multiscan monitors. It is only usefull for amigas equipped with */
/* an ECS Denise and a multiscan monitor. Using productivity with a normal */
/* non-multisync monitor may seriously damage it, and I am not responsible */
/* for any damage while using this program.				   */
/*									   */
/* Usage :								   */
/*	   Start it with 'DoPro'					   */
/*	   to quit and save database, start it a second time		   */
/*									   */
/*	Michael Illgner 						   */
/*	Theodorstr. 27  						   */
/*	W-4790 Paderborn						   */
/*	Germany 							   */
/*	Tel.: 05251/26488 or 05251/60-2331				   */
/*									   */
/*	email: fillg1@uni-paderborn.de  				   */
/*									   */
/***************************************************************************/
/*									   */
/*		D O P R O   V1.5					   */
/*									   */
/***************************************************************************/
/*									   */
/* BTW. This is my first attempt in writing OS2.0 software		   */
/*									   */
/***************************************************************************/

#ifdef DEBUG			/* don't forget to link with ddebug.lib !! */
void DPrintF(char *, ...);
void DPutStr(char *);
#endif

#define MyPortName "DoProd.port"

long	_stack  	= 4000;
char *  _procname	= "DoPro";
long	_priority	= 0;
long	_BackGroundIO	= 0;

extern struct IntuitionBase *IntuitionBase = NULL;
extern struct GfxBase	    *GfxBase	   = NULL;
extern struct GadToolsBase  *GadToolsBase  = NULL;

BOOL MayUseProductivity;


/* Disable SAS/C CTRL/C handling */
void chkabort(void) {}

/* dummy message to quit */
static struct Message MyMsg =
{
 {NULL, NULL, NT_MESSAGE, NULL, NULL},
 NULL,
 sizeof(struct Message)
};

/* needed to store old OpenScreen */
static struct Screen __regargs *(*OldOpenScreen)(struct NewScreen *);

/* a simple requester */
static void MyRequest(char *Text)
{
 struct EasyStruct MyES;

 MyES.es_StructSize = sizeof(struct EasyStruct);
 MyES.es_Flags = 0;
 MyES.es_Title = ProgTitle;
 MyES.es_TextFormat = Text;
 MyES.es_GadgetFormat = "Continue";

 EasyRequest(IntuitionBase->ActiveWindow, &MyES, NULL, NULL);
}

/* a boolean requester */
static BOOL BoolRequest(char *Text)
{
 struct EasyStruct BoolES;

 BoolES.es_StructSize = sizeof(struct EasyStruct);
 BoolES.es_Flags = 0;
 BoolES.es_Title = ProgTitle;
 BoolES.es_TextFormat = Text;
 BoolES.es_GadgetFormat = "Yes|No";

 return EasyRequest(IntuitionBase->ActiveWindow, &BoolES, NULL, NULL);
}

/* open the required libraries */
static void OpenSystem(void)
{
 /* if i can get intuition, i can get graphics too ! ;-) */
 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37);
 if (!(GadToolsBase = OpenLibrary("gadtools.library", 37)))
 {
  MyRequest("Can't open gadtools.library !");
  exit(10);
 }
}

/* close them */
static int CloseSystem(void)
{
 if (GfxBase)	    CloseLibrary(GfxBase);
 if (GadToolsBase)  CloseLibrary(GadToolsBase);

 return(0);
}

/* the new OpenScreen routine */
static struct Screen * __asm __saveds MyOpenScreen(register __a0 struct NewScreen *ns)
{
 ULONG DisplayID;
 
 #if DEBUG
 DPrintF("Screen : %s\n", ns->DefaultTitle);
 #endif
 
 if ((ns->Type & NS_EXTENDED) || (ns->CustomBitMap != NULL))
   return OldOpenScreen(ns);
   /* caller wants special screen ? she gets what she wants */
 else
 {
  DisplayID = CheckScreen(ns);
  return OpenScreenTags(ns,
			SA_DisplayID, DisplayID,
			TAG_DONE
		       );
 }
}



void main(void)
{
 struct MsgPort *MyPort;
 struct Message *Msg;

 onexit(CloseSystem);
 
 #if DEBUG
 DPrintF("DoPro Start\n");
 #endif

 /* you will need OS2.0 to use this program */
 if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37))
 {
  OpenSystem();

  /* may i use productivity ? */
  MayUseProductivity = !ModeNotAvailable(VGAPRODUCT_KEY);

  /* if this is the second start, send message to myself to quit & restore */
  if (MyPort = FindPort(MyPortName))
  {
   if (BoolRequest("Do you really want to quit DoPro ?")) PutMsg(MyPort, &MyMsg);
  }
  else
  {
   /* install MsgPort */
   if (MyPort = CreatePort(MyPortName, 0))
   {
    /* load database */
    LoadData();

    /* patch OpenScreen */
    OldOpenScreen = SetFunction(IntuitionBase, -0xC6, (void *)MyOpenScreen);

    /* wait for the end */
    WaitPort(MyPort);
    while (Msg = GetMsg(MyPort)) ReplyMsg(Msg);
    
    /* This's the end, my only friend -- the end */
    DeletePort(MyPort);

    /* remove patch */
    SetFunction(IntuitionBase, -0xC6, (void *)OldOpenScreen);
    MyRequest(ProgTitle" removed !");

    /* save database */
    SaveData();
   }
  }

  /* cleanup */
  CloseLibrary(IntuitionBase);
 }
}
