Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS (XINPUT) Input - GamePad.c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

#pragma warning( push, 3 )
#include <windows.h>
#include <xinput.h>
#pragma warning( pop )


/* == Input ============================================================================================================================================================= */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions and Data                                                                                                                                         */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

static struct STg2_IN_GamePad
{
    TgFLOAT32                                   m_afData[2][ETgPROP_GAMEPAD_MAX];
    TgUINT32                                    m_uiState;
    TgUINT32                                    m_uiBuffer;
    TgUINT32                                    m_uiPacket;
    TgBOOL                                      m_bValid;
}                                           s_asGamePad[KTgCONTROLLER_GAMEPAD_MAX];

TgFORCEINLINE TgFLOAT32                     tgIN_Convert_Thumbstick_Value( SHORT, C_TgFLOAT32 );
static TgRESULT                             tgIN_PM_Rumble_GamePad( C_ETgCONTROLLER );




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Internal Functions                                                                                                                                                    */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgIN_PM_Init_GamePad -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Init_GamePad( TgVOID )
{
    memset( s_asGamePad, 0x00, sizeof( s_asGamePad ) );

    return (KTgS_OK);
}


/* ---- tgIN_PM_Boot_GamePad -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Boot_GamePad( TgVOID )
{
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_pfnProduce = tgIN_PM_Update_GamePad;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_enController = ETgCONTROLLER_GAMEPAD_0;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_psConsumer = nullptr;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_0].m_fLt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_0].m_fRt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_0].m_pfnRumble = tgIN_PM_Rumble_GamePad;

    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_pfnProduce = tgIN_PM_Update_GamePad;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_enController = ETgCONTROLLER_GAMEPAD_1;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_psConsumer = nullptr;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_1].m_fLt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_1].m_fRt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_1].m_pfnRumble = tgIN_PM_Rumble_GamePad;

    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_pfnProduce = tgIN_PM_Update_GamePad;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_enController = ETgCONTROLLER_GAMEPAD_2;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_psConsumer = nullptr;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_2].m_fLt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_2].m_fRt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_2].m_pfnRumble = tgIN_PM_Rumble_GamePad;

    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_pfnProduce = tgIN_PM_Update_GamePad;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_enController = ETgCONTROLLER_GAMEPAD_3;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_psConsumer = nullptr;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_3].m_fLt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_3].m_fRt = 0.0F;
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_3].m_pfnRumble = tgIN_PM_Rumble_GamePad;

    return (KTgS_OK);
}


/* ---- tgIN_PM_Stop_GamePad -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Stop_GamePad( TgVOID )
{
    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_3].m_pfnRumble = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_pfnProduce = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_enController = ETgCONTROLLER_MAX;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_3].m_psConsumer = nullptr;

    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_2].m_pfnRumble = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_pfnProduce = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_enController = ETgCONTROLLER_MAX;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_2].m_psConsumer = nullptr;

    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_1].m_pfnRumble = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_pfnProduce = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_enController = ETgCONTROLLER_MAX;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_1].m_psConsumer = nullptr;

    g_asController_Rumble[ETgCONTROLLER_GAMEPAD_0].m_pfnRumble = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_pfnProduce = nullptr;
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_enController = ETgCONTROLLER_MAX;
    tgCM_UTM_LA_RB_SRSW__Init( &g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_sLA_RB_SRSW, KTgCONTROLLER_MAX_EVENT );
    g_asController_Active[ETgCONTROLLER_GAMEPAD_0].m_psConsumer = nullptr;

    return (KTgS_OK);
}


/* ---- tgIN_PM_Free_GamePad -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Free_GamePad( TgVOID )
{
    return (KTgS_OK);
}


/* ---- tgIN_PM_Update_GamePad ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_PM_Update_GamePad( C_ETgCONTROLLER enController )
{
    C_TgUINT32                          uiID = (TgUINT32)(enController - ETgCONTROLLER_GAMEPAD_0);
    C_TgUINT32                          uiNext = (s_asGamePad[uiID].m_uiBuffer & 1) ^ 1;
    C_TgUINT32                          uiPrev = (s_asGamePad[uiID].m_uiBuffer & 1);
    TgUINT32                            uiIndex;
    TgUINT32                            uiEvent;
    TgUINT32                            uiState = ETgSTATE_GAMEPAD_VALID;
    XINPUT_STATE                        xState;
    TgBOOL                              bValid;
    TgUINT32                            wButtons;

    TgERROR( uiID < KTgCONTROLLER_GAMEPAD_MAX );
    s_asGamePad[uiID].m_uiBuffer = uiNext; /* Advance the buffer id value (this is the device not the event buffer). */

    /* Check the state of the game pad - this determines if its connected to the machine. */
    bValid = 0L == g_pfnXInputGetState( uiID, &xState );

    if (bValid && bValid != s_asGamePad[uiID].m_bValid)
    {
        XINPUT_CAPABILITIES                 xInput;

        if (bValid &= (0L == g_pfnXInputGetCapabilities( uiID, XINPUT_FLAG_GAMEPAD, &xInput )))
        {
            memset( s_asGamePad[uiID].m_afData[uiPrev], 0x00, sizeof( s_asGamePad[uiID].m_afData[0] ) );
            s_asGamePad[uiID].m_uiBuffer = 0;
            s_asGamePad[uiID].m_uiPacket = 0;
            g_fGamePad_Stick_Dead_Zone[uiID][0] = g_fDefault_GamePad_Stick_0_X_Dead_Zone;
            g_fGamePad_Stick_Dead_Zone[uiID][1] = g_fDefault_GamePad_Stick_1_X_Dead_Zone;
            g_fGamePad_Stick_Dead_Zone[uiID][2] = g_fDefault_GamePad_Stick_0_Y_Dead_Zone;
            g_fGamePad_Stick_Dead_Zone[uiID][3] = g_fDefault_GamePad_Stick_1_Y_Dead_Zone;
        };
    };

    if (bValid != s_asGamePad[uiID].m_bValid)
    {
        while (0 == tgCM_UTM_LA_RB_SRSW__Query_Free_Size( &g_asController_Active[enController].m_sLA_RB_SRSW ))
        {
            tgTR_Yield();
        };

        uiEvent = (TgUINT32)tgCM_UTM_LA_RB_SRSW__Push_Next_Index( &g_asController_Active[enController].m_sLA_RB_SRSW );

        g_asController_Active[enController].m_asEvent[uiEvent].m_enController = enController;
        g_asController_Active[enController].m_asEvent[uiEvent].m_uiState.GamePad = bValid ? ETgSTATE_GAMEPAD_VALID : 0;
        g_asController_Active[enController].m_asEvent[uiEvent].m_enProp.GamePad = ETgPROP_GAMEPAD_MAX;
        g_asController_Active[enController].m_asEvent[uiEvent].m_uiUTF32 = 0;
        g_asController_Active[enController].m_asEvent[uiEvent].m_fValue = 0.0F;
        g_asController_Active[enController].m_asEvent[uiEvent].m_fDiff = 0.0F;

        tgCM_UTM_LA_RB_SRSW__Push_Commit( &g_asController_Active[enController].m_sLA_RB_SRSW );

        /* Set the new state of the game pad */
        s_asGamePad[uiID].m_bValid = bValid;
    };

    if (!bValid || s_asGamePad[uiID].m_uiPacket == xState.dwPacketNumber)
    {
        return;
    };

    wButtons = xState.Gamepad.wButtons;

    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_A] = 0 != (wButtons & XINPUT_GAMEPAD_A) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_B] = 0 != (wButtons & XINPUT_GAMEPAD_B) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_X] = 0 != (wButtons & XINPUT_GAMEPAD_X) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_Y] = 0 != (wButtons & XINPUT_GAMEPAD_Y) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_DPAD_UP] = 0 != (wButtons & XINPUT_GAMEPAD_DPAD_UP) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_DPAD_DOWN] = 0 != (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_DPAD_LEFT] = 0 != (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_DPAD_RIGHT] = 0 != (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_LSHOULDER] = 0 != (wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_RSHOULDER] = 0 != (wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_LTHUMB] = 0 != (wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_RTHUMB] = 0 != (wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_START] = 0 != (wButtons & XINPUT_GAMEPAD_START) ? 1.0F : 0.0F;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_BACK] = 0 != (wButtons & XINPUT_GAMEPAD_BACK) ? 1.0F : 0.0F;

    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_LTRIGGER] = (TgFLOAT32)xState.Gamepad.bLeftTrigger / (TgFLOAT32)KTgMAX_U08;
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_RTRIGGER] = (TgFLOAT32)xState.Gamepad.bRightTrigger / (TgFLOAT32)KTgMAX_U08;

    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_LTHUMB_X] = tgIN_Convert_Thumbstick_Value( xState.Gamepad.sThumbLX, g_fGamePad_Stick_Dead_Zone[uiID][0] );
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_LTHUMB_Y] = tgIN_Convert_Thumbstick_Value( xState.Gamepad.sThumbLY, g_fGamePad_Stick_Dead_Zone[uiID][1] );
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_RTHUMB_X] = tgIN_Convert_Thumbstick_Value( xState.Gamepad.sThumbRX, g_fGamePad_Stick_Dead_Zone[uiID][2] );
    s_asGamePad[uiID].m_afData[uiNext][ETgPROP_GAMEPAD_RTHUMB_Y] = tgIN_Convert_Thumbstick_Value( xState.Gamepad.sThumbRY, g_fGamePad_Stick_Dead_Zone[uiID][3] );

    s_asGamePad[uiID].m_uiPacket = xState.dwPacketNumber;

    if (nullptr == g_asController_Active[enController].m_psConsumer)
    {
        return;
    };

    /* Generate events based on the change between this state and the previous state. */
    for (uiIndex = 0; uiIndex < ETgPROP_GAMEPAD_MAX; ++uiIndex)
    {
        if (
            ((uiIndex <  ETgPROP_GAMEPAD_LTRIGGER) && (s_asGamePad[uiID].m_afData[0][uiIndex] == s_asGamePad[uiID].m_afData[1][uiIndex])) ||
            ((uiIndex >= ETgPROP_GAMEPAD_LTRIGGER) && (!(s_asGamePad[uiID].m_afData[uiNext][uiIndex] > 0.0F)))
            )
        {
            continue;
        };

        while (0 == tgCM_UTM_LA_RB_SRSW__Query_Free_Size( &g_asController_Active[enController].m_sLA_RB_SRSW ))
        {
            tgTR_Yield();
        };

        uiEvent = (TgUINT32)tgCM_UTM_LA_RB_SRSW__Push_Next_Index( &g_asController_Active[enController].m_sLA_RB_SRSW );

        g_asController_Active[enController].m_asEvent[uiEvent].m_enController = enController;
        g_asController_Active[enController].m_asEvent[uiEvent].m_uiState.GamePad = uiState;
        g_asController_Active[enController].m_asEvent[uiEvent].m_enProp.GamePad = (ETgPROP_GAMEPAD)uiIndex;
        g_asController_Active[enController].m_asEvent[uiEvent].m_uiUTF32 = 0;
        g_asController_Active[enController].m_asEvent[uiEvent].m_fValue = s_asGamePad[uiID].m_afData[uiNext][uiIndex];
        g_asController_Active[enController].m_asEvent[uiEvent].m_fDiff = s_asGamePad[uiID].m_afData[uiNext][uiIndex] - s_asGamePad[uiID].m_afData[uiPrev][uiIndex];

        tgCM_UTM_LA_RB_SRSW__Push_Commit( &g_asController_Active[enController].m_sLA_RB_SRSW );
    };
}


