/*
** Zapster plugin interface
** ------------------------
**
** © 2000 by David Gerber <zapek@vapor.com>
** All rights reserved
**
** $Id$
**
*/

#define BUILDPLUGIN

#include "v_plugin.h"

#include <exec/memory.h>
#include <dos.h>
#include <dos/dostags.h>
#include <dos/dosextens.h>
#include <libraries/dos.h>

/* MUI */
#include <graphics/gfxmacros.h>
#include <libraries/mui.h>
#include <libraries/gadtools.h>
#include <intuition/intuition.h>

/* Protos */
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/utility.h>
#include <proto/intuition.h>
#include <proto/muimaster.h>

#include "zapster_mcc.h"


/*
 * Some colormaps, bitmaps and bitplanes. We put them to chipram because
 * I'm lazy but we should really use AllocBitMap() + remapping but that's
 * not so trivial
 */

static ULONG flash_imageCMap32[]={
   0x95959595,0x95959595,0x95959595,
   0x00000000,0x00000000,0x00000000,
   0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
   0x3B3B3B3B,0x67676767,0xA2A2A2A2,
   0x7B7B7B7B,0x7B7B7B7B,0x7B7B7B7B,
   0xAFAFAFAF,0xAFAFAFAF,0xAFAFAFAF,
   0xAAAAAAAA,0x90909090,0x7C7C7C7C,
   0xFFFFFFFF,0xA9A9A9A9,0x97979797
};

static USHORT __chip flash_imageData[84]={
	/* BitPlane 0 */
	0x0c00,0x3000,0x7f81,0xfe00,0xefff,0xf100,0x53ff,0xca00,
	0x6c7e,0x3200,0x2b81,0xd400,0x34ff,0x2400,0x3524,0xa800,
	0x1b24,0xc800,0x0dff,0x9000,0x0701,0x6000,0x01ff,0x8000,
	0x0000,0x0000,0x0000,0x0000,
	/* BitPlane 1 */
	0x0000,0x0000,0x0c00,0x3000,0x7f81,0xfe00,0x3fff,0xfc00,
	0x33ff,0xdc00,0x1c7e,0x3800,0x1f00,0xf800,0x1edb,0x7000,
	0x0edb,0x7000,0x07ff,0xe000,0x01ff,0x8000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,
	/* BitPlane 2 */
	0x0000,0x0000,0x8000,0x0100,0x7c00,0x3e00,0xaf81,0xf500,
	0xb3ff,0xcd00,0x507e,0x0b00,0x5881,0x1a00,0x1924,0x9600,
	0x0d24,0xb400,0x0600,0x6c00,0x01ff,0x9800,0x0200,0x7000,
	0x01ff,0xc000,0x0000,0x0000};

static struct BitMap flash_imageBitMap={4,14,0,3,0,(PLANEPTR)&flash_imageData[0],(PLANEPTR)&flash_imageData[28],(PLANEPTR)&flash_imageData[56]};

struct GfxBase 			*GfxBase;
struct Library 			*MUIMasterBase;
struct DosLibrary 		*DOSBase;
struct Library 			*UtilityBase;
struct IntuitionBase    *IntuitionBase;

struct MUI_CustomClass	*ZapsterClass;
struct MUI_CustomClass	*PrefsClass;

struct vplug_functable 	*ft;

/*
 * Displays a failure message
 */
void Fail(STRPTR txt)
{
	static struct IntuiText BodyText = { 255, 255, 0, 4, 4, NULL, NULL, NULL };
	static struct IntuiText ContinueText = { 255, 255, 0, 4, 4, NULL, "Ok", NULL };

	BodyText.IText = txt;
	AutoRequest(NULL, &BodyText, NULL, &ContinueText, 0, 0, 320, 60);
}


// Move lib down the liblist (we're nice, aren't we?)
long __asm __saveds __UserLibInit( register __a6 struct Library *libbase )
{
	libbase->lib_Node.ln_Pri = -128;
	return( 0 );
}


struct TagItem tags[] = {
	VPLUG_Query_Version, VERSION,
	VPLUG_Query_Revision, REVISION,
	VPLUG_Query_Copyright, (ULONG)"(C) 2000 David Gerber <zapek@vapor.com>, All Rights Reserved",
	VPLUG_Query_Infostring, (ULONG)"Voyager Zapster Demo Plugin",
	VPLUG_Query_APIVersion, 2,
	VPLUG_Query_RegisterMIMEType, (ULONG)"application/zapster",
	VPLUG_Query_PluginID, (ULONG)"Zapster",
	VPLUG_Query_HasPrefs, TRUE,
	TAG_DONE
};


/*
 * Voyager gets some informations
 * here
 */
