/*=========================================================================
* IPT_Main.c : 
*     Variables declaration and init function
*
* (c) Ubi Studios 1997
*=======================================================================*/
 
#include "IPT_CPA.h"
 
#include "IPT_Hdl.h"
 
 
#define D_IPT_Input_StructureDefine
#define D_IPT_Input_VariableDefine
 
#include "IPT_Main.h"
 
#include "MemIPT.h"
#include "ErrIPT.h"
 
#include "IPT_Kwan.h"
#include "IPT_KDef.h"
#include "IPT_Str.h"
#include "IPT_Scpt.h"
 
#include "IPT_Hist.h"
#include "IPT_Def.h"
#include "IPT_Anal.h"



#include "ACP_base.h"
#include "GAM/ToolsCPA.h"
#include "gam.h"
#include "acp_driver.h"
/*XB*/
#include <string.h>
/*End XB*/
#undef D_IPT_Input_StructureDefine
#undef D_IPT_Input_VariableDefine
 
 
/*************************************************************************/
void IPT_fn_vFirstInitInput()
{
 Erm_M_InitErrMsg(IPT);
 Mmg_M_InitMmg(IPT);
 Mmg_fn_cCheckAlignmentValidity();
}
/*************************************************************************/
void IPT_fn_vInitMemory(unsigned long _ulMemSize)
{
#if !defined(U64)
	Mmg_M_InitBlock(IPT,E_ucIPTMemoryBlock,_ulMemSize);
#else /* !U64 */
	#ifdef CHECK_MEMORY
		Mmg_M_InitSpecificBlock(IPT,E_ucIPTMemoryBlock,_ulMemSize, C_ulDefaultMaxNbStaticMalloc, 4, C_BlockWithFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#else /* CHECK_MEMORY */
  Mmg_M_InitSpecificBlock(IPT,E_ucIPTMemoryBlock,_ulMemSize, C_ulDefaultMaxNbStaticMalloc, 4, C_BlockWithoutFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#endif /* CHECK_MEMORY */
#endif /* !U64 */
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void IPT_fn_vInitInputWhenLoadSnapshot( HINSTANCE hInstance, HWND hWindow )
{
	long i;
 
#if (!defined(U64))
	/**** Init the Keyboard library ****/
	if (fn_wKbInitKeyboard(IPT_C_ACP_HistoricSize,hInstance,hWindow)!=C_wKbError)
	{
		fn_wKbResetKeyboard();
 
		/* Mark keyboard as active instead of Valide.*/
		IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] = IPT_C_ActivateDevice;
	}
	else
		IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] = IPT_C_InvalideDevice;
#endif /* !U64 */
 
	/**** Init the Joystick library ****/
	IPT_g_hInputStructure.ucOnePadActivate = IPT_C_InvalideDevice;
	IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent] = IPT_C_InvalideDevice;
	
	if (fn_wJoyInitGamePort(IPT_C_ACP_HistoricSize)!=C_wJoyError)
	{
		fn_wJoyResetGamePort();
		for (i=0;i<fn_wJoyMaximumJoystickNumber();i++)
		{
			if ( fn_wJoyAllocJoystick((short)i)!=C_wJoyError && fn_wJoyIsConnected((short)i)==C_wJoyTrue)
			{
				IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+i] = IPT_C_ValideDevice;
			}
		}
 
		/* Now, activate joy 0*/
		/* This is usually done using scripts but we are in snapshot version...*/
		if( IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent] == IPT_C_ValideDevice )
		{
			IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent] = IPT_C_ActivateJoystickDevice;
			IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ActivateDevice;
			fn_wJoyAddActiveJoystick( 0 );
		}
	}
 
}
 
/*************************************************************************/
 
void IPT_fn_vDesinitInputWhenLoadSnapshot()
{
	long i;
 
#if (!defined(U64))
	fn_wKbFreeKeyboard();
#endif /* !U64 */
	for (i=0;i<fn_wJoyMaximumJoystickNumber();i++)
	{
		if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+i]==IPT_C_ActivateDevice)
		fn_wJoyRemoveActiveJoystick((short)i); /*DFM :  Il faut tous*/
	}
	fn_wJoyFreeGamePort();
 