/* ---- tgIN_PM_Set_GamePad_Rumble -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Set_GamePad_Rumble( C_ETgCONTROLLER enController, C_TgFLOAT32 fLt, C_TgFLOAT32 fRt )
{
    XINPUT_VIBRATION                    dxVibration;

    memset( &dxVibration, 0x00, sizeof( XINPUT_VIBRATION ) );

    dxVibration.wLeftMotorSpeed = (WORD)(65535.0F*tgCM_MIN_F32( 1.0F, fLt ));
    dxVibration.wRightMotorSpeed = (WORD)(65535.0F*tgCM_MIN_F32( 1.0F, fRt ));

    g_pfnXInputSetState( enController - ETgCONTROLLER_GAMEPAD_0, &dxVibration );

    return (KTgS_OK);
}


/* ---- tgIN_PM_Query_Fixed_Memory_GamePad ------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgIN_PM_Query_Fixed_Memory_GamePad( TgVOID )
{
    return (0
        + sizeof( s_asGamePad )
    );
}




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions                                                                                                                                                  */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgIN_Convert_Thumbstick_Value ----------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFORCEINLINE TgFLOAT32 tgIN_Convert_Thumbstick_Value( SHORT sThumbstickValue, C_TgFLOAT32 fDeadZone )
{
    C_TgFLOAT32                         fValue = sThumbstickValue / (TgFLOAT32)KTgMAX_S16;

    return (tgPM_FSEL_F32( fValue - fDeadZone, fValue, tgPM_FSEL_F32( -(fValue + fDeadZone), fValue, 0.0F ) ));
}


/* ---- tgIN_PM_Rumble_GamePad ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgRESULT tgIN_PM_Rumble_GamePad( C_ETgCONTROLLER enController )
{
    C_TgUINT32                          uiID = (TgUINT32)(enController - ETgCONTROLLER_GAMEPAD_0);
    XINPUT_VIBRATION                    xRumble;

    xRumble.wLeftMotorSpeed = (WORD)(g_asController_Rumble[enController].m_fLt * (TgFLOAT32)KTgMAX_U16);
    xRumble.wRightMotorSpeed = (WORD)(g_asController_Rumble[enController].m_fRt * (TgFLOAT32)KTgMAX_U16);

    g_pfnXInputSetState( uiID, &xRumble );
    return (KTgS_OK);
}