Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (?)
    »File«      TgS (WIN) Input - Module.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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Input ============================================================================================================================================================= */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Private Data                                                                                                                                                          */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

#if defined(TgCOMPILE_WIDE_CHAR)
static const GUID  IID_IDirectInput8 = { 0xBF798031L,0x483A,0x4DA2,{0xAA,0x99,0x5D,0x64,0xED,0x36,0x97,0x00} };
#else
static const GUID IID_IDirectInput8 = { 0xBF798030L,0x483A,0x4DA2,{0xAA,0x99,0x5D,0x64,0xED,0x36,0x97,0x00} };
#endif




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgIN_PM_Module_Init --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Module_Init( TgVOID )
{
    g_fDefault_GamePad_Stick_0_X_Dead_Zone = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE / 32767.0F;
    g_fDefault_GamePad_Stick_0_Y_Dead_Zone = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE / 32767.0F;
    g_fDefault_GamePad_Stick_1_X_Dead_Zone = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE / 32767.0F;
    g_fDefault_GamePad_Stick_1_Y_Dead_Zone = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE / 32767.0F;

    /* Load the dinput functions from the DLL */
    if (TgTRUE != tgGB_Load_Lib( &g_hDLL_DInput8, TgT("dinput8.dll") ))
    {
        TgCRITICAL_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unable to load dinput8 DLL.\n" ), TgT("Global"), TgT("Platform") );
        return (KTgE_FAIL);
    };

    if (
           TgTRUE != tgGB_Get_Func_Address( (TgMS_FARPROC*)&g_pfnDirectInput8Create, g_hDLL_DInput8, "DirectInput8Create" )
    )
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unable to find function in dinput8 DLL.\n" ), TgT("Global"), TgT("Platform") );
        tgIN_PM_Module_Free();
        return (KTgE_FAIL);
    };


    /* Load the xInput functions from the DLL */
    if (TgTRUE != tgGB_Load_Lib( &g_hDLL_xInput, TgT("xinput9_1_0.dll") ))
    {
        TgCRITICAL_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unable to load xinput DLL.\n" ), TgT("Global"), TgT("Platform") );
        tgIN_PM_Module_Free();
        return (KTgE_FAIL);
    };

    if (
           TgTRUE != tgGB_Get_Func_Address( (TgMS_FARPROC*)&g_pfnXInputGetState, g_hDLL_xInput, "XInputGetState" )
        || TgTRUE != tgGB_Get_Func_Address( (TgMS_FARPROC*)&g_pfnXInputGetCapabilities, g_hDLL_xInput, "XInputGetCapabilities" )
        || TgTRUE != tgGB_Get_Func_Address( (TgMS_FARPROC*)&g_pfnXInputSetState, g_hDLL_xInput, "XInputSetState" )
    )
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unable to find function in xinput DLL.\n" ), TgT("Global"), TgT("Platform") );
        tgIN_PM_Module_Free();
        return (KTgE_FAIL);
    };


    if (TgFAILED( g_pfnDirectInput8Create( g_hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8, (PP_TgVOID)&g_pdxDI, 0 ) ))
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Direct input 8.0 failed.\n" ), TgT("Input"), TgT("PM_Module_Init") );
        tgIN_PM_Module_Free();
        return (KTgE_FAIL);
    };

    if (TgFAILED( tgIN_PM_Init_Function_Table() ))
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Failed to acquire input funcitons.\n" ), TgT("Input"), TgT("PM_Module_Init") );
        tgIN_PM_Module_Free();
        return (KTgE_FAIL);
    };

    tgIN_PM_Init_Keyboard();
    tgIN_PM_Init_Mouse();
    tgIN_PM_Init_GamePad();
    tgIN_PM_Init_GUI_Mouse();
    tgIN_PM_Init_GUI_Keyboard();

    return (KTgS_OK);
}


/* ---- tgIN_PM_Module_Boot --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_PM_Module_Boot( TgVOID )
{
    if (
           TgFAILED( tgIN_PM_Boot_Keyboard() )
        || TgFAILED( tgIN_PM_Boot_Mouse() )
        || TgFAILED( tgIN_PM_Boot_GamePad() )
        || TgFAILED( tgIN_PM_Boot_GUI_Mouse() )
        || TgFAILED( tgIN_PM_Boot_GUI_Keyboard() )
    )
    {
        tgIN_PM_Stop_GUI_Mouse();
        tgIN_PM_Stop_GamePad();
        tgIN_PM_Stop_Mouse();
        tgIN_PM_Stop_Keyboard();
        return (KTgE_FAIL);
    };

    return (KTgS_OK);
}


/* ---- tgIN_PM_Module_Stop --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_PM_Module_Stop( TgVOID )
{
    /* Stop the controller systems */
    TgVERIFY( KTgS_OK == tgIN_PM_Stop_GUI_Keyboard() );
    TgVERIFY( KTgS_OK == tgIN_PM_Stop_GUI_Mouse() );
    TgVERIFY( KTgS_OK == tgIN_PM_Stop_GamePad() );
    TgVERIFY( KTgS_OK == tgIN_PM_Stop_Mouse() );
    TgVERIFY( KTgS_OK == tgIN_PM_Stop_Keyboard() );
}


/* ---- tgIN_PM_Module_Free --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_PM_Module_Free( TgVOID )
{
    TgVERIFY( KTgS_OK == tgIN_PM_Free_GUI_Keyboard() );
    TgVERIFY( KTgS_OK == tgIN_PM_Free_GUI_Mouse() );
    TgVERIFY( KTgS_OK == tgIN_PM_Free_GamePad() );
    TgVERIFY( KTgS_OK == tgIN_PM_Free_Mouse() );
    TgVERIFY( KTgS_OK == tgIN_PM_Free_Keyboard() );

    if (nullptr != g_pdxDI)
    {
        TgVERIFY( 1 != g_pfnDIN8_Release( g_pdxDI ) );
        g_pdxDI = nullptr;
    };

    if (nullptr != g_hDLL_xInput)
    {
        tgGB_Free_Lib( &g_hDLL_xInput );
    };
    g_pfnXInputGetState = 0;
    g_pfnXInputGetCapabilities = 0;
    g_pfnXInputSetState = 0;

    if (nullptr != g_hDLL_DInput8)
    {
        tgGB_Free_Lib( &g_hDLL_DInput8 );
    };
    g_pfnDirectInput8Create = 0;
}