/*=========================================================================
* IPT_Scpt.c : 
*     Script read
*
* (c) Ubi Studios 1997
*=======================================================================*/
 
#include "IPT_CPA.h"
#include "MemIPT.h"
#include "ErrIPT.h"
 
#include "IPT_Def.h"
#include "IPT_Hdl.h"
 
#define D_IPT_Input_StructureDefine
#include "IPT_KDef.h"
#include "IPT_Str.h"
#undef D_IPT_Input_StructureDefine
 
#include "IPT_Main.h"
#include "IPT_Scpt.h"
 
#include "Tmp.h"
 
#if !defined(U64)
void IPT_fn_vComputeArrayFromList(void)
{
 unsigned long i,j;
 IPT_tdxHandleToEntryElement hEntryElement,hEntryElementNext;
 IPT_tdxHandleToKeyWordElement hKeyWord,hKeyWordNext;
 
 /**** Destroy arrays ****/
 #if !defined(ACTIVE_EDITOR)
 
 if (IPT_g_hInputStructure.d_stEntryElementArray!=NULL)
 {
  for (i=0;i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
  {
   IPT_fn_vFree(&IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray);
  }
  IPT_fn_vFree(&IPT_g_hInputStructure.d_stEntryElementArray);
 }
 
 #endif /* ACTIVE_EDITOR */
 
 /**** Allocation of the EntryElement array ****/
 IPT_g_hInputStructure.ulNumberOfEntryElement = LST2_M_DynamicGetNumberOfElements(&IPT_g_hInputStructure.hEntryElement);
 if (IPT_g_hInputStructure.ulNumberOfEntryElement!=0)
  IPT_g_hInputStructure.d_stEntryElementArray = (IPT_tdstEntryElement *)IPT_fn_p_vAlloc(IPT_g_hInputStructure.ulNumberOfEntryElement* sizeof(struct IPT_tdstEntryElement_ ));
 
 LST2_M_DynamicForEachMovingElementOf(&IPT_g_hInputStructure.hEntryElement,hEntryElement,hEntryElementNext,i)
 {
  IPT_g_hInputStructure.d_stEntryElementArray[i].p_szActionName = hEntryElement->p_szActionName;
  IPT_g_hInputStructure.d_stEntryElementArray[i].p_szEntryName = hEntryElement->p_szEntryName;
  IPT_g_hInputStructure.d_stEntryElementArray[i].lState = hEntryElement->lState;
  IPT_g_hInputStructure.d_stEntryElementArray[i].xAnalogicValue = hEntryElement->xAnalogicValue;
  IPT_g_hInputStructure.d_stEntryElementArray[i].bIsActivate = hEntryElement->bIsActivate;
  IPT_g_hInputStructure.d_stEntryElementArray[i].ulNumberOfKeyWordElement = LST2_M_DynamicGetNumberOfElements(&hEntryElement->hKeyWordList);
  if (IPT_g_hInputStructure.d_stEntryElementArray[i].ulNumberOfKeyWordElement!=0)
   IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray = (IPT_tdstKeyWordElement *)IPT_fn_p_vAlloc(IPT_g_hInputStructure.d_stEntryElementArray[i].ulNumberOfKeyWordElement*sizeof(struct IPT_tdstKeyWordElement_));
 
  LST2_M_DynamicForEachMovingElementOf(&hEntryElement->hKeyWordList,hKeyWord,hKeyWordNext,j)
  {
   IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray[j].u_ElementUnion = hKeyWord->u_ElementUnion;
   IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray[j].ucResult = hKeyWord->ucResult;
   IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray[j].ucMinCounterInput = hKeyWord->ucMinCounterInput;
   IPT_g_hInputStructure.d_stEntryElementArray[i].d_stKeyWordElementArray[j].ucMaxCounterInput = hKeyWord->ucMaxCounterInput;
 
   if (  hKeyWord->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustValidated
       ||hKeyWord->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionValidated
       ||hKeyWord->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustInvalidated
       ||hKeyWord->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionInvalidated)
   {
    LST2_M_DynamicGetNextElement(hKeyWord)->u_ElementUnion.hEntryElement = IPT_fn_hGetEntryActionHandle((char *)(LST2_M_DynamicGetNextElement(hKeyWord)->u_ElementUnion.hEntryElement));
   }
 
   #if !defined(ACTIVE_EDITOR)
   LST2_M_DynamicIsolate(hKeyWord);
   IPT_fn_vFree(&hKeyWord);
   #endif /* ACTIVE_EDITOR */
  }
  #if !defined(ACTIVE_EDITOR)
  LST2_M_DynamicIsolate(hEntryElement);
  IPT_fn_vFree(&hEntryElement);
  #endif /* ACTIVE_EDITOR */
 }
}
/*************************************************************************/
void IPT_fn_vReadInputScript(char *_szName)
{
 SCR_fnp_st_RdL0_AnalyseSection(_szName, SCR_CDF_uw_Anl_ForceAnalyse);
}
void IPT_fn_vEndReadInputScript(void)
{
 /**** Transform list in array ****/
 IPT_fn_vComputeArrayFromList();
}
 
/*************************************************************************/
IPT_tdxHandleToEntryElement IPT_fn_hGetEntryActionHandle(char *_szKeyActionName)
{
/*
 IPT_tdxHandleToEntryElement hAction = NULL;
 SCR_tdst_Link_Value *p_xIndex;
 
 p_xIndex = SCR_fnp_st_Link_SearchKey(&IPT_g_hInputStructure.stEntryLink,_szKeyActionName);
 
 if (p_xIndex!=NULL&&SCR_M_e_Link_GetState(p_xIndex)==SCR_ELS_Link_Initialized)
  hAction = (IPT_tdxHandleToEntryElement)SCR_M_ul_Link_GetValue(p_xIndex);
 
 return(hAction);
*/
 unsigned long i;
 IPT_tdxHandleToEntryElement hAction = NULL;
 
 for (i=0;hAction==NULL&&i<IPT_g_hInputStructure.ulNumberOfEntryElement;i++)
 {
  if (!stricmp(IPT_g_hInputStructure.d_stEntryElementArray[i].p_szActionName,_szKeyActionName))
   hAction = &IPT_g_hInputStructure.d_stEntryElementArray[i];
 }
 
 return(hAction);
 
}
/*************************************************************************/
 
SCR_tde_Anl_ReturnValue IPT_fn_eAddKeyActionScriptCallBack(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
 SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
 IPT_tdxHandleToEntryElement hEntryElement;
/* SCR_tdst_Link_Value *p_xIndex;*/
 
 if (M_IsTitle)
 {
/*
  Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
  MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
  if ((hEntryElement = (IPT_tdxHandleToEntryElement)Mmg_fn_p_vAlloc4Ch(sizeof(struct IPT_tdstEntryElement_),C_ucMmgDefaultChannel))==NULL)
   M_IPTFatalError(E_uwIPT_AllocNewKeyAction);
*/
  MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
  hEntryElement = (IPT_tdxHandleToEntryElement)IPT_fn_p_vAlloc(sizeof(struct IPT_tdstEntryElement_));
 
  memset(hEntryElement,0,sizeof(struct IPT_tdstEntryElement_));
  SCR_M_RdL0_SetSectionLong(C_ThisSection,0,hEntryElement);
  LST2_M_DynamicInitElement(hEntryElement);
  LST2_M_DynamicAddTail(&IPT_g_hInputStructure.hEntryElement,hEntryElement);
  LST2_M_DynamicInitAnchor(&hEntryElement->hKeyWordList);
  hEntryElement->lState = -2;
  hEntryElement->bIsActivate = IPT_C_TRUE;
 
  if (_ap_szParams[0]!=NULL&&!stricmp(_ap_szParams[0],IPT_C_ParamOptions))
  {
   MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
/*   if ((hEntryElement->p_szEntryName = (char*)Mmg_fn_p_vAlloc4Ch((strlen(_p_szName)+1)*sizeof(char),C_ucMmgDefaultChannel))==NULL)
    M_IPTFatalError(E_uwIPT_AllocNewKeyAction);
*/
   hEntryElement->p_szEntryName = (char*)IPT_fn_p_vAlloc(strlen(_p_szName)+1);
   strcpy(hEntryElement->p_szEntryName,_p_szName);
  }
  hEntryElement->p_szActionName = (char*)TMP_M_p_Malloc(strlen(_p_szName)+1);
  strcpy(hEntryElement->p_szActionName,_p_szName);
 
/*  p_xIndex=SCR_fnp_st_Link_CreateOrGetLinkFromKey(&IPT_g_hInputStructure.stEntryLink,_p_szName);
 
  if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_NotInitialized)
  {
   SCR_fnp_st_Link_SetValue(&IPT_g_hInputStructure.stEntryLink,_p_szName,(unsigned long)hEntryElement);
  }
  else if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_Initialized)
  {
   M_IPTFatalError(E_uwIPT_NewKeyActionAlreadyExist);
  }
*/
 }
 else if (M_IsEntry)
 {
  IPT_tdxHandleToKeyWordElement hKeyWordOrderElement,hKeyWordElement;
 
  SCR_M_RdL0_GetSectionLong(C_ThisSection,0,IPT_tdxHandleToEntryElement,hEntryElement);
 
  hKeyWordOrderElement=IPT_fn_hAllocKeyWord();
  hKeyWordOrderElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(_p_szName,IPT_g_hScriptKeyWordValue);
  LST2_M_DynamicInitElement(hKeyWordOrderElement);
  LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordOrderElement);
 
  /**** All following keyword take a key in parameter ****/
  if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyJustPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyJustReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyReleased)
  {
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(_ap_szParams[0],IPT_g_hScriptKeyValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
  }
  /**** All following keyword take a pad in parameter ****/
  if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadJustPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadJustReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadJustPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadJustReleased)
  {
   /**** Pad Number ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swPadNumber = atoi(_ap_szParams[0]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
   /**** Pad Action ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swPadAction = IPT_fn_swGetKeyValue(_ap_szParams[1],IPT_g_hScriptPadValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
  }
  /**** All following keyword take a joystick in parameter ****/
  if ( hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickAxeValue)
  {
   /**** Joystick Number ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(_ap_szParams[0]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
   /**** Joystick Action ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyAction = IPT_fn_swGetKeyValue(_ap_szParams[1],IPT_g_hScriptPadValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
   /**** Joystick Min Value ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyValueMin = atoi(_ap_szParams[2]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
   /**** Joystick Max Value ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyValueMax = atoi(_ap_szParams[3]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
  }
  /**** All following keyword take a joystick in parameter ****/
  if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickJustPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickJustReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickReleased)
  {
   /**** Pad Number ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(_ap_szParams[0]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
   /**** Pad Action ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyAction = IPT_fn_swGetKeyValue(_ap_szParams[1],IPT_g_hScriptPadValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
  }
  /**** All following keyword take a Entry Handle in parameter ****/
  if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustValidated
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionValidated
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustInvalidated
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionInvalidated)
  {
   hKeyWordElement=IPT_fn_hAllocKeyWord();
/*   hKeyWordElement->u_ElementUnion.hEntryElement = IPT_fn_hGetEntryActionHandle(_ap_szParams[0]);*/
   hKeyWordElement->u_ElementUnion.hEntryElement = TMP_M_p_Malloc(strlen(_ap_szParams[0])+1);
   strcpy((char*)hKeyWordElement->u_ElementUnion.hEntryElement,_ap_szParams[0]);
 
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
/*
   if (!IPT_fn_bIsAValidEntryActionHandle(hKeyWordElement->u_ElementUnion.hEntryElement))
    M_IPTFatalError(E_uwIPT_UnknownActionValue);
*/
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
  }
  /**** All following keyword can have time notion ****/
  if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyReleased
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionValidated
      ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionInvalidated)
  {
   if (_ap_szParams[1]!=NULL)
   {
    hKeyWordElement->ucMinCounterInput = atoi(_ap_szParams[1]);
    if (_ap_szParams[2]!=NULL)
     hKeyWordElement->ucMaxCounterInput = atoi(_ap_szParams[2]);
   }
  }
  else if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadPressed
           ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadReleased
           ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickPressed
           ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickReleased
           ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed
           ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadReleased)
  {
   if (_ap_szParams[2]!=NULL)
   {
    hKeyWordElement->ucMinCounterInput = atoi(_ap_szParams[2]);
    if (_ap_szParams[3]!=NULL)
     hKeyWordElement->ucMaxCounterInput = atoi(_ap_szParams[3]);
   }
  }
 }
 
 return(eReturnValue);
}
 
