/* ******************************************************************** */
/* C code generated by:							*/
/* Visual Arts Version 2.1						*/
/* Copyright 1994-95 Danny Y. Wong  All rights reserved			*/
/* Calgary, Alberta (CANADA)                        			*/
/* Partial of the code is copyright by Jaba Development			*/
/* ******************************************************************** */

#include <VisualArts.h>
#include <clib/VisualArts_protos.h>


#include "SerialConsole_func.c" 


int GetPubScreen(void);
void ClosePubScreen(void);
int OpenSerConWindow(char windtitle[]);
void CloseSerConWindow(void);
int InitSerConConsole(void);
extern int SerConConsoleHandler(void);
int InitSerConSerial(void);
extern int SerConSerialHandler(void);
int SerConHandler(void);
int SerConMainHandler(void);
void DrawSerConObjs(void);
int main(int argc, char *argv[]);

UBYTE				*PubScrName = "Workbench";
struct DrawInfo			*ScrDrawInfo = NULL;
APTR				 VisualInfo = NULL;
struct Screen			*Scr = NULL;
struct Window			*SerConWnd = NULL;
struct Window			*SerConConWnd = NULL;
struct MsgPort			*SerConConWritePort = NULL;
struct MsgPort			*SerConConReadPort = NULL;
struct IOStdReq			*SerConConWriteReq = NULL;
struct IOStdReq			*SerConConReadReq = NULL;
struct MsgPort			*SerConSerWritePort = NULL;
struct MsgPort			*SerConSerReadPort = NULL;
struct IOExtSer			*SerConSerWriteReq = NULL;
struct IOExtSer			*SerConSerReadReq = NULL;
struct IntuiMessage		 SerConMsg;
UWORD				 SerConLeft = 43;
UWORD				 SerConTop = 16;
UWORD				 SerConWidth = 525;
UWORD				 SerConHeight = 79;
char				 character;
struct TextAttr topaz8 = { (STRPTR)"topaz.font", 8, 0x00, 0x01 };
struct TextAttr topaz800 = { (STRPTR)"topaz.font", 8, 0x00, 0x00 };

struct IntuiText SerConIText[] = {
  1, 0, JAM2, 9, 1, &topaz800, (UBYTE *)"Instructions:", &SerConIText[1],
  1, 0, JAM2, 19, 13, &topaz800, (UBYTE *)"1. Activate the Console Window", &SerConIText[2],
  1, 0, JAM2, 19, 23, &topaz800, (UBYTE *)"2. Type in the command 'at' and press return", &SerConIText[3],
  1, 0, JAM2, 19, 33, &topaz800, (UBYTE *)"3. You should get a 'OK' as a reply", &SerConIText[4],
  1, 0, JAM2, 19, 43, &topaz800, (UBYTE *)"4. Now enter a BBS number you want to call and press return", &SerConIText[5],
  1, 0, JAM2, 19, 53, &topaz800, (UBYTE *)"5. An example 1-403-2745545 in Calgary, Canada", NULL,
};


int GetPubScreen(void)
{
  if (!(Scr = LockPubScreen(PubScrName)))
	return(1L);

  if (!(VisualInfo = GetVisualInfo(Scr, TAG_DONE)))
	return(2L);

  if (!(ScrDrawInfo = GetScreenDrawInfo(Scr)))
	return(3L);
  return(0L);
}

void ClosePubScreen(void)
{
  if (VisualInfo)
    FreeVisualInfo(VisualInfo);
  if (Scr)
    UnlockPubScreen(NULL,  Scr);
  if (ScrDrawInfo)
    FreeScreenDrawInfo(Scr,  ScrDrawInfo);
}

