/***************************************************************************/
/*  Description: CPA Joystick.lib.                                         */
/*                                                                         */
/*  Author: F. Jentey                                                      */
/*  Last Update: 05/03/97                                                  */
/***************************************************************************/
 
 
 
#include "CPA_std.h"
 
/*XB*/
#ifdef U64
#define INO_USE_CPA
#endif /* U64 */
/*End XB*/
#ifdef INO_USE_CPA
 
#include "errINO.h"
#include "mmgINO.h"
 
#endif
 
#include "Specif/JoyWin.h"
 
/*XB*/
#ifdef U64
#include "Specif/MacroINO.h"
#else /* U64 */
#include "MacroINO.h"
#endif /* U64 */
/*End XB*/
 
#include "INO/Joystick.h"
#include "INO/Specif/Joy.h"
 
 
#ifdef PSX
	#include "libetc.h"
#endif
 
/***********************************************************
    Global variable
***********************************************************/
 
/*  State of the module  */
static tdstGamePort                 JoyLib_tdstGp = {0,0,0,0,0,0,0};
 
/*  Last error code  */
static u_long                       ulJoystickErrorCode;
 
/*  Caps for each joystick  */
static tdstJoystickCaps             *JoyLib_tdstJC[16];
 
/*  State for each joystick  */
static tdstJoystickStatus           *JoyLib_tdstJS[16];
 
/*  Historic for each joystick  */
static tdstJoystickRecord           *JoyLib_tdstH[16];
 
#ifndef _FIRE_DEADCODE_U64_
/*  Demo mode variable  */
static short                        JoyLib_wMode;
static u_short                      DemoRecordJoystick;
static u_long                       JoyLib_ulDemoMaxStep;
#endif /* _FIRE_DEADCODE_U64_ */
 
#ifndef _FIRE_DEADCODE_U64_
/*  Count of step and current step of the demo sequence  */
static u_long                       JoyLib_ulDemoRecordCount, JoyLib_ulDemoRecordCurrent;
 
/*  Pointer to a buffer to store demo data  */
static tdstJoystickRecord           *JoyLib_pstDemoRecordBuffer;
 
/*  File handle to store demo data  */
static FILE                         *DataDemoFile;
 
/*  Name of the demo data file  */
static char                         JoyDemoFileHeader[] = "JoystickSequence";
#endif /* _FIRE_DEADCODE_U64_ */
 
/*  Error message string  */
#ifndef _FIRE_DEADCODE_U64_
static char JoyErrorString[17][90] =
{
"No joystick driver detected.",
"The joystick.lib isn't initialized.Call fn_wJoyInitGamePort first.",
"The specified joystick isn't active. See fn_wJoyAddActiveJoystick.",
"You request unavailable informations, like z-coordinate. See fn_wJoyGetMovmentCaps.",
"You try to use unavailable button. Check yhe joystick button number.",
"The maximum joystick number is 16.",
"This function can only be called in demo recording mode.",
"You are currently in demo recording or playing mode.",
"This function can only be called in demo playing mode.",
"Unrecognize file format.",
"The function can't open or read the demo file.",
"The index in the historic tab is out of range.",
"This joystick is not allocated",
"The joystick isn't configured in the control panel",
"No joystick available",
"This joystick is already allocated",
"Memory allocation error"
};
#endif /* _FIRE_DEADCODE_U64_ */
 
/*XB980505*/
/*#include "Specif\Joy.c"*/
#include "Specif\Joy.cxx"
/*End XB*/
 
/**************************************************************
   Initialization of the Game port.
**************************************************************/
/*short INO_fn_wInitGamePort(short wHistorySize)*/
/*{*/
/*    short wNbJoystick;*/
 
/*    if ( JoyLib_tdstGp.m_wGameportOk == 1 )*/
/*        return (0);*/
 
/*    wNbJoystick = fn_wJoyGetMaxJoystickCount();*/
/*    if ( wNbJoystick == 0 )*/
/*    {*/
/*        /* There is no gameport   */
/*        ulJoystickErrorCode = C_ErrNoJoystickDriver;*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    memset(&JoyLib_tdstGp,0,sizeof(tdstGamePort));*/
/*    memset(JoyLib_tdstJC,0,sizeof(tdstJoystickCaps*)*16);*/
/*    memset(JoyLib_tdstJS,0,sizeof(tdstJoystickStatus*)*16);*/
 
/*    JoyLib_tdstGp.m_wHistorySize = wHistorySize;*/
/*    JoyLib_tdstGp.m_wNbMaxiJoystick = wNbJoystick;*/
 
/*    JoyLib_tdstGp.m_wNbJoystick = 0;*/
/*    JoyLib_tdstGp.m_wGameportOk = 1;*/
/*    JoyLib_tdstGp.m_uwActiveFlag = 0;*/
 
/*    /* Demo mode initialization: mode normal and 0 record frame   */
/*    JoyLib_pstDemoRecordBuffer = NULL;*/
/*    JoyLib_wMode = C_wJoyNormalMode;*/
/*    DemoRecordJoystick = C_wJoystick1;*/
 
