Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11193 from jordan-woyak/ciface-input-backend-inte…
…rface

ControllerInterface: Add InputBackend interface.
  • Loading branch information
AdmiralCurtiss committed Nov 3, 2022
2 parents 0210d11 + 168a49c commit 1d07332
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 186 deletions.
2 changes: 2 additions & 0 deletions Source/Core/DolphinLib.props
Expand Up @@ -487,6 +487,7 @@
<ClInclude Include="InputCommon\ControllerEmu\StickGate.h" />
<ClInclude Include="InputCommon\ControllerInterface\ControllerInterface.h" />
<ClInclude Include="InputCommon\ControllerInterface\CoreDevice.h" />
<ClInclude Include="InputCommon\ControllerInterface\InputBackend.h" />
<ClInclude Include="InputCommon\ControllerInterface\DInput\DInput.h" />
<ClInclude Include="InputCommon\ControllerInterface\DInput\DInput8.h" />
<ClInclude Include="InputCommon\ControllerInterface\DInput\DInputJoystick.h" />
Expand Down Expand Up @@ -1111,6 +1112,7 @@
<ClCompile Include="InputCommon\ControllerEmu\StickGate.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\ControllerInterface.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\CoreDevice.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\InputBackend.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\DInput\DInput.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\DInput\DInputJoystick.cpp" />
<ClCompile Include="InputCommon\ControllerInterface\DInput\DInputKeyboardMouse.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/InputCommon/CMakeLists.txt
Expand Up @@ -56,6 +56,8 @@ add_library(inputcommon
ControllerInterface/ControllerInterface.h
ControllerInterface/CoreDevice.cpp
ControllerInterface/CoreDevice.h
ControllerInterface/InputBackend.cpp
ControllerInterface/InputBackend.h
ControllerInterface/MappingCommon.cpp
ControllerInterface/MappingCommon.h
ControllerInterface/Wiimote/WiimoteController.cpp
Expand Down
50 changes: 21 additions & 29 deletions Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp
Expand Up @@ -64,19 +64,19 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)
// nothing needed for OSX and Quartz
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::Init();
m_input_backends.emplace_back(ciface::SDL::CreateInputBackend(this));
#endif
#ifdef CIFACE_USE_ANDROID
// nothing needed
#endif
#ifdef CIFACE_USE_EVDEV
ciface::evdev::Init();
m_input_backends.emplace_back(ciface::evdev::CreateInputBackend(this));
#endif
#ifdef CIFACE_USE_PIPES
// nothing needed
#endif
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
ciface::DualShockUDPClient::Init();
m_input_backends.emplace_back(ciface::DualShockUDPClient::CreateInputBackend(this));
#endif

// Don't allow backends to add devices before the first RefreshDevices() as they will be cleaned
Expand Down Expand Up @@ -181,21 +181,15 @@ void ControllerInterface::RefreshDevices(RefreshReason reason)
ciface::Quartz::PopulateDevices(m_wsi.render_window);
}
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::PopulateDevices();
#endif
#ifdef CIFACE_USE_ANDROID
ciface::Android::PopulateDevices();
#endif
#ifdef CIFACE_USE_EVDEV
ciface::evdev::PopulateDevices();
#endif
#ifdef CIFACE_USE_PIPES
ciface::Pipes::PopulateDevices();
#endif
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
ciface::DualShockUDPClient::PopulateDevices();
#endif

for (auto& backend : m_input_backends)
backend->PopulateDevices();

WiimoteReal::PopulateDevices();

Expand Down Expand Up @@ -242,18 +236,12 @@ void ControllerInterface::Shutdown()
ciface::OSX::DeInit();
ciface::Quartz::DeInit();
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::DeInit();
#endif
#ifdef CIFACE_USE_ANDROID
// nothing needed
#endif
#ifdef CIFACE_USE_EVDEV
ciface::evdev::Shutdown();
#endif
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
ciface::DualShockUDPClient::DeInit();
#endif

// Empty the container of input backends to deconstruct and deinitialize them.
m_input_backends.clear();

// Make sure no devices had been added within Shutdown() in the time
// between checking they checked atomic m_is_init bool and we changed it.
Expand Down Expand Up @@ -384,15 +372,19 @@ void ControllerInterface::UpdateInput()

// TODO: if we are an emulation input channel, we should probably always lock
// Prefer outdated values over blocking UI or CPU thread (avoids short but noticeable frame drop)
if (m_devices_mutex.try_lock())
if (!m_devices_mutex.try_lock())
return;

std::lock_guard lk(m_devices_mutex, std::adopt_lock);

for (auto& backend : m_input_backends)
backend->UpdateInput();

for (const auto& d : m_devices)
{
std::lock_guard lk(m_devices_mutex, std::adopt_lock);
for (const auto& d : m_devices)
{
// Theoretically we could avoid updating input on devices that don't have any references to
// them, but in practice a few devices types could break in different ways, so we don't
d->UpdateInput();
}
// Theoretically we could avoid updating input on devices that don't have any references to
// them, but in practice a few devices types could break in different ways, so we don't
d->UpdateInput();
}
}

Expand Down
Expand Up @@ -12,6 +12,7 @@
#include "Common/Matrix.h"
#include "Common/WindowSystemInfo.h"
#include "InputCommon/ControllerInterface/CoreDevice.h"
#include "InputCommon/ControllerInterface/InputBackend.h"

// enable disable sources
#ifdef _WIN32
Expand Down Expand Up @@ -133,6 +134,8 @@ class ControllerInterface : public ciface::Core::DeviceContainer
WindowSystemInfo m_wsi;
std::atomic<float> m_aspect_ratio_adjustment = 1;
std::atomic<bool> m_requested_mouse_centering = false;

std::vector<std::unique_ptr<ciface::InputBackend>> m_input_backends;
};

namespace ciface
Expand Down

0 comments on commit 1d07332

Please sign in to comment.