int InitSerConConsole(void)
{
  SerConConWnd = OpenWindowTags( NULL,
	WA_Left,		0,
	WA_Top,			100,
	WA_Width,		640,
	WA_Height,		100,
	WA_Title,		"Console Window",
	WA_IDCMP,		NULL,
	WA_Flags,		WFLG_DRAGBAR | WFLG_SIMPLE_REFRESH |
 				 WFLG_DEPTHGADGET | WFLG_SIZEGADGET | WFLG_SIZEBRIGHT,
	WA_CustomScreen,	Scr,
  	WA_MaxWidth,		640,
  	WA_MaxHeight,		200,
	TAG_DONE );

  if ( SerConConWnd == NULL )
	return(6L);

  SerConConWritePort = CreatePort("SerCon.con.write", 0);
  if ( !SerConConWritePort)
	return(1L);
  SerConConWriteReq = CreateStdIO(SerConConWritePort);
  if ( !SerConConWriteReq)
	return(2L);
  SerConConReadPort = CreatePort("SerCon.con.read", 0);
  if ( !SerConConReadPort)
	return(3L);
  SerConConReadReq =  CreateStdIO(SerConConReadPort);
  if ( !SerConConReadReq)
	return(4L);
  if ((OpenConsole(SerConConWriteReq, SerConConReadReq, SerConConWnd)) != 0)
	return(5L);

  return(0L);
}


int InitSerConSerial(void)
{
  SerConSerWritePort = CreatePort("SerCon.ser.write", 0);
  if ( !SerConSerWritePort)
	return(1L);
  SerConSerWriteReq = (struct IOExtSer *)CreateExtIO(SerConSerWritePort, sizeof(struct IOExtSer));
  if ( !SerConSerWriteReq)
	return(2L);
  SerConSerReadPort = CreatePort("SerCon.ser.read", 0);
  if ( !SerConSerReadPort)
	return(3L);
  SerConSerReadReq =  (struct IOExtSer *)CreateExtIO(SerConSerReadPort, sizeof(struct IOExtSer));
  if ( !SerConSerReadReq)
	return(4L);
  if ((OpenSerial(SerConSerReadReq, SerConSerWriteReq)) != 0)
	return(5L);
  if ((SetParams( SerConSerReadReq, 4096, 8, 8, 750000L,
 	19200,  SERF_XDISABLED | SERF_SHARED , 48484848, 48484848)) != 0)
	return(6L);
  return(0L);
}


int OpenSerConWindow(char windtitle[80])
{
  if (!(SerConWnd = OpenWindowTags(NULL,
  	WA_Left,	SerConLeft,
  	WA_Top, 	SerConTop,
  	WA_Width,	SerConWidth,
  	WA_Height,	SerConHeight + kWindowOffSetY,
  	WA_NewLookMenus, TRUE,
  	WA_IDCMP,	IDCMP_CLOSEWINDOW | IDCMP_GADGETUP ,
  	WA_Flags,	WFLG_CLOSEGADGET  |  WFLG_SMART_REFRESH | 
			 WFLG_RMBTRAP |  WFLG_ACTIVATE | 
			 WFLG_DRAGBAR ,
  	WA_Gadgets,	NULL,
  	WA_Title,	windtitle,
  	WA_ScreenTitle,	"Visual Arts V2.2 Copyright 1994-95 Danny Y. Wong  All Rights Reserved.",
  	WA_PubScreen,	Scr,
  	WA_MinWidth,	160,
  	WA_MinHeight,	50,
  	WA_MaxWidth,	640,
  	WA_MaxHeight,	200,
  	TAG_DONE)))
  	    return(3L);

  DrawSerConObjs();
  GT_RefreshWindow(SerConWnd, NULL);
  if (InitSerConConsole())
	return(4L);
  if (InitSerConSerial())
	return(5L);
  return(0L);
}

