From ee868e2362d09427d6416c52f37dff85bf25a6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Wed, 19 Jul 2017 15:55:45 +0800 Subject: [PATCH] Move the Wiimote connect code out of Host I don't know who thought it would be a good idea to put the Wiimote connect code as part of the Host interface, and have that called from both the UI code and the core. And then hack around it by having "force connect" events whenever Host_ConnectWiimote is called from the core... --- Source/Android/jni/MainAndroid.cpp | 4 --- Source/Core/Core/HW/Wiimote.cpp | 25 +++++++++++++++++ Source/Core/Core/HW/Wiimote.h | 1 + Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp | 2 +- .../Core/Core/HW/WiimoteReal/WiimoteReal.cpp | 22 ++++++++------- Source/Core/Core/Host.h | 1 - Source/Core/DolphinNoGUI/MainNoGUI.cpp | 19 ------------- Source/Core/DolphinQt2/Host.cpp | 3 --- Source/Core/DolphinWX/Frame.cpp | 16 ----------- Source/Core/DolphinWX/Frame.h | 1 - Source/Core/DolphinWX/FrameTools.cpp | 27 +++---------------- Source/Core/DolphinWX/Globals.h | 12 --------- Source/Core/DolphinWX/Main.cpp | 20 -------------- Source/UnitTests/StubHost.cpp | 3 --- 14 files changed, 42 insertions(+), 114 deletions(-) diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp index f6c305efa06..cda73a9fe52 100644 --- a/Source/Android/jni/MainAndroid.cpp +++ b/Source/Android/jni/MainAndroid.cpp @@ -140,10 +140,6 @@ bool Host_RendererIsFullscreen() return false; } -void Host_ConnectWiimote(int wm_idx, bool connect) -{ -} - void Host_ShowVideoConfig(void*, const std::string&) { } diff --git a/Source/Core/Core/HW/Wiimote.cpp b/Source/Core/Core/HW/Wiimote.cpp index e3e9b244f43..4d821479481 100644 --- a/Source/Core/Core/HW/Wiimote.cpp +++ b/Source/Core/Core/HW/Wiimote.cpp @@ -6,9 +6,15 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" +#include "Common/StringUtil.h" +#include "Core/ConfigManager.h" +#include "Core/Core.h" #include "Core/HW/WiimoteEmu/WiimoteEmu.h" #include "Core/HW/WiimoteReal/WiimoteReal.h" +#include "Core/IOS/IOS.h" +#include "Core/IOS/USB/Bluetooth/BTEmu.h" +#include "Core/IOS/USB/Bluetooth/WiimoteDevice.h" #include "Core/Movie.h" #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" @@ -81,6 +87,25 @@ void Initialize(InitializeMode init_mode) Movie::ChangeWiiPads(); } +void Connect(unsigned int index, bool connect) +{ + if (SConfig::GetInstance().m_bt_passthrough_enabled || index >= MAX_BBMOTES) + return; + + const auto ios = IOS::HLE::GetIOS(); + if (!ios) + return; + + const auto bluetooth = std::static_pointer_cast( + ios->GetDeviceByName("/dev/usb/oh1/57e/305")); + + if (bluetooth) + bluetooth->AccessWiiMote(index | 0x100)->Activate(connect); + + const char* message = connect ? "Wii Remote %i connected" : "Wii Remote %i disconnected"; + Core::DisplayMessage(StringFromFormat(message, index + 1), 3000); +} + void ResetAllWiimotes() { for (int i = WIIMOTE_CHAN_0; i < MAX_BBMOTES; ++i) diff --git a/Source/Core/Core/HW/Wiimote.h b/Source/Core/Core/HW/Wiimote.h index e08510aec53..4b8f3a1a91c 100644 --- a/Source/Core/Core/HW/Wiimote.h +++ b/Source/Core/Core/HW/Wiimote.h @@ -58,6 +58,7 @@ enum class InitializeMode void Shutdown(); void Initialize(InitializeMode init_mode); +void Connect(unsigned int index, bool connect); void ResetAllWiimotes(); void LoadConfig(); void Resume(); diff --git a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp index 8fcdb19fc35..0b4ceafc3f8 100644 --- a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp @@ -948,7 +948,7 @@ void Wiimote::ConnectOnInput() if (buttons != 0 || m_extension->IsButtonPressed()) { - Host_ConnectWiimote(m_index, true); + ::Wiimote::Connect(m_index, true); // arbitrary value so it doesn't try to send multiple requests before Dolphin can react // if Wii Remotes are polled at 200Hz then this results in one request being sent per 500ms m_last_connect_request_counter = 100; diff --git a/Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp index 60a0e28c6eb..f7b864035c6 100644 --- a/Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp @@ -18,6 +18,7 @@ #include "Common/Thread.h" #include "Core/ConfigManager.h" #include "Core/Core.h" +#include "Core/HW/Wiimote.h" #include "Core/HW/WiimoteCommon/WiimoteHid.h" #include "Core/HW/WiimoteEmu/WiimoteEmu.h" #include "Core/HW/WiimoteReal/IOAndroid.h" @@ -390,7 +391,7 @@ void Wiimote::ConnectOnInput() // check any button without checking accelerometer data if ((rpt[2] & 0x1F) != 0 || (rpt[3] & 0x9F) != 0) { - Host_ConnectWiimote(m_index, true); + ::Wiimote::Connect(m_index, true); // see WiimoteEmu::Wiimote::ConnectOnInput(), same idea here m_last_connect_request_counter = 100; } @@ -795,11 +796,14 @@ void ChangeWiimoteSource(unsigned int index, int source) } // reconnect to the emulator - Host_ConnectWiimote(index, false); - if (WIIMOTE_SRC_EMU & source) - Host_ConnectWiimote(index, true); + Core::RunAsCPUThread([index, source] { + ::Wiimote::Connect(index, false); + if (WIIMOTE_SRC_EMU & source) + ::Wiimote::Connect(index, true); + }); } +// Called from the Wiimote scanner thread static bool TryToConnectWiimoteToSlot(Wiimote* wm, unsigned int i) { if (WIIMOTE_SRC_REAL & g_wiimote_sources[i] && !g_wiimotes[i]) @@ -808,7 +812,7 @@ static bool TryToConnectWiimoteToSlot(Wiimote* wm, unsigned int i) { NOTICE_LOG(WIIMOTE, "Connected to Wiimote %i.", i + 1); g_wiimotes[i] = wm; - Host_ConnectWiimote(i, true); + Core::RunAsCPUThread([i] { ::Wiimote::Connect(i, true); }); std::lock_guard lk(s_known_ids_mutex); s_known_ids.insert(wm->GetId()); } @@ -884,13 +888,11 @@ void Update(int wiimote_number) if (g_wiimotes[wiimote_number]) g_wiimotes[wiimote_number]->Update(); + g_wiimotes_mutex.unlock(); + // Wiimote::Update() may remove the Wiimote if it was disconnected. if (!g_wiimotes[wiimote_number]) - { - Host_ConnectWiimote(wiimote_number, false); - } - - g_wiimotes_mutex.unlock(); + ::Wiimote::Connect(wiimote_number, false); } void ConnectOnInput(int wiimote_number) diff --git a/Source/Core/Core/Host.h b/Source/Core/Core/Host.h index c33fc47f06b..0a8a8231aec 100644 --- a/Source/Core/Core/Host.h +++ b/Source/Core/Core/Host.h @@ -26,7 +26,6 @@ bool Host_UINeedsControllerState(); bool Host_RendererHasFocus(); bool Host_RendererIsFullscreen(); -void Host_ConnectWiimote(int wm_idx, bool connect); void Host_Message(int Id); void Host_NotifyMapLoaded(); void Host_RefreshDSPDebuggerWindow(); diff --git a/Source/Core/DolphinNoGUI/MainNoGUI.cpp b/Source/Core/DolphinNoGUI/MainNoGUI.cpp index 89a7cfe8328..fc91ccac049 100644 --- a/Source/Core/DolphinNoGUI/MainNoGUI.cpp +++ b/Source/Core/DolphinNoGUI/MainNoGUI.cpp @@ -22,12 +22,9 @@ #include "Core/BootManager.h" #include "Core/ConfigManager.h" #include "Core/Core.h" -#include "Core/HW/Wiimote.h" #include "Core/Host.h" #include "Core/IOS/IOS.h" #include "Core/IOS/STM/STM.h" -#include "Core/IOS/USB/Bluetooth/BTEmu.h" -#include "Core/IOS/USB/Bluetooth/WiimoteDevice.h" #include "Core/State.h" #include "UICommon/CommandLineParse.h" @@ -131,22 +128,6 @@ bool Host_RendererIsFullscreen() return rendererIsFullscreen; } -void Host_ConnectWiimote(int wm_idx, bool connect) -{ - Core::QueueHostJob([=] { - const auto ios = IOS::HLE::GetIOS(); - if (!ios || SConfig::GetInstance().m_bt_passthrough_enabled) - return; - Core::RunAsCPUThread([&] { - const auto bt = std::static_pointer_cast( - ios->GetDeviceByName("/dev/usb/oh1/57e/305")); - if (bt) - bt->AccessWiiMote(wm_idx | 0x100)->Activate(connect); - Host_UpdateMainFrame(); - }); - }); -} - void Host_ShowVideoConfig(void*, const std::string&) { } diff --git a/Source/Core/DolphinQt2/Host.cpp b/Source/Core/DolphinQt2/Host.cpp index 0f0c008b096..2509d8c9b98 100644 --- a/Source/Core/DolphinQt2/Host.cpp +++ b/Source/Core/DolphinQt2/Host.cpp @@ -104,9 +104,6 @@ void Host_NotifyMapLoaded() void Host_UpdateDisasmDialog() { } -void Host_ConnectWiimote(int wm_idx, bool connect) -{ -} void Host_ShowVideoConfig(void* parent, const std::string& backend_name) { } diff --git a/Source/Core/DolphinWX/Frame.cpp b/Source/Core/DolphinWX/Frame.cpp index fc5efddb34c..28e25b48065 100644 --- a/Source/Core/DolphinWX/Frame.cpp +++ b/Source/Core/DolphinWX/Frame.cpp @@ -817,22 +817,6 @@ void CFrame::OnHostMessage(wxCommandEvent& event) case IDM_STOPPED: OnStopped(); break; - - case IDM_FORCE_CONNECT_WIIMOTE1: - case IDM_FORCE_CONNECT_WIIMOTE2: - case IDM_FORCE_CONNECT_WIIMOTE3: - case IDM_FORCE_CONNECT_WIIMOTE4: - case IDM_FORCE_CONNECT_BALANCEBOARD: - ConnectWiimote(event.GetId() - IDM_FORCE_CONNECT_WIIMOTE1, true); - break; - - case IDM_FORCE_DISCONNECT_WIIMOTE1: - case IDM_FORCE_DISCONNECT_WIIMOTE2: - case IDM_FORCE_DISCONNECT_WIIMOTE3: - case IDM_FORCE_DISCONNECT_WIIMOTE4: - case IDM_FORCE_DISCONNECT_BALANCEBOARD: - ConnectWiimote(event.GetId() - IDM_FORCE_DISCONNECT_WIIMOTE1, false); - break; } } diff --git a/Source/Core/DolphinWX/Frame.h b/Source/Core/DolphinWX/Frame.h index a4489b83da3..afc555af7a8 100644 --- a/Source/Core/DolphinWX/Frame.h +++ b/Source/Core/DolphinWX/Frame.h @@ -256,7 +256,6 @@ private: void OnStopped(); void OnRenderWindowSizeRequest(int width, int height); void UpdateTitle(const wxString& str); - static void ConnectWiimote(int wm_idx, bool connect); // Event functions void PostEvent(wxCommandEvent& event); diff --git a/Source/Core/DolphinWX/FrameTools.cpp b/Source/Core/DolphinWX/FrameTools.cpp index d633561cfd3..e448d2329c9 100644 --- a/Source/Core/DolphinWX/FrameTools.cpp +++ b/Source/Core/DolphinWX/FrameTools.cpp @@ -1406,27 +1406,6 @@ void CFrame::OnFifoPlayer(wxCommandEvent& WXUNUSED(event)) } } -void CFrame::ConnectWiimote(int wm_idx, bool connect) -{ - if (Core::IsRunning() && SConfig::GetInstance().bWii && - !SConfig::GetInstance().m_bt_passthrough_enabled) - { - Core::RunAsCPUThread([&] { - const auto ios = IOS::HLE::GetIOS(); - if (!ios) - return; - - const auto bt = std::static_pointer_cast( - ios->GetDeviceByName("/dev/usb/oh1/57e/305")); - if (bt) - bt->AccessWiiMote(wm_idx | 0x100)->Activate(connect); - const char* message = connect ? "Wii Remote %i connected" : "Wii Remote %i disconnected"; - Core::DisplayMessage(StringFromFormat(message, wm_idx + 1), 3000); - Host_UpdateMainFrame(); - }); - } -} - void CFrame::OnConnectWiimote(wxCommandEvent& event) { const auto ios = IOS::HLE::GetIOS(); @@ -1435,9 +1414,9 @@ void CFrame::OnConnectWiimote(wxCommandEvent& event) Core::RunAsCPUThread([&] { const auto bt = std::static_pointer_cast( ios->GetDeviceByName("/dev/usb/oh1/57e/305")); - const bool is_connected = - bt && bt->AccessWiiMote((event.GetId() - IDM_CONNECT_WIIMOTE1) | 0x100)->IsConnected(); - ConnectWiimote(event.GetId() - IDM_CONNECT_WIIMOTE1, !is_connected); + const unsigned int wiimote_index = event.GetId() - IDM_CONNECT_WIIMOTE1; + const bool is_connected = bt && bt->AccessWiiMote(wiimote_index | 0x100)->IsConnected(); + Wiimote::Connect(wiimote_index, !is_connected); }); } diff --git a/Source/Core/DolphinWX/Globals.h b/Source/Core/DolphinWX/Globals.h index 3abcec98f3e..d11f42efaeb 100644 --- a/Source/Core/DolphinWX/Globals.h +++ b/Source/Core/DolphinWX/Globals.h @@ -308,18 +308,6 @@ enum IDM_STOPPED, IDM_HOST_MESSAGE, - // Used for Host_ConnectWiimote() - IDM_FORCE_CONNECT_WIIMOTE1, - IDM_FORCE_CONNECT_WIIMOTE2, - IDM_FORCE_CONNECT_WIIMOTE3, - IDM_FORCE_CONNECT_WIIMOTE4, - IDM_FORCE_CONNECT_BALANCEBOARD, - IDM_FORCE_DISCONNECT_WIIMOTE1, - IDM_FORCE_DISCONNECT_WIIMOTE2, - IDM_FORCE_DISCONNECT_WIIMOTE3, - IDM_FORCE_DISCONNECT_WIIMOTE4, - IDM_FORCE_DISCONNECT_BALANCEBOARD, - IDM_MPANEL, ID_STATUSBAR, diff --git a/Source/Core/DolphinWX/Main.cpp b/Source/Core/DolphinWX/Main.cpp index 869bdfc74e3..2ef465531a4 100644 --- a/Source/Core/DolphinWX/Main.cpp +++ b/Source/Core/DolphinWX/Main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -70,8 +69,6 @@ std::string wxStringTranslator(const char*); CFrame* main_frame = nullptr; -static std::mutex s_init_mutex; - bool DolphinApp::Initialize(int& c, wxChar** v) { #if defined HAVE_X11 && HAVE_X11 @@ -122,8 +119,6 @@ bool DolphinApp::OnInit() ParseCommandLine(); - std::lock_guard lk(s_init_mutex); - UICommon::SetUserDirectory(m_user_path.ToStdString()); UICommon::CreateDirectories(); InitLanguageSupport(); // The language setting is loaded from the user directory @@ -473,21 +468,6 @@ bool Host_RendererIsFullscreen() return main_frame->RendererIsFullscreen(); } -void Host_ConnectWiimote(int wm_idx, bool connect) -{ - std::lock_guard lk(s_init_mutex); - if (connect) - { - wxCommandEvent event(wxEVT_HOST_COMMAND, IDM_FORCE_CONNECT_WIIMOTE1 + wm_idx); - main_frame->GetEventHandler()->AddPendingEvent(event); - } - else - { - wxCommandEvent event(wxEVT_HOST_COMMAND, IDM_FORCE_DISCONNECT_WIIMOTE1 + wm_idx); - main_frame->GetEventHandler()->AddPendingEvent(event); - } -} - void Host_ShowVideoConfig(void* parent, const std::string& backend_name) { wxWindow* const parent_window = static_cast(parent); diff --git a/Source/UnitTests/StubHost.cpp b/Source/UnitTests/StubHost.cpp index eec3d995576..6348b1c7760 100644 --- a/Source/UnitTests/StubHost.cpp +++ b/Source/UnitTests/StubHost.cpp @@ -51,9 +51,6 @@ bool Host_RendererIsFullscreen() { return false; } -void Host_ConnectWiimote(int, bool) -{ -} void Host_ShowVideoConfig(void*, const std::string&) { }