/*    return(0);*/
/*}*/
 
 
/**************************************************************
  Reinitialization of the game port.
**************************************************************/
short INO_fn_wResetGamePort(void)
{
    int i;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 1 )
    {
        for ( i=0 ; i<16 ; i++ )
        {
        if ( JoyLib_tdstJC[i] != NULL )
            M_INOFree(JoyLib_tdstJC[i]);
        if ( JoyLib_tdstJS[i] != NULL )
            M_INOFree(JoyLib_tdstJS[i]);
        if ( JoyLib_tdstH[i] != NULL )
            M_INOFree(JoyLib_tdstH[i]);
        JoyLib_tdstJC[i] = NULL;
        JoyLib_tdstJS[i] = NULL;
        JoyLib_tdstH[i] = NULL;
        }
        JoyLib_tdstGp.m_uwActiveFlag = 0;
        JoyLib_tdstGp.m_wNbJoystick = 0;
    }
    return (0);
}
 
 
/**************************************************************
  Check if there is a gameport driver.
**************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
short INO_fn_wUpdateGamePort(void)
{
    if ( fn_wJoyGetMaxJoystickCount() == 0 )
    {
        JoyLib_tdstGp.m_wGameportOk = 0;
        return (C_wJoyFalse);
    }
    else
    {
        JoyLib_tdstGp.m_wGameportOk = 1;
        return (C_wJoyTrue);
    }
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
/**************************************************************
  Release joystick ressource.
**************************************************************/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 07/06/99 */
short INO_fn_wFreeGamePort(void)
{
    int i;
 
    /*if ( JoyLib_tdstGp.m_wGameportOk == 1 ) we can free joystick if Gameport isn't OK
    {*/
        for ( i=0 ; i<16 ; i++ )
        {
        if ( JoyLib_tdstJC[i] != NULL )
            M_INOFree(JoyLib_tdstJC[i]);
        if ( JoyLib_tdstJS[i] != NULL )
            M_INOFree(JoyLib_tdstJS[i]);
        if ( JoyLib_tdstH[i] != NULL )
            M_INOFree(JoyLib_tdstH[i]);
        JoyLib_tdstJC[i] = NULL;
        JoyLib_tdstJS[i] = NULL;
        JoyLib_tdstH[i] = NULL;
        }
        JoyLib_tdstGp.m_uwActiveFlag = 0;
        JoyLib_tdstGp.m_wNbJoystick = 0;
        JoyLib_tdstGp.m_wGameportOk = 0;
    /*}*/
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 07/06/99 */

 
 
/**************************************************************
  Try to allocate a joystick.
  If ok return a handle to the joystick
**************************************************************/
short INO_fn_wAllocJoystick(short wJoystickNum)
{
    short wResult;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
 
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
 
    if ( JoyLib_tdstJC[wJoystickNum] != NULL )
    {
        ulJoystickErrorCode = C_ErrAlreadyAllocated;
        return (C_wJoyError);
    }
 
    /* Init the joystick capabilities */
    /*JoyLib_tdstJC[wJoystickNum] = GlobalAlloc(GPTR,sizeof(tdstJoystickCaps));*/
	M_INOAlloc( JoyLib_tdstJC[wJoystickNum], tdstJoystickCaps *, sizeof(tdstJoystickCaps) );
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrMemoryError;
        return (C_wJoyError);
    }
    if ( fn_wJoyGetJoystickCaps(wJoystickNum,JoyLib_tdstJC[wJoystickNum]) == C_wJoyError )
    {
        M_INOFree(JoyLib_tdstJC[wJoystickNum]);
        JoyLib_tdstJC[wJoystickNum] = NULL;
        ulJoystickErrorCode = C_ErrJoystickNotConfigured;
        return (C_wJoyError);
    }
 
    /* Init the joystick state */
    /*JoyLib_tdstJS[wJoystickNum] = GlobalAlloc(GPTR,sizeof(tdstJoystickStatus));*/
	M_INOAlloc( JoyLib_tdstJS[wJoystickNum], tdstJoystickStatus *, sizeof(tdstJoystickStatus) );
    if ( JoyLib_tdstJS[wJoystickNum] == NULL )
    {
        M_INOFree(JoyLib_tdstJC[wJoystickNum]);
        JoyLib_tdstJC[wJoystickNum] = NULL;
        ulJoystickErrorCode = C_ErrMemoryError;
        return (C_wJoyError);
    }
    wResult = fn_wJoyCheckConnection(wJoystickNum);
    if ( wResult == C_wJoyError )
    {
        M_INOFree(JoyLib_tdstJC[wJoystickNum]);
        M_INOFree(JoyLib_tdstJS[wJoystickNum]);
        JoyLib_tdstJC[wJoystickNum] = NULL;
        JoyLib_tdstJS[wJoystickNum] = NULL;
        ulJoystickErrorCode = C_ErrJoystickNotConfigured;
        return (C_wJoyError);
    }
    if ( wResult == C_wJoyFalse )
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyNone;
    else
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyTypeJoystick;
 
    /* Init the joystick historic */
    /*JoyLib_tdstH[wJoystickNum] = GlobalAlloc(GPTR,sizeof(tdstJoystickRecord)*JoyLib_tdstGp.m_wHistorySize);*/
	M_INOAlloc( JoyLib_tdstH[wJoystickNum], tdstJoystickRecord *, sizeof(tdstJoystickRecord)*JoyLib_tdstGp.m_wHistorySize );
    if ( JoyLib_tdstH[wJoystickNum] == NULL )
    {
        M_INOFree(JoyLib_tdstJC[wJoystickNum]);
        M_INOFree(JoyLib_tdstJS[wJoystickNum]);
        JoyLib_tdstJC[wJoystickNum] = NULL;
        JoyLib_tdstJS[wJoystickNum] = NULL;
        ulJoystickErrorCode = C_ErrMemoryError;
        return (C_wJoyError);
    }
 
    /* Increase the number of allocated joystick */
    JoyLib_tdstGp.m_wNbJoystick++;
 
    return(wJoystickNum);
}
 
 
/**************************************************************
  Release a joystick.
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wFreeJoystick(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
 
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
 
    if ( JoyLib_tdstJC[wJoystickNum] != NULL )
    {
        GlobalFree(JoyLib_tdstJC[wJoystickNum]);
        if ( JoyLib_tdstJS[wJoystickNum] != NULL )
            M_INOFree(JoyLib_tdstJS[wJoystickNum]);
        if ( JoyLib_tdstH[wJoystickNum] != NULL )
            M_INOFree(JoyLib_tdstH[wJoystickNum]);
        JoyLib_tdstJC[wJoystickNum] = NULL;
        JoyLib_tdstJS[wJoystickNum] = NULL;
        JoyLib_tdstH[wJoystickNum] = NULL;
        JoyLib_tdstGp.m_uwActiveFlag &= ~(1 << wJoystickNum);
        JoyLib_tdstGp.m_wNbJoystick--;
    }
 
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
 This function return the maximum joystick number supported by the driver.
**************************************************************/
short INO_fn_wGetMaximumJoystickNumber()
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    return (JoyLib_tdstGp.m_wNbMaxiJoystick);
}
 
 
/**************************************************************
  fn_wJoyIsAllocated
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wIsJoystickAllocated(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyIsConnected
**************************************************************/
short INO_fn_wIsJoystickConnected(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyFalse);
    }
    if ( JoyLib_tdstJC[wJoystickNum]->m_uwType == C_wJoyNone )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
 
 
