2017-11-10 17:56:13 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-11-10 17:56:13 +00:00
|
|
|
|
|
|
|
#include "InputCommon/ControllerInterface/Win32/Win32.h"
|
|
|
|
|
2022-08-08 00:28:41 +00:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <cfgmgr32.h>
|
|
|
|
// must be before hidclass
|
|
|
|
#include <initguid.h>
|
|
|
|
|
|
|
|
#include <hidclass.h>
|
2017-11-10 20:30:37 +00:00
|
|
|
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
#include <mutex>
|
2017-11-10 20:30:37 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
#include "Common/Flag.h"
|
2017-11-10 20:30:37 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-11-10 17:56:13 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInput.h"
|
2020-10-20 16:30:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/WGInput/WGInput.h"
|
2017-11-10 17:56:13 +00:00
|
|
|
#include "InputCommon/ControllerInterface/XInput/XInput.h"
|
|
|
|
|
2022-08-08 00:28:41 +00:00
|
|
|
#pragma comment(lib, "OneCoreUAP.Lib")
|
2017-11-10 20:30:37 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
// Dolphin's render window
|
|
|
|
static HWND s_hwnd;
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
static std::mutex s_populate_mutex;
|
2022-08-08 00:28:41 +00:00
|
|
|
// TODO is this really needed?
|
2021-05-15 09:20:20 +00:00
|
|
|
static Common::Flag s_first_populate_devices_asked;
|
2022-08-08 00:28:41 +00:00
|
|
|
static HCMNOTIFICATION s_notify_handle;
|
2017-11-10 20:30:37 +00:00
|
|
|
|
2022-08-08 00:28:41 +00:00
|
|
|
_Pre_satisfies_(EventDataSize >= sizeof(CM_NOTIFY_EVENT_DATA)) static DWORD CALLBACK
|
|
|
|
OnDevicesChanged(_In_ HCMNOTIFICATION hNotify, _In_opt_ PVOID Context,
|
|
|
|
_In_ CM_NOTIFY_ACTION Action,
|
|
|
|
_In_reads_bytes_(EventDataSize) PCM_NOTIFY_EVENT_DATA EventData,
|
|
|
|
_In_ DWORD EventDataSize)
|
2017-11-10 20:30:37 +00:00
|
|
|
{
|
2022-08-08 00:28:41 +00:00
|
|
|
if (Action == CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL ||
|
|
|
|
Action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL)
|
2017-11-10 20:30:37 +00:00
|
|
|
{
|
2021-05-15 09:20:20 +00:00
|
|
|
// Windows automatically sends this message before we ask for it and before we are "ready" to
|
|
|
|
// listen for it.
|
|
|
|
if (s_first_populate_devices_asked.IsSet())
|
|
|
|
{
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
std::lock_guard lk_population(s_populate_mutex);
|
2021-05-15 09:20:20 +00:00
|
|
|
// 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);
|
|
|
|
ciface::XInput::PopulateDevices();
|
|
|
|
});
|
|
|
|
}
|
2017-11-10 20:30:37 +00:00
|
|
|
}
|
2022-08-08 00:28:41 +00:00
|
|
|
return ERROR_SUCCESS;
|
2017-11-10 20:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ciface::Win32::Init(void* hwnd)
|
2017-11-10 17:56:13 +00:00
|
|
|
{
|
2017-11-10 20:30:37 +00:00
|
|
|
s_hwnd = static_cast<HWND>(hwnd);
|
2022-08-08 00:28:41 +00:00
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
XInput::Init();
|
2020-10-20 16:30:15 +00:00
|
|
|
WGInput::Init();
|
2017-11-10 20:30:37 +00:00
|
|
|
|
2022-08-08 00:28:41 +00:00
|
|
|
CM_NOTIFY_FILTER notify_filter{.cbSize = sizeof(notify_filter),
|
|
|
|
.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE,
|
|
|
|
.u{.DeviceInterface{.ClassGuid = GUID_DEVINTERFACE_HID}}};
|
|
|
|
const CONFIGRET cfg_rv =
|
|
|
|
CM_Register_Notification(¬ify_filter, nullptr, OnDevicesChanged, &s_notify_handle);
|
|
|
|
if (cfg_rv != CR_SUCCESS)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "CM_Register_Notification failed: {:x}", cfg_rv);
|
|
|
|
}
|
2017-11-10 17:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ciface::Win32::PopulateDevices(void* hwnd)
|
|
|
|
{
|
2022-08-08 00:28:41 +00:00
|
|
|
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::XInput::PopulateDevices();
|
|
|
|
ciface::WGInput::PopulateDevices();
|
2017-11-10 17:56:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
void ciface::Win32::ChangeWindow(void* hwnd)
|
|
|
|
{
|
2022-08-08 00:28:41 +00:00
|
|
|
s_hwnd = static_cast<HWND>(hwnd);
|
|
|
|
std::lock_guard lk_population(s_populate_mutex);
|
|
|
|
ciface::DInput::ChangeWindow(s_hwnd);
|
2021-05-15 09:20:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
void ciface::Win32::DeInit()
|
|
|
|
{
|
2022-08-08 00:28:41 +00:00
|
|
|
s_first_populate_devices_asked.Clear();
|
|
|
|
DInput::DeInit();
|
|
|
|
s_hwnd = nullptr;
|
|
|
|
|
|
|
|
if (s_notify_handle)
|
2017-11-10 20:30:37 +00:00
|
|
|
{
|
2022-08-08 00:28:41 +00:00
|
|
|
const CONFIGRET cfg_rv = CM_Unregister_Notification(s_notify_handle);
|
|
|
|
if (cfg_rv != CR_SUCCESS)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "CM_Unregister_Notification failed: {:x}", cfg_rv);
|
|
|
|
}
|
|
|
|
s_notify_handle = nullptr;
|
2017-11-10 20:30:37 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 17:56:13 +00:00
|
|
|
XInput::DeInit();
|
2020-10-20 16:30:15 +00:00
|
|
|
WGInput::DeInit();
|
2017-11-10 17:56:13 +00:00
|
|
|
}
|