mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-10 14:39:01 +01:00
Merge pull request #7693 from jordan-woyak/hotplug-callback-fix
ControllerInterface: Hotplug callback fixes.
This commit is contained in:
commit
a8bc6f9899
@ -475,7 +475,6 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Update references in case controllers were refreshed
|
|
||||||
g_controller_interface.ChangeWindow(wsi.render_surface);
|
g_controller_interface.ChangeWindow(wsi.render_surface);
|
||||||
Pad::LoadConfig();
|
Pad::LoadConfig();
|
||||||
Keyboard::LoadConfig();
|
Keyboard::LoadConfig();
|
||||||
@ -485,12 +484,14 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
|
|||||||
const bool delete_savestate = boot->delete_savestate;
|
const bool delete_savestate = boot->delete_savestate;
|
||||||
|
|
||||||
// Load and Init Wiimotes - only if we are booting in Wii mode
|
// Load and Init Wiimotes - only if we are booting in Wii mode
|
||||||
|
bool init_wiimotes = false;
|
||||||
if (core_parameter.bWii && !SConfig::GetInstance().m_bt_passthrough_enabled)
|
if (core_parameter.bWii && !SConfig::GetInstance().m_bt_passthrough_enabled)
|
||||||
{
|
{
|
||||||
if (init_controllers)
|
if (init_controllers)
|
||||||
{
|
{
|
||||||
Wiimote::Initialize(savestate_path ? Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES :
|
Wiimote::Initialize(savestate_path ? Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES :
|
||||||
Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
|
Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
|
||||||
|
init_wiimotes = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -501,11 +502,13 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
|
|||||||
NetPlay::SetupWiimotes();
|
NetPlay::SetupWiimotes();
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::ScopeGuard controller_guard{[init_controllers] {
|
Common::ScopeGuard controller_guard{[init_controllers, init_wiimotes] {
|
||||||
if (!init_controllers)
|
if (!init_controllers)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Wiimote::Shutdown();
|
if (init_wiimotes)
|
||||||
|
Wiimote::Shutdown();
|
||||||
|
|
||||||
Keyboard::Shutdown();
|
Keyboard::Shutdown();
|
||||||
Pad::Shutdown();
|
Pad::Shutdown();
|
||||||
g_controller_interface.Shutdown();
|
g_controller_interface.Shutdown();
|
||||||
|
@ -26,6 +26,8 @@ InputConfig* GetConfig()
|
|||||||
|
|
||||||
void Shutdown()
|
void Shutdown()
|
||||||
{
|
{
|
||||||
|
s_config.UnregisterHotplugCallback();
|
||||||
|
|
||||||
s_config.ClearControllers();
|
s_config.ClearControllers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ void Initialize()
|
|||||||
s_config.CreateController<GCKeyboard>(i);
|
s_config.CreateController<GCKeyboard>(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_controller_interface.RegisterDevicesChangedCallback(LoadConfig);
|
s_config.RegisterHotplugCallback();
|
||||||
|
|
||||||
// Load the saved controller config
|
// Load the saved controller config
|
||||||
s_config.LoadConfig(true);
|
s_config.LoadConfig(true);
|
||||||
@ -57,4 +59,4 @@ KeyboardStatus GetStatus(int port)
|
|||||||
{
|
{
|
||||||
return static_cast<GCKeyboard*>(s_config.GetController(port))->GetInput();
|
return static_cast<GCKeyboard*>(s_config.GetController(port))->GetInput();
|
||||||
}
|
}
|
||||||
}
|
} // namespace Keyboard
|
||||||
|
@ -23,6 +23,8 @@ InputConfig* GetConfig()
|
|||||||
|
|
||||||
void Shutdown()
|
void Shutdown()
|
||||||
{
|
{
|
||||||
|
s_config.UnregisterHotplugCallback();
|
||||||
|
|
||||||
s_config.ClearControllers();
|
s_config.ClearControllers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ void Initialize()
|
|||||||
s_config.CreateController<GCPad>(i);
|
s_config.CreateController<GCPad>(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_controller_interface.RegisterDevicesChangedCallback(LoadConfig);
|
s_config.RegisterHotplugCallback();
|
||||||
|
|
||||||
// Load the saved controller config
|
// Load the saved controller config
|
||||||
s_config.LoadConfig(true);
|
s_config.LoadConfig(true);
|
||||||
@ -74,4 +76,4 @@ bool GetMicButton(const int pad_num)
|
|||||||
{
|
{
|
||||||
return static_cast<GCPad*>(s_config.GetController(pad_num))->GetMicButton();
|
return static_cast<GCPad*>(s_config.GetController(pad_num))->GetMicButton();
|
||||||
}
|
}
|
||||||
}
|
} // namespace Pad
|
||||||
|
@ -67,6 +67,8 @@ ControllerEmu::ControlGroup* GetTurntableGroup(int number, WiimoteEmu::Turntable
|
|||||||
|
|
||||||
void Shutdown()
|
void Shutdown()
|
||||||
{
|
{
|
||||||
|
s_config.UnregisterHotplugCallback();
|
||||||
|
|
||||||
s_config.ClearControllers();
|
s_config.ClearControllers();
|
||||||
|
|
||||||
WiimoteReal::Stop();
|
WiimoteReal::Stop();
|
||||||
@ -80,7 +82,7 @@ void Initialize(InitializeMode init_mode)
|
|||||||
s_config.CreateController<WiimoteEmu::Wiimote>(i);
|
s_config.CreateController<WiimoteEmu::Wiimote>(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_controller_interface.RegisterDevicesChangedCallback(LoadConfig);
|
s_config.RegisterHotplugCallback();
|
||||||
|
|
||||||
LoadConfig();
|
LoadConfig();
|
||||||
|
|
||||||
@ -215,4 +217,4 @@ void DoState(PointerWrap& p)
|
|||||||
for (int i = 0; i < MAX_BBMOTES; ++i)
|
for (int i = 0; i < MAX_BBMOTES; ++i)
|
||||||
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
|
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
|
||||||
}
|
}
|
||||||
}
|
} // namespace Wiimote
|
||||||
|
@ -238,7 +238,7 @@ void Initialize()
|
|||||||
if (s_config.ControllersNeedToBeCreated())
|
if (s_config.ControllersNeedToBeCreated())
|
||||||
s_config.CreateController<HotkeyManager>();
|
s_config.CreateController<HotkeyManager>();
|
||||||
|
|
||||||
g_controller_interface.RegisterDevicesChangedCallback(LoadConfig);
|
s_config.RegisterHotplugCallback();
|
||||||
|
|
||||||
// load the saved controller config
|
// load the saved controller config
|
||||||
s_config.LoadConfig(true);
|
s_config.LoadConfig(true);
|
||||||
@ -260,9 +260,11 @@ ControllerEmu::ControlGroup* GetHotkeyGroup(HotkeyGroup group)
|
|||||||
|
|
||||||
void Shutdown()
|
void Shutdown()
|
||||||
{
|
{
|
||||||
|
s_config.UnregisterHotplugCallback();
|
||||||
|
|
||||||
s_config.ClearControllers();
|
s_config.ClearControllers();
|
||||||
}
|
}
|
||||||
}
|
} // namespace HotkeyManagerEmu
|
||||||
|
|
||||||
struct HotkeyGroupInfo
|
struct HotkeyGroupInfo
|
||||||
{
|
{
|
||||||
|
@ -292,11 +292,11 @@ void MainWindow::ShutdownControllers()
|
|||||||
{
|
{
|
||||||
m_hotkey_scheduler->Stop();
|
m_hotkey_scheduler->Stop();
|
||||||
|
|
||||||
g_controller_interface.Shutdown();
|
|
||||||
Pad::Shutdown();
|
Pad::Shutdown();
|
||||||
Keyboard::Shutdown();
|
Keyboard::Shutdown();
|
||||||
Wiimote::Shutdown();
|
Wiimote::Shutdown();
|
||||||
HotkeyManagerEmu::Shutdown();
|
HotkeyManagerEmu::Shutdown();
|
||||||
|
g_controller_interface.Shutdown();
|
||||||
|
|
||||||
m_hotkey_scheduler->deleteLater();
|
m_hotkey_scheduler->deleteLater();
|
||||||
}
|
}
|
||||||
|
@ -249,10 +249,20 @@ void ControllerInterface::UpdateInput()
|
|||||||
|
|
||||||
// Register a callback to be called when a device is added or removed (as from the input backends'
|
// Register a callback to be called when a device is added or removed (as from the input backends'
|
||||||
// hotplug thread), or when devices are refreshed
|
// hotplug thread), or when devices are refreshed
|
||||||
void ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback)
|
// Returns a handle for later removing the callback.
|
||||||
|
ControllerInterface::HotplugCallbackHandle
|
||||||
|
ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
||||||
m_devices_changed_callbacks.emplace_back(std::move(callback));
|
m_devices_changed_callbacks.emplace_back(std::move(callback));
|
||||||
|
return std::prev(m_devices_changed_callbacks.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unregister a device callback.
|
||||||
|
void ControllerInterface::UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
|
||||||
|
m_devices_changed_callbacks.erase(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke all callbacks that were registered
|
// Invoke all callbacks that were registered
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "Common/WindowSystemInfo.h"
|
#include "Common/WindowSystemInfo.h"
|
||||||
#include "InputCommon/ControllerInterface/Device.h"
|
#include "InputCommon/ControllerInterface/Device.h"
|
||||||
@ -40,6 +40,8 @@
|
|||||||
class ControllerInterface : public ciface::Core::DeviceContainer
|
class ControllerInterface : public ciface::Core::DeviceContainer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using HotplugCallbackHandle = std::list<std::function<void()>>::iterator;
|
||||||
|
|
||||||
ControllerInterface() : m_is_init(false) {}
|
ControllerInterface() : m_is_init(false) {}
|
||||||
void Initialize(const WindowSystemInfo& wsi);
|
void Initialize(const WindowSystemInfo& wsi);
|
||||||
void ChangeWindow(void* hwnd);
|
void ChangeWindow(void* hwnd);
|
||||||
@ -50,11 +52,12 @@ public:
|
|||||||
bool IsInit() const { return m_is_init; }
|
bool IsInit() const { return m_is_init; }
|
||||||
void UpdateInput();
|
void UpdateInput();
|
||||||
|
|
||||||
void RegisterDevicesChangedCallback(std::function<void(void)> callback);
|
HotplugCallbackHandle RegisterDevicesChangedCallback(std::function<void(void)> callback);
|
||||||
|
void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle);
|
||||||
void InvokeDevicesChangedCallbacks() const;
|
void InvokeDevicesChangedCallbacks() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::function<void()>> m_devices_changed_callbacks;
|
std::list<std::function<void()>> m_devices_changed_callbacks;
|
||||||
mutable std::mutex m_callbacks_mutex;
|
mutable std::mutex m_callbacks_mutex;
|
||||||
std::atomic<bool> m_is_init;
|
std::atomic<bool> m_is_init;
|
||||||
std::atomic<bool> m_is_populating_devices{false};
|
std::atomic<bool> m_is_populating_devices{false};
|
||||||
|
@ -147,6 +147,21 @@ std::size_t InputConfig::GetControllerCount() const
|
|||||||
return m_controllers.size();
|
return m_controllers.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InputConfig::RegisterHotplugCallback()
|
||||||
|
{
|
||||||
|
// Update control references on all controllers
|
||||||
|
// as configured devices may have been added or removed.
|
||||||
|
m_hotplug_callback_handle = g_controller_interface.RegisterDevicesChangedCallback([this] {
|
||||||
|
for (auto& controller : m_controllers)
|
||||||
|
controller->UpdateReferences(g_controller_interface);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConfig::UnregisterHotplugCallback()
|
||||||
|
{
|
||||||
|
g_controller_interface.UnregisterDevicesChangedCallback(m_hotplug_callback_handle);
|
||||||
|
}
|
||||||
|
|
||||||
bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
|
bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
|
||||||
{
|
{
|
||||||
if (static_cast<size_t>(index) >= m_controllers.size())
|
if (static_cast<size_t>(index) >= m_controllers.size())
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
||||||
|
|
||||||
namespace ControllerEmu
|
namespace ControllerEmu
|
||||||
{
|
{
|
||||||
class EmulatedController;
|
class EmulatedController;
|
||||||
@ -40,7 +42,12 @@ public:
|
|||||||
std::string GetProfileName() const { return m_profile_name; }
|
std::string GetProfileName() const { return m_profile_name; }
|
||||||
std::size_t GetControllerCount() const;
|
std::size_t GetControllerCount() const;
|
||||||
|
|
||||||
|
// These should be used after creating all controllers and before clearing them, respectively.
|
||||||
|
void RegisterHotplugCallback();
|
||||||
|
void UnregisterHotplugCallback();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
ControllerInterface::HotplugCallbackHandle m_hotplug_callback_handle;
|
||||||
std::vector<std::unique_ptr<ControllerEmu::EmulatedController>> m_controllers;
|
std::vector<std::unique_ptr<ControllerEmu::EmulatedController>> m_controllers;
|
||||||
const std::string m_ini_name;
|
const std::string m_ini_name;
|
||||||
const std::string m_gui_name;
|
const std::string m_gui_name;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user