/**************************************************************
  fn_wJoyAllocFirstJoystickAvailable()
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wAllocFirstJoystickAvailable()
{
    short i, wResult;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
 
    for ( i=0 ; i<16 ; i++ )
    {
        wResult = INO_fn_wAllocJoystick(i);
        if ( wResult != C_wJoyError )
            return (i);
    }
    ulJoystickErrorCode = C_ErrNoJoystickAvailable;
    return (C_wJoyError);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyUpdateJoystick(short wJoystickNum)
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wUpdateJoystick(short wJoystickNum)
{
    short wResult;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyFalse);
    }
 
    if ( fn_wJoyGetJoystickCaps(wJoystickNum,JoyLib_tdstJC[wJoystickNum]) == C_wJoyError )
    {
        ulJoystickErrorCode = C_ErrJoystickNotConfigured;
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyNone;
        return (C_wJoyError);
    }
 
    wResult = fn_wJoyCheckConnection(wJoystickNum);
    if ( wResult == C_wJoyError )
    {
        ulJoystickErrorCode = C_ErrJoystickNotConfigured;
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyNone;
        return (C_wJoyError);
    }
    if ( wResult == C_wJoyFalse )
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyNone;
    else
        JoyLib_tdstJC[wJoystickNum]->m_uwType  = C_wJoyTypeJoystick;
 
    return(0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  Set the type of joystick
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wSetJoystickType(short wJoystickNum, short wType)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    JoyLib_tdstJC[wJoystickNum]->m_uwType = wType;
 
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  Get the type of joystick
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickType(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
 
    return (JoyLib_tdstJC[wJoystickNum]->m_uwType);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyButtonNumber
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickButtonNumber(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    return (JoyLib_tdstJC[wJoystickNum]->m_uwNbButton);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyAxisNumber
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickAxisNumber(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    return (JoyLib_tdstJC[wJoystickNum]->m_uwNbAxe);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyMovmentCaps
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickMovmentCaps(short wJoystickNum, u_long *p_ulFlagInfo)
{
    *p_ulFlagInfo = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    *p_ulFlagInfo = JoyLib_tdstJC[wJoystickNum]->m_ulCapsFlag;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/************************************************************************************
  This function sets the joystick extra features (like z-axis) the fn_JoyReadJoystick
  muist update. The ulFlagInfo parameter is a combinaisson of the capabilities flags.
************************************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wSetJoystickActiveCaps(u_long ulCapsFlag)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    JoyLib_tdstGp.m_ulGameFeatures |= ulCapsFlag;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*****************************************************************
                DEMO MODE SUPPORT FUNCTIONS
*****************************************************************/
 
