From 8098be3dfa07aa529f61ea2ec4ff4077ef01b2d8 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 11 Mar 2024 01:42:32 -0500 Subject: InputCommon: Add XInput2 InputBackend class. --- .../ControllerInterface/Xlib/XInput2.cpp | 22 ++++++++++++++++++++-- .../InputCommon/ControllerInterface/Xlib/XInput2.h | 3 ++- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface/Xlib') diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp index f041dd16a1..5de0840354 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp @@ -66,9 +66,27 @@ constexpr int XINPUT_MAJOR = 2, XINPUT_MINOR = 1; namespace ciface::XInput2 { +class InputBackend final : public ciface::InputBackend +{ +public: + using ciface::InputBackend::InputBackend; + void PopulateDevices() override; +}; + +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface) +{ + return std::make_unique(controller_interface); +} + // This function will add zero or more KeyboardMouse objects to devices. -void PopulateDevices(void* const hwnd) +void InputBackend::PopulateDevices() { + const WindowSystemInfo wsi = GetControllerInterface().GetWindowSystemInfo(); + if (wsi.type != WindowSystemType::X11) + return; + + const auto hwnd = wsi.render_window; + Display* dpy = XOpenDisplay(nullptr); // xi_opcode is important; it will be used to identify XInput events by @@ -119,7 +137,7 @@ void PopulateDevices(void* const hwnd) } // Since current_master is a master pointer, its attachment must // be a master keyboard. - g_controller_interface.AddDevice( + GetControllerInterface().AddDevice( std::make_shared((Window)hwnd, xi_opcode, current_master->deviceid, current_master->attachment, scroll_increment)); } diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h index a8960c1d23..427c81b18a 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h @@ -16,10 +16,11 @@ extern "C" { #include "Common/CommonTypes.h" #include "Common/Matrix.h" #include "InputCommon/ControllerInterface/ControllerInterface.h" +#include "InputCommon/ControllerInterface/InputBackend.h" namespace ciface::XInput2 { -void PopulateDevices(void* const hwnd); +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface); class KeyboardMouse : public Core::Device { -- cgit v1.2.3 From 3665f7abac4d5c93936f0325fe40bba73703813d Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 11 Mar 2024 02:20:54 -0500 Subject: InputCommon: Implement xlib window change logic. --- .../Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'Source/Core/InputCommon/ControllerInterface/Xlib') diff --git a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp index 5de0840354..e4b717e3af 100644 --- a/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp @@ -66,11 +66,14 @@ constexpr int XINPUT_MAJOR = 2, XINPUT_MINOR = 1; namespace ciface::XInput2 { +constexpr std::string_view SOURCE_NAME = "XInput2"; + class InputBackend final : public ciface::InputBackend { public: using ciface::InputBackend::InputBackend; void PopulateDevices() override; + void HandleWindowChange() override; }; std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface) @@ -78,6 +81,14 @@ std::unique_ptr CreateInputBackend(ControllerInterface* co return std::make_unique(controller_interface); } +void InputBackend::HandleWindowChange() +{ + GetControllerInterface().RemoveDevice( + [](const auto* dev) { return dev->GetSource() == SOURCE_NAME; }, true); + + PopulateDevices(); +} + // This function will add zero or more KeyboardMouse objects to devices. void InputBackend::PopulateDevices() { @@ -400,7 +411,7 @@ std::string KeyboardMouse::GetName() const std::string KeyboardMouse::GetSource() const { - return "XInput2"; + return std::string(SOURCE_NAME); } KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard) -- cgit v1.2.3