void CloseSerConWindow(void)
{
  if (SerConConWnd)
	CloseWindow(SerConConWnd);
  if (SerConConWriteReq)
	CloseDevice((struct IORequest *)SerConConWriteReq);
  if (SerConConReadReq)
	DeleteStdIO(SerConConReadReq);
  if (SerConConReadPort)
	DeletePort(SerConConReadPort);
  if (SerConConWriteReq)
	DeleteStdIO(SerConConWriteReq);
  if (SerConConWriteReq)
	DeletePort(SerConConWritePort);
  AbortIO((struct IORequest *)SerConSerReadReq);
  WaitIO((struct IORequest *)SerConSerReadReq);
  if (SerConSerWriteReq)
	CloseDevice((struct IORequest *)SerConSerWriteReq);
  if (SerConSerReadReq)
	DeleteExtIO( (struct IORequest *)SerConSerReadReq);
  if (SerConSerReadPort)
	DeletePort(SerConSerReadPort);
  if (SerConSerWriteReq)
	DeleteExtIO( (struct IORequest *)SerConSerWriteReq);
  if (SerConSerWriteReq)
	DeletePort(SerConSerWritePort);
  if (SerConWnd)
	CloseWindow(SerConWnd);
}

int SerConHandler(void)
{
  struct IntuiMessage	*msg;
  struct VAobject	VAObject;
  int running	= 1;
  int (*func)(struct VAobject VAObject);
  ULONG class;
  UWORD code;

  while (msg=GT_GetIMsg(SerConWnd->UserPort))
  {
    CopyMem((char *)msg, (char *)&SerConMsg, (long)sizeof(struct IntuiMessage));
    class = msg->Class;
    code  = msg->Code;

    GT_ReplyIMsg(msg);
    switch(class)
    {

	  case IDCMP_CLOSEWINDOW:
	     return(0);
	     break;

	  case IDCMP_GADGETUP:
	    func = (void *)((struct Gadget *)SerConMsg.IAddress)->UserData;
	    if (func != NULL)
	      running =  func(VAObject);
	    break;

    }
  }
  return(running);
}

void DrawSerConObjs(void)
{
  PrintIText(SerConWnd->RPort, SerConIText, SerConWnd->BorderLeft, SerConWnd->BorderTop);

}

int SerConConsoleHandler(void)
{
  while( CheckIO((struct IORequest *)SerConConReadReq))
  {
	WaitIO( (struct IORequest *)SerConConReadReq);
	SerPutChar(SerConSerWriteReq, character);	/* write to serial */
	QueueRead(SerConConReadReq, &character);
  }
  return(1);
}


int SerConSerialHandler(void)
{
  while( CheckIO((struct IORequest *)SerConSerReadReq))
  {
	WaitIO( (struct IORequest *)SerConSerReadReq);
	ConPutChar(SerConConWriteReq, character);	/* write to console */
	QueueSerRead(SerConSerReadReq, &character);
  }
  return(1);
}


int SerConMainHandler(void)
{
  int running = 1;
  ULONG windsig, signals;

  ULONG consolesig;
  ULONG serialsig;
  QueueRead(SerConConReadReq, &character);
  consolesig = 1L << SerConConReadReq->io_Message.mn_ReplyPort->mp_SigBit;
  QueueSerRead(SerConSerReadReq, &character);
  serialsig = 1L << SerConSerReadReq->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  windsig = 1L << SerConWnd->UserPort->mp_SigBit;

  while (running == 1)
  {
    signals = Wait( windsig | consolesig | serialsig );
    if (signals & windsig)
    {
	  running = SerConHandler();
    }
    else
    if (signals & consolesig)
	  running = SerConConsoleHandler();
    else
    if (signals & serialsig)
	  running = SerConSerialHandler();
  }
  return(running);
}

int main(int argc, char *argv[])
{
  int rc;

  if (!(GetPubScreen()))
  {
	if (!(OpenSerConWindow("Serial and Console Demo - Unmodified Codes")))
	{
	  rc = SerConMainHandler();
	  CloseSerConWindow();
	}
	ClosePubScreen();
  }
  return(0L);
}