/*****************************************************************
  This function is used to define the exit demo button for each
  joystick. You can select several button by a combinaison the
  C_ulButtonFlag'n' value, or all button with C_ulButtonAll.
  Default is none.
  For the joystick you are recording, you shouldn't specified a
  button that is used in the game.
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wSetStopRecordingJoystickButton(short wJoystickNum, u_long ulButtonFlag)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & ((1 << JoyLib_tdstJC[wJoystickNum]->m_uwNbButton) - 1)) != ulButtonFlag )
    {
        ulJoystickErrorCode = C_ErrBadButtonFlag;
        return (C_wJoyError);
    }
 
    JoyLib_tdstJS[wJoystickNum]->m_ulExitRecordingModeButton = ulButtonFlag;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*****************************************************************
  This function define the joystick to record. Default is
  C_wJoystick1.
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wSetJoystickToRecord(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
 
    DemoRecordJoystick = wJoystickNum;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*****************************************************************
  This function start the demo recording mode.
  The demo data will be store in the file with the specified name
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wStartRecordingJoystick(char *p_szFileName, short wDemoSize)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( JoyLib_wMode != C_wJoyNormalMode )
    {
        ulJoystickErrorCode = C_ErrNotInJoyNormalMode;
        return (C_wJoyError);
    }
 
    /*JoyLib_pstDemoRecordBuffer = GlobalAlloc(GPTR,sizeof(tdstJoystickRecord)*wDemoSize);*/
	M_INOAlloc( JoyLib_pstDemoRecordBuffer, tdstJoystickRecord*, sizeof(tdstJoystickRecord)*wDemoSize );
    if ( JoyLib_pstDemoRecordBuffer == NULL )
        return (C_wJoyError);
 
    JoyLib_ulDemoMaxStep = wDemoSize;
 
    JoyLib_ulDemoRecordCount = 0;
    DataDemoFile = fopen(p_szFileName,"wb");
    if ( DataDemoFile == NULL )
    {
        ulJoystickErrorCode = C_ErrJoyDemoFileError;
        if ( JoyLib_pstDemoRecordBuffer != NULL )
            M_INOFree(JoyLib_pstDemoRecordBuffer);
        JoyLib_pstDemoRecordBuffer = NULL;
        return (C_wJoyError);
    }
 
    JoyLib_wMode = C_wJoyDemoRecordingMode;
 
    return (0);
}
 
 
/*****************************************************************
  This function quit the demo recording mode.
******************************************************************/
short INO_fn_wStopRecordingJoystick()
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( JoyLib_wMode != C_wJoyDemoRecordingMode)
    {
        ulJoystickErrorCode = C_ErrNotInJoyPlayingMode;
        return (C_wJoyError);
    }
    fwrite(JoyDemoFileHeader,1,16,DataDemoFile);
    fwrite(&JoyLib_ulDemoRecordCount,1,sizeof(u_long),DataDemoFile);
    fwrite(JoyLib_pstDemoRecordBuffer,sizeof(tdstJoystickRecord),JoyLib_ulDemoRecordCount,DataDemoFile);
    fclose(DataDemoFile);
    if ( JoyLib_pstDemoRecordBuffer != NULL )
        M_INOFree(JoyLib_pstDemoRecordBuffer);
    JoyLib_pstDemoRecordBuffer = NULL;
    JoyLib_wMode = C_wJoyNormalMode;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*****************************************************************
  This function return the number of frame of the demo sequence.
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
u_long INO_fn_ulGetJoystickDemoFrameCount()
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( JoyLib_wMode == C_wJoyDemoRecordingMode)
    {
        ulJoystickErrorCode = C_ErrNotInJoyRecordingMode;
        return (C_wJoyError);
    }
    return (JoyLib_ulDemoRecordCount);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*****************************************************************
  This function start the demo playing mode.
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wStartJoystickDemo(char *p_szFileName)
{
    char    header[18];
    u_long  ulStepRead;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( JoyLib_wMode != C_wJoyNormalMode )
    {
        ulJoystickErrorCode = C_ErrNotInJoyNormalMode;
        return (C_wJoyError);
    }
 
    DataDemoFile = fopen(p_szFileName,"rb");
    if ( DataDemoFile == NULL )
    {
        ulJoystickErrorCode = C_ErrJoyDemoFileError;
        return (C_wJoyError);
    }
 
    fread(header,1,16,DataDemoFile);
    if ( strncmp(header,JoyDemoFileHeader,16) != 0 )
    {
        ulJoystickErrorCode = C_ErrJoyDemoFileError;
        fclose(DataDemoFile);
        return (C_wJoyError);
    }
 
    ulStepRead = fread(&JoyLib_ulDemoRecordCount,sizeof(u_long),1,DataDemoFile);
    if ( ulStepRead != 1 )
    {
        ulJoystickErrorCode = C_ErrJoyDemoFileError;
        fclose(DataDemoFile);
        return (C_wJoyError);
    }
 
    /*JoyLib_pstDemoRecordBuffer = GlobalAlloc(GPTR,sizeof(tdstJoystickRecord)*JoyLib_ulDemoRecordCount);*/
	M_INOAlloc(JoyLib_pstDemoRecordBuffer, tdstJoystickRecord*, sizeof(tdstJoystickRecord)*JoyLib_ulDemoRecordCount );
    if ( JoyLib_pstDemoRecordBuffer == NULL )
        return (C_wJoyError);
 
    ulStepRead = fread(JoyLib_pstDemoRecordBuffer,sizeof(tdstJoystickRecord),JoyLib_ulDemoRecordCount,DataDemoFile);
    if ( ulStepRead == 0 )
    {
        if ( JoyLib_pstDemoRecordBuffer != NULL )
            M_INOFree(JoyLib_pstDemoRecordBuffer);
        JoyLib_pstDemoRecordBuffer = NULL;
        ulJoystickErrorCode = C_ErrJoyDemoFileError;
        DataDemoFile = NULL;
        JoyLib_ulDemoRecordCount = 0;
        fclose(DataDemoFile);
        return (C_wJoyError);
    }
    fclose(DataDemoFile);
 
    JoyLib_ulDemoRecordCurrent = 0;
    JoyLib_wMode = C_wJoyDemoPlayingMode;
    return (0);
}
 
 
/*****************************************************************
  This function quit the demo playing mode.
  You quit the demo playing mode by pushing any one of the button.
******************************************************************/
short INO_fn_wStopJoystickDemo()
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( JoyLib_wMode != C_wJoyDemoPlayingMode)
    {
        ulJoystickErrorCode = C_ErrNotInJoyPlayingMode;
        return (C_wJoyError);
    }
    if ( JoyLib_pstDemoRecordBuffer != NULL )
        M_INOFree(JoyLib_pstDemoRecordBuffer);
    JoyLib_pstDemoRecordBuffer = NULL;
    JoyLib_wMode = C_wJoyNormalMode;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
 
/*****************************************************************
  This function return the current joystick mode.(normal,demo
  recording or demo playing).
******************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickCurrentMode()
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    return (JoyLib_wMode);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
 
/*****************************************************************
  Joystick query function
*****************************************************************/
 
/******************************************************************
  short  fn_wJoyReadJoystick(): read all connected joystick.
******************************************************************/
/*short INO_fn_wReadJoystick(short wReadMode)*/
/*{*/
/*    u_short     i,j;*/
/*    short       wResult;*/
 
/*    JoyLib_tdstGp.m_ulTimeCount++;*/
 
/*    /* Check if the gameport is Ok   */
/*    INO_fn_wUpdateGamePort();*/
 
