New Wiimote Plugin: Added real wiimote support.(only tested on Windows, still a few probs: after refresh alt+F{5..8} x2 is needed) New Wiimote/GCPad: Re-merged Keyboard+Mouse for easier kb+mouse configuration, like before.(DirectInput doesn't support individual kb/mice anymore like I thought it did) Fixed some bugs and maybe leaks in GUI.(got rid of evil dynamic_cast) Renamed stuff from DirectInput to DInput, cause it's shorter and rhymes with XInput, I guess. (I remembered eol-style native, shuffle lost his job :P)

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5822 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2010-07-03 08:04:10 +00:00
parent 0dc8833396
commit 0bf3dfda03
25 changed files with 1276 additions and 736 deletions

View File

@ -0,0 +1,84 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DINPUT
#include "DInput.h"
#include <StringUtil.h>
#ifdef CIFACE_USE_DINPUT_JOYSTICK
#include "DInputJoystick.h"
#endif
#ifdef CIFACE_USE_DINPUT_KBM
#include "DInputKeyboardMouse.h"
#endif
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "dxguid.lib")
namespace ciface
{
namespace DInput
{
//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef)
//{
// ((std::list<DIEFFECTINFO>*)pvRef)->push_back(*pdei);
// return DIENUM_CONTINUE;
//}
BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
{
((std::list<DIDEVICEOBJECTINSTANCE>*)pvRef)->push_back(*lpddoi);
return DIENUM_CONTINUE;
}
BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
((std::list<DIDEVICEINSTANCE>*)pvRef)->push_back(*lpddi);
return DIENUM_CONTINUE;
}
std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device)
{
std::string out;
DIPROPSTRING str;
ZeroMemory(&str, sizeof(str));
str.diph.dwSize = sizeof(str);
str.diph.dwHeaderSize = sizeof(str.diph);
str.diph.dwHow = DIPH_DEVICE;
if (SUCCEEDED(device->GetProperty(DIPROP_PRODUCTNAME, &str.diph)))
{
const int size = WideCharToMultiByte(CP_ACP, 0, str.wsz, -1, NULL, 0, NULL, NULL);
char* const data = new char[size];
if (size == WideCharToMultiByte(CP_ACP, 0, str.wsz, -1, data, size, NULL, NULL))
out.assign(data);
delete[] data;
}
return StripSpaces(out);
}
void Init( std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ )
{
IDirectInput8* idi8;
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&idi8, NULL)))
return;
#ifdef CIFACE_USE_DINPUT_KBM
InitKeyboardMouse(idi8, devices);
#endif
#ifdef CIFACE_USE_DINPUT_JOYSTICK
InitJoystick(idi8, devices/*, hwnd*/);
#endif
idi8->Release();
}
}
}
#endif

View File

@ -0,0 +1,31 @@
#ifndef _CIFACE_DINPUT_H_
#define _CIFACE_DINPUT_H_
#include "../ControllerInterface.h"
#define DINPUT_SOURCE_NAME "DInput"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
#include <list>
namespace ciface
{
namespace DInput
{
//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef);
BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef);
BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device);
void Init( std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ );
}
}
#endif

View File

