/*******************************************************************
*
* $VER: xPlay.c 1.1 (8.6.97) Tak Tang (tst92@ecs.soton.ac.uk)
*
* Most of this code came from xPlay 1.0 by Christian Buchner
*
* This file is in the Public Domain
*
* Future ideas :
*     IFFONLY/S Toggle (defaults to OFF)
*     Stereo replay (open same file twice)
*     Ability to play Fibonacci-delta-encoded IFF-Samples
*     Ability to play Exponential-delta-encoded IFF-Samples
*     Workbench AppIcon for Playing Samples
*     Commodity with embedded UPD-Emulation
*
********************************************************************
*
*/


/**** Header files ****/

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/alerts.h>
#include <exec/execbase.h>

#include <dos/dos.h>
#include <libraries/xpk.h>
#include <workbench/startup.h>

#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>

#include <string.h>

#include "xPlay.h"
#include "UseCli.h"
#include "UseWB.h"


/**** Static data ****/

STRPTR VersTag = "$VER: " PROGNAME " " PROGVER " (" PROGDATE ")";


/****** main() *****************************************************
*
*   NAME
*       __main() -- main entry point to xPlay
*
*   SYNOPSIS
*       error = __main( )
*
*       LONG = __main( void );
*
*   FUNCTION
*       This is the main entry point for xPlay.  It gets and replies
*       to the workbench message, and opens tthe dos library and
*       xpk library.
*
*   INPUTS
*
*   RESULT
*       error - dos return code
*
*   EXAMPLE
*
*   NOTES
*       Compile this _without_ any start up module such as c.o
*
*   BUGS
*
*   SEE ALSO
*
********************************************************************
*
* 1.1 (9.6.97) tst
*     Added workbench support
*
* 1.0 (?.?.93) cb
*     First version
*
*/

#undef DOSBase
#undef XpkBase

LONG __saveds main(void)
{
  struct Process *MyProc;
  struct WBStartup *wbMsg = NULL;
  ULONG ReturnCode=RETURN_FAIL;
  struct GlobalData *gd;

  MyProc=(struct Process *)FindTask(NULL);
  if ( NULL == MyProc->pr_CLI )
  {
    (void)WaitPort(&MyProc->pr_MsgPort);
    wbMsg = (struct WBStartup *) GetMsg (&MyProc->pr_MsgPort);
  } /* if run from WB */

  gd = AllocVec (sizeof (struct GlobalData), MEMF_CLEAR);
  if (gd)
  {

    if (gd->DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L))
    {
      if (gd->XpkBase=OpenLibrary(XPKNAME,2L))
      {
        if (wbMsg)
        {
          ReturnCode=UseWB(gd,wbMsg);
        }
        else
        {
          ReturnCode=UseCli(gd);
        }
        CloseLibrary(gd->XpkBase);
      } /* if OpenLibrary(XPK) OK */
      else
      {
#define DOSBase gd->DOSBase
        Printf("xPlay: %s V2 or newer required !\n",XPKNAME);
#undef DOSBase
      }
      CloseLibrary((struct Library *)gd->DOSBase);
    } /* if OpenLibrary(DOS) OK */
    else
    {
      Alert (AT_Recovery|AG_OpenLib|AO_DOSLib);
      ReturnCode=RETURN_FAIL;
    } /* if OpenLibrary(DOS) FAIL */
    FreeVec(gd);
  } /* if gd=AllocVec() OK */

  if (wbMsg)
  {
    Forbid ();
    ReplyMsg ((struct Message *) wbMsg);
  } /* if run from WB */

  return(ReturnCode);
} /* __main() */

/**** End of file ****/

