From 877106b027431c6edb2d2c702fa6d811dac3d250 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sun, 16 Jun 2013 20:07:10 -0400 Subject: [PATCH] InputCommon: Split Device stuff out The ExpressionParser needs this to be out of here to prevent issues with cyclic references. --- Source/Core/DolphinWX/Src/InputConfigDiag.cpp | 18 +- Source/Core/DolphinWX/Src/InputConfigDiag.h | 2 +- Source/Core/InputCommon/CMakeLists.txt | 3 +- Source/Core/InputCommon/InputCommon.vcxproj | 4 +- .../InputCommon/InputCommon.vcxproj.filters | 3 + Source/Core/InputCommon/Src/ControllerEmu.h | 3 +- .../ControllerInterface/Android/Android.cpp | 2 +- .../Src/ControllerInterface/Android/Android.h | 6 +- .../ControllerInterface.cpp | 186 +---------------- .../ControllerInterface/ControllerInterface.h | 161 +-------------- .../Src/ControllerInterface/DInput/DInput.cpp | 19 +- .../Src/ControllerInterface/DInput/DInput.h | 4 +- .../DInput/DInputJoystick.cpp | 11 +- .../DInput/DInputJoystick.h | 13 +- .../DInput/DInputKeyboardMouse.cpp | 17 +- .../DInput/DInputKeyboardMouse.h | 6 +- .../Src/ControllerInterface/Device.cpp | 193 ++++++++++++++++++ .../Src/ControllerInterface/Device.h | 171 ++++++++++++++++ .../Src/ControllerInterface/OSX/OSX.h | 4 +- .../Src/ControllerInterface/OSX/OSX.mm | 9 +- .../Src/ControllerInterface/OSX/OSXJoystick.h | 4 +- .../ControllerInterface/OSX/OSXJoystick.mm | 3 +- .../Src/ControllerInterface/OSX/OSXKeyboard.h | 4 +- .../ControllerInterface/OSX/OSXKeyboard.mm | 3 +- .../Src/ControllerInterface/SDL/SDL.cpp | 11 +- .../Src/ControllerInterface/SDL/SDL.h | 10 +- .../Src/ControllerInterface/XInput/XInput.cpp | 7 +- .../Src/ControllerInterface/XInput/XInput.h | 14 +- .../Src/ControllerInterface/Xlib/Xlib.cpp | 2 +- .../Src/ControllerInterface/Xlib/Xlib.h | 6 +- 30 files changed, 448 insertions(+), 451 deletions(-) create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/Device.cpp create mode 100644 Source/Core/InputCommon/Src/ControllerInterface/Device.h diff --git a/Source/Core/DolphinWX/Src/InputConfigDiag.cpp b/Source/Core/DolphinWX/Src/InputConfigDiag.cpp index ef4ae8d29b..ae52c7dbf1 100644 --- a/Source/Core/DolphinWX/Src/InputConfigDiag.cpp +++ b/Source/Core/DolphinWX/Src/InputConfigDiag.cpp @@ -184,13 +184,13 @@ void ControlDialog::UpdateListContents() { control_lbox->Clear(); - ControllerInterface::Device* const dev = g_controller_interface.FindDevice(m_devq); + Device* const dev = g_controller_interface.FindDevice(m_devq); if (dev) { if (control_reference->is_input) { // for inputs - std::vector::const_iterator + std::vector::const_iterator i = dev->Inputs().begin(), e = dev->Inputs().end(); for (; i!=e; ++i) @@ -199,7 +199,7 @@ void ControlDialog::UpdateListContents() else { // for outputs - std::vector::const_iterator + std::vector::const_iterator i = dev->Outputs().begin(), e = dev->Outputs().end(); for (; i!=e; ++i) @@ -418,7 +418,7 @@ void ControlDialog::DetectControl(wxCommandEvent& event) wxButton* const btn = (wxButton*)event.GetEventObject(); const wxString lbl = btn->GetLabel(); - ControllerInterface::Device* const dev = g_controller_interface.FindDevice(m_devq); + Device* const dev = g_controller_interface.FindDevice(m_devq); if (dev) { btn->SetLabel(_("[ waiting ]")); @@ -427,7 +427,7 @@ void ControlDialog::DetectControl(wxCommandEvent& event) wxTheApp->Yield(); std::lock_guard lk(m_plugin.controls_lock); - ControllerInterface::Device::Control* const ctrl = control_reference->Detect(DETECT_WAIT_TIME, dev); + Device::Control* const ctrl = control_reference->Detect(DETECT_WAIT_TIME, dev); // if we got input, select it in the list if (ctrl) @@ -442,7 +442,7 @@ void GamepadPage::DetectControl(wxCommandEvent& event) ControlButton* btn = (ControlButton*)event.GetEventObject(); // find device :/ - ControllerInterface::Device* const dev = g_controller_interface.FindDevice(controller->default_device); + Device* const dev = g_controller_interface.FindDevice(controller->default_device); if (dev) { btn->SetLabel(_("[ waiting ]")); @@ -451,7 +451,7 @@ void GamepadPage::DetectControl(wxCommandEvent& event) wxTheApp->Yield(); std::lock_guard lk(m_plugin.controls_lock); - ControllerInterface::Device::Control* const ctrl = btn->control_reference->Detect(DETECT_WAIT_TIME, dev); + Device::Control* const ctrl = btn->control_reference->Detect(DETECT_WAIT_TIME, dev); // if we got input, update expression and reference if (ctrl) @@ -614,11 +614,11 @@ void InputConfigDialog::UpdateDeviceComboBox() { std::vector< GamepadPage* >::iterator i = m_padpages.begin(), e = m_padpages.end(); - ControllerInterface::DeviceQualifier dq; + DeviceQualifier dq; for (; i != e; ++i) { (*i)->device_cbox->Clear(); - std::vector::const_iterator + std::vector::const_iterator di = g_controller_interface.Devices().begin(), de = g_controller_interface.Devices().end(); for (; di!=de; ++di) diff --git a/Source/Core/DolphinWX/Src/InputConfigDiag.h b/Source/Core/DolphinWX/Src/InputConfigDiag.h index 882437452a..a11cea2cb3 100644 --- a/Source/Core/DolphinWX/Src/InputConfigDiag.h +++ b/Source/Core/DolphinWX/Src/InputConfigDiag.h @@ -112,7 +112,7 @@ public: private: GamepadPage* const m_parent; wxStaticText* m_bound_label; - ControllerInterface::DeviceQualifier m_devq; + DeviceQualifier m_devq; }; class ExtensionButton : public wxButton diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index f6995583ff..265f5a433d 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -2,7 +2,8 @@ set(SRCS Src/ControllerEmu.cpp Src/InputConfig.cpp Src/UDPWiimote.cpp Src/UDPWrapper.cpp - Src/ControllerInterface/ControllerInterface.cpp) + Src/ControllerInterface/ControllerInterface.cpp + Src/ControllerInterface/Device.cpp) if(WIN32) set(SRCS ${SRCS} diff --git a/Source/Core/InputCommon/InputCommon.vcxproj b/Source/Core/InputCommon/InputCommon.vcxproj index dc987d83a2..c3243854a2 100644 --- a/Source/Core/InputCommon/InputCommon.vcxproj +++ b/Source/Core/InputCommon/InputCommon.vcxproj @@ -166,6 +166,7 @@ + @@ -178,6 +179,7 @@ + @@ -195,4 +197,4 @@ - \ No newline at end of file + diff --git a/Source/Core/InputCommon/InputCommon.vcxproj.filters b/Source/Core/InputCommon/InputCommon.vcxproj.filters index 311476bc72..b4d5ff074f 100644 --- a/Source/Core/InputCommon/InputCommon.vcxproj.filters +++ b/Source/Core/InputCommon/InputCommon.vcxproj.filters @@ -39,6 +39,9 @@ ControllerInterface + + ControllerInterface\Device + ControllerInterface\DInput diff --git a/Source/Core/InputCommon/Src/ControllerEmu.h b/Source/Core/InputCommon/Src/ControllerEmu.h index cf87f2562e..5dc54b463f 100644 --- a/Source/Core/InputCommon/Src/ControllerEmu.h +++ b/Source/Core/InputCommon/Src/ControllerEmu.h @@ -449,8 +449,7 @@ public: std::vector< ControlGroup* > groups; - ControllerInterface::DeviceQualifier default_device; - + DeviceQualifier default_device; }; diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.cpp b/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.cpp index 6469945971..ef95f9b363 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.cpp @@ -23,7 +23,7 @@ namespace ciface namespace Android { -void Init( std::vector& devices ) +void Init( std::vector& devices ) { devices.push_back(new Touchscreen()); } diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.h b/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.h index 21f35165f1..e38c6a221a 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/Android/Android.h @@ -17,7 +17,7 @@ #ifndef _CIFACE_ANDROID_H_ #define _CIFACE_ANDROID_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #include "Android/ButtonManager.h" namespace ciface @@ -25,8 +25,8 @@ namespace ciface namespace Android { -void Init( std::vector& devices ); -class Touchscreen : public ControllerInterface::Device +void Init( std::vector& devices ); +class Touchscreen : public Core::Device { private: class Button : public Input diff --git a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.cpp index 6db79605a7..e672e4598c 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.cpp @@ -179,56 +179,6 @@ bool ControllerInterface::UpdateOutput(const bool force) return (m_devices.size() == ok_count); } -// -// Device :: ~Device -// -// Destructor, delete all inputs/outputs on device destruction -// -ControllerInterface::Device::~Device() -{ - { - // delete inputs - std::vector::iterator - i = m_inputs.begin(), - e = m_inputs.end(); - for ( ;i!=e; ++i) - delete *i; - } - - { - // delete outputs - std::vector::iterator - o = m_outputs.begin(), - e = m_outputs.end(); - for ( ;o!=e; ++o) - delete *o; - } -} - -void ControllerInterface::Device::AddInput(Input* const i) -{ - m_inputs.push_back(i); -} - -void ControllerInterface::Device::AddOutput(Output* const o) -{ - m_outputs.push_back(o); -} - -// -// Device :: ClearInputState -// -// Device classes should override this function -// ControllerInterface will call this when the device returns failure during UpdateInput -// used to try to set all buttons and axes to their default state when user unplugs a gamepad during play -// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging -// -void ControllerInterface::Device::ClearInputState() -{ - // this is going to be called for every UpdateInput call that fails - // kinda slow but, w/e, should only happen when user unplugs a device while playing -} - // // InputReference :: State // @@ -303,72 +253,6 @@ ControlState ControllerInterface::OutputReference::State(const ControlState stat return state; // just return the output, watever } -// -// DeviceQualifier :: ToString -// -// get string from a device qualifier / serialize -// -std::string ControllerInterface::DeviceQualifier::ToString() const -{ - if (source.empty() && (cid < 0) && name.empty()) - return ""; - std::ostringstream ss; - ss << source << '/'; - if ( cid > -1 ) - ss << cid; - ss << '/' << name; - return ss.str(); -} - -// -// DeviceQualifier :: FromString -// -// set a device qualifier from a string / unserialize -// -void ControllerInterface::DeviceQualifier::FromString(const std::string& str) -{ - std::istringstream ss(str); - - std::getline(ss, source = "", '/'); - - // silly - std::getline(ss, name, '/'); - std::istringstream(name) >> (cid = -1); - - std::getline(ss, name = ""); -} - -// -// DeviceQualifier :: FromDevice -// -// set a device qualifier from a device -// -void ControllerInterface::DeviceQualifier::FromDevice(const ControllerInterface::Device* const dev) -{ - name = dev->GetName(); - cid = dev->GetId(); - source= dev->GetSource(); -} - -bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::Device* const dev) const -{ - if (dev->GetId() == cid) - if (dev->GetName() == name) - if (dev->GetSource() == source) - return true; - return false; -} - -bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::DeviceQualifier& devq) const -{ - if (cid == devq.cid) - if (name == devq.name) - if (source == devq.source) - return true; - - return false; -} - // // UpdateReference // @@ -376,7 +260,7 @@ bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface: // need to call this to re-parse a control reference's expression after changing it // void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref - , const ControllerInterface::DeviceQualifier& default_device) const + , const DeviceQualifier& default_device) const { ref->m_controls.clear(); @@ -439,18 +323,6 @@ void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* } } -ControllerInterface::Device* ControllerInterface::FindDevice(const ControllerInterface::DeviceQualifier& devq) const -{ - std::vector::const_iterator - di = m_devices.begin(), - de = m_devices.end(); - for (; di!=de; ++di) - if (devq == *di) - return *di; - - return NULL; -} - // // InputReference :: Detect // @@ -461,7 +333,7 @@ ControllerInterface::Device* ControllerInterface::FindDevice(const ControllerInt // upon input, return pointer to detected Control // else return NULL // -ControllerInterface::Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device) +Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device) { unsigned int time = 0; std::vector states(device->Inputs().size()); @@ -511,7 +383,7 @@ ControllerInterface::Device::Control* ControllerInterface::InputReference::Detec // // set all binded outputs to power for x milliseconds return false // -ControllerInterface::Device::Control* ControllerInterface::OutputReference::Detect(const unsigned int ms, Device* const device) +Device::Control* ControllerInterface::OutputReference::Detect(const unsigned int ms, Device* const device) { // ignore device @@ -534,55 +406,3 @@ ControllerInterface::Device::Control* ControllerInterface::OutputReference::Dete } return NULL; } - -ControllerInterface::Device::Input* ControllerInterface::Device::FindInput(const std::string &name) const -{ - std::vector::const_iterator - it = m_inputs.begin(), - itend = m_inputs.end(); - for (; it != itend; ++it) - if ((*it)->GetName() == name) - return *it; - - return NULL; -} - -ControllerInterface::Device::Output* ControllerInterface::Device::FindOutput(const std::string &name) const -{ - std::vector::const_iterator - it = m_outputs.begin(), - itend = m_outputs.end(); - for (; it != itend; ++it) - if ((*it)->GetName() == name) - return *it; - - return NULL; -} - -ControllerInterface::Device::Input* ControllerInterface::FindInput(const std::string& name, const Device* def_dev) const -{ - if (def_dev) - { - Device::Input* const inp = def_dev->FindInput(name); - if (inp) - return inp; - } - - std::vector::const_iterator - di = m_devices.begin(), - de = m_devices.end(); - for (; di != de; ++di) - { - Device::Input* const i = (*di)->FindInput(name); - - if (i) - return i; - } - - return NULL; -} - -ControllerInterface::Device::Output* ControllerInterface::FindOutput(const std::string& name, const Device* def_dev) const -{ - return def_dev->FindOutput(name); -} diff --git a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h index 8c1a044860..db3a82bf3b 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h @@ -9,18 +9,13 @@ #include "Common.h" #include "Thread.h" +#include "Device.h" // enable disable sources #ifdef _WIN32 #define CIFACE_USE_XINPUT - #define CIFACE_USE_DINPUT_JOYSTICK - #define CIFACE_USE_DINPUT_KBM #define CIFACE_USE_DINPUT -//#ifndef CIFACE_USE_DINPUT_JOYSTICK -// enable SDL 1.2 in addition to DirectInput on windows, -// to support a few gamepads that aren't behaving with DInput #define CIFACE_USE_SDL -//#endif #endif #if defined(HAVE_X11) && HAVE_X11 #define CIFACE_USE_XLIB @@ -32,9 +27,8 @@ #ifdef ANDROID #define CIFACE_USE_ANDROID #endif - -// idk in case I wanted to change it to double or something, idk what's best -typedef float ControlState; + +using namespace ciface::Core; // // ControllerInterface @@ -42,148 +36,10 @@ typedef float ControlState; // some crazy shit I made to control different device inputs and outputs // from lots of different sources, hopefully more easily // -class ControllerInterface +class ControllerInterface : public DeviceContainer { public: - // Forward declarations - class DeviceQualifier; - - // - // Device - // - // a device class - // - class Device - { - public: - class Input; - class Output; - - // - // Control - // - // control includes inputs and outputs - // - class Control // input or output - { - public: - virtual std::string GetName() const = 0; - virtual ~Control() {} - - virtual Input* ToInput() { return NULL; } - virtual Output* ToOutput() { return NULL; } - }; - - // - // Input - // - // an input on a device - // - class Input : public Control - { - public: - // things like absolute axes/ absolute mouse position will override this - virtual bool IsDetectable() { return true; } - - virtual ControlState GetState() const = 0; - - Input* ToInput() { return this; } - }; - - // - // Output - // - // an output on a device - // - class Output : public Control - { - public: - virtual ~Output() {} - - virtual void SetState(ControlState state) = 0; - - Output* ToOutput() { return this; } - }; - - virtual ~Device(); - - virtual std::string GetName() const = 0; - virtual int GetId() const = 0; - virtual std::string GetSource() const = 0; - virtual bool UpdateInput() = 0; - virtual bool UpdateOutput() = 0; - - virtual void ClearInputState(); - - const std::vector& Inputs() const { return m_inputs; } - const std::vector& Outputs() const { return m_outputs; } - - Input* FindInput(const std::string& name) const; - Output* FindOutput(const std::string& name) const; - - protected: - void AddInput(Input* const i); - void AddOutput(Output* const o); - - class FullAnalogSurface : public Input - { - public: - FullAnalogSurface(Input* low, Input* high) - : m_low(*low), m_high(*high) - {} - - ControlState GetState() const - { - return (1 + m_high.GetState() - m_low.GetState()) / 2; - } - - std::string GetName() const - { - return m_low.GetName() + *m_high.GetName().rbegin(); - } - - private: - Input& m_low; - Input& m_high; - }; - - void AddAnalogInputs(Input* low, Input* high) - { - AddInput(low); - AddInput(high); - AddInput(new FullAnalogSurface(low, high)); - AddInput(new FullAnalogSurface(high, low)); - } - - private: - std::vector m_inputs; - std::vector m_outputs; - }; - - // - // DeviceQualifier - // - // device qualifier used to match devices - // currently has ( source, id, name ) properties which match a device - // - class DeviceQualifier - { - public: - DeviceQualifier() : cid(-1) {} - DeviceQualifier(const std::string& _source, const int _id, const std::string& _name) - : source(_source), cid(_id), name(_name) {} - void FromDevice(const Device* const dev); - void FromString(const std::string& str); - std::string ToString() const; - bool operator==(const DeviceQualifier& devq) const; - bool operator==(const Device* const dev) const; - - std::string source; - int cid; - std::string name; - }; - // // ControlReference // @@ -260,22 +116,13 @@ public: bool UpdateInput(const bool force = false); bool UpdateOutput(const bool force = false); - Device::Input* FindInput(const std::string& name, const Device* def_dev) const; - Device::Output* FindOutput(const std::string& name, const Device* def_dev) const; - - const std::vector& Devices() const { return m_devices; } - Device* FindDevice(const DeviceQualifier& devq) const; - std::recursive_mutex update_lock; private: bool m_is_init; - std::vector m_devices; void* m_hwnd; }; -typedef std::vector DeviceList; - extern ControllerInterface g_controller_interface; #endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.cpp index 01baf9cf1a..6b7b492ea9 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.cpp @@ -1,17 +1,10 @@ -#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 +#include "DInputJoystick.h" +#include "DInputKeyboardMouse.h" #pragma comment(lib, "Dinput8.lib") #pragma comment(lib, "dxguid.lib") @@ -55,18 +48,14 @@ std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device) return result; } -void Init(std::vector& devices, HWND hwnd) +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_DINPUT_KBM InitKeyboardMouse(idi8, devices, hwnd); -#endif -#ifdef CIFACE_USE_DINPUT_JOYSTICK InitJoystick(idi8, devices, hwnd); -#endif idi8->Release(); @@ -74,5 +63,3 @@ void Init(std::vector& devices, HWND hwnd) } } - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.h b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.h index 9ee48c190f..86452c23ab 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInput.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_DINPUT_H_ #define _CIFACE_DINPUT_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #define DINPUT_SOURCE_NAME "DInput" @@ -23,7 +23,7 @@ BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVO BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef); std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device); -void Init(std::vector& devices, HWND hwnd); +void Init(std::vector& devices, HWND hwnd); } } diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.cpp index 916d142149..24502e7a8a 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.cpp @@ -1,10 +1,11 @@ -#include "../ControllerInterface.h" - -#ifdef CIFACE_USE_DINPUT_JOYSTICK #include "DInputJoystick.h" #include "DInput.h" +#include +#include +#include + namespace ciface { namespace DInput @@ -142,7 +143,7 @@ LCleanup: } #endif -void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd) +void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd) { std::list joysticks; idi8->EnumDevices( DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, DIEDFL_ATTACHEDONLY ); @@ -599,5 +600,3 @@ Joystick::Force

::Force(u8 index, EffectState& state) } } - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.h b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.h index a783381154..ad383bd764 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputJoystick.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_DINPUT_JOYSTICK_H_ #define _CIFACE_DINPUT_JOYSTICK_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #define DIRECTINPUT_VERSION 0x0800 #define WIN32_LEAN_AND_MEAN @@ -11,21 +11,14 @@ #include -#ifdef CIFACE_USE_XINPUT - // this takes so long, idk if it should be enabled :( - #define NO_DUPLICATE_DINPUT_XINPUT - #include - #include -#endif - namespace ciface { namespace DInput { -void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd); +void InitJoystick(IDirectInput8* const idi8, std::vector& devices, HWND hwnd); -class Joystick : public ControllerInterface::Device +class Joystick : public Core::Device { private: struct EffectState diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.cpp index ea6452376d..850ff27281 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.cpp @@ -1,6 +1,3 @@ -#include "../ControllerInterface.h" - -#ifdef CIFACE_USE_DINPUT_KBM #include "DInputKeyboardMouse.h" #include "DInput.h" @@ -42,7 +39,7 @@ static const struct // lil silly static HWND hwnd; -void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd) +void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd) { hwnd = _hwnd; @@ -252,16 +249,6 @@ 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 { @@ -322,5 +309,3 @@ void KeyboardMouse::Light::SetState(const ControlState state) } } - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.h b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.h index cacef516b0..73398ca1b1 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/DInput/DInputKeyboardMouse.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_DINPUT_KBM_H_ #define _CIFACE_DINPUT_KBM_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #define DIRECTINPUT_VERSION 0x0800 #define WIN32_LEAN_AND_MEAN @@ -14,9 +14,9 @@ namespace ciface namespace DInput { -void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd); +void InitKeyboardMouse(IDirectInput8* const idi8, std::vector& devices, HWND _hwnd); -class KeyboardMouse : public ControllerInterface::Device +class KeyboardMouse : public Core::Device { private: struct State diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Device.cpp b/Source/Core/InputCommon/Src/ControllerInterface/Device.cpp new file mode 100644 index 0000000000..965ecfc817 --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/Device.cpp @@ -0,0 +1,193 @@ + +#include "Device.h" + +#include +#include + +namespace ciface +{ +namespace Core +{ + +// +// Device :: ~Device +// +// Destructor, delete all inputs/outputs on device destruction +// +Device::~Device() +{ + { + // delete inputs + std::vector::iterator + i = m_inputs.begin(), + e = m_inputs.end(); + for ( ;i!=e; ++i) + delete *i; + } + + { + // delete outputs + std::vector::iterator + o = m_outputs.begin(), + e = m_outputs.end(); + for ( ;o!=e; ++o) + delete *o; + } +} + +void Device::AddInput(Device::Input* const i) +{ + m_inputs.push_back(i); +} + +void Device::AddOutput(Device::Output* const o) +{ + m_outputs.push_back(o); +} + +Device::Input* Device::FindInput(const std::string &name) const +{ + std::vector::const_iterator + it = m_inputs.begin(), + itend = m_inputs.end(); + for (; it != itend; ++it) + if ((*it)->GetName() == name) + return *it; + + return NULL; +} + +Device::Output* Device::FindOutput(const std::string &name) const +{ + std::vector::const_iterator + it = m_outputs.begin(), + itend = m_outputs.end(); + for (; it != itend; ++it) + if ((*it)->GetName() == name) + return *it; + + return NULL; +} + +// +// Device :: ClearInputState +// +// Device classes should override this function +// ControllerInterface will call this when the device returns failure during UpdateInput +// used to try to set all buttons and axes to their default state when user unplugs a gamepad during play +// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging +// +void Device::ClearInputState() +{ + // this is going to be called for every UpdateInput call that fails + // kinda slow but, w/e, should only happen when user unplugs a device while playing +} + +// +// DeviceQualifier :: ToString +// +// get string from a device qualifier / serialize +// +std::string DeviceQualifier::ToString() const +{ + if (source.empty() && (cid < 0) && name.empty()) + return ""; + std::ostringstream ss; + ss << source << '/'; + if ( cid > -1 ) + ss << cid; + ss << '/' << name; + return ss.str(); +} + +// +// DeviceQualifier :: FromString +// +// set a device qualifier from a string / unserialize +// +void DeviceQualifier::FromString(const std::string& str) +{ + std::istringstream ss(str); + + std::getline(ss, source = "", '/'); + + // silly + std::getline(ss, name, '/'); + std::istringstream(name) >> (cid = -1); + + std::getline(ss, name = ""); +} + +// +// DeviceQualifier :: FromDevice +// +// set a device qualifier from a device +// +void DeviceQualifier::FromDevice(const Device* const dev) +{ + name = dev->GetName(); + cid = dev->GetId(); + source= dev->GetSource(); +} + +bool DeviceQualifier::operator==(const Device* const dev) const +{ + if (dev->GetId() == cid) + if (dev->GetName() == name) + if (dev->GetSource() == source) + return true; + return false; +} + +bool DeviceQualifier::operator==(const DeviceQualifier& devq) const +{ + if (cid == devq.cid) + if (name == devq.name) + if (source == devq.source) + return true; + + return false; +} + +Device* DeviceContainer::FindDevice(const DeviceQualifier& devq) const +{ + std::vector::const_iterator + di = m_devices.begin(), + de = m_devices.end(); + for (; di!=de; ++di) + if (devq == *di) + return *di; + + return NULL; +} + +Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const +{ + if (def_dev) + { + Device::Input* const inp = def_dev->FindInput(name); + if (inp) + return inp; + } + + std::vector::const_iterator + di = m_devices.begin(), + de = m_devices.end(); + for (; di != de; ++di) + { + Device::Input* const i = (*di)->FindInput(name); + + if (i) + return i; + } + + return NULL; +} + +Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const +{ + return def_dev->FindOutput(name); +} + +} +} diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Device.h b/Source/Core/InputCommon/Src/ControllerInterface/Device.h new file mode 100644 index 0000000000..a7c26b867d --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/Device.h @@ -0,0 +1,171 @@ + +#ifndef _DEVICE_H_ +#define _DEVICE_H_ + +#include +#include + +#include "Common.h" + +// idk in case I wanted to change it to double or something, idk what's best +typedef float ControlState; + +namespace ciface +{ +namespace Core +{ + +// Forward declarations +class DeviceQualifier; + +// +// Device +// +// a device class +// +class Device +{ +public: + class Input; + class Output; + + // + // Control + // + // control includes inputs and outputs + // + class Control // input or output + { + public: + virtual std::string GetName() const = 0; + virtual ~Control() {} + + virtual Input* ToInput() { return NULL; } + virtual Output* ToOutput() { return NULL; } + }; + + // + // Input + // + // an input on a device + // + class Input : public Control + { + public: + // things like absolute axes/ absolute mouse position will override this + virtual bool IsDetectable() { return true; } + + virtual ControlState GetState() const = 0; + + Input* ToInput() { return this; } + }; + + // + // Output + // + // an output on a device + // + class Output : public Control + { + public: + virtual ~Output() {} + + virtual void SetState(ControlState state) = 0; + + Output* ToOutput() { return this; } + }; + + virtual ~Device(); + + virtual std::string GetName() const = 0; + virtual int GetId() const = 0; + virtual std::string GetSource() const = 0; + virtual bool UpdateInput() = 0; + virtual bool UpdateOutput() = 0; + + virtual void ClearInputState(); + + const std::vector& Inputs() const { return m_inputs; } + const std::vector& Outputs() const { return m_outputs; } + + Input* FindInput(const std::string& name) const; + Output* FindOutput(const std::string& name) const; + +protected: + void AddInput(Input* const i); + void AddOutput(Output* const o); + + class FullAnalogSurface : public Input + { + public: + FullAnalogSurface(Input* low, Input* high) + : m_low(*low), m_high(*high) + {} + + ControlState GetState() const + { + return (1 + m_high.GetState() - m_low.GetState()) / 2; + } + + std::string GetName() const + { + return m_low.GetName() + *m_high.GetName().rbegin(); + } + + private: + Input& m_low; + Input& m_high; + }; + + void AddAnalogInputs(Input* low, Input* high) + { + AddInput(low); + AddInput(high); + AddInput(new FullAnalogSurface(low, high)); + AddInput(new FullAnalogSurface(high, low)); + } + +private: + std::vector m_inputs; + std::vector m_outputs; +}; + +// +// DeviceQualifier +// +// device qualifier used to match devices +// currently has ( source, id, name ) properties which match a device +// +class DeviceQualifier +{ +public: + DeviceQualifier() : cid(-1) {} + DeviceQualifier(const std::string& _source, const int _id, const std::string& _name) + : source(_source), cid(_id), name(_name) {} + void FromDevice(const Device* const dev); + void FromString(const std::string& str); + std::string ToString() const; + bool operator==(const DeviceQualifier& devq) const; + bool operator==(const Device* const dev) const; + + std::string source; + int cid; + std::string name; +}; + +class DeviceContainer +{ +public: + Device::Input* FindInput(const std::string& name, const Device* def_dev) const; + Device::Output* FindOutput(const std::string& name, const Device* def_dev) const; + + const std::vector& Devices() const { return m_devices; } + Device* FindDevice(const DeviceQualifier& devq) const; +protected: + std::vector m_devices; +}; + +} +} + +#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.h b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.h index 225906c35d..a922e3f2dd 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.h @@ -1,13 +1,13 @@ #pragma once -#include "../ControllerInterface.h" +#include "../Device.h" namespace ciface { namespace OSX { -void Init(std::vector& devices, void *window); +void Init(std::vector& devices, void *window); void DeInit(); void DeviceElementDebugPrint(const void *, void *); diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.mm b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.mm index aef8f283c1..8a859f46c8 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.mm +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSX.mm @@ -2,11 +2,12 @@ #include #include -#include "../ControllerInterface.h" #include "OSX.h" #include "OSXKeyboard.h" #include "OSXJoystick.h" +#include + namespace ciface { namespace OSX @@ -145,8 +146,8 @@ static void DeviceMatching_callback(void* inContext, DeviceDebugPrint(inIOHIDDeviceRef); - std::vector *devices = - (std::vector *)inContext; + std::vector *devices = + (std::vector *)inContext; // Add to the devices vector if it's of a type we want if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, @@ -164,7 +165,7 @@ static void DeviceMatching_callback(void* inContext, name, joy_name_counts[name]++)); } -void Init(std::vector& devices, void *window) +void Init(std::vector& devices, void *window) { HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.h b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.h index ecc443ad3a..ebe908522b 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.h @@ -1,13 +1,13 @@ #include -#include "../ControllerInterface.h" +#include "../Device.h" namespace ciface { namespace OSX { -class Joystick : public ControllerInterface::Device +class Joystick : public Core::Device { private: class Button : public Input diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.mm b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.mm index 0d89c2e393..ebd713d142 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.mm +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXJoystick.mm @@ -1,9 +1,10 @@ #include #include -#include "../ControllerInterface.h" #include "OSXJoystick.h" +#include + namespace ciface { namespace OSX diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.h b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.h index d59651c98a..930f849a17 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.h @@ -1,13 +1,13 @@ #include -#include "../ControllerInterface.h" +#include "../Device.h" namespace ciface { namespace OSX { -class Keyboard : public ControllerInterface::Device +class Keyboard : public Core::Device { private: class Key : public Input diff --git a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.mm b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.mm index c22e267044..003240d4a9 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.mm +++ b/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.mm @@ -3,9 +3,10 @@ #include #include // wxWidgets -#include "../ControllerInterface.h" #include "OSXKeyboard.h" +#include + namespace ciface { namespace OSX diff --git a/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp index 6c74c55d97..006b82d4ac 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp @@ -1,10 +1,11 @@ -#include "../ControllerInterface.h" - -#ifdef CIFACE_USE_SDL #include "SDL.h" #include +#include +#include +#include + #ifdef _WIN32 #if SDL_VERSION_ATLEAST(1, 3, 0) #pragma comment(lib, "SDL.1.3.lib") @@ -27,7 +28,7 @@ std::string GetJoystickName(int index) #endif } -void Init( std::vector& devices ) +void Init( std::vector& devices ) { // this is used to number the joysticks // multiple joysticks with the same name shall get unique ids starting at 0 @@ -399,5 +400,3 @@ ControlState Joystick::Hat::GetState() const } } - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h b/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h index fa770c403d..6a65efeedb 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_SDL_H_ #define _CIFACE_SDL_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #include @@ -23,9 +23,9 @@ namespace ciface namespace SDL { -void Init( std::vector& devices ); +void Init( std::vector& devices ); -class Joystick : public ControllerInterface::Device +class Joystick : public Core::Device { private: @@ -40,7 +40,7 @@ private: }; #endif - class Button : public Input + class Button : public Core::Device::Input { public: std::string GetName() const; @@ -51,7 +51,7 @@ private: const u8 m_index; }; - class Axis : public Input + class Axis : public Core::Device::Input { public: std::string GetName() const; diff --git a/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.cpp b/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.cpp index 3c7f3c48ae..c687bef856 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.cpp @@ -1,6 +1,3 @@ -#include "../ControllerInterface.h" - -#ifdef CIFACE_USE_XINPUT #include "XInput.h" @@ -51,7 +48,7 @@ static const char* const named_motors[] = "Motor R" }; -void Init(DeviceList& devices) +void Init(std::vector& devices) { XINPUT_CAPABILITIES caps; for (int i = 0; i != 4; ++i) @@ -210,5 +207,3 @@ void Device::Motor::SetState(ControlState state) } } - -#endif diff --git a/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.h b/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.h index eb51a58779..8fe6dd1dee 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/XInput/XInput.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_XINPUT_H_ #define _CIFACE_XINPUT_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #define NOMINMAX #include @@ -12,12 +12,12 @@ namespace ciface namespace XInput { -void Init(DeviceList& devices); +void Init(std::vector& devices); -class Device : public ControllerInterface::Device +class Device : public Core::Device { private: - class Button : public Input + class Button : public Core::Device::Input { public: std::string GetName() const; @@ -28,7 +28,7 @@ private: u8 m_index; }; - class Axis : public Input + class Axis : public Core::Device::Input { public: std::string GetName() const; @@ -40,7 +40,7 @@ private: const u8 m_index; }; - class Trigger : public Input + class Trigger : public Core::Device::Input { public: std::string GetName() const; @@ -52,7 +52,7 @@ private: const u8 m_index; }; - class Motor : public Output + class Motor : public Core::Device::Output { public: std::string GetName() const; diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.cpp b/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.cpp index dad0c6bbf8..7016c4908b 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.cpp @@ -7,7 +7,7 @@ namespace ciface namespace Xlib { -void Init(std::vector& devices, void* const hwnd) +void Init(std::vector& devices, void* const hwnd) { devices.push_back(new KeyboardMouse((Window)hwnd)); } diff --git a/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.h b/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.h index 2af95ff7f9..c178f1e9b4 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.h @@ -1,7 +1,7 @@ #ifndef _CIFACE_XLIB_H_ #define _CIFACE_XLIB_H_ -#include "../ControllerInterface.h" +#include "../Device.h" #include #include @@ -11,9 +11,9 @@ namespace ciface namespace Xlib { -void Init(std::vector& devices, void* const hwnd); +void Init(std::vector& devices, void* const hwnd); -class KeyboardMouse : public ControllerInterface::Device +class KeyboardMouse : public Core::Device { private: