diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 53cc8019ab..6727194714 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -44,7 +44,7 @@ volatile bool g_run_wiimote_thread = false; std::thread g_wiimote_threads[MAX_WIIMOTES] = {}; Common::CriticalSection g_refresh_critsec; -void WiimoteThreadFunc(Wiimote& arg); +void WiimoteThreadFunc(Wiimote* arg); void StartWiimoteThreads(); void StopWiimoteThreads(); @@ -425,6 +425,8 @@ void Refresh() if (WIIMOTE_SRC_REAL & g_wiimote_sources[i]) ++wanted_wiimotes; + StopWiimoteThreads(); + g_refresh_critsec.Enter(); // Remove wiimotes that are paired with slots no longer configured for a @@ -433,32 +435,28 @@ void Refresh() if (g_wiimotes[i] && (!(WIIMOTE_SRC_REAL & g_wiimote_sources[i]) || !g_wiimotes[i]->IsConnected())) { - // TODO: this looks broken delete g_wiimotes[i]; g_wiimotes[i] = NULL; - g_wiimote_threads[i].join(); --g_wiimotes_found; } - // Don't scan for wiimotes if we don't want any more - if (wanted_wiimotes <= g_wiimotes_found) + // Scan for wiimotes if we want more + if (wanted_wiimotes > g_wiimotes_found) { - g_refresh_critsec.Leave(); - return; - } + // Scan for wiimotes + unsigned int num_wiimotes = FindWiimotes(g_wiimotes, wanted_wiimotes); - // Scan for wiimotes - unsigned int num_wiimotes = FindWiimotes(g_wiimotes, wanted_wiimotes); - - DEBUG_LOG(WIIMOTE, "Found %i Real Wiimotes, %i wanted", num_wiimotes, wanted_wiimotes); + DEBUG_LOG(WIIMOTE, "Found %i Real Wiimotes, %i wanted", num_wiimotes, wanted_wiimotes); #ifndef _WIN32 - // Connect newly found wiimotes. - int num_new_wiimotes = ConnectWiimotes(g_wiimotes); + // Connect newly found wiimotes. + int num_new_wiimotes = ConnectWiimotes(g_wiimotes); - DEBUG_LOG(WIIMOTE, "Connected to %i additional Real Wiimotes", num_new_wiimotes); + DEBUG_LOG(WIIMOTE, "Connected to %i additional Real Wiimotes", num_new_wiimotes); #endif - g_wiimotes_found = num_wiimotes; + + g_wiimotes_found = num_wiimotes; + } g_refresh_critsec.Leave(); @@ -510,8 +508,8 @@ void StartWiimoteThreads() { g_run_wiimote_thread = true; for (unsigned int i = 0; i < MAX_WIIMOTES; ++i) - if (g_wiimotes[i]) - g_wiimote_threads[i] = std::thread(WiimoteThreadFunc, *g_wiimotes[i]); + if (g_wiimotes[i] && !g_wiimote_threads[i].joinable()) + g_wiimote_threads[i] = std::thread(WiimoteThreadFunc, g_wiimotes[i]); } void StopWiimoteThreads() @@ -522,35 +520,35 @@ void StopWiimoteThreads() g_wiimote_threads[i].join(); } -void WiimoteThreadFunc(Wiimote& wiimote) +void WiimoteThreadFunc(Wiimote* wiimote) { #ifdef __APPLE__ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; #endif char thname[] = "Wiimote # Thread"; - thname[8] = (char)('1' + wiimote.index); + thname[8] = (char)('1' + wiimote->index); Common::SetCurrentThreadName(thname); // rumble briefly - wiimote.Rumble(); + wiimote->Rumble(); - Host_ConnectWiimote(wiimote.index, true); + Host_ConnectWiimote(wiimote->index, true); // main loop - while (g_run_wiimote_thread && wiimote.IsConnected()) + while (g_run_wiimote_thread && wiimote->IsConnected()) { // hopefully this is alright - while (wiimote.Write()) {} + while (wiimote->Write()) {} #ifndef __APPLE__ // sleep if there was nothing to read - if (false == wiimote.Read()) + if (false == wiimote->Read()) #endif Common::SleepCurrentThread(1); } - Host_ConnectWiimote(wiimote.index, false); + Host_ConnectWiimote(wiimote->index, false); #ifdef __APPLE__ [pool release]; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index c04c5af2e9..038aa70ca8 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -35,7 +35,7 @@ typedef std::pair Report; namespace WiimoteReal { -class Wiimote +class Wiimote : NonCopyable { friend class WiimoteEmu::Wiimote; public: