From 9e3b65368873fd26f21e2568b2c67d00ec96d6a5 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 21 Jun 2010 03:12:16 +0000 Subject: GCPad/New Wiimote Plugin: Individual keyboard and mouse devices are now listed on Windows(2 player with 2 keyboards possible). Improved the ability to map multiple inputs to the same control. Inputs from different devices can be mapped to the same button (example: Mouse Left and XInput A). More advanced mappings such as "Button 1 or 2 and NOT button 3" are possible. I hope the GUI after right clicking a button isn't too confusing(may change it to be a bit more user friendly). Hopefully, I didn't break OSX stuff by 'const'ing a few functions. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5757 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../DirectInput/DirectInput.cpp | 61 ++++- .../ControllerInterface/DirectInput/DirectInput.h | 12 +- .../DirectInput/DirectInputJoystick.cpp | 89 +++---- .../DirectInput/DirectInputJoystick.h | 10 +- .../DirectInput/DirectInputKeyboard.cpp | 187 ++++++++++++++ .../DirectInput/DirectInputKeyboard.h | 99 ++++++++ .../DirectInput/DirectInputKeyboardMouse.cpp | 274 --------------------- .../DirectInput/DirectInputKeyboardMouse.h | 125 ---------- .../DirectInput/DirectInputMouse.cpp | 175 +++++++++++++ .../DirectInput/DirectInputMouse.h | 83 +++++++ 10 files changed, 642 insertions(+), 473 deletions(-) create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.h delete mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.cpp delete mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.h create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.cpp create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.h (limited to 'Source/Core/InputCommon/Src/ControllerInterface/DirectInput') diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.cpp index b1f4bb067a..d36e76b3c1 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.cpp @@ -4,6 +4,18 @@ #include "DirectInput.h" +#include + +#ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK + #include "DirectInputJoystick.h" +#endif +#ifdef CIFACE_USE_DIRECTINPUT_KEYBOARD + #include "DirectInputKeyboard.h" +#endif +#ifdef CIFACE_USE_DIRECTINPUT_MOUSE + #include "DirectInputMouse.h" +#endif + #pragma comment(lib, "Dinput8.lib") #pragma comment(lib, "dxguid.lib") @@ -12,17 +24,60 @@ namespace ciface namespace DirectInput { +//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef) +//{ +// ((std::list*)pvRef)->push_back(*pdei); +// return DIENUM_CONTINUE; +//} + +BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) +{ + ((std::list*)pvRef)->push_back(*lpddoi); + return DIENUM_CONTINUE; +} + +BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) +{ + ((std::list*)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& devices/*, HWND hwnd*/ ) { IDirectInput8* idi8; if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&idi8, NULL))) return; -#ifdef CIFACE_USE_DIRECTINPUT_KBM - InitKeyboardMouse( idi8, devices ); +#ifdef CIFACE_USE_DIRECTINPUT_KEYBOARD + InitKeyboard(idi8, devices); +#endif +#ifdef CIFACE_USE_DIRECTINPUT_MOUSE + InitMouse(idi8, devices); #endif #ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK - InitJoystick( idi8, devices/*, hwnd*/ ); + InitJoystick(idi8, devices/*, hwnd*/); #endif idi8->Release(); diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.h b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.h index 7edbe3abf8..9f4a045a55 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInput.h @@ -9,18 +9,18 @@ #include #include -#ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK - #include "DirectInputJoystick.h" -#endif -#ifdef CIFACE_USE_DIRECTINPUT_KBM - #include "DirectInputKeyboardMouse.h" -#endif +#include namespace ciface { namespace DirectInput { +//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& devices/*, HWND hwnd*/ ); } diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp index a0ef031639..0cd7459187 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp @@ -3,7 +3,7 @@ #ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK #include "DirectInputJoystick.h" -#include +#include "DirectInput.h" inline bool operator<(const GUID & lhs, const GUID & rhs) { @@ -123,41 +123,9 @@ LCleanup: } #endif -std::string TStringToString( const std::basic_string& in ) -{ - const int size = WideCharToMultiByte(CP_ACP, 0, in.data(), int(in.length()), NULL, 0, NULL, NULL); - - if ( 0 == size ) - return ""; - - char* const data = new char[size]; - WideCharToMultiByte(CP_ACP, 0, in.data(), int(in.length()), data, size, NULL, NULL); - const std::string out( data, size ); - delete[] data; - return out; -} - -//BOOL CALLBACK DIEnumEffectsCallback( LPCDIEFFECTINFO pdei, LPVOID pvRef ) -//{ -// ((std::vector*)pvRef)->push_back( *pdei ); -// return DIENUM_CONTINUE; -//} - -BOOL CALLBACK DIEnumDeviceObjectsCallback( LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef ) -{ - ((std::vector*)pvRef)->push_back( *lpddoi ); - return DIENUM_CONTINUE; -} - -BOOL CALLBACK DIEnumDevicesCallback( LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef ) -{ - ((std::vector*)pvRef)->push_back( *lpddi ); - return DIENUM_CONTINUE; -} - void InitJoystick( IDirectInput8* const idi8, std::vector& devices/*, HWND hwnd*/ ) { - std::vector joysticks; + std::list joysticks; idi8->EnumDevices( DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, DIEDFL_ATTACHEDONLY ); // this is used to number the joysticks @@ -169,7 +137,8 @@ void InitJoystick( IDirectInput8* const idi8, std::vector::iterator i = joysticks.begin(), + std::list::iterator + i = joysticks.begin(), e = joysticks.end(); for ( ; i!=e; ++i ) { @@ -247,17 +216,17 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI // buttons for ( unsigned int i = 0; i < js_caps.dwButtons; ++i ) - inputs.push_back( new Button( 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 ) - inputs.push_back( new Hat( i, d ) ); + AddInput( new Hat( i, d ) ); } // get up to 6 axes and 2 sliders - std::vector axes; - m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_ABSAXIS ); + std::list axes; + m_device->EnumObjects(DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_ABSAXIS); unsigned int cur_slider = 0; @@ -271,7 +240,8 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI types[GUID_RzAxis] = 5; // going in reverse leaves the list more organized in the end for me :/ - std::vector::const_reverse_iterator i = axes.rbegin(), + std::list::const_reverse_iterator + i = axes.rbegin(), e = axes.rend(); for( ; i!=e; ++i ) { @@ -287,7 +257,7 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI // 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))) + if (SUCCEEDED(m_device->GetProperty(DIPROP_RANGE, &range.diph))) { int offset = -1; @@ -314,15 +284,15 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI { const LONG base = (range.lMin + range.lMax) / 2; // each axis gets a negative and a positive input instance associated with it - inputs.push_back( new Axis( offset, base, range.lMin-base ) ); - inputs.push_back( new Axis( offset, base, range.lMax-base ) ); + AddInput( new Axis( offset, base, range.lMin-base ) ); + AddInput( new Axis( offset, base, range.lMax-base ) ); } } } // get supported ff effects - std::vector objects; - m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&objects, DIDFT_AXIS ); + std::list objects; + m_device->EnumObjects(DIEnumDeviceObjectsCallback, (LPVOID)&objects, DIDFT_AXIS); // got some ff axes or something if ( objects.size() ) { @@ -347,13 +317,13 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI if (SUCCEEDED(m_device->CreateEffect(GUID_ConstantForce, &eff, &pEffect, NULL))) { // temp - outputs.push_back( new Force( 0 ) ); + AddOutput( new Force( 0 ) ); m_state_out.push_back( EffectState( pEffect ) ); } } // disable autocentering - if ( outputs.size() ) + if ( Outputs().size() ) { DIPROPDWORD dipdw; dipdw.diph.dwSize = sizeof( DIPROPDWORD ); @@ -392,14 +362,7 @@ void Joystick::ClearInputState() std::string Joystick::GetName() const { - DIPROPSTRING str; - ZeroMemory( &str, sizeof(str) ); - str.diph.dwSize = sizeof(str); - str.diph.dwHeaderSize = sizeof(str.diph); - str.diph.dwHow = DIPH_DEVICE; - m_device->GetProperty( DIPROP_PRODUCTNAME, &str.diph ); - return StripSpaces(TStringToString(str.wsz)); - //return m_name; + return GetDeviceName(m_device); } int Joystick::GetId() const @@ -409,7 +372,7 @@ int Joystick::GetId() const std::string Joystick::GetSource() const { - return "DirectInput"; + return DINPUT_SOURCE_NAME; } // update IO @@ -428,6 +391,9 @@ bool Joystick::UpdateInput() 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); @@ -445,6 +411,9 @@ GETDEVDATA : } // 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; } @@ -534,7 +503,7 @@ std::string Joystick::Force::GetName() const // get / set state -ControlState Joystick::GetInputState( const ControllerInterface::Device::Input* const input ) +ControlState Joystick::GetInputState( const ControllerInterface::Device::Input* const input ) const { return ((Input*)input)->GetState( &m_state_in ); } @@ -546,17 +515,17 @@ void Joystick::SetOutputState( const ControllerInterface::Device::Output* const // get / set state -ControlState Joystick::Axis::GetState( const DIJOYSTATE* const joystate ) +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 ) +ControlState Joystick::Button::GetState( const DIJOYSTATE* const joystate ) const { return ControlState( joystate->rgbButtons[m_index] > 0 ); } -ControlState Joystick::Hat::GetState( const DIJOYSTATE* const joystate ) +ControlState Joystick::Hat::GetState( const DIJOYSTATE* const joystate ) const { // can this func be simplified ? const DWORD val = joystate->rgdwPOV[m_index]; diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.h b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.h index a42a7cb4a9..bdee377f72 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.h @@ -42,7 +42,7 @@ protected: { friend class Joystick; protected: - virtual ControlState GetState( const DIJOYSTATE* const joystate ) = 0; + virtual ControlState GetState( const DIJOYSTATE* const joystate ) const = 0; }; // can probably eliminate this base class @@ -60,7 +60,7 @@ protected: std::string GetName() const; protected: Button( const unsigned int index ) : m_index(index) {} - ControlState GetState( const DIJOYSTATE* const joystate ); + ControlState GetState( const DIJOYSTATE* const joystate ) const; private: const unsigned int m_index; }; @@ -72,7 +72,7 @@ protected: 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 ); + ControlState GetState( const DIJOYSTATE* const joystate ) const; private: const unsigned int m_index; const LONG m_base; @@ -86,7 +86,7 @@ protected: 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 ); + ControlState GetState( const DIJOYSTATE* const joystate ) const; private: const unsigned int m_index; const unsigned int m_direction; @@ -107,7 +107,7 @@ protected: bool UpdateInput(); bool UpdateOutput(); - ControlState GetInputState( const ControllerInterface::Device::Input* const input ); + ControlState GetInputState( const ControllerInterface::Device::Input* const input ) const; void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state ); void ClearInputState(); diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp new file mode 100644 index 0000000000..6f39467deb --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp @@ -0,0 +1,187 @@ +#include "../ControllerInterface.h" + +#ifdef CIFACE_USE_DIRECTINPUT_KEYBOARD + +#include "DirectInputKeyboard.h" +#include "DirectInput.h" + +// TODO: maybe add a ClearInputState function to this device + +namespace ciface +{ +namespace DirectInput +{ + +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 InitKeyboard(IDirectInput8* const idi8, std::vector& devices) +{ + LPDIRECTINPUTDEVICE8 kb_device; + + unsigned int kb_count = 0; + + std::list keyboards; + idi8->EnumDevices(DI8DEVCLASS_KEYBOARD, DIEnumDevicesCallback, (LPVOID)&keyboards, DIEDFL_ATTACHEDONLY); + + // add other keyboard devices + std::list::iterator + i = keyboards.begin(), + e = keyboards.end(); + for (; i!=e; ++i) + { + if (SUCCEEDED(idi8->CreateDevice(i->guidInstance, &kb_device, NULL))) + { + if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard))) + if (SUCCEEDED(kb_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) + { + devices.push_back(new Keyboard(kb_device, kb_count++)); + return; + } + } + kb_device->Release(); + } +} + +Keyboard::~Keyboard() +{ + // kb + m_kb_device->Unacquire(); + m_kb_device->Release(); + +} + +Keyboard::Keyboard(const LPDIRECTINPUTDEVICE8 kb_device, const int index) + : m_kb_device(kb_device) + , m_index(index) +{ + m_kb_device->Acquire(); + + 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)); +} + +bool Keyboard::UpdateInput() +{ + HRESULT hr = m_kb_device->GetDeviceState(sizeof(m_state_in), m_state_in); + + if (DIERR_INPUTLOST == hr || DIERR_NOTACQUIRED == hr) + m_kb_device->Acquire(); + + return SUCCEEDED(hr); +} + +bool Keyboard::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] > wxGetLocalTimeMillis() % 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 Keyboard::GetName() const +{ + return "Keyboard"; +} + +int Keyboard::GetId() const +{ + return m_index; +} + +std::string Keyboard::GetSource() const +{ + return DINPUT_SOURCE_NAME; +} + +ControlState Keyboard::GetInputState(const ControllerInterface::Device::Input* const input) const +{ + return (((Input*)input)->GetState(m_state_in)); +} + +void Keyboard::SetOutputState(const ControllerInterface::Device::Output* const output, const ControlState state) +{ + ((Output*)output)->SetState(state, m_state_out); +} + +// names +std::string Keyboard::Key::GetName() const +{ + return named_keys[m_index].name; +} + +std::string Keyboard::Light::GetName() const +{ + return named_lights[ m_index ].name; +} + +// get/set state +ControlState Keyboard::Key::GetState(const BYTE keys[]) const +{ + return (keys[named_keys[m_index].code] != 0); +} + +void Keyboard::Light::SetState( const ControlState state, unsigned char* const state_out ) +{ + state_out[m_index] = state * 255; +} + +} +} + +#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.h b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.h new file mode 100644 index 0000000000..b51a38faa4 --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.h @@ -0,0 +1,99 @@ +#ifndef _CIFACE_DIRECTINPUT_KEYBOARD_H_ +#define _CIFACE_DIRECTINPUT_KEYBOARD_H_ + +#include "../ControllerInterface.h" + +#define DIRECTINPUT_VERSION 0x0800 +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include +#include + +#include +#include + +namespace ciface +{ +namespace DirectInput +{ + +void InitKeyboard(IDirectInput8* const idi8, std::vector& devices); + +class Keyboard : public ControllerInterface::Device +{ + friend class ControllerInterface; + friend class ControllerInterface::ControlReference; + +protected: + + struct State + { + + }; + + class Input : public ControllerInterface::Device::Input + { + friend class Keyboard; + protected: + virtual ControlState GetState(const BYTE keys[]) const = 0; + }; + + class Output : public ControllerInterface::Device::Output + { + friend class Keyboard; + protected: + virtual void SetState(const ControlState state, unsigned char* const state_out) = 0; + }; + + class Key : public Input + { + friend class Keyboard; + public: + std::string GetName() const; + protected: + Key(const unsigned int index) : m_index(index) {} + ControlState GetState(const BYTE keys[]) const; + private: + const unsigned int m_index; + }; + + class Light : public Output + { + friend class Keyboard; + 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: + Keyboard(const LPDIRECTINPUTDEVICE8 kb_device, const int index); + ~Keyboard(); + + std::string GetName() const; + int GetId() const; + std::string GetSource() const; + +private: + const LPDIRECTINPUTDEVICE8 m_kb_device; + const int m_index; + + wxLongLong m_last_update; + BYTE m_state_in[256]; + unsigned char m_state_out[3]; // NUM CAPS SCROLL + bool m_current_state_out[3]; // NUM CAPS SCROLL +}; + +} +} + +#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.cpp deleted file mode 100644 index 76ac358d7c..0000000000 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.cpp +++ /dev/null @@ -1,274 +0,0 @@ -#include "../ControllerInterface.h" - -#ifdef CIFACE_USE_DIRECTINPUT_KBM - -#include "DirectInputKeyboardMouse.h" - -// TODO: maybe add a ClearInputState function to this device - - // (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 DirectInput -{ - -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& 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 = wxGetLocalTimeMillis(); - - 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 ) - inputs.push_back( new Key( i ) ); - // add lights - for ( unsigned int i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i ) - outputs.push_back( 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 ) - inputs.push_back( 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 - inputs.push_back( new Axis( i, (2==i) ? -1 : -MOUSE_AXIS_SENSITIVITY ) ); - inputs.push_back( 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 - wxLongLong cur_time = wxGetLocalTimeMillis(); - 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] > wxGetLocalTimeMillis() % 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 "DirectInput"; -} - -ControlState KeyboardMouse::GetInputState( const ControllerInterface::Device::Input* const input ) -{ - 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("Mouse "); - 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 ) -{ - return ( state->keyboard[named_keys[m_index].code] > 0 ); -} - -ControlState KeyboardMouse::Button::GetState( const State* const state ) -{ - return ( state->mouse.rgbButtons[m_index] > 0 ); -} - -ControlState KeyboardMouse::Axis::GetState( const State* const state ) -{ - 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 ] = state * 255; -} - -} -} - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.h b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.h deleted file mode 100644 index 6af5fb10d0..0000000000 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboardMouse.h +++ /dev/null @@ -1,125 +0,0 @@ -#ifndef _CIFACE_DIRECTINPUT_KBM_H_ -#define _CIFACE_DIRECTINPUT_KBM_H_ - -#include "../ControllerInterface.h" - -#define DIRECTINPUT_VERSION 0x0800 -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX -#include -#include - -#include -#include - -namespace ciface -{ -namespace DirectInput -{ - -void InitKeyboardMouse( IDirectInput8* const idi8, std::vector& 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 ) = 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 ); - 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 ); - 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 ); - 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 ); - 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; - - wxLongLong 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 diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.cpp new file mode 100644 index 0000000000..122582bf4f --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.cpp @@ -0,0 +1,175 @@ +#include "../ControllerInterface.h" + +#ifdef CIFACE_USE_DIRECTINPUT_MOUSE + +#include "DirectInputMouse.h" +#include "DirectInput.h" + +// TODO: maybe add a ClearInputState function to this device + + // (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 DirectInput +{ + +void InitMouse( IDirectInput8* const idi8, std::vector& devices ) +{ + LPDIRECTINPUTDEVICE8 mo_device; + + unsigned int mo_count = 0; + + std::list mice; + idi8->EnumDevices(DI8DEVCLASS_POINTER, DIEnumDevicesCallback, (LPVOID)&mice, DIEDFL_ATTACHEDONLY); + + // add other keyboard devices + std::list::iterator + i = mice.begin(), + e = mice.end(); + for (; i!=e; ++i) + { + if (SUCCEEDED(idi8->CreateDevice(i->guidInstance, &mo_device, NULL))) + { + if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2))) + if (SUCCEEDED(mo_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) + { + devices.push_back(new Mouse(mo_device, mo_count++)); + return; + } + } + mo_device->Release(); + } +} + +Mouse::~Mouse() +{ + // mouse + m_mo_device->Unacquire(); + m_mo_device->Release(); +} + +Mouse::Mouse(const LPDIRECTINPUTDEVICE8 mo_device, const int index) + : m_mo_device(mo_device) + , m_index(index) +{ + m_mo_device->Acquire(); + + m_last_update = GetTickCount(); + ZeroMemory(&m_state_in, sizeof(m_state_in)); + + // 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 Mouse::UpdateInput() +{ + DIMOUSESTATE2 tmp_mouse; + HRESULT hr = m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse); + + // if mouse position hasn't been updated in a short while, skip a dev state + const DWORD cur_time = GetTickCount(); + if (cur_time - m_last_update > DROP_INPUT_TIME) + { + // set buttons/axes to zero + // skip this input state + ZeroMemory(&m_state_in, sizeof(m_state_in)); + } + else if (SUCCEEDED(hr)) + { + // need to smooth out the axes, otherwise it doesnt work for shit + for (unsigned int i = 0; i < 3; ++i) + ((&m_state_in.lX)[i] += (&tmp_mouse.lX)[i]) /= 2; + + // copy over the buttons + memcpy(m_state_in.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.rgbButtons)); + } + + m_last_update = cur_time; + + if (DIERR_INPUTLOST == hr || DIERR_NOTACQUIRED == hr) + m_mo_device->Acquire(); + + return SUCCEEDED(hr); +} + +bool Mouse::UpdateOutput() +{ + return true; +} + +std::string Mouse::GetName() const +{ + return "Mouse"; +} + +int Mouse::GetId() const +{ + return m_index; +} + +std::string Mouse::GetSource() const +{ + return DINPUT_SOURCE_NAME; +} + +ControlState Mouse::GetInputState( const ControllerInterface::Device::Input* const input ) const +{ + return (((Input*)input)->GetState(&m_state_in)); +} + +void Mouse::SetOutputState(const ControllerInterface::Device::Output* const output, const ControlState state) +{ + return; +} + +// names +std::string Mouse::Button::GetName() const +{ + return std::string("Button ") + char('0'+m_index); +} + +std::string Mouse::Axis::GetName() const +{ + std::string tmpstr("Mouse "); + tmpstr += "XYZ"[m_index]; tmpstr += ( m_range>0 ? '+' : '-' ); + return tmpstr; +} + +// get/set state +ControlState Mouse::Button::GetState(const DIMOUSESTATE2* const state) const +{ + return (state->rgbButtons[m_index] != 0); +} + +ControlState Mouse::Axis::GetState(const DIMOUSESTATE2* const state) const +{ + return std::max(0.0f, ControlState((&state->lX)[m_index]) / m_range); +} + +} +} + +#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.h b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.h new file mode 100644 index 0000000000..c645d02727 --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputMouse.h @@ -0,0 +1,83 @@ +#ifndef _CIFACE_DIRECTINPUT_MOUSE_H_ +#define _CIFACE_DIRECTINPUT_MOUSE_H_ + +#include "../ControllerInterface.h" + +#define DIRECTINPUT_VERSION 0x0800 +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include +#include + +namespace ciface +{ +namespace DirectInput +{ + +void InitMouse(IDirectInput8* const idi8, std::vector& devices); + +class Mouse : public ControllerInterface::Device +{ + friend class ControllerInterface; + friend class ControllerInterface::ControlReference; + +protected: + + class Input : public ControllerInterface::Device::Input + { + friend class Mouse; + protected: + virtual ControlState GetState(const DIMOUSESTATE2* const boardstate) const = 0; + }; + + class Button : public Input + { + friend class Mouse; + public: + std::string GetName() const; + protected: + Button(const unsigned int index) : m_index(index) {} + ControlState GetState(const DIMOUSESTATE2* const state) const; + private: + const unsigned int m_index; + }; + + class Axis : public Input + { + friend class Mouse; + public: + std::string GetName() const; + protected: + Axis(const unsigned int index, const LONG range) : m_index(index), m_range(range) {} + ControlState GetState(const DIMOUSESTATE2* const state) const; + private: + const unsigned int m_index; + const LONG m_range; + }; + + 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: + Mouse(const LPDIRECTINPUTDEVICE8 mo_device, const int index); + ~Mouse(); + + std::string GetName() const; + int GetId() const; + std::string GetSource() const; + +private: + const LPDIRECTINPUTDEVICE8 m_mo_device; + const int m_index; + + DWORD m_last_update; + DIMOUSESTATE2 m_state_in; +}; + +} +} + +#endif -- cgit v1.2.3