#if !defined(U64)
	SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_EntryActionSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
	SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_SequenceSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
	SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_ActivateDeviceSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
	SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_AddCommandsSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
 
	Mmg_M_DeleteBlock(IPT,E_ucIPTMemoryBlock);
#endif /* U64 */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*************************************************************************/
void IPT_fn_vInitInput(short _sHistoricSize,HINSTANCE hInstance, HWND hWindow)
{
 long i;
 
 /**** Historic initialisation ****/
 IPT_g_hInputStructure.sEventHistoricSize = _sHistoricSize;
 Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
 MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
 IPT_g_hInputStructure.hEventHistoric = Mmg_fn_p_vAlloc4Ch(_sHistoricSize*sizeof(struct IPT_tdstHistoricElement_),C_ucMmgDefaultChannel);
 memset(IPT_g_hInputStructure.hEventHistoric,0,_sHistoricSize*sizeof(struct IPT_tdstHistoricElement_));
 
 /**** Keyboard timer initialisation ****/
 memset(IPT_g_hInputStructure.p_ucKeyboardCounter,0,256*sizeof(unsigned char));
 
 /**** Joystick timer initialisation ****/
 memset(IPT_g_hInputStructure.p_ucPadAndJoyCounter,0,IPT_E_NumberOfDeviceEvent*IPT_E_NumberOfPadAndJoyAction*sizeof(unsigned char));
 
#if !defined(U64)
 /**** Script initialisation ****/
 SCR_fn_v_RdL0_RegisterCallback(IPT_C_EntryActionSection,IPT_fn_eAddKeyActionScriptCallBack,SCR_CRC_c_RdL0_ForSection);
 SCR_fn_v_RdL0_RegisterCallback(IPT_C_SequenceSection,IPT_fn_eSequenceCallBack,SCR_CRC_c_RdL0_ForSection);
 SCR_fn_v_RdL0_RegisterCallback(IPT_C_ActivateDeviceSection,IPT_fn_eActivateDeviceScriptCallBack,SCR_CRC_c_RdL0_ForSection);
 SCR_fn_v_RdL0_RegisterCallback(IPT_C_KeyDefineSection,IPT_fn_eKeyDefineScriptCallBack,SCR_CRC_c_RdL0_ForSection);
 
 /**** Link table initialisation ****/
 SCR_fn_v_Link_InitTable(&IPT_g_hInputStructure.stEntryLink);
 SCR_fn_v_Link_InitTable(&IPT_g_hInputStructure.stCommandLink);
#endif /* U64 */
 
 /**** Init static list ****/
 LST2_M_DynamicInitAnchor(&IPT_g_hInputStructure.hEntryElement);
 
 /**** Valide device initialisation ****/
 IPT_g_hInputStructure.ucOnePadActivate = 0;
 memset(IPT_g_hInputStructure.p_ucValideAndActiveDevice,0,IPT_E_NumberOfDeviceEvent*sizeof(unsigned char));
 
#if (!defined(U64))
 /**** Init the Keyboard library ****/
 if (fn_wKbInitKeyboard(IPT_C_ACP_HistoricSize,hInstance,hWindow)!=C_wKbError)
 {
  fn_wKbResetKeyboard();
  IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] = IPT_C_ValideDevice;
  IPT_g_hInputStructure.ucKeyboardType = g_cIsAzerty?IPT_C_AZERTYKeyBoardType:IPT_C_QWERTYKeyBoardType;
 }
#endif /* !U64 */
 
#ifdef USE_DIRECTX //init mouse
/**** Init the Mose library ****/
if( INO_fn_wInitMouseWithGLD(hInstance,g_stEngineStructure.hGLDDevice)==C_wMouseError)
{
	INO_fn_vFreeMouse();
}
#endif