/*************************************************************************/
SCR_tde_Anl_ReturnValue IPT_fn_eSequenceCallBack(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
 SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
 IPT_tdxHandleToEntryElement hEntryElement;
 IPT_tdxHandleToKeyWordElement hSequenceKeyWordElement,hSequenceOrderKeyWordElement;
 IPT_tdxHandleToKeyWordElement hKeyWordElement;
 long ulNumberOfSequence;
 
 SCR_M_RdL0_GetSectionLong(C_ParentSection,0,IPT_tdxHandleToEntryElement,hEntryElement);
 
 if (M_IsTitle)
 {
  hSequenceKeyWordElement=IPT_fn_hAllocKeyWord();
  hSequenceKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(IPT_C_SequenceSection,IPT_g_hScriptKeyWordValue);
  LST2_M_DynamicInitElement(hSequenceKeyWordElement);
  LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hSequenceKeyWordElement);
  SCR_M_RdL0_SetSectionLong(C_ThisSection,0,hSequenceKeyWordElement);
  SCR_M_RdL0_SetSectionLong(C_ThisSection,1,0);
 }
 else if (M_IsEntry)
 {
  SCR_M_RdL0_GetSectionLong(C_ThisSection,0,IPT_tdxHandleToKeyWordElement,hSequenceKeyWordElement);
  SCR_M_RdL0_GetSectionLong(C_ThisSection,1,unsigned long,ulNumberOfSequence);
  SCR_M_RdL0_SetSectionLong(C_ThisSection,1,ulNumberOfSequence+1);
 
  hSequenceOrderKeyWordElement=IPT_fn_hAllocKeyWord();
  hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(_p_szName,IPT_g_hScriptKeyWordValue);
  LST2_M_DynamicInitElement(hSequenceOrderKeyWordElement);
  LST2_M_DynamicInsertBetween(hSequenceOrderKeyWordElement,hSequenceKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceKeyWordElement));
 
  if (hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_SequenceKey)
  {
   hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequenceKeyEnd;
   if (LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)!=NULL)
    LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequenceKey;
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(_ap_szParams[0],IPT_g_hScriptKeyValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   if (_ap_szParams[1]!=NULL)
   {
    hKeyWordElement->ucMinCounterInput = atoi(_ap_szParams[1]);
    if (_ap_szParams[2]!=NULL)
     hKeyWordElement->ucMaxCounterInput = atoi(_ap_szParams[2]);
   }
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
  }
  else if (hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_SequencePad)
  {
   hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequencePadEnd;
   if (LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)!=NULL)
    LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequencePad;
   /**** Pad Action ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(_ap_szParams[1],IPT_g_hScriptPadValue);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   if (_ap_szParams[2]!=NULL)
   {
    hKeyWordElement->ucMinCounterInput = atoi(_ap_szParams[2]);
    if (_ap_szParams[3]!=NULL)
     hKeyWordElement->ucMaxCounterInput = atoi(_ap_szParams[3]);
   }
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
   /**** Pad Number ****/
   hKeyWordElement=IPT_fn_hAllocKeyWord();
   hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(_ap_szParams[0]);
   hKeyWordElement->ucMinCounterInput = 0;
   hKeyWordElement->ucMaxCounterInput = 255;
   LST2_M_DynamicInitElement(hKeyWordElement);
   LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
  }
 }
 else if (M_IsEnd)
 {
  SCR_M_RdL0_GetSectionLong(C_ThisSection,0,IPT_tdxHandleToKeyWordElement,hSequenceKeyWordElement);
  SCR_M_RdL0_GetSectionLong(C_ThisSection,1,unsigned long,ulNumberOfSequence);
  hKeyWordElement=IPT_fn_hAllocKeyWord();
  hKeyWordElement->u_ElementUnion.ulNumberOfSequence = ulNumberOfSequence;
  LST2_M_DynamicInitElement(hKeyWordElement);
  LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceKeyWordElement));
 }
 return(eReturnValue);
}
 