@ -0,0 +1,547 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DINPUT_JOYSTICK
#include "DInputJoystick.h"
#include "DInput.h"
inline bool operator<(const GUID & lhs, const GUID & rhs)
{
return memcmp(&lhs, &rhs, sizeof(GUID)) < 0;
}
namespace ciface
{
namespace DInput
{
#define DATA_BUFFER_SIZE 32
#ifdef NO_DUPLICATE_DINPUT_XINPUT
//-----------------------------------------------------------------------------
// Modified some MSDN code to get all the XInput device GUID.Data1 values in a vector,
// faster than checking all the devices for each DirectInput device, like MSDN says to do
//-----------------------------------------------------------------------------
void GetXInputGUIDS( std::vector<DWORD>& guids )
{
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
IWbemLocator* pIWbemLocator = NULL;
IEnumWbemClassObject* pEnumDevices = NULL;
IWbemClassObject* pDevices[20] = {0};
IWbemServices* pIWbemServices = NULL;
BSTR bstrNamespace = NULL;
BSTR bstrDeviceID = NULL;
BSTR bstrClassName = NULL;
DWORD uReturned = 0;
VARIANT var;
HRESULT hr;
// CoInit if needed
hr = CoInitialize(NULL);
bool bCleanupCOM = SUCCEEDED(hr);
// Create WMI
hr = CoCreateInstance( __uuidof(WbemLocator),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IWbemLocator),
(LPVOID*) &pIWbemLocator);
if( FAILED(hr) || pIWbemLocator == NULL )
goto LCleanup;
bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" );if( bstrNamespace == NULL ) goto LCleanup;
bstrClassName = SysAllocString( L"Win32_PNPEntity" ); if( bstrClassName == NULL ) goto LCleanup;
bstrDeviceID = SysAllocString( L"DeviceID" ); if( bstrDeviceID == NULL ) goto LCleanup;
// Connect to WMI
hr = pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, 0L, 0L, NULL, NULL, &pIWbemServices );
if( FAILED(hr) || pIWbemServices == NULL )
goto LCleanup;
// Switch security level to IMPERSONATE.
CoSetProxyBlanket( pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
hr = pIWbemServices->CreateInstanceEnum( bstrClassName, 0, NULL, &pEnumDevices );
if( FAILED(hr) || pEnumDevices == NULL )
goto LCleanup;
// Loop over all devices
while( true )
{
// Get 20 at a time
hr = pEnumDevices->Next( 10000, 20, pDevices, &uReturned );
if( FAILED(hr) || uReturned == 0 )
break;
for( UINT iDevice=0; iDevice<uReturned; ++iDevice )
{
// For each device, get its device ID
hr = pDevices[iDevice]->Get( bstrDeviceID, 0L, &var, NULL, NULL );
if( SUCCEEDED( hr ) && var.vt == VT_BSTR && var.bstrVal != NULL )
{
// Check if the device ID contains "IG_". If it does, then it's an XInput device
// This information can not be found from DirectInput
if( wcsstr( var.bstrVal, L"IG_" ) )
{
// If it does, then get the VID/PID from var.bstrVal
DWORD dwPid = 0, dwVid = 0;
WCHAR* strVid = wcsstr( var.bstrVal, L"VID_" );
if( strVid && swscanf( strVid, L"VID_%4X", &dwVid ) != 1 )
dwVid = 0;
WCHAR* strPid = wcsstr( var.bstrVal, L"PID_" );
if( strPid && swscanf( strPid, L"PID_%4X", &dwPid ) != 1 )
dwPid = 0;
// Compare the VID/PID to the DInput device
DWORD dwVidPid = MAKELONG( dwVid, dwPid );
guids.push_back( dwVidPid );
//bIsXinputDevice = true;
}
}
SAFE_RELEASE( pDevices[iDevice] );
}
}
LCleanup:
if(bstrNamespace)
SysFreeString(bstrNamespace);
if(bstrDeviceID)
SysFreeString(bstrDeviceID);
if(bstrClassName)
SysFreeString(bstrClassName);
for( UINT iDevice=0; iDevice<20; iDevice++ )
SAFE_RELEASE( pDevices[iDevice] );
SAFE_RELEASE( pEnumDevices );
SAFE_RELEASE( pIWbemLocator );
SAFE_RELEASE( pIWbemServices );
if( bCleanupCOM )
CoUninitialize();
}
#endif
void InitJoystick( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ )
{
std::list<DIDEVICEINSTANCE> joysticks;
idi8->EnumDevices( DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, DIEDFL_ATTACHEDONLY );
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map< std::basic_string<TCHAR>, int> name_counts;
#ifdef NO_DUPLICATE_DINPUT_XINPUT
std::vector<DWORD> xinput_guids;
GetXInputGUIDS( xinput_guids );
#endif
std::list<DIDEVICEINSTANCE>::iterator
i = joysticks.begin(),
e = joysticks.end();
for ( ; i!=e; ++i )
{
#ifdef NO_DUPLICATE_DINPUT_XINPUT
// skip XInput Devices
if ( std::find( xinput_guids.begin(), xinput_guids.end(), i->guidProduct.Data1 ) != xinput_guids.end() )
continue;
#endif
LPDIRECTINPUTDEVICE8 js_device;
if (SUCCEEDED(idi8->CreateDevice(i->guidInstance, &js_device, NULL)))
{
if (SUCCEEDED(js_device->SetDataFormat(&c_dfDIJoystick)))
{
// using foregroundwindow seems like a hack
if (FAILED(js_device->SetCooperativeLevel(GetForegroundWindow(), DISCL_BACKGROUND | DISCL_EXCLUSIVE)))
{
//PanicAlert("SetCooperativeLevel(DISCL_EXCLUSIVE) failed!");
// fall back to non-exclusive mode, with no rumble
if (FAILED(js_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
//PanicAlert("SetCooperativeLevel failed!");
js_device->Release();
continue;
}
}
Joystick* js = new Joystick(/*&*i, */js_device, name_counts[i->tszInstanceName]++);
// only add if it has some inputs/outpus
if (js->Inputs().size() || js->Outputs().size())
devices.push_back(js);
else
delete js;
}
else
{
//PanicAlert("SetDataFormat failed!");
js_device->Release();
}
}
}
}
Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index )
: m_device(device)
, m_index(index)
//, m_name(TStringToString(lpddi->tszInstanceName))
{
// get joystick caps
DIDEVCAPS js_caps;
ZeroMemory( &js_caps, sizeof(js_caps) );
js_caps.dwSize = sizeof(js_caps);
m_device->GetCapabilities(&js_caps);
// max of 32 buttons and 4 hats / the limit of the data format i am using
js_caps.dwButtons = std::min((DWORD)32, js_caps.dwButtons);
js_caps.dwPOVs = std::min((DWORD)4, js_caps.dwPOVs);
// polled or buffered data
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = DATA_BUFFER_SIZE;
// set the buffer size,
// if we can't set the property, we can't use buffered data,
// must use polling, which apparently doesn't work as well
m_must_poll = (DI_OK != m_device->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph));
}
m_device->Acquire();
// buttons
for ( unsigned int i = 0; i < js_caps.dwButtons; ++i )
AddInput( new Button( i ) );
// hats
for ( unsigned int i = 0; i < js_caps.dwPOVs; ++i )
{
// each hat gets 4 input instances associated with it, (up down left right)
for ( unsigned int d = 0; d<4; ++d )
AddInput( new Hat( i, d ) );
}
// get up to 6 axes and 2 sliders
std::list<DIDEVICEOBJECTINSTANCE> axes;
m_device->EnumObjects(DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_ABSAXIS);
unsigned int cur_slider = 0;
// map of axis offsets in joystate dataformat based on axis guidType
std::map<GUID,int> types;
types[GUID_XAxis] = 0;
types[GUID_YAxis] = 1;
types[GUID_ZAxis] = 2;
types[GUID_RxAxis] = 3;
types[GUID_RyAxis] = 4;
types[GUID_RzAxis] = 5;
// going in reverse leaves the list more organized in the end for me :/
std::list<DIDEVICEOBJECTINSTANCE>::const_reverse_iterator
i = axes.rbegin(),
e = axes.rend();
for( ; i!=e; ++i )
{
DIPROPRANGE range;
ZeroMemory( &range, sizeof(range ) );
range.diph.dwSize = sizeof(range);
range.diph.dwHeaderSize = sizeof(range.diph);
range.diph.dwHow = DIPH_BYID;
range.diph.dwObj = i->dwType;
// try to set some nice power of 2 values (8192)
range.lMin = -(1<<13);
range.lMax = (1<<13);
// but i guess not all devices support setting range
m_device->SetProperty( DIPROP_RANGE, &range.diph );
// so i getproperty right afterward incase it didn't set :P
if (SUCCEEDED(m_device->GetProperty(DIPROP_RANGE, &range.diph)))
{
int offset = -1;
if (GUID_Slider ==i->guidType)
{
// max of 2 sliders / limit of used data format
if (cur_slider < 2)
offset = 6 + cur_slider++;
}
else
{
// don't add duplicate axes, some buggy drivers report the same axis twice
const std::map<GUID,int>::iterator f = types.find(i->guidType);
if (types.end() != f)
{
offset = f->second;
// remove from the map so it isn't added again
types.erase(f);
}
}
if ( offset >= 0 )
{
const LONG base = (range.lMin + range.lMax) / 2;
// each axis gets a negative and a positive input instance associated with it
AddInput( new Axis( offset, base, range.lMin-base ) );
AddInput( new Axis( offset, base, range.lMax-base ) );
}
}
}
// get supported ff effects
std::list<DIDEVICEOBJECTINSTANCE> objects;
m_device->EnumObjects(DIEnumDeviceObjectsCallback, (LPVOID)&objects, DIDFT_AXIS);
// got some ff axes or something
if ( objects.size() )
{
// temporary
DWORD rgdwAxes[] = { DIJOFS_X, DIJOFS_Y };
LONG rglDirection[] = { 0, 0 };
DICONSTANTFORCE cf = { 0 };
DIEFFECT eff;
ZeroMemory(&eff, sizeof(DIEFFECT));
eff.dwSize = sizeof(DIEFFECT);
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.dwDuration = INFINITE;
eff.dwGain = DI_FFNOMINALMAX;
eff.dwTriggerButton = DIEB_NOTRIGGER;
eff.cAxes = std::min( (DWORD)2, (DWORD)objects.size() );
eff.rgdwAxes = rgdwAxes;
eff.rglDirection = rglDirection;
eff.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
eff.lpvTypeSpecificParams = &cf;
LPDIRECTINPUTEFFECT pEffect;
if (SUCCEEDED(m_device->CreateEffect(GUID_ConstantForce, &eff, &pEffect, NULL)))
{
// temp
AddOutput( new Force( 0 ) );
m_state_out.push_back( EffectState( pEffect ) );
}
}
// disable autocentering
if ( Outputs().size() )
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof( DIPROPDWORD );
dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER );
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = DIPROPAUTOCENTER_OFF;
m_device->SetProperty( DIPROP_AUTOCENTER, &dipdw.diph );
}
ClearInputState();
}
Joystick::~Joystick()
{
// release the ff effect iface's
std::vector<EffectState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
{
i->iface->Stop();
i->iface->Unload();
i->iface->Release();
}
m_device->Unacquire();
m_device->Release();
}
void Joystick::ClearInputState()
{
ZeroMemory(&m_state_in, sizeof(m_state_in));
// set hats to center
memset( m_state_in.rgdwPOV, 0xFF, sizeof(m_state_in.rgdwPOV) );
}
std::string Joystick::GetName() const
{
return GetDeviceName(m_device);
}
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::GetSource() const
{
return DINPUT_SOURCE_NAME;
}
// update IO
bool Joystick::UpdateInput()
{
HRESULT hr = 0;
if (m_must_poll)
{
m_device->Poll();
hr = m_device->GetDeviceState(sizeof(m_state_in), &m_state_in);
}
else
{
DIDEVICEOBJECTDATA evtbuf[DATA_BUFFER_SIZE];
DWORD numevents;
//DWORD wantevents = INFINITE;
//hr = m_device->GetDeviceData(sizeof(*evtbuf), NULL, &wantevents, DIGDD_PEEK);
GETDEVDATA :
numevents = DATA_BUFFER_SIZE;
hr = m_device->GetDeviceData(sizeof(*evtbuf), evtbuf, &numevents, 0);
if (SUCCEEDED(hr))
{
for (LPDIDEVICEOBJECTDATA evt = evtbuf; evt != (evtbuf + numevents); ++evt)
{
// all the buttons are at the end of the data format
// they are bytes rather than longs
if (evt->dwOfs < DIJOFS_BUTTON(0))
*(DWORD*)(((BYTE*)&m_state_in) + evt->dwOfs) = evt->dwData;
else
((BYTE*)&m_state_in)[evt->dwOfs] = (BYTE)evt->dwData;
}
// if there is more data to be received
// it seems like to me this could cause problems,
// if the device was producing so many events that GetDeviceData always returned bufferoverflow
// it seems to be working fine tho
if (DI_BUFFEROVERFLOW == hr)
goto GETDEVDATA;
}
}
// try reacquire if input lost
if (DIERR_INPUTLOST == hr || DIERR_NOTACQUIRED == hr)
hr = m_device->Acquire();
return SUCCEEDED(hr);
}
bool Joystick::UpdateOutput()
{
// temporary
size_t ok_count = 0;
std::vector<EffectState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
{
if ( i->changed )
{
i->changed = false;
DICONSTANTFORCE cf;
cf.lMagnitude = LONG(10000 * i->magnitude);
if ( cf.lMagnitude )
{
DIEFFECT eff;
ZeroMemory( &eff, sizeof( eff ) );
eff.dwSize = sizeof( DIEFFECT );
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.cbTypeSpecificParams = sizeof( cf );
eff.lpvTypeSpecificParams = &cf;
// set params and start effect
ok_count += SUCCEEDED(i->iface->SetParameters(&eff, DIEP_TYPESPECIFICPARAMS | DIEP_START));
}
else
ok_count += SUCCEEDED(i->iface->Stop());
}
else
++ok_count;
}
return ( m_state_out.size() == ok_count );
}
// get name
std::string Joystick::Button::GetName() const
{
std::ostringstream ss;
ss << "Button " << m_index;
return ss.str();
}
std::string Joystick::Axis::GetName() const
{
std::ostringstream ss;
// axis
if ( m_index < 6 )
{
ss << "Axis " << "XYZ"[m_index%3];
if ( m_index > 2 )
ss << 'r';
}
// slider
else
ss << "Slider " << m_index-6;
ss << ( m_range>0 ? '+' : '-' );
return ss.str();
}
std::string Joystick::Hat::GetName() const
{
std::ostringstream ss;
ss << "Hat " << m_index << ' ' << "NESW"[m_direction];
return ss.str();
}
std::string Joystick::Force::GetName() const
{
// temporary
return "Constant";
}
// get / set state
ControlState Joystick::GetInputState( const ControllerInterface::Device::Input* const input ) const
{
return ((Input*)input)->GetState( &m_state_in );
}
void Joystick::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
((Output*)output)->SetState( state, &m_state_out[0] );
}
// get / set state
ControlState Joystick::Axis::GetState( const DIJOYSTATE* const joystate ) const
{
return std::max( 0.0f, ControlState((&joystate->lX)[m_index]-m_base) / m_range );
}
ControlState Joystick::Button::GetState( const DIJOYSTATE* const joystate ) const
{
return ControlState( joystate->rgbButtons[m_index] > 0 );
}
ControlState Joystick::Hat::GetState( const DIJOYSTATE* const joystate ) const
{
// can this func be simplified ?
const DWORD val = joystate->rgdwPOV[m_index];
// hat centered code from msdn
if ( 0xFFFF == LOWORD(val) )
return 0;
return ( abs( (int)(val/4500-m_direction*2+8)%8 - 4) > 2 );
}
void Joystick::Force::SetState( const ControlState state, Joystick::EffectState* const joystate )
{
joystate[m_index].magnitude = state;
joystate[m_index].changed = true;
}
}
}
#endif

