InputCommon: Add Win32 InputBackend class.

This commit is contained in:
Jordan Woyak
2024-03-11 02:09:31 -05:00
parent 2ac59bf581
commit 341f99a3f1
5 changed files with 50 additions and 29 deletions

View File

@ -20,13 +20,25 @@
#pragma comment(lib, "OneCoreUAP.Lib")
// Dolphin's render window
static HWND s_hwnd;
static std::mutex s_populate_mutex;
// TODO is this really needed?
static Common::Flag s_first_populate_devices_asked;
static HCMNOTIFICATION s_notify_handle;
namespace ciface::Win32
{
class InputBackend final : public ciface::InputBackend
{
public:
InputBackend(ControllerInterface* controller_interface);
~InputBackend();
void PopulateDevices() override;
void HandleWindowChange() override;
HWND GetHWND();
};
} // namespace ciface::Win32
_Pre_satisfies_(EventDataSize >= sizeof(CM_NOTIFY_EVENT_DATA)) static DWORD CALLBACK
OnDevicesChanged(_In_ HCMNOTIFICATION hNotify, _In_opt_ PVOID Context,
_In_ CM_NOTIFY_ACTION Action,
@ -43,8 +55,9 @@ _Pre_satisfies_(EventDataSize >= sizeof(CM_NOTIFY_EVENT_DATA)) static DWORD CALL
std::lock_guard lk_population(s_populate_mutex);
// TODO: we could easily use the message passed alongside this event, which tells
// whether a device was added or removed, to avoid removing old, still connected, devices
g_controller_interface.PlatformPopulateDevices([] {
ciface::DInput::PopulateDevices(s_hwnd);
g_controller_interface.PlatformPopulateDevices([&] {
ciface::DInput::PopulateDevices(
static_cast<ciface::Win32::InputBackend*>(Context)->GetHWND());
ciface::XInput::PopulateDevices();
});
}
@ -52,10 +65,21 @@ _Pre_satisfies_(EventDataSize >= sizeof(CM_NOTIFY_EVENT_DATA)) static DWORD CALL
return ERROR_SUCCESS;
}
void ciface::Win32::Init(void* hwnd)
namespace ciface::Win32
{
s_hwnd = static_cast<HWND>(hwnd);
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)
{
return std::make_unique<InputBackend>(controller_interface);
}
HWND InputBackend::GetHWND()
{
return static_cast<HWND>(GetControllerInterface().GetWindowSystemInfo().render_window);
}
InputBackend::InputBackend(ControllerInterface* controller_interface)
: ciface::InputBackend(controller_interface)
{
XInput::Init();
WGInput::Init();
@ -63,35 +87,32 @@ void ciface::Win32::Init(void* hwnd)
.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE,
.u{.DeviceInterface{.ClassGuid = GUID_DEVINTERFACE_HID}}};
const CONFIGRET cfg_rv =
CM_Register_Notification(&notify_filter, nullptr, OnDevicesChanged, &s_notify_handle);
CM_Register_Notification(&notify_filter, this, OnDevicesChanged, &s_notify_handle);
if (cfg_rv != CR_SUCCESS)
{
ERROR_LOG_FMT(CONTROLLERINTERFACE, "CM_Register_Notification failed: {:x}", cfg_rv);
}
}
void ciface::Win32::PopulateDevices(void* hwnd)
void InputBackend::PopulateDevices()
{
s_hwnd = static_cast<HWND>(hwnd);
std::lock_guard lk_population(s_populate_mutex);
s_first_populate_devices_asked.Set();
ciface::DInput::PopulateDevices(s_hwnd);
ciface::DInput::PopulateDevices(GetHWND());
ciface::XInput::PopulateDevices();
ciface::WGInput::PopulateDevices();
}
void ciface::Win32::ChangeWindow(void* hwnd)
void InputBackend::HandleWindowChange()
{
s_hwnd = static_cast<HWND>(hwnd);
std::lock_guard lk_population(s_populate_mutex);
ciface::DInput::ChangeWindow(s_hwnd);
ciface::DInput::ChangeWindow(GetHWND());
}
void ciface::Win32::DeInit()
InputBackend::~InputBackend()
{
s_first_populate_devices_asked.Clear();
DInput::DeInit();
s_hwnd = nullptr;
if (s_notify_handle)
{
@ -106,3 +127,5 @@ void ciface::Win32::DeInit()
XInput::DeInit();
WGInput::DeInit();
}
} // namespace ciface::Win32