/*************************************************************************/
 
SCR_tde_Anl_ReturnValue IPT_fn_eActivateDeviceScriptCallBack(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
 SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
 
 if (M_IsEntry)
 {
  if (M_ActionIs(IPT_C_ActivatePadAction))
  {
   unsigned long ulPadNumber = atoi(_ap_szParams[0]);
   if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber]==IPT_C_ActivateJoystickDevice)
    M_IPTFatalError(E_uwIPT_AlreadyActivateAsAJoystick);
 
   if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] == IPT_C_ValideDevice)
   {
    fn_wJoyAddActiveJoystick((short)ulPadNumber);
    IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] = IPT_C_ActivatePadDevice;
    IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ActivateDevice;
   }
  }
  else if (M_ActionIs(IPT_C_ActivateJoystickAction))
  {
   unsigned long ulPadNumber = atoi(_ap_szParams[0]);
   if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber]==IPT_C_ActivatePadDevice)
    M_IPTFatalError(E_uwIPT_AlreadyActivateAsAPad);
 
   if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] == IPT_C_ValideDevice)
   {
    fn_wJoyAddActiveJoystick((short)ulPadNumber);
    IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] = IPT_C_ActivateJoystickDevice;
    IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ActivateDevice;
   }
  }
  else if (M_ActionIs(IPT_C_ActivateKeyboardAction))
  {
   if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] == IPT_C_ValideDevice)
    IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] = IPT_C_ActivateDevice;
  }
 }
 
 return(eReturnValue);
}
 