/*    if ( JoyLib_tdstGp.m_wGameportOk == 0 )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrDriverNotInitialized;*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    for ( i = 0 ; i < JoyLib_tdstGp.m_wNbMaxiJoystick ; i++)*/
/*    {*/
/*        if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << i)) != 0 )*/
/*        {*/
/*            /* If the joystick isn't connected and the function mode is C_wJoyReadConnected   */
/*            /* skip this joystick   */
/*            if ( (wReadMode == C_wJoyReadConnected) && (JoyLib_tdstJC[i]->m_uwType == C_wJoyNone) )*/
/*                break;*/
 
/*            wResult = fn_wJoyGetJoystickState(i, JoyLib_tdstJS[i], JoyLib_tdstJC[i] );*/
/*            if ( wResult == C_wJoyError )*/
/*                JoyLib_tdstJC[i]->m_uwType = C_wJoyNone;*/
 
/*            /* If we are currently in play demo mode   */
/*            /* Any button press stop the demo playing mode   */
/*            if ( JoyLib_wMode == C_wJoyDemoPlayingMode )*/
/*            {*/
/*                if ( (JoyLib_tdstJS[i]->m_ulButton != 0) || (JoyLib_ulDemoRecordCurrent >= JoyLib_ulDemoRecordCount) )*/
/*                    INO_fn_wStopJoystickDemo();*/
/*                else*/
/*                {*/
/*                    if ( i == DemoRecordJoystick )*/
/*                    {*/
/*                        tdstJoystickAxisValues* p_stTmp ,* p_stTmp2;*/
/*                        JoyLib_tdstJS[i]->m_ulButton     = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulButton;*/
/*                        JoyLib_tdstJS[i]->m_ulDirection  = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulDirection;*/
/*                        p_stTmp = &(JoyLib_tdstJS[i]->m_stAxisValues);*/
/*                        p_stTmp2 = &(JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_stAxisValues);*/
/*                        M_JoyCopyAxisValues(p_stTmp, p_stTmp2);*/
/*                        /**/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwXValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulXValue;*/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwYValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulYValue;*/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwZValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulZValue;*/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwRValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulRValue;*/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwUValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulUValue;*/
/*                        JoyLib_tdstJS[i]->m_stAxisValues.m_uwVValue = JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCurrent].m_ulVValue;*/
/*                          */
/*                        JoyLib_ulDemoRecordCurrent++;*/
/*                    }*/
/*                }*/
/*            }*/
 
/*            /* In normal mode or record mode, analyse joystick  */
/*            /* Check the activate directions   */
/*            else if ( wResult != C_wJoyError )*/
/*                fn_vJoyUpdateDirection(i, JoyLib_tdstJS[i], JoyLib_tdstJC[i] );*/
 
/*            /* If we are currently in Record demo mode   */
/*            if ( JoyLib_wMode == C_wJoyDemoRecordingMode )*/
/*            {*/
/*                if ( ((JoyLib_tdstJS[i]->m_ulExitRecordingModeButton & JoyLib_tdstJS[i]->m_ulButton) != 0)*/
/*                    || (JoyLib_ulDemoRecordCount >= JoyLib_ulDemoMaxStep) )*/
/*                    INO_fn_wStopRecordingJoystick();*/
/*                else*/
/*                {*/
/*                    if ( i == DemoRecordJoystick )*/
/*                    {*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulButton = JoyLib_tdstJS[i]->m_ulButton;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulDirection = JoyLib_tdstJS[i]->m_ulDirection;*/
/*                        M_JoyCopyAxisValues(*/
/*                                            &(JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_stAxisValues),*/
/*                                            &(JoyLib_tdstJS[i]->m_stAxisValues)*/
/*                                           );*/
/*                        /**/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulXValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwXValue;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulYValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwYValue;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulZValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwZValue;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulRValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwRValue;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulUValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwUValue;*/
/*                        JoyLib_pstDemoRecordBuffer[JoyLib_ulDemoRecordCount].m_ulVValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwVValue;*/
/*                          */
/*                        JoyLib_ulDemoRecordCount++;*/
/*                    }*/
/*                }*/
/*            }*/
 
