/***************************************************************************/
/*  Description: INOInit.c, part of CPA INO library                        */                                
/*               Declaration of the global variables for MMG and ERM       */
/*               for joystick and  keyboard and mouse                      */
/*                                                                         */
/*  Author: F. Jentey                                                      */
/*  Creation date: 21/04/97                                                */
/*  Last Update:   30/07/97  Direct input initialisation                   */
/***************************************************************************/
 
 
/*========  CPA Header  ========*/
#ifdef INO_USE_CPA
 
/* CPA Error */
#define __DeclareGlobalVariableErrINO_h__
#include "INO/errINO.h"
#undef __DeclareGlobalVariableErrINO_h__
 
/* CPA Memory */
#define __DeclareGlobalVariableMemINO_h__
#include "INO/MmgINO.h"
#undef __DeclareGlobalVariableMemINO_h__
 
#endif
/*==============================*/
 
 
/*========  Direct Input header  ========*/
#ifndef INITGUID
#define INITGUID
#include "DInput.h"
#undef  INITGUID
#else
#include "DInput.h"
#endif
 
#define DIRECTINPUT_VERSION_3    0x0300
#include "INOInit.h"
/*=======================================*/
 
 
/* CPA ERM and MMG init OK ? */
short INO_g_wErrorInitialisationOK       = 0;
short INO_g_wMemoryInitialisationOK      = 0;
 
LPDIRECTINPUT  INO_g_p_stDInputInterface = NULL;
HINSTANCE      INO_g_hAppInstance;
HWND           INO_g_hAppMainWindow;
 
 
 
#ifdef INO_USE_CPA
/***********************************/
/*  Init CPA errors for module INO */
/***********************************/
short INO_fn_wInitError()
{
    if (INO_g_wErrorInitialisationOK) return(0);
         
    Erm_M_InitErrMsg(INO);
    INO_g_wErrorInitialisationOK = 1;
 
    return(0);
}
 
/***********************************/
/*  Init CPA memory for module INO */
/***********************************/
short INO_fn_wInitMemory()
{
    if (INO_g_wMemoryInitialisationOK) return(0);
         
    M_INOInitMem();
    INO_g_wMemoryInitialisationOK = 1;
 
    return(0);
}
#endif
 
/***********************************/
/*       Init Direct Input         */
/*  Return 1 if OK, else 0         */
/***********************************/
short INO_fn_wInitDirectInput(HINSTANCE hInstance, HWND hWindow)
{
    HRESULT  DInputErr;
 
    if (INO_g_p_stDInputInterface) return(1);
#ifdef  OLD_DIRECTINPUT 
    DInputErr = DirectInputCreate(hInstance,DIRECTINPUT_VERSION,&INO_g_p_stDInputInterface,NULL);
    if (DInputErr==DIERR_OLDDIRECTINPUTVERSION)
        /* If compiled with Direct Input 5, and Direct Input 3 is installed */
        DInputErr = DirectInputCreate(hInstance,DIRECTINPUT_VERSION_3,&INO_g_p_stDInputInterface,NULL);
#else
	DInputErr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8,&INO_g_p_stDInputInterface, NULL );
  
#endif
 
    switch (DInputErr)
    {
    case DI_OK:
        INO_g_hAppInstance = hInstance;
        INO_g_hAppMainWindow = hWindow;
        break;
 
    case DIERR_INVALIDPARAM:
    case DIERR_OUTOFMEMORY:
    case DIERR_OLDDIRECTINPUTVERSION:
    case DIERR_BETADIRECTINPUTVERSION:
        INO_g_p_stDInputInterface = NULL;
        break;
	default:
		break;
    }
 
    return (INO_g_p_stDInputInterface != NULL);
}
 
 
/***********************************/
/*     Release Direct Input        */
/***********************************/
void INO_fn_vReleaseDirectInput()
{
    if (INO_g_p_stDInputInterface) return;
    
    INO_g_p_stDInputInterface->lpVtbl->Release(INO_g_p_stDInputInterface);
    INO_g_p_stDInputInterface = NULL;
}