/*************************************************************************/
 
SCR_tde_Anl_ReturnValue IPT_fn_eKeyDefineScriptCallBack(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
 SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
 unsigned long ulKeyNumber;
 unsigned long ulKeyMaxNumber;
 
 if (M_IsTitle)
 {
  ulKeyMaxNumber = atoi(_ap_szParams[0]);
  Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
  MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
  if ((IPT_gd_hKeyAndPadDefine = (struct IPT_tdstKeyAndPadDefine_ *)Mmg_fn_p_vAlloc4Ch((ulKeyMaxNumber+1)*sizeof(struct IPT_tdstKeyAndPadDefine_),C_ucMmgDefaultChannel))==NULL)
   M_IPTFatalError(E_uwIPT_NoMemory);
  ulKeyNumber = 0;
  SCR_M_RdL0_SetSectionLong(C_ThisSection,0,ulKeyNumber);
  SCR_M_RdL0_SetSectionLong(C_ThisSection,1,ulKeyMaxNumber);
 }
 else if (M_IsEntry)
 {
  SCR_M_RdL0_GetSectionLong(C_ThisSection,0,unsigned long,ulKeyNumber);
  SCR_M_RdL0_GetSectionLong(C_ThisSection,1,unsigned long,ulKeyMaxNumber);
  if (M_ActionIs(IPT_C_KeyDefineAction))
  {
   if (ulKeyNumber>ulKeyMaxNumber)
    M_IPTFatalError(E_uwIPT_TooMuchKeyDefine);
 
   if (IPT_fn_p_szGetKeyPointerOfName(_ap_szParams[0],IPT_g_hScriptKeyValue)==NULL)
   {
    if (IPT_fn_p_szGetKeyPointerOfName(_ap_szParams[0],IPT_g_hScriptPadValue)==NULL)
    {
     M_IPTFatalError(E_uwIPT_UnknownKeyValue);
    }
    else
    {
     IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = IPT_fn_swGetKeyValue(_ap_szParams[0],IPT_g_hScriptPadValue)|0x8000;
     IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey   =
     IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_fn_p_szGetKeyPointerOfName(_ap_szParams[0],IPT_g_hScriptPadValue);
    }
   }
   else
   {
    IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = IPT_fn_swGetKeyValue(_ap_szParams[0],IPT_g_hScriptKeyValue);
    IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey   =
    IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_fn_p_szGetKeyPointerOfName(_ap_szParams[0],IPT_g_hScriptKeyValue);
   }
 
   if (SCR_fn_uc_RdL0_GetNumberOfParameters(_ap_szParams)>1)
   {
    Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
	MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
    if ((IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey = (char *)Mmg_fn_p_vAlloc4Ch(strlen(_ap_szParams[1])+1,C_ucMmgDefaultChannel))==NULL)
     M_IPTFatalError(E_uwIPT_NoMemory);
    strcpy(IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey,_ap_szParams[1]);
    if (SCR_fn_uc_RdL0_GetNumberOfParameters(_ap_szParams)>2)
    {
     Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
	 MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
     if ((IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = (char *)Mmg_fn_p_vAlloc4Ch(strlen(_ap_szParams[2])+1,C_ucMmgDefaultChannel))==NULL)
     M_IPTFatalError(E_uwIPT_NoMemory);
     strcpy(IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey,_ap_szParams[2]);
    }
    else
    {
     IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey;
    }
   }
 
   ulKeyNumber++;
   SCR_M_RdL0_SetSectionLong(C_ThisSection,0,ulKeyNumber);
  }
 }
 else if (M_IsEnd)
 {
  SCR_M_RdL0_GetSectionLong(C_ThisSection,0,unsigned long,ulKeyNumber);
  SCR_M_RdL0_GetSectionLong(C_ThisSection,1,unsigned long,ulKeyMaxNumber);
  if (ulKeyNumber>ulKeyMaxNumber)
   M_IPTFatalError(E_uwIPT_TooMuchKeyDefine);
  IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = 0xffff;
 }
 
 return(eReturnValue);
}
 
 
#endif /* U64 */
 
 
 
 
/*************************************************************************/
 