/**** Init the Joystick library ****/
 if (fn_wJoyInitGamePort(IPT_C_ACP_HistoricSize)!=C_wJoyError)
 {
  IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ValideDevice;
  fn_wJoyResetGamePort();
  for (i=0;i<fn_wJoyMaximumJoystickNumber();i++)
  {
   if (  fn_wJoyAllocJoystick((short)i)!=C_wJoyError
       &&fn_wJoyIsConnected((short)i)==C_wJoyTrue)
   {
    IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+i] = IPT_C_ValideDevice;
   }
  }
 }
}
/*************************************************************************/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void IPT_fn_vDesinitInput(void)
{
 long i;
 
#if (!defined(U64))
 fn_wKbFreeKeyboard();
#endif /* !U64 */
 for (i=0;i<fn_wJoyMaximumJoystickNumber();i++)
 {
  if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+i]==IPT_C_ActivateDevice)
   fn_wJoyRemoveActiveJoystick((short)i); /*DFM :  Il faut tous*/
 }
 fn_wJoyFreeGamePort();
 
#if !defined(U64)
 SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_EntryActionSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
 SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_SequenceSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
 SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_ActivateDeviceSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
 SCR_fn_v_RdL0_DeleteRegisterCallback(IPT_C_AddCommandsSection,SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
 
 Mmg_M_DeleteBlock(IPT,E_ucIPTMemoryBlock);
 SCR_fn_v_Link_CloseTable(&IPT_g_hInputStructure.stEntryLink);
 SCR_fn_v_Link_CloseTable(&IPT_g_hInputStructure.stCommandLink);
#endif /* U64 */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

/*************************************************************************/
void IPT_fn_vResetInputEntry(void)
{
 unsigned long i;
/* IPT_tdxHandleToEntryElement hEntryElement;*/
 
/* LST2_M_DynamicForEachElementOf(&IPT_g_hInputStructure.hEntryElement,hEntryElement,i)*/
 for (i=0;i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
 {
/*hEntryElement->lState=-2;*/
	IPT_g_hInputStructure.d_stEntryElementArray[i].lState=-2;
 }
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
IPT_tdxHandleToEntryElement IPT_fn_hGetEntryActionHandleForOptions(char *_szKeyActionName)
{
 unsigned long i;
/* IPT_tdxHandleToEntryElement hEntryElement;*/
 IPT_tdxHandleToEntryElement hAction = NULL;
 
/* LST2_M_DynamicForEachElementOf(&IPT_g_hInputStructure.hEntryElement,hEntryElement,i)*/
/* {*/
/*  if (hEntryElement->p_szEntryName!=NULL&&!stricmp(hEntryElement->p_szEntryName,_szKeyActionName))*/
 for (i=0;i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
 {
  if (IPT_g_hInputStructure.d_stEntryElementArray[i].p_szEntryName!=NULL&&!stricmp(IPT_g_hInputStructure.d_stEntryElementArray[i].p_szEntryName,_szKeyActionName))
/*   hAction = hEntryElement;*/
   hAction = &IPT_g_hInputStructure.d_stEntryElementArray[i];
 }
 
 return(hAction);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
char *IPT_fn_szGetEntryActionName(IPT_tdxHandleToEntryElement hKeyActionHandle)
{
/*
 char *szName = NULL;
#if !defined(U64)
 SCR_tdst_Link_Value *p_xIndex;
 
 p_xIndex=SCR_fnp_st_Link_SearchValue(&IPT_g_hInputStructure.stEntryLink,(unsigned long)hKeyActionHandle);
 
 if (p_xIndex!=NULL&&SCR_M_e_Link_GetState(p_xIndex)==SCR_ELS_Link_Initialized)
  szName = SCR_M_p_sz_Link_GetKey(p_xIndex);
 
#endif 
 return(szName);
*/
#if !defined(U64)
 if (hKeyActionHandle)
  return(hKeyActionHandle->p_szActionName);
 else
#endif /* U64*/
  return(NULL);
 
}
/*************************************************************************/
#ifndef _FIRE_DEADCODE_U64_
unsigned char IPT_fn_bIsAValidEntryActionHandle(IPT_tdxHandleToEntryElement hKeyActionHandle)
{
/*
 unsigned char ucReturn = IPT_C_FALSE;
#if !defined(U64)
 SCR_tdst_Link_Value *p_xIndex;
 
 if (hKeyActionHandle!=NULL)
 {
  p_xIndex=SCR_fnp_st_Link_SearchValue(&IPT_g_hInputStructure.stEntryLink,(unsigned long)hKeyActionHandle);
 
 if (p_xIndex!=NULL&&SCR_M_e_Link_GetState(p_xIndex)==SCR_ELS_Link_Initialized)
  ucReturn = IPT_C_TRUE;
 }
 
#endif // U64 
 return(ucReturn);
*/
#if !defined(U64)
unsigned long i;
 
 if (hKeyActionHandle!=NULL)
 {
  for (i=0;i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
  {
   if (hKeyActionHandle==&IPT_g_hInputStructure.d_stEntryElementArray[i])
      return (IPT_C_TRUE);
  }
 }
#endif /* U64 */
 return(IPT_C_FALSE);
 
}
#endif /* _FIRE_DEADCODE_U64_ */
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned long IPT_fn_ulNumberOfEntryAction(void)
{
/* unsigned long ulNumberOfEntryAction;
 
 ulNumberOfEntryAction=LST2_M_DynamicGetNumberOfElements(&IPT_g_hInputStructure.hEntryElement);
 
 return(ulNumberOfEntryAction);
*/
 return(IPT_g_hInputStructure.ulNumberOfEntryElement);
 
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
#ifndef _FIRE_DEADCODE_U64_
char *IPT_fn_szEntryActionNumber(unsigned long _ulEntryAction)
{
/*
 unsigned long i;
 char *szEntryAction = NULL;
 IPT_tdxHandleToEntryElement hElement;
 
 if (_ulEntryAction<IPT_fn_ulNumberOfEntryAction())
 {
  LST2_M_DynamicGetElementNumber(&IPT_g_hInputStructure.hEntryElement,hElement,_ulEntryAction,i);
  szEntryAction = IPT_fn_szGetEntryActionName(hElement);
 }
 
 return (szEntryAction);
*/
 char *szEntryAction = NULL;
    
 if (_ulEntryAction<IPT_fn_ulNumberOfEntryAction())
 {
  szEntryAction = IPT_fn_szGetEntryActionName(&IPT_g_hInputStructure.d_stEntryElementArray[_ulEntryAction]);
 }
    
 return (szEntryAction);
}
#endif /* _FIRE_DEADCODE_U64_ */

/*************************************************************************/
void IPT_fn_vActivateAllEntryElements ( void )
{
	IPT_tdxHandleToEntryElement hEntryElement ;
	unsigned long ulI ;

	for ( ulI = 0 , hEntryElement = IPT_g_hInputStructure.d_stEntryElementArray ; ulI < IPT_g_hInputStructure . ulNumberOfEntryElement ; ulI ++ , hEntryElement ++ )
	{
		IPT_fn_vActivateEntryElement ( hEntryElement ) ;
	}
}
/*************************************************************************/
void IPT_fn_vActivateEntryElement(IPT_tdxHandleToEntryElement _hEntryElement)
{
 _hEntryElement->bIsActivate=IPT_C_TRUE;
 _hEntryElement->lState = -2;
}
/*************************************************************************/
void IPT_fn_vDesactivateEntryElement(IPT_tdxHandleToEntryElement _hEntryElement)
{
 _hEntryElement->bIsActivate=IPT_C_FALSE;
 _hEntryElement->lState = -2;
}
/*************************************************************************/
#ifndef _FIRE_DEADCODE_U64_
unsigned char IPT_fn_bGetActivationFlagOfEntryElement(IPT_tdxHandleToEntryElement _hEntryElement)
{
 return(_hEntryElement->bIsActivate);
}
#endif /* _FIRE_DEADCODE_U64_ */
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
char IPT_fn_cGetValidAsciiKey(char *p_szValideKey)
{
#if (!defined(U64))
 short wKey = 0;
 char  cKey = 0;
 
 wKey = INO_fn_wGetLastASCIICode();
 cKey = (char)wKey;
 if (wKey==C_wKbError||strchr(p_szValideKey,cKey)==NULL)
  cKey = 0;
 
 return(cKey);
#else
 return(0);
#endif /* !U64 */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
#if !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS)
void IPT_PrintUsedStaticMemory(void)
{
	Mmg_M_PrintUsedStaticMemoryInModule(IPT);
}
#endif /* !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS) */
 
/**************************************************************************/
/*XB*/
#ifdef CHECK_MEMORY
void IPT_CheckMemory(void)
{
	Mmg_M_CheckMemory(IPT);
}
#endif /* CHECK_MEMORY */
/*End XB*/
 
/**************************************************************************/
/* XB 02/06/99 */
#ifndef FINAL_VERSION
unsigned long IPT_fn_ulGetUsedStaticMemory(void)
{
	return Mmg_M_GetUsedStaticMemory(IPT);
}
#endif /* FINAL_VERSION */
/* End XB 02/06/99 */
 
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned char IPT_fn_bIsKeyboardControlAvailable(void)
{
 unsigned char ucReturn = IPT_C_FALSE;
 
 if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent]==IPT_C_ActivateDevice)
  ucReturn = IPT_C_TRUE;
 
 return(ucReturn);
}
/*************************************************************************/
unsigned char IPT_fn_bIsPaddleControlAvailable(void)
{
 unsigned long i;
 unsigned char ucReturn = IPT_C_FALSE;
 
 for (i=IPT_E_Pad00DeviceEvent;i<=IPT_E_Pad15DeviceEvent;i++)
 {
  if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivatePadDevice)
   ucReturn = IPT_C_TRUE;
 }
 
 return(ucReturn);
}
/*************************************************************************/
unsigned char IPT_fn_bIsJoystickControlAvailable(void)
{
 unsigned long i;
 unsigned char ucReturn = IPT_C_FALSE;
 
 for (i=IPT_E_Pad00DeviceEvent;i<=IPT_E_Pad15DeviceEvent;i++)
 {
  if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivateJoystickDevice)
   ucReturn = IPT_C_TRUE;
 }
 
 return(ucReturn);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
void IPT_fn_vDesactiveJoystickAndPadControl(void)
{
 unsigned long i;
 
 for (i=IPT_E_Pad00DeviceEvent;i<=IPT_E_Pad15DeviceEvent;i++)
 {
  if (  IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivatePadDevice
      ||IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivateJoystickDevice)
  {
   IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]=IPT_C_ValideDevice;
   fn_wJoyRemoveActiveJoystick((short)(i-IPT_E_Pad00DeviceEvent));
  }
 }
}
/*************************************************************************/
void IPT_fn_vActiveJoystickControl(void)
{
 unsigned long i;
 for (i=IPT_E_Pad00DeviceEvent;i<=IPT_E_Pad15DeviceEvent;i++)
 {
  if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ValideDevice)
  {
   IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]=IPT_C_ActivateJoystickDevice;
   fn_wJoyAddActiveJoystick((short)(i-IPT_E_Pad00DeviceEvent));
  }
 }
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void IPT_fn_vActivePaddleControl(void)
{
 unsigned long i;
 for (i=IPT_E_Pad00DeviceEvent;i<=IPT_E_Pad15DeviceEvent;i++)
 {
  if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ValideDevice)
  {
   IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]=IPT_C_ActivatePadDevice;
   fn_wJoyAddActiveJoystick((short)(i-IPT_E_Pad00DeviceEvent));
  }
 }
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
IPT_tdxHandleToEntryElement IPT_fn_hSearchElementInOptionWithKeyWord(signed short _swEntryType,signed short swPadNumber,signed short _swKeyValue)
{
 unsigned long i;
 IPT_tdxHandleToEntryElement hEntryElement;
 IPT_tdxHandleToEntryElement hAction = NULL;
 IPT_tdxHandleToKeyWordElement hNextElement;
    
 for (i=0;i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
 {
  hEntryElement = &IPT_g_hInputStructure.d_stEntryElementArray[i];
  if (hEntryElement->p_szEntryName!=NULL)
  {
   if (_swEntryType==IPT_E_KeyWord_KeyPressed)
   {
    hNextElement = &hEntryElement->d_stKeyWordElementArray[0];
    if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed)
    {
     hNextElement = &hEntryElement->d_stKeyWordElementArray[1];
     if (hNextElement->u_ElementUnion.swKey==_swKeyValue)
      hAction = hEntryElement;
    }
   }
   else if (_swEntryType==IPT_E_KeyWord_JoystickOrPadPressed)
   {
    hNextElement = &hEntryElement->d_stKeyWordElementArray[0];
    if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed)
    {
     hNextElement = &hEntryElement->d_stKeyWordElementArray[1];
     if (hNextElement->u_ElementUnion.swPadNumber==swPadNumber)
     {
      hNextElement = &hEntryElement->d_stKeyWordElementArray[2];
      if (hNextElement->u_ElementUnion.swKey==_swKeyValue)
       hAction = hEntryElement;
     }
    }
   }
  }
 }
 return(hAction);
}
/*************************************************************************/
/* replace an ActionValidated action inside another one */
unsigned char IPT_fn_bModifyIPTAction ( IPT_tdxHandleToEntryElement _hElementToChange, IPT_tdxHandleToEntryElement _hReplacingElement )
{
	long lI ;
	for ( lI = 0 ; lI < ( long ) _hElementToChange -> ulNumberOfKeyWordElement ; lI ++ )
	if ( _hElementToChange -> d_stKeyWordElementArray [ lI ] . u_ElementUnion . swKeyWord == IPT_E_KeyWord_ActionValidated )
	{
		_hElementToChange -> d_stKeyWordElementArray [ lI + 1 ] . u_ElementUnion . hEntryElement = _hReplacingElement ;
		return IPT_C_TRUE ;
	}
		return IPT_C_FALSE ;
}
/*************************************************************************/
unsigned char IPT_fn_bChangeIPTValue(IPT_tdxHandleToEntryElement _hElement,unsigned char ucWhichTest)
{
 signed short swPadNumber;
 unsigned char ucReturn = IPT_C_FALSE;
 unsigned long i;
 unsigned char bFlag = FALSE;
 
 IPT_tdxHandleToEntryElement hExchangeEntry;
 IPT_tdxHandleToKeyWordElement hExchangeNextElement;
 IPT_tdxHandleToKeyWordElement hNextElement;
 
/* hNextElement = LST2_M_DynamicGetFirstElement(&_hElement->hKeyWordList);*/
 hNextElement = &_hElement->d_stKeyWordElementArray[0];
#if (!defined(U64))
 if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed)
 {
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
  hNextElement++;
  for (i=0;IPT_g_hScriptKeyValue[i].swKeyValue!=-1&&fn_wKbJustDown(IPT_g_hScriptKeyValue[i].swKeyValue)!=C_wKbTrue;i++);
  if (IPT_g_hScriptKeyValue[i].swKeyValue!=-1&&IPT_fn_p_szGetNameOfKeyName(IPT_g_hScriptKeyValue[i].swKeyValue,0)!=NULL)
  {
   hExchangeEntry = IPT_fn_hSearchElementInOptionWithKeyWord(IPT_E_KeyWord_KeyPressed,0,IPT_g_hScriptKeyValue[i].swKeyValue);
   if (ucWhichTest==IPT_C_Duplicate)
    bFlag = TRUE;
   else if (ucWhichTest==IPT_C_NoDuplicate)
   {
    if (hExchangeEntry==NULL||hExchangeEntry==_hElement)
     bFlag = TRUE;
   }
   else if (ucWhichTest==IPT_C_Exchange)
   {
    bFlag = TRUE;
    if (hExchangeEntry!=NULL&&hExchangeEntry!=_hElement)
    {
/*     hExchangeNextElement = LST2_M_DynamicGetNextElement(LST2_M_DynamicGetFirstElement(&hExchangeEntry->hKeyWordList));*/
     hExchangeNextElement = &hExchangeEntry->d_stKeyWordElementArray[1];
     hExchangeNextElement->u_ElementUnion.swKey = hNextElement->u_ElementUnion.swKey;
    }
   }
   if (bFlag)
   {
    hNextElement->u_ElementUnion.swKey = IPT_g_hScriptKeyValue[i].swKeyValue;
    ucReturn = IPT_C_TRUE;
   }
  }
 }
 else
#endif /* !U64 */
 if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed)
 {
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
  hNextElement++;
  swPadNumber = hNextElement->u_ElementUnion.swPadNumber;
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
	hNextElement++;
  for (i=IPT_E_PadFirstButton;i<=IPT_E_PadLastButton&&INO_fn_wJoystickButtonJustPressed(swPadNumber,IPT_g_ulPadAndJoyButtonFlag[i-IPT_E_PadFirstButton])!=C_wJoyTrue;i++);
  if (i<=IPT_E_PadLastButton)
  {
   hExchangeEntry = IPT_fn_hSearchElementInOptionWithKeyWord(IPT_E_KeyWord_JoystickOrPadPressed,swPadNumber,IPT_g_hScriptPadValue[i].swKeyValue);
   if (ucWhichTest==IPT_C_Duplicate)
    bFlag = TRUE;
   else if (ucWhichTest==IPT_C_NoDuplicate)
   {
    if (hExchangeEntry==NULL||hExchangeEntry==_hElement)
     bFlag = TRUE;
   }
   else if (ucWhichTest==IPT_C_Exchange)
   {
    bFlag = TRUE;
    if (hExchangeEntry!=NULL&&hExchangeEntry!=_hElement)
    {
/*     hExchangeNextElement = LST2_M_DynamicGetNextElement(LST2_M_DynamicGetNextElement(LST2_M_DynamicGetFirstElement(&hExchangeEntry->hKeyWordList)));*/
      hExchangeNextElement = &hExchangeEntry->d_stKeyWordElementArray[2];
    hExchangeNextElement->u_ElementUnion.swKey = hNextElement->u_ElementUnion.swKey;
    }
   }
   if (bFlag)
   {
    hNextElement->u_ElementUnion.swKey = IPT_g_hScriptPadValue[i].swKeyValue;
    ucReturn = IPT_C_TRUE;
   }
  }
 }
 
 return(ucReturn);
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
char *IPT_fn_szGetStringOf(IPT_tdxHandleToEntryElement _hElement)
{
 IPT_tdxHandleToKeyWordElement hNextElement;
 
 /*hNextElement = LST2_M_DynamicGetFirstElement(&_hElement->hKeyWordList);*/
 hNextElement = &_hElement->d_stKeyWordElementArray[0];;
 if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed)
 {
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
  hNextElement++;
 
  if (IPT_g_hInputStructure.ucKeyboardType==IPT_C_AZERTYKeyBoardType)
   return(IPT_fn_p_szGetNameOfKeyName(hNextElement->u_ElementUnion.swKey,0));
  else
   return(IPT_fn_p_szGetNameOfKeyName(hNextElement->u_ElementUnion.swKey,1));
 }
 else if (hNextElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed)
 {
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
/*  hNextElement = LST2_M_DynamicGetNextElement(hNextElement);*/
  hNextElement+=2;
  return(IPT_fn_p_szGetNameOfKeyName((unsigned short)(hNextElement->u_ElementUnion.swPadAction|0x8000),0));
 }
 return NULL;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
