/* =========================================================================

FILE: AEEDemoExtension.c

SERVICES: Sample IDemoExtension implementation file

DESCRIPTION
  This file contains sample implementation of the IDemoExtension interface

           Copyright © 1999-2002 QUALCOMM Incorporated.
                     All Rights Reserved.
                   QUALCOMM Proprietary/GTDR
=========================================================================== */

#include "stdafx.h"
#include "StaticExtensionInc.h"

#include "AEEModTable.h"
#include "AEEStdlib.h"
#include "AEEDemoExtension.h"		// Our header to distribute for our class

#include "DemoExtension.bid"		// Our .BID file to distribute for our class

/*===============================================================================
Class Definitions
- If you have more than one class in this module, you should list them here.
=============================================================================== */
typedef struct IDemoExtension
{
	// Declare our VTable
	// By doing so, we have placed function pointers
	// in the begininng of this structure
	AEEVTBL(IDemoExtension) *pvt;

	// Class member variables
	uint32		m_nRefs;		// Reference Count
	IShell		*pIShell;		// Pointer to Shell interface ..
}IDemoExtension;

/*===============================================================================
Function Prototypes
===============================================================================*/
static uint32		DemoExtension_AddRef(IDemoExtension * po);
static uint32		DemoExtension_Release(IDemoExtension * po);
static int			DemoExtension_DrawHello(IDemoExtension * po, AEEPoint ptXYOffset);
int					DemoExtension_New(IShell *pShell, AEECLSID cls, void **ppif);

/*===============================================================================
Initialize the vtbl
===============================================================================*/
static const AEEVTBL(IDemoExtension) gAEEDemoExtensionFuncs = {
            DemoExtension_AddRef,
            DemoExtension_Release,
		    DemoExtension_DrawHello
};

// Create an AEEStaticClass array that lists the information about the interface 
const AEEStaticClass gAEEDemoExtensionClasses[] = {
   { AEECLSID_DEMO_EXTENSION, ASCF_UPGRADE, 0, 0, DemoExtension_New }, 
   { 0, 0, 0, 0, NULL},
};

// Define this if you are intending to build this as a static extension.
// Otherwise this implementation file can be statically built and linked
// in the target build.
#ifdef _BUILD_AS_STATIC_EXTENSION_
/*===========================================================================

Function:  GetStaticClassEntries()

Description:
	This function is required to be exposed if this interface will be 
	exposed as a static extension DLL. BREW Simulator loads all the DLLs
	that expose this function as a Static Extension DLL.

Prototype:
		const AEEStaticClass* GetStaticClassEntries(void)

Parameters: None	

Return Value:
	return an array of AEEStaticClass

Comments:  None

Side Effects: None

==============================================================================*/
BREWSTATICEXTENSION_API const AEEStaticClass* GetStaticClassEntries(void)
{
	return gAEEDemoExtensionClasses;
}
#endif // _BUILD_AS_STATIC_EXTENSION_

/*===========================================================================

Function:  DemoExtension_New()

Description:
	This function is called from AEEClsCreateInstance. This helps 
	with construction of the class and initialization of it's members.	

Prototype:
		int DemoExtension_New(IShell *pShell, AEECLSID cls, void **ppif)

Parameters:
	pShell: Pointer to IShell interface
	cls : ClassID passed in 
	ppif: [out]: On return, *ppif contains a valid pointer to the IDemoExtension structure 
	(or class)

Return Value:
	AEE_SUCCESS: If successful
	Error code: IF failed

Comments:  None

Side Effects: None

==============================================================================*/
int DemoExtension_New(IShell *pShell, AEECLSID cls, void **ppif)
{
	IDemoExtension* pMe = NULL;		 

	if( !pShell)
		return EFAILED;

	if (cls == AEECLSID_DEMO_EXTENSION)
	{
		pMe = (IDemoExtension *)MALLOC(sizeof(IDemoExtension));
		if (!pMe)
			return ENOMEMORY;

		// initialize the vtable
		pMe->pvt = &gAEEDemoExtensionFuncs;

		// initialize the data members
		pMe->m_nRefs = 1;
		pMe->pIShell = pShell;
		*ppif = pMe;
	}
	
	return AEE_SUCCESS;
}

/*===========================================================================

Function:  DemoExtension_AddRef()

Description:
	This function increases the reference count for the DemoExtension object

Prototype:
	uint32 DemoExtension_AddRef(IDemoExtension * po)

Parameters:
   po: Pointer to IDemoExtension interface whos reference count is to be incremented

Return Value:
	The updated reference count

Comments:  None

Side Effects: None

==============================================================================*/
static uint32 DemoExtension_AddRef(IDemoExtension * po)
{
   return (++((IDemoExtension *)po)->m_nRefs);
}

/*===========================================================================

Function:  DemoExtension_Release()

Description:
	This function decreases the reference count for the DemoExtension object. If
	the reference count reaches zero, it does the required cleanup

Prototype:
	uint32 DemoExtension_Release(IDemoExtension* po)

Parameters:
   po: Pointer to the IDemoExtension interface whose reference count needs to be
	decremented.

Return Value:
   The updated reference count

Comments:  None

Side Effects: None

==============================================================================*/
static uint32 DemoExtension_Release(IDemoExtension * po)
{
	IDemoExtension *		pMe = (IDemoExtension *)po;

	if( --pMe->m_nRefs != 0 )
		return pMe->m_nRefs;

	//Free the object itself
	FREE_VTBL(pMe, IModule);
	FREE( pMe );

	return 0;
}

/*===========================================================================

Function:  DemoExtension_DrawHello()

Description:
	This function Draws the word HELLO on the screen at the location given
	This is the implementation of an exported function for the IDemoExtension
	interface

Prototype:
	int DemoExtension_DrawHello(IDemoExtension * po, AEEPoint ptXYOffset)

Parameters:
   po:            Pointer to IDemoExtension interface whose reference count is 
   to be incremented
   ptXYOffset:    XY Point of the Text to draw

Return Value:
	SUCCESS for successful execution
   EFAILED for error

Comments:  Caller must Update the display

Side Effects: None

==============================================================================*/
static int DemoExtension_DrawHello(IDemoExtension * po, AEEPoint ptXYOffset)
{
	IDemoExtension *		pMe = (IDemoExtension *)po;
	AECHAR		      szbuff[] = {'H','E','L','L','O','\0'};
	IDisplay *		  pIDisplay = NULL;	// Display interface pointer

	if( !pMe )
		return EFAILED;

	if( ISHELL_CreateInstance(pMe->pIShell, AEECLSID_DISPLAY, (void **)&pIDisplay) != SUCCESS )
		return EFAILED;

	IDISPLAY_DrawText(pIDisplay, AEE_FONT_BOLD, szbuff, -1, ptXYOffset.x, ptXYOffset.y, NULL, 0);

	IDISPLAY_Release(pIDisplay);

	return SUCCESS;

}