signed short IPT_fn_swGetKeyValue(char *_szKeyName,IPT_tdxHandleToScriptValue _hScriptValueTable)
{
 long i;
 signed short swReturn = -1;
 
 for (i=0;_hScriptValueTable[i].swKeyValue!=-1&&stricmp(_szKeyName,_hScriptValueTable[i].zsKeyName);i++);
 
 if (_hScriptValueTable[i].swKeyValue==-1)
 {
  M_IPTFatalError(E_uwIPT_UnknownKeyValue);
 }
 else
  swReturn = _hScriptValueTable[i].swKeyValue;
 
 return(swReturn);
}
 
/*************************************************************************/
 
char *IPT_fn_szGetKeyName(signed short swValue,IPT_tdxHandleToScriptValue _hScriptValueTable)
{
 long i;
 char *szReturn;
 
 for (i=0;_hScriptValueTable[i].swKeyValue!=-1&&_hScriptValueTable[i].swKeyValue!=swValue;i++);
 
 if (_hScriptValueTable[i].swKeyValue==-1)
 {
  M_IPTFatalError(E_uwIPT_UnknownKeyValue);
 }
 else
  szReturn = _hScriptValueTable[i].zsKeyName;
 
 return(szReturn);
}
 
/*************************************************************************/
 
char *IPT_fn_p_szGetKeyPointerOfName(char *_szKeyName,IPT_tdxHandleToScriptValue _hScriptValueTable)
{
 long i;
 char *szReturn = NULL;
 
 for (i=0;_hScriptValueTable[i].swKeyValue!=-1&&stricmp(_szKeyName,_hScriptValueTable[i].zsKeyName);i++);
 
 if (_hScriptValueTable[i].swKeyValue!=-1)
  szReturn = _hScriptValueTable[i].zsKeyName;
 
 return(szReturn);
}
/*************************************************************************/
 
signed short IPT_fn_swConvertAZERTYtoQWERTY(signed short swAZERTY)
{
 long i;
 
 for (i=0;IPT_g_ConvertAZERTYtoQWERTY[i].swAZERTYKeyValue!=-1&&IPT_g_ConvertAZERTYtoQWERTY[i].swAZERTYKeyValue!=swAZERTY;i++);
 if (IPT_g_ConvertAZERTYtoQWERTY[i].swAZERTYKeyValue==-1)
  return(swAZERTY);
 else
  return(IPT_g_ConvertAZERTYtoQWERTY[i].swQWERTYKeyValue);
}
 
/*************************************************************************/
 
