/*******************************************************************
*
* $VER: UseWB.c 1.1 (8.6.97) Tak Tang (tst92@ecs.soton.ac.uk)
*
* The UseWB() module handles the case of being run from workbench.
* Currently, it just calls PlaySample() for each arg passed.
*
* This file is in the Public Domain
*
********************************************************************
*
*/

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

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

#include <dos/dos.h>
#include <workbench/icon.h>
#include <workbench/startup.h>

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

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

#include "xPlay.h"
#include "PlaySample.h"
#include "UseWB.h"


/****** UseWB() ****************************************************
*
*   NAME
*       UseWB() -- Handles case of being run by workbench
*
*   SYNOPSIS
*       error UseWB( GlobalData , WBStartMessage )
*
*       ULONG UseWB( struct GlobalData *, struct WBStartup *);
*
*   FUNCTION
*       Calls PlaySample() for each file passed by workbench, checking
*       each icon for the following tooltypes :
*         VOLUME = number
*         FILTER =ON | OFF
*         FREQUENCY = number
*         PERIOD = number
*         MONO
*         DOUBLE
*
*   INPUTS
*       GlobalData     - pointer to a structure containing global
*                        data, such as library bases
*       WBStartMessage - pointer to start up message sent by workbench
*
*   RESULT
*       RETURN_OK - if all files played OK
*       (others)
*
*   EXAMPLE
*
*   NOTES
*
*   FUTURE
*       Open an ASL file requester if no args.
*
*   BUGS
*       Doesnt filter out devices, drawers, or appicons
*
*   SEE ALSO
*
********************************************************************
*
* 1.1 (9.6.97) tst
*     Added simple error reporting via EasyRequest
*     Added examination of an arg's icon for tooltypes
*
* 1.0 (4.6.97) tst
*     First version hacked from prargs.c
*
*/

ULONG UseWB(struct GlobalData *gd, struct WBStartup *wbs)
{
  struct Library *IconBase;
  struct Library *IntuitionBase;
  ULONG ReturnCode=RETURN_FAIL;
  ULONG i;
  struct WBArg *wba;
  BPTR oldlock;
  struct EasyStruct ez = { sizeof(struct EasyStruct), 0, "xPlay 1.1", "%s: %s","OK|ABORT"};
  struct DiskObject *dob;
  STRPTR tmp;
  UBYTE buffer[81];
  ULONG o_period;
  ULONG o_filter;
  ULONG o_volume;
  ULONG o_double;
  ULONG o_mono;

  if (IntuitionBase = OpenLibrary ("intuition.library", 36L))
  {
    if (IconBase = OpenLibrary (ICONNAME, 36L))
    {
      if ( wbs->sm_NumArgs>1 )
      {
        /* for each arg passed by workbench ... */
        wba=( wbs->sm_ArgList)+1;
        for ( i= wbs->sm_NumArgs-1; i && (FALSE==gd->UserStop); i--, wba++)
        {
          if (NULL!=wba->wa_Lock)
          {
            oldlock=CurrentDir( wba->wa_Lock );

            /* Examine Samples's icon for properties */
            if (dob = GetDiskObjectNew (wba->wa_Name))
            {
              o_filter=0;
              if (tmp = FindToolType (dob->do_ToolTypes, "FILTER"))
              {
                if (TRUE==MatchToolValue(tmp,"ON")) o_filter=1;
                if (TRUE==MatchToolValue(tmp,"OFF")) o_filter=2;
              } /* FILTER */

              o_volume=0;
              if (tmp = FindToolType (dob->do_ToolTypes, "VOLUME"))
              {
                if (StrToLong (tmp, (LONG *)&o_volume) > 0L)
                {
                  if (o_volume>64) o_volume=64;
                }
                else
                {
                  o_volume=0L;
                }
              } /* VOLUME */

              o_period=0;
              if (tmp = FindToolType (dob->do_ToolTypes, "PERIOD"))
              {
                if (StrToLong (tmp, (LONG *)&o_period) < 0L)
                {
                  o_period=0;
                }
              } /* PERIOD */

              if (tmp = FindToolType (dob->do_ToolTypes, "FREQUENCY"))
              {
                if (StrToLong (tmp, (LONG *)&o_period) > 0L)
                {
                  o_period =(*(struct ExecBase**)(4))->ex_EClockFrequency*5/o_period;

                }
              } /* FREQUENCY */

              o_mono=0;
              if (tmp = FindToolType (dob->do_ToolTypes, "MONO"))
              {
                o_mono=1;
              } /* MONO */

              o_double=0;
              if (tmp = FindToolType (dob->do_ToolTypes, "DOUBLE"))
              {
                o_double=1;
              } /* DOUBLE */

              FreeDiskObject (dob);
            } /* if dob=GetDiskObjectNew() */


            ReturnCode=PlaySample(
              gd, wba->wa_Name, o_period, o_volume, 0, o_double, o_mono,
              1, (1==o_filter)?1:0, (2==o_filter)?1:0, 0);

            if (ReturnCode && (ReturnCode!=ERROR_BREAK) )
            {
              if ( Fault( ReturnCode, NULL, buffer, 81) )
              {
                if ( 0==EasyRequest(NULL, &ez, NULL, wba->wa_Name, buffer) )
                {
                  gd->UserStop=TRUE;
                } /* if 0==choice */
              } /* if fault() */
            } /* if error */
            CurrentDir(oldlock);
          } /* NULL != if wba_wa_Lock */
        } /* for */
      } /* if wbs->Num4rgs */

      CloseLibrary(IconBase);
    } /* if OpenLibrary(IconBase) */
    CloseLibrary(IntuitionBase);
  } /* if OpenLibrary(Intuition) */


  return ReturnCode;
} /* UseWB */

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