struct TagItem * __asm __saveds VPLUG_Query( void )
{
	/*
	 * So we specify the tags we need
	 */
	return tags;
}


/*
 * Setup function
 */
BOOL __asm __saveds VPLUG_Setup( register __a0 struct vplug_functable *table )
{
	if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
	{
		return(FALSE);
	}

	if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME, 18)))
	{
		Fail("Couldn't open "MUIMASTER_NAME" v18 or higher.");
		return(FALSE);
	}
	
	GfxBase = (APTR)OpenLibrary( "graphics.library", 39 );

	/*
	 * Setup our main custom classes, subclass of Area class
	 */
	if (!(ZapsterClass = MUI_CreateCustomClass(NULL, MUIC_Area, NULL, sizeof(struct Data), dispatch)))
	{
		Fail("Couldn't create custom class.");
		return(FALSE);
	}
	
	DOSBase = (struct DosLibrary *)ZapsterClass->mcc_DOSBase;
	UtilityBase = ZapsterClass->mcc_UtilityBase;

	/*
	 * Setup our preference custom class, subclass of Group class
	 */
	if (!(PrefsClass = MUI_CreateCustomClass(NULL, MUIC_Group, NULL, sizeof(struct PrefsData), prefs_dispatch)))
	{
		Fail("Couldn't create custom class."); 
		return(FALSE);
	}

	/*
	 * Now we save the A4 register (this is *NEEDED*
	 * for a library + dispatchers' hooks)
	 */
	ZapsterClass->mcc_Class->cl_UserData  = PrefsClass->mcc_Class->cl_UserData = getreg(REG_A4);

	ft = table;

	/*
	 * This is a good place to setup default prefs
	 */

	return(TRUE);
}

/* Cleanup */
void __asm __saveds VPLUG_Cleanup( void )
{
	if (PrefsClass)
	{
		MUI_DeleteCustomClass(PrefsClass);
	}

	if (ZapsterClass)
	{
		MUI_DeleteCustomClass(ZapsterClass);
	}

	if (GfxBase)
	{
		CloseLibrary((APTR)GfxBase);
	}

	if (MUIMasterBase)
	{
		CloseLibrary(MUIMasterBase);
	}

	if (IntuitionBase)
	{
		CloseLibrary((struct Library *)IntuitionBase);
	}
}

/*
 * Embedding objects, V will NewObject() it.
 * We just return the class here
 */
APTR __asm __saveds VPLUG_GetClass( register __a0 STRPTR mimetype )
{
	return(ZapsterClass->mcc_Class);
}

APTR __asm __saveds VPLUG_ProcessURLMethod( register __a0 STRPTR url )
{
	return(0); // dummy
}

APTR __asm __saveds VPLUG_GetURLData( register __a0 APTR handle )
{
	return(0); // dummy
}

STRPTR __asm __saveds VPLUG_GetURLMIMEType( register __a0 APTR handle )
{
	return(0); // dummy
}

void __asm __saveds VPLUG_FreeURLData( register __a0 APTR handle )
{
	return; // dummy
}

void __asm __saveds VPLUG_FinalSetup( void )
{
	return; // dummy
}

int	__asm __saveds VPLUG_GetInfo( register __a0 struct PluginInfo *pi, register __a1 APTR nethandle )
{
	return(TRUE); // nothing done
}

int __asm __saveds VPLUG_GetURLDataSize( register __a0 APTR handle)
{
	return(0); // dummy
}

void __asm __saveds VPLUG_Hook_Prefs( register __d0 ULONG methodid, register __a0 struct vplug_prefs *prefs )
{
	switch(methodid)
	{
		case VPLUGPREFS_Setup:
			/*
			 * Optionally give the colormap and bitmap
			 * if we want to display a custom image
			 * in the preferences.
			 */
			prefs->bitmap = &flash_imageBitMap;
			prefs->colormap = flash_imageCMap32;
			break;

		case VPLUGPREFS_Create:
			/*
			 * We create the MUI prefs object here
			 */
			prefs->object = VGroup,
								Child, NewObject(PrefsClass->mcc_Class, NULL, TAG_DONE),
							End;
			break;

		case VPLUGPREFS_Dispose:
			MUI_DisposeObject(prefs->object);
			break;

		case VPLUGPREFS_Use:
			/*
			 * Use whatever you want to make the
			 * current prefs active. Sending a method
			 * to our prefs object is a good way.
			 */
			DoMethod(prefs->object, MM_Prefs_StorePrefs);
			break;

		case VPLUGPREFS_Save:
			/*
			 * Here we should save the prefs to disk. Suggested
			 * path is PROGDIR:Plugins/Data
			 */
			break;
	}
	return;
}

