/*=========================================================================
* IPT_Hist.c : 
*     Historic
*
* (c) Ubi Studios 1997
*=======================================================================*/
 
#include <limits.h>
 
#include "IPT_CPA.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_Def.h"
#include "IPT_Anal.h"
#include "IPT_Hist.h"
 
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_
unsigned long IPT_fn_ulSizeOfHistoric()
{
 return(IPT_g_hInputStructure.sEventHistoricSize);
}
 
/*************************************************************************/
 
unsigned char IPT_fn_bIsEmptyHistoricElement(unsigned long _ulElementNumber)
{
 return((IPT_g_hInputStructure.hEventHistoric[_ulElementNumber].bf6DeviceType==0)?IPT_C_TRUE:IPT_C_FALSE);
}
 
/*************************************************************************/
 
unsigned char IPT_fn_bIsKeyboardHistoricElement(unsigned long _ulElementNumber)
{
 return(((IPT_g_hInputStructure.hEventHistoric[_ulElementNumber].bf6DeviceType)==IPT_E_KeyboardDeviceEvent)?IPT_C_TRUE:IPT_C_FALSE);
}
/*************************************************************************/
 
unsigned char IPT_fn_bIsPadHistoricElement(unsigned long _ulElementNumber)
{
 return(((IPT_g_hInputStructure.hEventHistoric[_ulElementNumber].bf6DeviceType)>=IPT_E_Pad00DeviceEvent&&(IPT_g_hInputStructure.hEventHistoric[_ulElementNumber].bf6DeviceType)<=IPT_E_Pad15DeviceEvent)?IPT_C_TRUE:IPT_C_FALSE);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*************************************************************************/
 
void IPT_fn_vActualizeHistoric()
{
 long  i,j;
 long  lPadNumber;
 long  lFound = 0;
 
 struct IPT_tdstHistoricElement_ hHistoricElement;
 
#if (!defined(U64))
 /**** KeyBoard Test ****/
 for (i=0;IPT_g_hScriptKeyValue[i].swKeyValue!=-1;i++)
 {
  if (fn_wKbJustDown(IPT_g_hScriptKeyValue[i].swKeyValue)==C_wKbTrue)
  {
   lFound = 1;
   hHistoricElement.bf2State = IPT_E_InProgress;
   hHistoricElement.bf6DeviceType  = IPT_E_KeyboardDeviceEvent;
   hHistoricElement.swDeviceValue = IPT_g_hScriptKeyValue[i].swKeyValue;
   hHistoricElement.ucCounter     = 0;
   IPT_g_hInputStructure.p_ucKeyboardCounter[IPT_g_hScriptKeyValue[i].swKeyValue] = 0;
  }
  else if (fn_wKbJustUp(IPT_g_hScriptKeyValue[i].swKeyValue)==C_wKbTrue)
   IPT_g_hInputStructure.p_ucKeyboardCounter[IPT_g_hScriptKeyValue[i].swKeyValue] = 0;
  IPT_g_hInputStructure.p_ucKeyboardCounter[IPT_g_hScriptKeyValue[i].swKeyValue] = 
	  min(UCHAR_MAX,IPT_g_hInputStructure.p_ucKeyboardCounter[IPT_g_hScriptKeyValue[i].swKeyValue]+1);
 }
#endif /* !U64 */
 
 /**** Joystick Test ****/
 if (!lFound&&IPT_g_hInputStructure.ucOnePadActivate == IPT_C_ActivateDevice)
 {
  for (i=IPT_E_Pad00DeviceEvent;!lFound&&i<IPT_E_NumberOfDeviceEvent;i++)
  {
   lPadNumber = i-IPT_E_Pad00DeviceEvent;
   if (  IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivateJoystickDevice
       ||IPT_g_hInputStructure.p_ucValideAndActiveDevice[i]==IPT_C_ActivatePadDevice)
   {
    /**** Axis test ****/
    if (INO_fn_wJoystickJustUp((short)(lPadNumber))==C_wJoyTrue)
    {
     lFound = 1;
     hHistoricElement.bf2State = IPT_E_InProgress;
     hHistoricElement.bf6DeviceType  = (unsigned char)i;
     hHistoricElement.swDeviceValue = IPT_E_PadUp;
     hHistoricElement.ucCounter     = 0;
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadUp] = 0;
    }
    else if (INO_fn_wJoystickDirectionJustReleased((short)(lPadNumber),C_ulJoyUp)==C_wJoyTrue)
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadUp] = 0;
    IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadUp] = 
     min(UCHAR_MAX,IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadUp]+1);
 
    if (INO_fn_wJoystickJustDown((short)(lPadNumber))==C_wJoyTrue)
    {
     lFound = 1;
     hHistoricElement.bf2State = IPT_E_InProgress;
     hHistoricElement.bf6DeviceType  = (unsigned char)i;
     hHistoricElement.swDeviceValue = IPT_E_PadDown;
     hHistoricElement.ucCounter     = 0;
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadDown] = 0;
    }
    else if (INO_fn_wJoystickDirectionJustReleased((short)(lPadNumber),C_ulJoyDown)==C_wJoyTrue)
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadDown] = 0;
    IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadDown] = 
     min(UCHAR_MAX,IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadDown]+1);
 
    if (INO_fn_wJoystickJustLeft((short)(lPadNumber))==C_wJoyTrue)
    {
     lFound = 1;
     hHistoricElement.bf2State = IPT_E_InProgress;
     hHistoricElement.bf6DeviceType  = (unsigned char)i;
     hHistoricElement.swDeviceValue = IPT_E_PadLeft;
     hHistoricElement.ucCounter     = 0;
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadLeft] = 0;
    }
    else if (INO_fn_wJoystickDirectionJustReleased((short)(lPadNumber),C_ulJoyLeft)==C_wJoyTrue)
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadLeft] = 0;
    IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadLeft] = 
     min(UCHAR_MAX,IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadLeft]+1);
 
    if (INO_fn_wJoystickJustRight((short)(lPadNumber))==C_wJoyTrue)
    {
     lFound = 1;
     hHistoricElement.bf2State = IPT_E_InProgress;
     hHistoricElement.bf6DeviceType  = (unsigned char)i;
     hHistoricElement.swDeviceValue = IPT_E_PadRight;
     hHistoricElement.ucCounter     = 0;
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadRight] = 0;
    }
    else if (INO_fn_wJoystickDirectionJustReleased((short)(lPadNumber),C_ulJoyRight)==C_wJoyTrue)
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadRight] = 0;
    IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadRight] = 
     min(UCHAR_MAX,IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_E_PadRight]+1);
 
    /**** Buttons test ****/
    for (j=IPT_E_PadFirstButton;!lFound&&j<IPT_E_JoyLastButton;j++)
    {
     if (INO_fn_wJoystickButtonJustPressed((unsigned char)(lPadNumber),IPT_g_ulPadAndJoyButtonFlag[j-IPT_E_PadFirstButton])==C_wJoyTrue)
     {
      lFound = 1;
      hHistoricElement.bf2State = IPT_E_InProgress;
      hHistoricElement.bf6DeviceType  = (unsigned char)i;
      hHistoricElement.swDeviceValue = IPT_g_hScriptPadValue[j].swKeyValue;
      hHistoricElement.ucCounter     = 0;
      IPT_g_hInputStructure.p_ucPadAndJoyCounter[i][IPT_g_hScriptPadValue[j].swKeyValue] = 0;
     }
     else if (INO_fn_wJoystickButtonJustReleased((unsigned char)(lPadNumber),IPT_g_ulPadAndJoyButtonFlag[j-IPT_E_PadFirstButton])==C_wJoyTrue)
      IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_g_hScriptPadValue[j].swKeyValue] = 0;
     IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_g_hScriptPadValue[j].swKeyValue] = 
	     min(UCHAR_MAX,IPT_g_hInputStructure.p_ucPadAndJoyCounter[lPadNumber][IPT_g_hScriptPadValue[j].swKeyValue]+1);
    }
   }
  }
 }
 
 /**** Update Historic ****/
 if (lFound)
 {
/* ANNECY AV {*/
/*
  for (i=IPT_g_hInputStructure.sEventHistoricSize-1;i>0;i--)
  { memcpy(&(IPT_g_hInputStructure.hEventHistoric[i]),&(IPT_g_hInputStructure.hEventHistoric[i-1]),sizeof(struct IPT_tdstHistoricElement_)); }
  memcpy(&(IPT_g_hInputStructure.hEventHistoric[0]),&hHistoricElement,sizeof(struct IPT_tdstHistoricElement_));
*/
  memmove (& IPT_g_hInputStructure.hEventHistoric [1] , & IPT_g_hInputStructure.hEventHistoric [0] , (IPT_g_hInputStructure.sEventHistoricSize-1) * sizeof (struct IPT_tdstHistoricElement_));
  IPT_g_hInputStructure.hEventHistoric [0] = hHistoricElement;
/* END ANNECY AV }*/
 }
 
 /**** Update the counter ****/
 for (i=0;i<IPT_g_hInputStructure.sEventHistoricSize;i++)
 {
  /**** Keyboard ****/
#if (!defined(U64))
  if (  IPT_g_hInputStructure.hEventHistoric[i].bf6DeviceType==IPT_E_KeyboardDeviceEvent
      &&IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_InProgress
      &&fn_wKbIsDown(IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue)==C_wKbTrue)
  {
   IPT_g_hInputStructure.hEventHistoric[i].ucCounter=min(UCHAR_MAX,IPT_g_hInputStructure.hEventHistoric[i].ucCounter+1);
  }
  else
#endif /* !U64 */
		if(  IPT_g_hInputStructure.hEventHistoric[i].bf6DeviceType>=IPT_E_Pad00DeviceEvent
          &&IPT_g_hInputStructure.hEventHistoric[i].bf6DeviceType<=IPT_E_Pad15DeviceEvent
          &&IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_InProgress)
  {
   lPadNumber = IPT_g_hInputStructure.hEventHistoric[i].bf6DeviceType-IPT_E_Pad00DeviceEvent;
   if (  (  IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue==IPT_E_PadUp
          &&INO_fn_wIsJoystickUp((unsigned char)(lPadNumber))==C_wJoyTrue)
       ||(  IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue==IPT_E_PadDown
          &&INO_fn_wIsJoystickDown((unsigned char)(lPadNumber))==C_wJoyTrue)
       ||(  IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue==IPT_E_PadLeft
          &&INO_fn_wIsJoystickLeft((unsigned char)(lPadNumber))==C_wJoyTrue)
       ||(  IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue==IPT_E_PadRight
          &&INO_fn_wIsJoystickRight((unsigned char)(lPadNumber))==C_wJoyTrue)
       ||(  IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue>=IPT_E_PadFirstButton
          &&IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue<=IPT_E_PadLastButton
          &&INO_fn_wIsJoystickButtonPressed((unsigned char)(lPadNumber),IPT_g_ulPadAndJoyButtonFlag[IPT_g_hInputStructure.hEventHistoric[i].swDeviceValue-IPT_E_PadFirstButton])==C_wJoyTrue)
      )
   {
    IPT_g_hInputStructure.hEventHistoric[i].ucCounter=min(UCHAR_MAX,IPT_g_hInputStructure.hEventHistoric[i].ucCounter+1);
   }
   else
   {
    if (IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_InProgress)
     IPT_g_hInputStructure.hEventHistoric[i].bf2State = IPT_E_Stopping;
    else if (IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_Stopping)
     IPT_g_hInputStructure.hEventHistoric[i].bf2State = IPT_E_Stopped;
   }
  }
  else
  {
   if (IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_InProgress)
    IPT_g_hInputStructure.hEventHistoric[i].bf2State = IPT_E_Stopping;
   else if (IPT_g_hInputStructure.hEventHistoric[i].bf2State==IPT_E_Stopping)
    IPT_g_hInputStructure.hEventHistoric[i].bf2State = IPT_E_Stopped;
  }
 }
}