View File

@ -0,0 +1,137 @@
#ifndef _CIFACE_DINPUT_JOYSTICK_H_
#define _CIFACE_DINPUT_JOYSTICK_H_
#include "../ControllerInterface.h"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
#ifdef CIFACE_USE_XINPUT
// this takes so long, idk if it should be enabled :(
#define NO_DUPLICATE_DINPUT_XINPUT
#include <wbemidl.h>
#include <oleauto.h>
#endif
namespace ciface
{
namespace DInput
{
void InitJoystick( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ );
class Joystick : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
struct EffectState
{
EffectState( LPDIRECTINPUTEFFECT eff ) : changed(0), iface(eff) {}
LPDIRECTINPUTEFFECT iface;
ControlState magnitude;
bool changed;
};
class Input : public ControllerInterface::Device::Input
{
friend class Joystick;
protected:
virtual ControlState GetState( const DIJOYSTATE* const joystate ) const = 0;
};
// can probably eliminate this base class
class Output : public ControllerInterface::Device::Output
{
friend class Joystick;
protected:
virtual void SetState( const ControlState state, EffectState* const joystate ) = 0;
};
class Button : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : m_index(index) {}
ControlState GetState( const DIJOYSTATE* const joystate ) const;
private:
const unsigned int m_index;
};
class Axis : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const LONG base, const LONG range ) : m_index(index), m_base(base), m_range(range) {}
ControlState GetState( const DIJOYSTATE* const joystate ) const;
private:
const unsigned int m_index;
const LONG m_base;
const LONG m_range;
};
class Hat : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Hat( const unsigned int index, const unsigned int direction ) : m_index(index), m_direction(direction) {}
ControlState GetState( const DIJOYSTATE* const joystate ) const;
private:
const unsigned int m_index;
const unsigned int m_direction;
};
class Force : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Force( const unsigned int index ) : m_index(index) {}
void SetState( const ControlState state, EffectState* const joystate );
private:
const unsigned int m_index;
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input ) const;
void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );
void ClearInputState();
public:
Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index );
~Joystick();
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
const LPDIRECTINPUTDEVICE8 m_device;
const unsigned int m_index;
//const std::string m_name;
DIJOYSTATE m_state_in;
std::vector<EffectState> m_state_out;
bool m_must_poll;
};
}
}
#endif