/*            /* Update the historic   */
/*            if ( wResult != C_wJoyError )*/
/*            {*/
/*                if ( JoyLib_tdstJS[i]->m_uwValidRecordNumber == 0 )*/
/*                {*/
/*                    JoyLib_tdstH[i][0].m_ulDirection = JoyLib_tdstJS[i]->m_ulDirection;*/
/*                    JoyLib_tdstH[i][0].m_ulButton = JoyLib_tdstJS[i]->m_ulButton;*/
/*                    M_JoyCopyAxisValues(*/
/*                                        &(JoyLib_tdstH[i][0].m_stAxisValues),*/
/*                                        &(JoyLib_tdstJS[i]->m_stAxisValues)*/
/*                                       );*/
/*                    /**/
/*                    JoyLib_tdstH[i][0].m_ulXValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwXValue;*/
/*                    JoyLib_tdstH[i][0].m_ulYValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwYValue;*/
/*                    JoyLib_tdstH[i][0].m_ulZValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwZValue;*/
/*                    JoyLib_tdstH[i][0].m_ulRValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwRValue;*/
/*                    JoyLib_tdstH[i][0].m_ulUValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwUValue;*/
/*                    JoyLib_tdstH[i][0].m_ulVValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwVValue;*/
/*                      */
/*                    /*JoyLib_tdstH[i][0].m_ulTimeCount = JoyLib_tdstGp.m_ulTimeCount;  */
/*                    JoyLib_tdstJS[i]->m_ulLastTimeCount = JoyLib_tdstGp.m_ulTimeCount;*/
/*                    JoyLib_tdstJS[i]->m_uwValidRecordNumber = 1;*/
/*                }*/
/*                else*/
/*                {*/
/*                    j = JoyLib_tdstJS[i]->m_uwHistoryHead;*/
/*                    /*    if ( (JoyLib_tdstH[i][j].m_ulDirection != JoyLib_tdstJS[i]->m_ulDirection) ||*/
/*                    (JoyLib_tdstH[i][j].m_ulButton    != JoyLib_tdstJS[i]->m_ulButton) )*/
/*                    {*/
/*                      */
/*                    j++;*/
/*                    if ( j > JoyLib_tdstGp.m_wHistorySize-1 ) j = 0;*/
/*                    JoyLib_tdstH[i][j].m_ulDirection = JoyLib_tdstJS[i]->m_ulDirection;*/
/*                    JoyLib_tdstH[i][j].m_ulButton = JoyLib_tdstJS[i]->m_ulButton;*/
/*                    M_JoyCopyAxisValues(*/
/*                                        &(JoyLib_tdstH[i][j].m_stAxisValues),*/
/*                                        &(JoyLib_tdstJS[i]->m_stAxisValues)*/
/*                                       );*/
/*                    /**/
/*                    JoyLib_tdstH[i][j].m_ulXValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwXValue;*/
/*                    JoyLib_tdstH[i][j].m_ulYValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwYValue;*/
/*                    JoyLib_tdstH[i][j].m_ulZValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwZValue;*/
/*                    JoyLib_tdstH[i][j].m_ulRValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwRValue;*/
/*                    JoyLib_tdstH[i][j].m_ulUValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwUValue;*/
/*                    JoyLib_tdstH[i][j].m_ulVValue = JoyLib_tdstJS[i]->m_stAxisValues.m_uwVValue;*/
/*                      */
/*                    /*JoyLib_tdstH[i][j].m_ulTimeCount = JoyLib_tdstGp.m_ulTimeCount;  */
/*                    JoyLib_tdstJS[i]->m_ulLastTimeCount = JoyLib_tdstGp.m_ulTimeCount;*/
/*                    JoyLib_tdstJS[i]->m_uwHistoryHead = j;*/
/*                    if ( JoyLib_tdstJS[i]->m_uwValidRecordNumber != JoyLib_tdstGp.m_wHistorySize )*/
/*                        JoyLib_tdstJS[i]->m_uwValidRecordNumber++;*/
/*                }*/
/*            }*/
/*        }*/
/*    }*/
/*    return (0);*/
/*}*/
 
 
/**************************************************************
  fn_wJoyAddActiveJoystick
**************************************************************/
short INO_fn_wAddActiveJoystick(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( JoyLib_tdstJC[wJoystickNum] == NULL )
    {
        ulJoystickErrorCode = C_ErrJoystickNotAllocated;
        return (C_wJoyError);
    }
    JoyLib_tdstGp.m_uwActiveFlag |= 1 << wJoystickNum;
    return (0);
}
 
 
/**************************************************************
  fn_wJoyRemoveActiveJoystick
**************************************************************/
short INO_fn_wRemoveActiveJoystick(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    JoyLib_tdstGp.m_uwActiveFlag &= ~(1 << wJoystickNum);
    return (0);
}
 
 
/**************************************************************
  fn_wJoyIsLeft
**************************************************************/
short INO_fn_wIsJoystickLeft(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyLeft) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyIsRight
**************************************************************/
short INO_fn_wIsJoystickRight(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyRight) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
 
/**************************************************************
  fn_wJoyIsUp
**************************************************************/
short INO_fn_wIsJoystickUp(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyUp) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyIsDown
**************************************************************/
short INO_fn_wIsJoystickDown(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyDown) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyIsZUp
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wIsJoystickZUp(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJC[wJoystickNum]->m_ulCapsFlag & C_ulJoyZ) == 0)
    {
        ulJoystickErrorCode = C_ErrFeatureNotAvailable;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyZUp) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyIsZDown
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wIsJoystickZDown(short wJoystickNum)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJC[wJoystickNum]->m_ulCapsFlag & C_ulJoyZ) == 0)
    {
        ulJoystickErrorCode = C_ErrFeatureNotAvailable;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyZDown) != 0 )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyJustLeft
**************************************************************/
short INO_fn_wJoystickJustLeft(short wJoystickNum)
{
    short wIndex;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyLeft) != 0 )
    {
        wIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if ( wIndex < 0)
            wIndex += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][wIndex].m_ulDirection & C_ulJoyLeft) == 0 )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyJustRight
**************************************************************/
short INO_fn_wJoystickJustRight(short wJoystickNum)
{
    short wIndex;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyRight) != 0 )
    {
        wIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if ( wIndex < 0)
            wIndex += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][wIndex].m_ulDirection & C_ulJoyRight) == 0 )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyJustUp
**************************************************************/
short INO_fn_wJoystickJustUp(short wJoystickNum)
{
    short wIndex;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyUp) != 0 )
    {
        wIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if ( wIndex < 0)
            wIndex += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][wIndex].m_ulDirection & C_ulJoyUp) == 0 )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyJustDown
**************************************************************/
short INO_fn_wJoystickJustDown(short wJoystickNum)
{
    short wIndex;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & C_ulJoyDown) != 0 )
    {
        wIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if ( wIndex < 0)
            wIndex += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][wIndex].m_ulDirection & C_ulJoyDown) == 0 )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  Return the current position of the joystick along the specified axis
  The return value is a double contained between -1.0 to 1.0.
**************************************************************/
/*short INO_fn_wGetJoystickCurrentPosition(short wJoystickNum, short wAxis, double *pdfPosition)*/
/*{*/
/*    double  dfPosition, dfMiddle, dfRange;*/
 
