// This is called from the GUI thread void Refresh() { std::lock_guard<std::mutex> lk(g_refresh_lock); #ifdef _WIN32 Shutdown(); Initialize(); #else // Make sure real wiimotes have been initialized if (!g_real_wiimotes_initialized) { Initialize(); return; } // Find the number of slots configured for real wiimotes unsigned int wanted_wiimotes = 0; for (unsigned int i = 0; i < MAX_WIIMOTES; ++i) if (WIIMOTE_SRC_REAL & g_wiimote_sources[i]) ++wanted_wiimotes; // Remove wiimotes that are paired with slots no longer configured for a // real wiimote or that are disconnected for (unsigned int i = 0; i < MAX_WIIMOTES; ++i) if (g_wiimotes[i] && (!(WIIMOTE_SRC_REAL & g_wiimote_sources[i]) || !g_wiimotes[i]->IsConnected())) { delete g_wiimotes[i]; g_wiimotes[i] = NULL; --g_wiimotes_found; } // Scan for wiimotes if we want more if (wanted_wiimotes > g_wiimotes_found) { // 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); // Connect newly found wiimotes. int num_new_wiimotes = ConnectWiimotes(g_wiimotes); DEBUG_LOG(WIIMOTE, "Connected to %i additional Real Wiimotes", num_new_wiimotes); g_wiimotes_found = num_wiimotes; } #endif }
void WiimoteScanner::ThreadFunc() { Common::SetCurrentThreadName("Wiimote Scanning Thread"); NOTICE_LOG(WIIMOTE, "Wiimote scanning has started."); while (m_run_thread.load()) { std::vector<Wiimote*> found_wiimotes; Wiimote* found_board = nullptr; //NOTICE_LOG(WIIMOTE, "In loop"); if (m_want_wiimotes.load() || m_want_bb.load()) { FindWiimotes(found_wiimotes, found_board); } else { // Does stuff needed to detect disconnects on Windows Update(); } //NOTICE_LOG(WIIMOTE, "After update"); // TODO: this is a fairly lame place for this CheckForDisconnectedWiimotes(); if (m_want_wiimotes.load()) HandleFoundWiimotes(found_wiimotes); if (m_want_bb.load() && found_board) TryToConnectBalanceBoard(found_board); //std::this_thread::yield(); Common::SleepCurrentThread(500); } NOTICE_LOG(WIIMOTE, "Wiimote scanning has stopped."); }
void WiimoteScanner::ThreadFunc() { Common::SetCurrentThreadName("Wiimote Scanning Thread"); NOTICE_LOG(WIIMOTE, "Wiimote scanning thread has started."); while (m_scan_thread_running.IsSet()) { m_scan_mode_changed_event.WaitFor(std::chrono::milliseconds(500)); CheckForDisconnectedWiimotes(); if (m_scan_mode.load() == WiimoteScanMode::DO_NOT_SCAN) continue; if (CalculateWantedWiimotes() != 0 || CalculateWantedBB() != 0) { std::vector<Wiimote*> found_wiimotes; Wiimote* found_board = nullptr; FindWiimotes(found_wiimotes, found_board); { std::lock_guard<std::mutex> lk(g_wiimotes_mutex); std::for_each(found_wiimotes.begin(), found_wiimotes.end(), TryToConnectWiimote); if (found_board) TryToConnectBalanceBoard(found_board); } } else { Update(); // Does stuff needed to detect disconnects on Windows } if (m_scan_mode.load() == WiimoteScanMode::SCAN_ONCE) m_scan_mode.store(WiimoteScanMode::DO_NOT_SCAN); } NOTICE_LOG(WIIMOTE, "Wiimote scanning thread has stopped."); }
unsigned int Initialize() { // Return if already initialized if (g_real_wiimotes_initialized) return g_wiimotes_found; memset(g_wiimotes, 0, sizeof(g_wiimotes)); // Only call FindWiimotes with the number of slots configured for real wiimotes unsigned int wanted_wiimotes = 0; for (unsigned int i = 0; i < MAX_WIIMOTES; ++i) if (WIIMOTE_SRC_REAL & g_wiimote_sources[i]) ++wanted_wiimotes; // Don't bother initializing if we don't want any real wiimotes if (0 == wanted_wiimotes) { g_wiimotes_found = 0; return 0; } // Initialized g_real_wiimotes_initialized = true; g_wiimotes_found = FindWiimotes(g_wiimotes, wanted_wiimotes); DEBUG_LOG(WIIMOTE, "Found %i Real Wiimotes, %i wanted", g_wiimotes_found, wanted_wiimotes); #ifndef _WIN32 atexit(WiimoteReal::Shutdown); g_wiimotes_found = ConnectWiimotes(g_wiimotes); #endif DEBUG_LOG(WIIMOTE, "Connected to %i Real Wiimotes", g_wiimotes_found); return g_wiimotes_found; }