#include "sysheaders.h"
#include "cdpanel.h"
#include "CompactPlayer.h"

#ifndef AREXX_DefExtension /* typo in headers */
#define AREXX_DefExtension AREXX_DefExtention
#endif

/*************************************************
 * ARexx interface
 */
 
static void __asm __saveds
ARexxPlay(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_Play( cmd->ac_ArgList[0] ? *(ULONG *)cmd->ac_ArgList[0] : 1 );
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxStop(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_Stop();

	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxPause(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_PauseResume(0x00);
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxResume(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_PauseResume(0x01);
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxEject(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_Eject(0x01);
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxLoad(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	CD_Eject(0x00);
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxQuit(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	done = TRUE;
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxActivate(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	UnIconify();
	
	cmd->ac_RC = 0;
}

static void __asm __saveds
ARexxDeactivate(register __a0 struct ARexxCmd *cmd, register __a1 struct RexxMsg *msg)
{
	Iconify();
	
	cmd->ac_RC = 0;
}

struct ARexxCmd arexxCommands[] =
{
	"PLAY", 1, ARexxPlay, "TRACK/N", 0, NULL, 0, 0, NULL,
	"STOP", 2, ARexxStop, NULL, 0, NULL, 0, 0, NULL,
	"PAUSE", 3, ARexxPause, NULL, 0, NULL, 0, 0, NULL,
	"RESUME", 4, ARexxResume, NULL, 0, NULL, 0, 0, NULL,
	"EJECT", 5, ARexxEject, NULL, 0, NULL, 0, 0, NULL,
	"LOAD", 6, ARexxLoad, NULL, 0, NULL, 0, 0, NULL,
	"QUIT", 7, ARexxQuit, NULL, 0, NULL, 0, 0, NULL,
	"ACTIVATE", 8, ARexxActivate, NULL, 0, NULL, 0, 0, NULL,
	"DEACTIVATE", 9, ARexxDeactivate, NULL, 0, NULL, 0, 0, NULL,
	NULL
};

Object *ARexxObj;

STRPTR
InitARexx( void )
{
	ULONG error;
	
	if (FindPort("COMPACTPLAYER")) 
	{
		LONG rc, rc2;
		STRPTR res;
		
		/* create a temporary ARexx object so that we can use it to signal the
		 * running CompactPlayer that it should wake up. */

		ARexxObj = ARexxObject, 
			AREXX_HostName, "COMPACTPLAYER",
			AREXX_ErrorCode, &error,
			End;

		DoMethod( ARexxObj, AM_EXECUTE,
			"'ACTIVATE'", "COMPACTPLAYER",
	 		&rc, &rc2, &res, NULL );
	 		
	 	kprintf("%lx\n", ARexxObj);
		 		
		CloseARexx();
		
		return (APTR)~0; 	
	}
	else
	{
		ARexxObj = ARexxObject, 
			AREXX_HostName, "COMPACTPLAYER",
			AREXX_DefExtension, "cp",
			AREXX_Commands, arexxCommands,
			AREXX_ErrorCode, &error,
			AREXX_NoSlot, TRUE,	/* this port will not have a number appended to it */
			End;
			
		if (ARexxObj)
		{
			STRPTR hostname;
			
			GetAttr( AREXX_HostName, ARexxObj, (ULONG *)&hostname );
	
			return hostname;
		}
	}

	return NULL;
}

void
CloseARexx( void )
{
	DisposeObject( ARexxObj );
	ARexxObj = NULL;
}