/*    if ( JoyLib_tdstGp.m_wGameportOk == 0 )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrDriverNotInitialized;*/
/*        return (C_wJoyError);*/
/*    }*/
/*    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrBadJoystick;*/
/*        return (C_wJoyError);*/
/*    }*/
/*    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrJoystickNotActive;*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    switch (wAxis)*/
/*    {*/
/*    case C_ulJoyX:*/
/*        dfPosition = (double)M_JoyX(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyXMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyXMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyXMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyXMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyY:*/
/*        dfPosition = (double)M_JoyY(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyYMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyYMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyYMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyYMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyZ:*/
/*        dfPosition = (double)M_JoyZ(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyZMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyZMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyZMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyZMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyR:*/
/*        dfPosition = (double)M_JoyR(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyRMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyRMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyRMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyRMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyU:*/
/*        dfPosition = (double)M_JoyU(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyUMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyUMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyUMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyUMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyV:*/
/*        dfPosition = (double)M_JoyV(JoyLib_tdstJS[wJoystickNum]);*/
/*        dfRange = (double)(M_JoyVMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyVMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyVMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyVMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    default:*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    dfPosition -= dfMiddle;*/
/*    *pdfPosition = dfPosition / dfRange;*/
 
/*    return (0);*/
/*}*/
 
 
/**************************************************************
  fn_wJoyIsButtonPressed
**************************************************************/
short INO_fn_wIsJoystickButtonPressed(short wJoystickNum, u_long ulButtonFlag)
{
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & ((1 << JoyLib_tdstJC[wJoystickNum]->m_uwNbButton) - 1)) != ulButtonFlag )
    {
        ulJoystickErrorCode = C_ErrBadButtonFlag;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & JoyLib_tdstJS[wJoystickNum]->m_ulButton) == ulButtonFlag )
        return (C_wJoyTrue);
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  INO_fn_wJoystickButtonJustPressed
**************************************************************/
short INO_fn_wJoystickButtonJustPressed(short wJoystickNum, u_long ulButtonFlag)
{
    long   index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & ((1 << JoyLib_tdstJC[wJoystickNum]->m_uwNbButton) - 1)) != ulButtonFlag )
    {
        ulJoystickErrorCode = C_ErrBadButtonFlag;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (ulButtonFlag & JoyLib_tdstJS[wJoystickNum]->m_ulButton) == ulButtonFlag )
    {
        index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if (index < 0) index += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][index].m_ulButton & ulButtonFlag) == 0 )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
/**************************************************************
  INO_fn_wJoystickButtonJustReleased
**************************************************************/
short INO_fn_wJoystickButtonJustReleased(short wJoystickNum, u_long ulButtonFlag)
{
    long   index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & ((1 << JoyLib_tdstJC[wJoystickNum]->m_uwNbButton) - 1)) != ulButtonFlag )
    {
        ulJoystickErrorCode = C_ErrBadButtonFlag;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (ulButtonFlag & JoyLib_tdstJS[wJoystickNum]->m_ulButton) == 0 )
    {
        index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if (index < 0) index += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][index].m_ulButton & ulButtonFlag) == ulButtonFlag )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
 
/**************************************************************
  fn_wJoyCurrentDirection
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickCurrentDirection(short wJoystickNum, u_long *p_ulDirectionFlag)
{
    *p_ulDirectionFlag = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    *p_ulDirectionFlag = JoyLib_tdstJS[wJoystickNum]->m_ulDirection;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  INO_fn_wJoystickDirectionJustReleased
**************************************************************/
short INO_fn_wJoystickDirectionJustReleased(short wJoystickNum, u_long ulDirectionFlag)
{
    short wIndex;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
 
    /* Only the current state is available */
    if ( JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber < 2 )
        return (C_wJoyFalse);
 
    if ( (JoyLib_tdstJS[wJoystickNum]->m_ulDirection & ulDirectionFlag) == 0 )
    {
        wIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - 1;
        if (wIndex < 0) wIndex += JoyLib_tdstGp.m_wHistorySize;
        if ( (JoyLib_tdstH[wJoystickNum][wIndex].m_ulDirection & ulDirectionFlag) == ulDirectionFlag )
            return (C_wJoyTrue);
        else
            return (C_wJoyFalse);
    }
    else
        return (C_wJoyFalse);
}
 
 
/**************************************************************
  fn_wJoyCurrentButton
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickCurrentButton(short wJoystickNum, u_long *p_ulButtonFlag)
{
    *p_ulButtonFlag = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    *p_ulButtonFlag = JoyLib_tdstJS[wJoystickNum]->m_ulButton;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasLeft
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickLeft(short wJoystickNum, short wHistoryNum)
{
    long   index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyLeft) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasRight
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickRight(short wJoystickNum, short wHistoryNum)
{
    long   index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyRight) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasUp
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickUp(short wJoystickNum, short wHistoryNum)
{
    long   index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyUp) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasDown
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickDown(short wJoystickNum, short wHistoryNum)
{
    long index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyDown) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasZUp
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickZUp(short wJoystickNum, short wHistoryNum)
{
    long index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJC[wJoystickNum]->m_ulCapsFlag & C_ulJoyZ) == 0)
    {
        ulJoystickErrorCode = C_ErrFeatureNotAvailable;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyZUp) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasZDown
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickZDown(short wJoystickNum, short wHistoryNum)
{
    long index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstJC[wJoystickNum]->m_ulCapsFlag & C_ulJoyZ) == 0)
    {
        ulJoystickErrorCode = C_ErrFeatureNotAvailable;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulDirection & C_ulJoyZDown) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyWasPressed
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wWasJoystickButtonPressed(short wJoystickNum, u_long ulButtonFlag, short wHistoryNum)
{
    long index;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( (ulButtonFlag & (( 1 << JoyLib_tdstJC[wJoystickNum]->m_uwNbButton) - 1)) != ulButtonFlag )
    {
        ulJoystickErrorCode = C_ErrBadButtonFlag;
        return (C_wJoyError);
    }
    if ( wHistoryNum >= JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    if ( (JoyLib_tdstH[wJoystickNum][index].m_ulButton & ulButtonFlag) == 0 )
        return (C_wJoyFalse);
    else
        return (C_wJoyTrue);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyPreviousDirection
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickPreviousDirection(short wJoystickNum, short wHistoryNum, u_long *p_ulDirectionFlag)
{
    long index;
 
    *p_ulDirectionFlag = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum > JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    *p_ulDirectionFlag = JoyLib_tdstH[wJoystickNum][index].m_ulDirection;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  fn_wJoyPreviousButton
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickPreviousButton(short wJoystickNum, short wHistoryNum, u_long *p_ulButtonFlag)
{
    long index;
 
    *p_ulButtonFlag = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum > JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    *p_ulButtonFlag = JoyLib_tdstH[wJoystickNum][index].m_ulButton;
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  Return the current position of the joystick along the specified axis
  The return value is a double contained between -1.0 to 1.0.
**************************************************************/
/*#ifndef _FIRE_DEADCODE_U64_*/
/*short INO_fn_wGetJoystickPreviousPosition(short wJoystickNum, short wAxis, short wHistoryNum, double *pdfPosition)*/
/*{*/
/*    long   iIndex;*/
/*    double dfPosition,dfRange,dfMiddle;*/
 