IPT_tdxHandleToKeyWordElement IPT_fn_hAllocKeyWord(void)
{
 IPT_tdxHandleToKeyWordElement hKeyWordElement;
 
 Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
 MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
 if ((hKeyWordElement = (IPT_tdxHandleToKeyWordElement)Mmg_fn_p_vAlloc4Ch(sizeof(struct IPT_tdstKeyWordElement_),C_ucMmgDefaultChannel))==NULL)
 {
  M_IPTFatalError(E_uwIPT_AllocNewKeyWord);
 }
 memset(hKeyWordElement,0,sizeof(struct IPT_tdstKeyWordElement_));
 
 return(hKeyWordElement);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
char *IPT_fn_p_szGetNameOfKeyName(unsigned short uwKeyCode,unsigned char ucFrenchOrAmerican)
{
 long i;
 char *szReturn = NULL;
 
 for (i=0;IPT_gd_hKeyAndPadDefine[i].uwBasedKey!=0xffff&&uwKeyCode!=IPT_gd_hKeyAndPadDefine[i].uwBasedKey;i++);
 
 if (IPT_gd_hKeyAndPadDefine[i].uwBasedKey!=0xffff)
 {
  if (ucFrenchOrAmerican==0)
   szReturn = IPT_gd_hKeyAndPadDefine[i].p_szFrenchKey;
  else if (ucFrenchOrAmerican==1)
   szReturn = IPT_gd_hKeyAndPadDefine[i].p_szAmericanKey;
 }
 
 return(szReturn);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
ACP_tdxBool IPT_fn_bAJoypadConnected()
{
		return ((IPT_g_hInputStructure.ucOnePadActivate == IPT_C_ActivateDevice)||
				(fn_wJoyReadJoystick(C_wJoyReadConnected)!=C_wJoyError));
}
 
/* LOL*/
#ifndef U64
/*************************************************************************/
ACP_tdxBool IPT_fn_bAKeyJustPressed()
{
	unsigned short					uwKeyCode;
	struct IPT_tdstScriptValue_		*p_hScriptKeyValue;
	struct IPT_tdstKeyAndPadDefine_ *p_stKeyAndPadDefine;
 
	p_hScriptKeyValue = &IPT_g_hScriptKeyValue[0];
	while (p_hScriptKeyValue->swKeyValue!=-1)
	{
		uwKeyCode = p_hScriptKeyValue->swKeyValue;
		if (fn_wKbJustDown(uwKeyCode)==C_wKbTrue)
		{
			p_stKeyAndPadDefine = &IPT_gd_hKeyAndPadDefine[0];
			while (p_stKeyAndPadDefine->uwBasedKey!=0xffff)
			{
				if (uwKeyCode==p_stKeyAndPadDefine->uwBasedKey)
					return TRUE;
				p_stKeyAndPadDefine++;
			}
		}
		p_hScriptKeyValue ++;
	}
	return FALSE;
}
#endif
 
/*************************************************************************/
ACP_tdxBool IPT_fn_bAButtonPadJustPressed()
{
	long						i;
	
 	i =IPT_E_PadFirstButton;
	while (i<=IPT_E_PadLastButton)
	{
		if (INO_fn_wJoystickButtonJustPressed(0,IPT_g_ulPadAndJoyButtonFlag[i-IPT_E_PadFirstButton])==C_wJoyTrue)
			return TRUE;
		i++;
	}
	return FALSE;
}
/*************************************************************************/
void *IPT_fn_p_vAlloc(unsigned long lSize)
{
 void *p_vPointer;
 
 Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
 p_vPointer=(void*)Mmg_fn_p_vAlloc4Ch(lSize,C_ucMmgDefaultChannel);
 memset(p_vPointer,0,lSize);
 if (p_vPointer==NULL)
  M_IPTFatalError(E_uwIPTMemoryPlace);
	return (p_vPointer);
}
/**************************************************************************/
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void IPT_fn_vFree(void **p_vPointer)
{
 Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
	Mmg_fn_vFree4Ch(*p_vPointer,C_ucMmgDefaultChannel);
 *p_vPointer = NULL;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/**************************************************************************/
