Home

Resume

Blog

Teikitu


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

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

/* ---- tgIN_Has_Producer ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgIN_Has_Producer( C_ETgCONTROLLER enController )
{
    ETgCONTROLLER                       enIndex;

    for (enIndex = 0; enIndex < ETgCONTROLLER_MAX; ++enIndex)
    {
        if (0 != g_asController_Active[enIndex].m_pfnProduce && enController == g_asController_Active[enIndex].m_enController)
        {
            return (TgTRUE);
        };
    };

    return (TgFALSE);
}


/* ---- tgIN_Update_Producers ------------------------------------------------------------------------------------------------------------------------------------------- */
/*  This function is wrapper by platform specific implementations to enforce a real time guaranteed execution.                                                            */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_Update_Producers( TgVOID )
{
    ETgCONTROLLER                       enIndex;

    for (enIndex = 0; enIndex < ETgCONTROLLER_MAX; ++enIndex)
    {
        if (0 != g_asController_Active[enIndex].m_pfnProduce)
        {
            g_asController_Active[enIndex].m_pfnProduce( enIndex );
        };
    };
}


/* ---- tgIN_Insert_Consumer -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgIN_CONSUMER_ID tgIN_Insert_Consumer( C_ETgCONTROLLER enController, C_TgUINT32 uiPriority, TgIN_FCN_CONSUMER pfnConsume, C_TgUINTPTR uiParam )
{
    PU_STg2_Input_Consumer              psNew_Consumer;
    TgSINT32                            iIndex;
    TgIN_CONSUMER_ID                    tiConsume;

    if (enController >= ETgCONTROLLER_MAX)
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unknown Input Controller as Parameter\n" ), TgT("Input"), TgT("Insert_Consumer") );
        return (KTgIN_CONSUMER_ID__INVALID);
    };

    if (nullptr == pfnConsume)
    {
        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Consume function is null\n" ), TgT("Input"), TgT("Insert_Consumer") );
        return (KTgIN_CONSUMER_ID__INVALID);
    };

    tgInit_IN_CONSUMER_ID( &tiConsume, KTgMAX_CONSUMER );
    psNew_Consumer = nullptr;
    for (iIndex = 0; iIndex < KTgMAX_CONSUMER; ++iIndex)
    {
        if (KTgIN_CONSUMER_ID__INVALID.m_iKI == tgAM64_XCMP( &(g_asConsumer[iIndex].m_tiConsumer.m_iKI), tiConsume.m_iKI, KTgIN_CONSUMER_ID__INVALID.m_iKI ))
        {
            memset( &g_asConsumer[iIndex].m_sMP_Element, 0, sizeof( g_asConsumer[iIndex].m_sMP_Element ) );
            g_asConsumer[iIndex].m_tiConsumer.m.iI = iIndex;
            psNew_Consumer = g_asConsumer + iIndex;
        };
    };

    if (nullptr == psNew_Consumer)
    {
    #if TgS_DEBUG_INPUT
        TgWARNING_MSGF( 0, TgT( "%-16.16s(%-32.32s): Exceed total number of alloweable input consumers.\n" ), TgT("Input"), TgT("Insert_Consumer") );
    #endif
        return (KTgIN_CONSUMER_ID__INVALID);
    };

    psNew_Consumer->m_pfnConsume = pfnConsume;
    psNew_Consumer->m_enController = enController;
    psNew_Consumer->m_uiParam = uiParam;
    psNew_Consumer->m_uiPriority = uiPriority;
    psNew_Consumer->m_bRemove = TgFALSE;

    tgCM_UTM_AM_ST_Push( &g_sConsumer_Insert, &psNew_Consumer->m_sMP_Element );

    return (psNew_Consumer->m_tiConsumer);
}


/* ---- tgIN_Remove_Consumer -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_Remove_Consumer( C_TgIN_CONSUMER_ID tiConsumer )
{
#if TgS_DEBUG_INPUT
    if (TgTRUE != tgEQ_IN_CONSUMER_ID( g_asConsumer[tiConsumer.m.iI].m_tiConsumer, tiConsumer ))
    {
        TgERROR_MSGF( TgFALSE, TgT( "%-16.16s(%-32.32s): Invalid or stale ID.\n" ), TgT("Input"), TgT("Remove_Consumer") );
    };
#endif

    g_asConsumer[tiConsumer.m.iI].m_bRemove = TgTRUE;

    return (KTgS_OK);
}


/* ---- tgIN_Pause_Controller ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgIN_Pause_Controller( C_ETgCONTROLLER enInput, C_TgBOOL bPause )
{
    if (enInput < ETgCONTROLLER_MAX)
    {
        g_abController_Paused[enInput] = bPause;
        return (TgTRUE);
    };

    TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Unknown Input Controller as Parameter\n" ), TgT("Input"), TgT("Pause_Context") );
    return (TgFALSE);
}


/* ---- tgIN_Pause_Input ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_Pause_Input( C_TgBOOL bPause )
{
    TgUINT32                            uiIndex = 0;

    for (; uiIndex < ETgCONTROLLER_MAX; ++uiIndex)
    {
        TgVERIFY( TgTRUE == tgIN_Pause_Controller( (ETgCONTROLLER)uiIndex, bPause ) );
    };
}


/* ---- tgIN_Set_Default_Dead_Zone -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_Set_Default_Dead_Zone( C_ETgDEAD_ZONE enZone, C_TgFLOAT32 fValue )
{
    switch (enZone)
    {
        case ETgDEAD_ZONE_MOUSE_X:
            g_fDefault_Mouse_X_Dead_Zone = fValue; break;
        case ETgDEAD_ZONE_MOUSE_Y:
            g_fDefault_Mouse_Y_Dead_Zone = fValue; break;
        case ETgDEAD_ZONE_MOUSE_Z:
            g_fDefault_Mouse_Z_Dead_Zone = fValue; break;

        case ETgDEAD_ZONE_STICK_0_X:
            g_fDefault_GamePad_Stick_0_X_Dead_Zone = fValue; break;
        case ETgDEAD_ZONE_STICK_0_Y:
            g_fDefault_GamePad_Stick_0_Y_Dead_Zone = fValue; break;
        case ETgDEAD_ZONE_STICK_1_X:
            g_fDefault_GamePad_Stick_1_X_Dead_Zone = fValue; break;
        case ETgDEAD_ZONE_STICK_1_Y:
            g_fDefault_GamePad_Stick_1_Y_Dead_Zone = fValue; break;
        
        case ETgDEAD_ZONE_MAX:
            break;

        default:
            TgS_NO_DEFAULT(break);
    };
}


/* ---- tgIN_Set_Dead_Zone ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgIN_Set_Dead_Zone( C_ETgCONTROLLER enController, C_ETgDEAD_ZONE enZone, C_TgFLOAT32 fValue )
{
    switch (enController)
    {
        case ETgCONTROLLER_GAMEPAD_0:
        case ETgCONTROLLER_GAMEPAD_1:
        case ETgCONTROLLER_GAMEPAD_2:
        case ETgCONTROLLER_GAMEPAD_3:
            TgPARAM_CHECK( enZone >= ETgDEAD_ZONE_STICK_0_X );
            g_fGamePad_Stick_Dead_Zone[enController - ETgCONTROLLER_GAMEPAD_0][enZone - ETgDEAD_ZONE_STICK_0_X] = fValue; break;

        case ETgCONTROLLER_GUI_MOUSE:
        case ETgCONTROLLER_MOUSE:
            if (enZone >= ETgDEAD_ZONE_MOUSE_X && enZone < ETgDEAD_ZONE_STICK_0_X)
            {
                g_fMouse_Dead_Zone[enController - ETgCONTROLLER_MOUSE][enZone - ETgDEAD_ZONE_MOUSE_X] = fValue;
            };
            break;

        case ETgCONTROLLER_KEYBOARD:
        case ETgCONTROLLER_GUI_KEYBOARD:
        case ETgCONTROLLER_NET_0:
        case ETgCONTROLLER_NET_1:
        case ETgCONTROLLER_NET_2:
        case ETgCONTROLLER_NET_3:
        case ETgCONTROLLER_MAX:
            break;

        default:
            TgS_NO_DEFAULT(break);
    };
}


/* ---- tgIN_Query_Default_Dead_Zone ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFLOAT32 tgIN_Query_Default_Dead_Zone( C_ETgDEAD_ZONE enZone )
{
    switch (enZone)
    {
        case ETgDEAD_ZONE_MOUSE_X:
            return (g_fDefault_Mouse_X_Dead_Zone);
        case ETgDEAD_ZONE_MOUSE_Y:
            return (g_fDefault_Mouse_Y_Dead_Zone);
        case ETgDEAD_ZONE_MOUSE_Z:
            return (g_fDefault_Mouse_Z_Dead_Zone);

        case ETgDEAD_ZONE_STICK_0_X:
            return (g_fDefault_GamePad_Stick_0_X_Dead_Zone);
        case ETgDEAD_ZONE_STICK_0_Y:
            return (g_fDefault_GamePad_Stick_0_Y_Dead_Zone);
        case ETgDEAD_ZONE_STICK_1_X:
            return (g_fDefault_GamePad_Stick_1_X_Dead_Zone);
        case ETgDEAD_ZONE_STICK_1_Y:
            return (g_fDefault_GamePad_Stick_1_Y_Dead_Zone);

        case ETgDEAD_ZONE_MAX:
            return (0.0F);

        default:
            TgS_NO_DEFAULT( return (0.0F) );
    };
}


/* ---- tgIN_Query_Dead_Zone -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFLOAT32 tgIN_Query_Dead_Zone( C_ETgCONTROLLER enController, C_ETgDEAD_ZONE enZone )
{
    switch (enController)
    {
        case ETgCONTROLLER_GAMEPAD_0:
        case ETgCONTROLLER_GAMEPAD_1:
        case ETgCONTROLLER_GAMEPAD_2:
        case ETgCONTROLLER_GAMEPAD_3:
            TgPARAM_CHECK( enZone >= ETgDEAD_ZONE_STICK_0_X );
            return (g_fGamePad_Stick_Dead_Zone[enController - ETgCONTROLLER_GAMEPAD_0][enZone - ETgDEAD_ZONE_STICK_0_X]);

        case ETgCONTROLLER_GUI_MOUSE:
        case ETgCONTROLLER_MOUSE:
            if (enZone >= ETgDEAD_ZONE_MOUSE_X && enZone < ETgDEAD_ZONE_STICK_0_X)
            {
                return (g_fMouse_Dead_Zone[enController - ETgCONTROLLER_MOUSE][enZone - ETgDEAD_ZONE_MOUSE_X]);
            }

        case ETgCONTROLLER_KEYBOARD:
        case ETgCONTROLLER_GUI_KEYBOARD:
        case ETgCONTROLLER_NET_0:
        case ETgCONTROLLER_NET_1:
        case ETgCONTROLLER_NET_2:
        case ETgCONTROLLER_NET_3:
        case ETgCONTROLLER_MAX:
            return (0.0F);

        default:
            TgS_NO_DEFAULT( return (0.0F) );
    };
}


/* ---- tgIN_Set_Rumble ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgIN_Set_Rumble( C_ETgCONTROLLER enController, C_TgFLOAT32 fLt, C_TgFLOAT32 fRt )
{
    if (nullptr == g_asController_Rumble[enController].m_pfnRumble)
    {
        return (KTgE_FAIL);
    };

    g_asController_Rumble[enController].m_fLt = fLt;
    g_asController_Rumble[enController].m_fRt = fRt;

    return (g_asController_Rumble[enController].m_pfnRumble( enController ));
}


/* ---- tgIN_Stat_Query_Total_Insert ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgS_STAT_INPUT
TgUINT32 tgIN_Stat_Query_Total_Insert( TgVOID )
{
    return (g_nuiStat_Insert[ETgCONTROLLER_MAX]);
}
#endif


/* ---- tgIN_Stat_Query_Total_Remove ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgS_STAT_INPUT
TgUINT32 tgIN_Stat_Query_Total_Remove( TgVOID )
{
    return (g_nuiStat_Remove[ETgCONTROLLER_MAX]);
}
#endif