/*    if ( JoyLib_tdstGp.m_wGameportOk == 0 )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrDriverNotInitialized;*/
/*        return (C_wJoyError);*/
/*    }*/
/*    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrBadJoystick;*/
/*        return (C_wJoyError);*/
/*    }*/
/*    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrJoystickNotActive;*/
/*        return (C_wJoyError);*/
/*    }*/
/*    if ( wHistoryNum > JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )*/
/*    {*/
/*        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    iIndex = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;*/
/*    switch (wAxis)*/
/*    {*/
/*    case C_ulJoyX:*/
/*        dfPosition = (double)M_JoyX(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyXMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyXMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyXMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyXMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyY:*/
/*        dfPosition = (double)M_JoyY(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyYMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyYMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyYMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyYMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyZ:*/
/*        dfPosition = (long)M_JoyZ(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyZMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyZMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyZMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyZMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyR:*/
/*        dfPosition = (double)M_JoyR(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyRMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyRMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyRMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyRMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyU:*/
/*        dfPosition = (double)M_JoyU(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyUMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyUMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyUMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyUMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    case C_ulJoyV:*/
/*        dfPosition = (double)M_JoyV(&(JoyLib_tdstH[wJoystickNum][iIndex]));*/
/*        dfRange = (double)(M_JoyVMax(JoyLib_tdstJC[wJoystickNum]) - M_JoyVMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        dfMiddle = (double)(M_JoyVMax(JoyLib_tdstJC[wJoystickNum]) + M_JoyVMin(JoyLib_tdstJC[wJoystickNum])) / 2.0;*/
/*        break;*/
/*    default:*/
/*        return (C_wJoyError);*/
/*    }*/
 
/*    dfPosition -= dfMiddle;*/
/*    *pdfPosition = dfPosition / dfRange;*/
 
/*    return (0);*/
/*}*/
/*#endif /* _FIRE_DEADCODE_U64_   */
 
/**************************************************************
  fn_wJoyInactivityTime
**************************************************************/
/*
short fn_wJoyInactivityTime(short wJoystickNum, u_long *p_ulLaps )
{
    *p_ulLaps = 0;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    *p_ulLaps = JoyLib_tdstGp.m_ulTimeCount - JoyLib_tdstJS[wJoystickNum]->m_ulLastTimeCount;
    return (0);
}
*/
 
/**************************************************************
  fn_wJoyElapsedTime
**************************************************************/
/*
short fn_wJoyElapsedTime(short wJoystickNum, short wHistoryNum, u_long *p_ulLaps )
{
    long index, j;
 
    if ( JoyLib_tdstGp.m_wGameportOk == 0 )
    {
        ulJoystickErrorCode = C_ErrDriverNotInitialized;
        return (C_wJoyError);
    }
    if ( (wJoystickNum < C_wJoystick1) || (wJoystickNum > C_wJoystick16) )
    {
        ulJoystickErrorCode = C_ErrBadJoystick;
        return (C_wJoyError);
    }
    if ( (JoyLib_tdstGp.m_uwActiveFlag & (1 << wJoystickNum)) == 0 )
    {
        ulJoystickErrorCode = C_ErrJoystickNotActive;
        return (C_wJoyError);
    }
    if ( wHistoryNum > JoyLib_tdstJS[wJoystickNum]->m_uwValidRecordNumber )
    {
        ulJoystickErrorCode = C_ErrJoyHistoricOverflow;
        return (C_wJoyError);
    }
 
    index = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead - wHistoryNum;
    if ( index < 0)
        index += JoyLib_tdstGp.m_wHistorySize;
    j = JoyLib_tdstJS[wJoystickNum]->m_uwHistoryHead;
    *p_ulLaps = JoyLib_tdstH[wJoystickNum][j].m_ulTimeCount - JoyLib_tdstH[wJoystickNum][index].m_ulTimeCount;
    return (0);
}
*/
 
 
/**************************************************************
  This function returns a pointer to the last error message string.
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
short INO_fn_wGetJoystickErrorString(char **pp_szErrorString)
{
    u_long i;
 
    i = ulJoystickErrorCode - C_FirstErrorJoystick;
    *pp_szErrorString = JoyErrorString[i];
    return (0);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/**************************************************************
  This function returns the last error code.
**************************************************************/
#ifndef _FIRE_DEADCODE_U64_
u_long INO_fn_wJGetLastJoystickErrorCode()
{
    return(ulJoystickErrorCode);
}
#endif /* _FIRE_DEADCODE_U64_ */