View File

@ -0,0 +1,272 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DINPUT_KBM
#include "DInputKeyboardMouse.h"
#include "DInput.h"
// (lower would be more sensitive) user can lower sensitivity by setting range
// seems decent here ( at 8 ), I dont think anyone would need more sensitive than this
// and user can lower it much farther than they would want to with the range
#define MOUSE_AXIS_SENSITIVITY 8
// if input hasn't been received for this many ms, mouse input will be skipped
// otherwise it is just some crazy value
#define DROP_INPUT_TIME 250
namespace ciface
{
namespace DInput
{
struct
{
const BYTE code;
const char* const name;
} named_keys[] =
{
#include "NamedKeys.h"
};
struct
{
const BYTE code;
const char* const name;
} named_lights[] =
{
{ VK_NUMLOCK, "NUM LOCK" },
{ VK_CAPITAL, "CAPS LOCK" },
{ VK_SCROLL, "SCROLL LOCK" }
};
void InitKeyboardMouse( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices )
{
// mouse and keyboard are a combined device, to allow shift+click and stuff
// if thats dumb, i will make a VirtualDevice class that just uses ranges of inputs/outputs from other devices
// so there can be a separated Keyboard and mouse, as well as combined KeyboardMouse
LPDIRECTINPUTDEVICE8 kb_device = NULL;
LPDIRECTINPUTDEVICE8 mo_device = NULL;
if (SUCCEEDED(idi8->CreateDevice( GUID_SysKeyboard, &kb_device, NULL)))
{
if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)))
if (SUCCEEDED(kb_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
if (SUCCEEDED(idi8->CreateDevice( GUID_SysMouse, &mo_device, NULL )))
{
if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)))
if (SUCCEEDED(mo_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
devices.push_back(new KeyboardMouse(kb_device, mo_device));
return;
}
}
}
}
if (kb_device)
kb_device->Release();
if (mo_device)
mo_device->Release();
}
KeyboardMouse::~KeyboardMouse()
{
// kb
m_kb_device->Unacquire();
m_kb_device->Release();
// mouse
m_mo_device->Unacquire();
m_mo_device->Release();
}
KeyboardMouse::KeyboardMouse( const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device )
: m_kb_device(kb_device)
, m_mo_device(mo_device)
{
m_kb_device->Acquire();
m_mo_device->Acquire();
m_last_update = GetTickCount();
ZeroMemory( &m_state_in, sizeof(m_state_in) );
ZeroMemory( m_state_out, sizeof(m_state_out) );
ZeroMemory( &m_current_state_out, sizeof(m_current_state_out) );
// KEYBOARD
// add keys
for ( unsigned int i = 0; i < sizeof(named_keys)/sizeof(*named_keys); ++i )
AddInput(new Key(i));
// add lights
for ( unsigned int i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i )
AddOutput(new Light(i));
// MOUSE
// get caps
DIDEVCAPS mouse_caps;
ZeroMemory( &mouse_caps, sizeof(mouse_caps) );
mouse_caps.dwSize = sizeof(mouse_caps);
m_mo_device->GetCapabilities(&mouse_caps);
// mouse buttons
for ( unsigned int i = 0; i < mouse_caps.dwButtons; ++i )
AddInput(new Button(i));
// mouse axes
for ( unsigned int i = 0; i < mouse_caps.dwAxes; ++i )
{
// each axis gets a negative and a positive input instance associated with it
AddInput(new Axis(i, (2==i) ? -1 : -MOUSE_AXIS_SENSITIVITY));
AddInput(new Axis(i, -(2==i) ? 1 : MOUSE_AXIS_SENSITIVITY));
}
}
bool KeyboardMouse::UpdateInput()
{
DIMOUSESTATE2 tmp_mouse;
// if mouse position hasn't been updated in a short while, skip a dev state
DWORD cur_time = GetTickCount();
if (cur_time - m_last_update > DROP_INPUT_TIME)
{
// set axes to zero
ZeroMemory(&m_state_in.mouse, sizeof(m_state_in.mouse));
// skip this input state
m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
}
m_last_update = cur_time;
HRESULT kb_hr = m_kb_device->GetDeviceState(sizeof(m_state_in.keyboard), &m_state_in.keyboard);
HRESULT mo_hr = m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
if (DIERR_INPUTLOST == kb_hr || DIERR_NOTACQUIRED == kb_hr)
m_kb_device->Acquire();
if (DIERR_INPUTLOST == mo_hr || DIERR_NOTACQUIRED == mo_hr)
m_mo_device->Acquire();
if (SUCCEEDED(kb_hr) && SUCCEEDED(mo_hr))
{
// need to smooth out the axes, otherwise it doesnt work for shit
for ( unsigned int i = 0; i < 3; ++i )
((&m_state_in.mouse.lX)[i] += (&tmp_mouse.lX)[i]) /= 2;
// copy over the buttons
memcpy( m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons) );
return true;
}
return false;
}
bool KeyboardMouse::UpdateOutput()
{
class KInput : public INPUT
{
public:
KInput( const unsigned char key, const bool up = false )
{
memset( this, 0, sizeof(*this) );
type = INPUT_KEYBOARD;
ki.wVk = key;
if (up) ki.dwFlags = KEYEVENTF_KEYUP;
}
};
std::vector< KInput > kbinputs;
for ( unsigned int i = 0; i < sizeof(m_state_out)/sizeof(*m_state_out); ++i )
{
bool want_on = false;
if ( m_state_out[i] )
want_on = m_state_out[i] > GetTickCount() % 255 ; // light should flash when output is 0.5
// lights are set to their original state when output is zero
if ( want_on ^ m_current_state_out[i] )
{
kbinputs.push_back( KInput( named_lights[i].code ) ); // press
kbinputs.push_back( KInput( named_lights[i].code, true ) ); // release
m_current_state_out[i] ^= 1;
}
}
if (kbinputs.size())
return ( kbinputs.size() == SendInput( (UINT)kbinputs.size(), &kbinputs[0], sizeof( kbinputs[0] ) ) );
else
return true;
}
std::string KeyboardMouse::GetName() const
{
return "Keyboard Mouse";
}
int KeyboardMouse::GetId() const
{
// should this be -1, idk
return 0;
}
std::string KeyboardMouse::GetSource() const
{
return DINPUT_SOURCE_NAME;
}
ControlState KeyboardMouse::GetInputState(const ControllerInterface::Device::Input* const input) const
{
return ( ((Input*)input)->GetState( &m_state_in ) );
}
void KeyboardMouse::SetOutputState(const ControllerInterface::Device::Output* const output, const ControlState state)
{
((Output*)output)->SetState( state, m_state_out );
}
// names
std::string KeyboardMouse::Key::GetName() const
{
return named_keys[m_index].name;
}
std::string KeyboardMouse::Button::GetName() const
{
return std::string("Button ") + char('0'+m_index);
}
std::string KeyboardMouse::Axis::GetName() const
{
std::string tmpstr("Axis ");
tmpstr += "XYZ"[m_index]; tmpstr += ( m_range>0 ? '+' : '-' );
return tmpstr;
}
std::string KeyboardMouse::Light::GetName() const
{
return named_lights[ m_index ].name;
}
// get/set state
ControlState KeyboardMouse::Key::GetState(const State* const state) const
{
return ( state->keyboard[named_keys[m_index].code] > 0 );
}
ControlState KeyboardMouse::Button::GetState(const State* const state) const
{
return ( state->mouse.rgbButtons[m_index] > 0 );
}
ControlState KeyboardMouse::Axis::GetState(const State* const state) const
{
return std::max( 0.0f, ControlState((&state->mouse.lX)[m_index]) / m_range );
}
void KeyboardMouse::Light::SetState(const ControlState state, unsigned char* const state_out)
{
state_out[m_index] = (unsigned char)(state * 255);
}
}
}
#endif

