Cxbx-Reloaded/Source/Win32/CxbxKrnl/EmuDInput.cpp

210 lines
6.8 KiB
C++
Raw Normal View History

2003-03-23 22:24:41 +00:00
// ******************************************************************
// *
// * .,-::::: .,:: .::::::::. .,:: .:
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
// * $$$ Y$$$P $$""""Y$$ Y$$$P
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
// *
2003-03-27 06:23:58 +00:00
// * Cxbx->Win32->CxbxKrnl->EmuDInput.cpp
2003-03-23 22:24:41 +00:00
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them under the terms of the GNU General Public
// * License as published by the Free Software Foundation; either
// * version 2 of the license, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA.
// *
// * (c) 2002-2003 Aaron Robinson <caustik@caustik.com>
// *
// * All rights reserved
// *
// ******************************************************************
#define _CXBXKRNL_INTERNAL
2003-04-02 02:23:30 +00:00
#define _XBOXKRNL_LOCAL_
#include "EmuD3D8.h"
#include "EmuDInput.h"
2003-04-01 23:11:03 +00:00
#include "InputConfig.h"
2003-03-23 22:24:41 +00:00
2003-04-02 02:23:30 +00:00
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
// ******************************************************************
// * prevent name collisions
// ******************************************************************
namespace xapi
{
#include <EmuXapi.h>
};
2003-03-23 22:24:41 +00:00
// ******************************************************************
// * globals
// ******************************************************************
2003-04-01 23:11:03 +00:00
LPDIRECTINPUT8 g_pDirectInput8 = NULL;
LPDIRECTINPUTDEVICE8 g_pInputDev[MAX_INPUT_DEVICES] = {0};
int g_pInputCur = 0;
2003-04-02 02:23:30 +00:00
xapi::XINPUT_STATE g_EmuController1;
2003-04-01 23:11:03 +00:00
2003-03-23 22:24:41 +00:00
// ******************************************************************
// * statics
// ******************************************************************
static BOOL CALLBACK EnumGameCtrlCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
static BOOL CALLBACK EnumObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef);
// ******************************************************************
2003-03-27 06:23:58 +00:00
// * func: EmuInitDInput
2003-03-23 22:24:41 +00:00
// ******************************************************************
2003-04-02 02:23:30 +00:00
void EmuInitDInput()
2003-03-23 22:24:41 +00:00
{
// ******************************************************************
// * Create DirectInput object
// ******************************************************************
{
2003-04-01 23:11:03 +00:00
DirectInput8Create
2003-03-23 22:24:41 +00:00
(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
2003-03-30 06:30:19 +00:00
(void**)&g_pDirectInput8,
2003-03-23 22:24:41 +00:00
NULL
);
}
// ******************************************************************
2003-03-30 06:30:19 +00:00
// * Enumerate Controller(s)
2003-03-23 22:24:41 +00:00
// ******************************************************************
2003-04-01 23:11:03 +00:00
if(g_pDirectInput8 != 0)
2003-03-23 22:24:41 +00:00
{
2003-04-01 23:11:03 +00:00
g_pDirectInput8->EnumDevices
2003-03-23 22:24:41 +00:00
(
DI8DEVCLASS_GAMECTRL,
EnumGameCtrlCallback,
NULL,
DIEDFL_ATTACHEDONLY
);
}
// ******************************************************************
2003-04-01 23:11:03 +00:00
// * Set cooperative level and acquire
2003-03-23 22:24:41 +00:00
// ******************************************************************
{
2003-04-01 23:11:03 +00:00
for(int v=g_pInputCur-1;v>=0;v--)
{
g_pInputDev[v]->SetCooperativeLevel(g_EmuWindow, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
g_pInputDev[v]->SetDataFormat(&c_dfDIJoystick);
g_pInputDev[v]->Acquire();
2003-03-23 22:24:41 +00:00
2003-04-01 23:11:03 +00:00
HRESULT hRet = g_pInputDev[v]->Poll();
2003-03-23 22:24:41 +00:00
2003-04-01 23:11:03 +00:00
if(FAILED(hRet))
{
hRet = g_pInputDev[v]->Acquire();
2003-03-23 22:24:41 +00:00
2003-04-01 23:11:03 +00:00
while(hRet == DIERR_INPUTLOST)
hRet = g_pInputDev[v]->Acquire();
if(hRet != DIERR_INPUTLOST)
break;
}
}
2003-03-23 22:24:41 +00:00
}
// ******************************************************************
2003-03-30 06:30:19 +00:00
// * Enumerate Controller objects
2003-03-23 22:24:41 +00:00
// ******************************************************************
{
2003-04-01 23:11:03 +00:00
for(int v=0;v<g_pInputCur;v++)
g_pInputDev[v]->EnumObjects(EnumObjectsCallback, (LPVOID)v, DIDFT_ALL);
2003-03-23 22:24:41 +00:00
}
}
// ******************************************************************
// * func: EnumGameCtrlCallback
// ******************************************************************
static BOOL CALLBACK EnumGameCtrlCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
2003-04-01 23:11:03 +00:00
HRESULT hRet = g_pDirectInput8->CreateDevice(lpddi->guidInstance, &g_pInputDev[g_pInputCur], NULL);
2003-03-23 22:24:41 +00:00
if(FAILED(hRet))
return DIENUM_CONTINUE;
2003-04-01 23:11:03 +00:00
g_pInputDev[g_pInputCur++]->SetDataFormat(&c_dfDIJoystick);
return DIENUM_CONTINUE;
2003-03-23 22:24:41 +00:00
}
// ******************************************************************
// * func: EnumObjectsCallback
// ******************************************************************
static BOOL CALLBACK EnumObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
{
if(lpddoi->dwType & DIDFT_AXIS)
{
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_BYID;
2003-04-01 23:11:03 +00:00
diprg.diph.dwObj = lpddoi->dwType;
2003-03-23 22:24:41 +00:00
diprg.lMin = -32768;
diprg.lMax = 32767;
// set axis range
2003-04-01 23:11:03 +00:00
HRESULT hRet = g_pInputDev[(int)pvRef]->SetProperty(DIPROP_RANGE, &diprg.diph);
2003-03-23 22:24:41 +00:00
if(FAILED(hRet))
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
// ******************************************************************
2003-03-30 06:30:19 +00:00
// * func: EmuPollController
2003-03-23 22:24:41 +00:00
// ******************************************************************
2003-04-02 02:23:30 +00:00
void EmuPollController()
2003-03-23 22:24:41 +00:00
{
2003-04-01 23:11:03 +00:00
/*
2003-03-30 06:30:19 +00:00
DIJOYSTATE ControllerState;
2003-03-23 22:24:41 +00:00
if(g_pGameCtrl == NULL)
return;
HRESULT hRet = g_pGameCtrl->Poll();
if(FAILED(hRet))
{
hRet = g_pGameCtrl->Acquire();
while(hRet == DIERR_INPUTLOST)
hRet = g_pGameCtrl->Acquire();
return;
}
2003-04-01 02:47:38 +00:00
hRet = g_pGameCtrl->GetDeviceState(sizeof(DIJOYSTATE), &ControllerState);
2003-03-23 22:24:41 +00:00
if(FAILED(hRet))
return;
2003-03-30 06:30:19 +00:00
g_EmuController1.dwPacketNumber++;
g_EmuController1.Gamepad.sThumbRX = (short)ControllerState.lX;
g_EmuController1.Gamepad.sThumbRY = (short)(-1 - ControllerState.lY);
g_EmuController1.Gamepad.sThumbLX = (short)ControllerState.lRx;
g_EmuController1.Gamepad.sThumbLY = (short)(-1 - ControllerState.lRy);
2003-04-01 23:11:03 +00:00
*/
2003-03-23 22:24:41 +00:00
return;
}