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

200 lines
6.5 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
#include "Cxbx.h"
2003-03-27 06:23:58 +00:00
#include "Emu.h"
2003-03-23 22:24:41 +00:00
using namespace win32;
// ******************************************************************
// * globals
// ******************************************************************
LPDIRECTINPUT8 g_pDI = NULL;
LPDIRECTINPUTDEVICE8 g_pGameCtrl = NULL;
2003-03-28 23:45:52 +00:00
xboxkrnl::XINPUT_STATE g_EmuGamepad1 = {0};
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-03-28 23:45:52 +00:00
void xboxkrnl::EmuInitDInput()
2003-03-23 22:24:41 +00:00
{
// ******************************************************************
// * Create DirectInput object
// ******************************************************************
{
HRESULT hRet = DirectInput8Create
(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&g_pDI,
NULL
);
if(hRet != DI_OK)
2003-03-27 06:23:58 +00:00
EmuPanic();
2003-03-23 22:24:41 +00:00
}
// ******************************************************************
2003-03-28 23:45:52 +00:00
// * Enumerate Gamepad(s)
2003-03-23 22:24:41 +00:00
// ******************************************************************
{
HRESULT hRet = g_pDI->EnumDevices
(
DI8DEVCLASS_GAMECTRL,
EnumGameCtrlCallback,
NULL,
DIEDFL_ATTACHEDONLY
);
if(hRet != DI_OK)
2003-03-27 06:23:58 +00:00
EmuPanic();
2003-03-23 22:24:41 +00:00
if(g_pGameCtrl == NULL)
return;
}
// ******************************************************************
2003-03-28 23:45:52 +00:00
// * Set Gamepad data format
2003-03-23 22:24:41 +00:00
// ******************************************************************
{
HRESULT hRet = g_pGameCtrl->SetDataFormat(&c_dfDIJoystick2);
if(FAILED(hRet))
2003-03-27 06:23:58 +00:00
EmuPanic();
2003-03-23 22:24:41 +00:00
}
// ******************************************************************
// * Set cooperative level
// ******************************************************************
{
2003-03-27 06:23:58 +00:00
HRESULT hRet = g_pGameCtrl->SetCooperativeLevel(g_EmuWindow, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
2003-03-23 22:24:41 +00:00
if(FAILED(hRet))
2003-03-27 06:23:58 +00:00
EmuPanic();
2003-03-23 22:24:41 +00:00
}
// ******************************************************************
2003-03-28 23:45:52 +00:00
// * Enumerate Gamepad objects
2003-03-23 22:24:41 +00:00
// ******************************************************************
{
HRESULT hRet = g_pGameCtrl->EnumObjects(EnumObjectsCallback, NULL, DIDFT_ALL);
if(FAILED(hRet))
2003-03-27 06:23:58 +00:00
EmuPanic();
2003-03-23 22:24:41 +00:00
}
}
// ******************************************************************
// * func: EnumGameCtrlCallback
// ******************************************************************
static BOOL CALLBACK EnumGameCtrlCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
HRESULT hRet = g_pDI->CreateDevice(lpddi->guidInstance, &g_pGameCtrl, NULL);
// keep on enumerating if this device could not be created
if(FAILED(hRet))
return DIENUM_CONTINUE;
// once we find a working device, stop enumeration (we just want one)
return DIENUM_STOP;
}
// ******************************************************************
// * 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;
diprg.diph.dwObj = lpddoi->dwType; // Specify the enumerated axis
diprg.lMin = -32768;
diprg.lMax = 32767;
// set axis range
HRESULT hRet = g_pGameCtrl->SetProperty(DIPROP_RANGE, &diprg.diph);
if(FAILED(hRet))
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
// ******************************************************************
2003-03-28 23:45:52 +00:00
// * func: EmuPollGamepad
2003-03-23 22:24:41 +00:00
// ******************************************************************
2003-03-28 23:45:52 +00:00
void xboxkrnl::EmuPollGamepad()
2003-03-23 22:24:41 +00:00
{
2003-03-28 23:45:52 +00:00
DIJOYSTATE2 GamepadState;
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-03-28 23:45:52 +00:00
hRet = g_pGameCtrl->GetDeviceState(sizeof(DIJOYSTATE2), &GamepadState);
2003-03-23 22:24:41 +00:00
if(FAILED(hRet))
return;
2003-03-28 23:45:52 +00:00
g_EmuGamepad1.dwPacketNumber++;
g_EmuGamepad1.Gamepad.sThumbRX = (short)GamepadState.lX;
g_EmuGamepad1.Gamepad.sThumbRY = (short)(-1 - GamepadState.lY);
g_EmuGamepad1.Gamepad.sThumbLX = (short)GamepadState.lRx;
g_EmuGamepad1.Gamepad.sThumbLY = (short)(-1 - GamepadState.lRy);
2003-03-23 22:24:41 +00:00
return;
}