View File

@ -0,0 +1,122 @@
#ifndef _CIFACE_DINPUT_KBM_H_
#define _CIFACE_DINPUT_KBM_H_
#include "../ControllerInterface.h"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
namespace ciface
{
namespace DInput
{
void InitKeyboardMouse( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices );
class KeyboardMouse : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
struct State
{
BYTE keyboard[256];
DIMOUSESTATE2 mouse;
};
class Input : public ControllerInterface::Device::Input
{
friend class KeyboardMouse;
protected:
virtual ControlState GetState(const State* const boardstate) const = 0;
};
class Output : public ControllerInterface::Device::Output
{
friend class KeyboardMouse;
protected:
virtual void SetState(const ControlState state, unsigned char* const state_out) = 0;
};
class Key : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Key( const unsigned int index ) : m_index(index) {}
ControlState GetState(const State* const state) const;
private:
const unsigned int m_index;
};
class Button : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : m_index(index) {}
ControlState GetState(const State* const state) const;
private:
const unsigned int m_index;
};
class Axis : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const LONG range ) : m_index(index), m_range(range) {}
ControlState GetState(const State* const state) const;
private:
const unsigned int m_index;
const LONG m_range;
};
class Light : public Output
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Light( const unsigned int index ) : m_index(index) {}
void SetState( const ControlState state, unsigned char* const state_out );
private:
const unsigned int m_index;
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input ) const;
void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );
public:
KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device);
~KeyboardMouse();
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
const LPDIRECTINPUTDEVICE8 m_kb_device;
const LPDIRECTINPUTDEVICE8 m_mo_device;
DWORD m_last_update;
State m_state_in;
unsigned char m_state_out[3]; // NUM CAPS SCROLL
bool m_current_state_out[3]; // NUM CAPS SCROLL
};
}
}
#endif

View File

@ -0,0 +1,144 @@
{ DIK_A, "A" },
{ DIK_B, "B" },
{ DIK_C, "C" },
{ DIK_D, "D" },
{ DIK_E, "E" },
{ DIK_F, "F" },
{ DIK_G, "G" },
{ DIK_H, "H" },
{ DIK_I, "I" },
{ DIK_J, "J" },
{ DIK_K, "K" },
{ DIK_L, "L" },
{ DIK_M, "M" },
{ DIK_N, "N" },
{ DIK_O, "O" },
{ DIK_P, "P" },
{ DIK_Q, "Q" },
{ DIK_R, "R" },
{ DIK_S, "S" },
{ DIK_T, "T" },
{ DIK_U, "U" },
{ DIK_V, "V" },
{ DIK_W, "W" },
{ DIK_X, "X" },
{ DIK_Y, "Y" },
{ DIK_Z, "Z" },
{ DIK_0, "0" },
{ DIK_1, "1" },
{ DIK_2, "2" },
{ DIK_3, "3" },
{ DIK_4, "4" },
{ DIK_5, "5" },
{ DIK_6, "6" },
{ DIK_7, "7" },
{ DIK_8, "8" },
{ DIK_9, "9" },
{ DIK_UP, "UP" },
{ DIK_DOWN, "DOWN" },
{ DIK_LEFT, "LEFT" },
{ DIK_RIGHT, "RIGHT" },
{ DIK_ABNT_C1, "ABNT_C1" },
{ DIK_ABNT_C2, "ABNT_C2" },
{ DIK_ADD, "ADD" },
{ DIK_APOSTROPHE, "APOSTROPHE" },
{ DIK_APPS, "APPS" },
{ DIK_AT, "AT" },
{ DIK_AX, "AX" },
{ DIK_BACK, "BACK" },
{ DIK_BACKSLASH, "BACKSLASH" },
{ DIK_CALCULATOR, "CALCULATOR" },
{ DIK_CAPITAL, "CAPITAL" },
{ DIK_COLON, "COLON" },
{ DIK_COMMA, "COMMA" },
{ DIK_CONVERT, "CONVERT" },
{ DIK_DECIMAL, "DECIMAL" },
{ DIK_DELETE, "DELETE" },
{ DIK_DIVIDE, "DIVIDE" },
{ DIK_EQUALS, "EQUALS" },
{ DIK_ESCAPE, "ESCAPE" },
{ DIK_F1, "F1" },
{ DIK_F2, "F2" },
{ DIK_F3, "F3" },
{ DIK_F4, "F4" },
{ DIK_F5, "F5" },
{ DIK_F6, "F6" },
{ DIK_F7, "F7" },
{ DIK_F8, "F8" },
{ DIK_F9, "F9" },
{ DIK_F10, "F10" },
{ DIK_F11, "F11" },
{ DIK_F12, "F12" },
{ DIK_F13, "F13" },
{ DIK_F14, "F14" },
{ DIK_F15, "F15" },
{ DIK_GRAVE, "GRAVE" },
{ DIK_HOME, "HOME" },
{ DIK_END, "END" },
{ DIK_INSERT, "INSERT" },
{ DIK_KANA, "KANA" },
{ DIK_KANJI, "KANJI" },
{ DIK_MAIL, "MAIL" },
{ DIK_MEDIASELECT, "MEDIASELECT" },
{ DIK_MEDIASTOP, "MEDIASTOP" },
{ DIK_MINUS, "MINUS" },
{ DIK_MULTIPLY, "MULTIPLY" },
{ DIK_MUTE, "MUTE" },
{ DIK_MYCOMPUTER, "MYCOMPUTER" },
{ DIK_NEXTTRACK, "NEXTTRACK" },
{ DIK_NOCONVERT, "NOCONVERT" },
{ DIK_NUMLOCK, "NUMLOCK" },
{ DIK_NUMPAD0, "NUMPAD0" },
{ DIK_NUMPAD1, "NUMPAD1" },
{ DIK_NUMPAD2, "NUMPAD2" },
{ DIK_NUMPAD3, "NUMPAD3" },
{ DIK_NUMPAD4, "NUMPAD4" },
{ DIK_NUMPAD5, "NUMPAD5" },
{ DIK_NUMPAD6, "NUMPAD6" },
{ DIK_NUMPAD7, "NUMPAD7" },
{ DIK_NUMPAD8, "NUMPAD8" },
{ DIK_NUMPAD9, "NUMPAD9" },
{ DIK_NUMPADCOMMA, "NUMPADCOMMA" },
{ DIK_NUMPADENTER, "NUMPADENTER" },
{ DIK_NUMPADEQUALS, "NUMPADEQUALS" },
{ DIK_OEM_102, "OEM_102" },
{ DIK_PAUSE, "PAUSE" },
{ DIK_PERIOD, "PERIOD" },
{ DIK_PLAYPAUSE, "PLAYPAUSE" },
{ DIK_POWER, "POWER" },
{ DIK_PREVTRACK, "PREVTRACK" },
{ DIK_PRIOR, "PRIOR" },
{ DIK_NEXT, "NEXT" },
{ DIK_RETURN, "RETURN" },
{ DIK_LBRACKET, "LBRACKET" },
{ DIK_RBRACKET, "RBRACKET" },
{ DIK_LCONTROL, "LCONTROL" },
{ DIK_RCONTROL, "RCONTROL" },
{ DIK_LMENU, "LMENU" },
{ DIK_RMENU, "RMENU" },
{ DIK_LSHIFT, "LSHIFT" },
{ DIK_RSHIFT, "RSHIFT" },
{ DIK_LWIN, "LWIN" },
{ DIK_RWIN, "RWIN" },
{ DIK_SCROLL, "SCROLL" },
{ DIK_SEMICOLON, "SEMICOLON" },
{ DIK_SLASH, "SLASH" },
{ DIK_SLEEP, "SLEEP" },
{ DIK_SPACE, "SPACE" },
{ DIK_STOP, "STOP" },
{ DIK_SUBTRACT, "SUBTRACT" },
{ DIK_SYSRQ, "SYSRQ" },
{ DIK_TAB, "TAB" },
{ DIK_UNDERLINE, "UNDERLINE" },
{ DIK_UNLABELED, "UNLABELED" },
{ DIK_VOLUMEDOWN, "VOLUMEDOWN" },
{ DIK_VOLUMEUP, "VOLUMEUP" },
{ DIK_WAKE, "WAKE" },
{ DIK_WEBBACK, "WEBBACK" },
{ DIK_WEBFAVORITES, "WEBFAVORITES" },
{ DIK_WEBFORWARD, "WEBFORWARD" },
{ DIK_WEBHOME, "WEBHOME" },
{ DIK_WEBREFRESH, "WEBREFRESH" },
{ DIK_WEBSEARCH, "WEBSEARCH" },
{ DIK_WEBSTOP, "WEBSTOP" },
{ DIK_YEN, "YEN" },