diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp index 826b9e67ce..f06bec2dde 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp @@ -133,6 +133,28 @@ void WiimoteScanner::FindWiimotes(std::vector & found_wiimotes, Wiimot } +void Wiimote::InitInternal() +{ + cmd_sock = -1; + int_sock = -1; + + int fds[2]; + if (pipe(fds)) + { + ERROR_LOG(WIIMOTE, "pipe failed"); + abort(); + } + wakeup_pipe_w = fds[1]; + wakeup_pipe_r = fds[0]; + bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}}; +} + +void Wiimote::TeardownInternal() +{ + close(wakeup_pipe_w); + close(wakeup_pipe_r); +} + // Connect to a wiimote with a known address. bool Wiimote::ConnectInternal() { diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp index 91bd1bec6c..58ebfbfa64 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp @@ -514,12 +514,6 @@ bool Wiimote::ConnectInternal() } #endif - hid_overlap_read = OVERLAPPED(); - hid_overlap_read.hEvent = CreateEvent(NULL, true, false, NULL); - - hid_overlap_write = OVERLAPPED(); - hid_overlap_write.hEvent = CreateEvent(NULL, true, false, NULL); - // TODO: thread isn't started here now, do this elsewhere // This isn't as drastic as it sounds, since the process in which the threads // reside is normal priority. Needed for keeping audio reports at a decent rate @@ -544,15 +538,30 @@ void Wiimote::DisconnectInternal() CloseHandle(dev_handle); dev_handle = 0; - CloseHandle(hid_overlap_read.hEvent); - CloseHandle(hid_overlap_write.hEvent); - #ifdef SHARE_WRITE_WIIMOTES std::lock_guard lk(g_connected_wiimotes_lock); g_connected_wiimotes.erase(devicepath); #endif } +void Wiimote::InitInternal() +{ + dev_handle = 0; + stack = MSBT_STACK_UNKNOWN; + + hid_overlap_read = OVERLAPPED(); + hid_overlap_read.hEvent = CreateEvent(NULL, true, false, NULL); + + hid_overlap_write = OVERLAPPED(); + hid_overlap_write.hEvent = CreateEvent(NULL, true, false, NULL); +} + +void Wiimote::TeardownInternal() +{ + CloseHandle(hid_overlap_read.hEvent); + CloseHandle(hid_overlap_write.hEvent); +} + bool Wiimote::IsConnected() const { return dev_handle != 0; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm index 0583cbf94c..e7e9033338 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm @@ -188,6 +188,22 @@ bool WiimoteScanner::IsReady() const return true; } +void Wiimote::InitInternal() +{ + inputlen = 0; + m_connected = false; + m_wiimote_thread_run_loop = NULL; +} + +void Wiimote::TeardownInternal() +{ + if (m_wiimote_thread_run_loop) + { + CFRelease(m_wiimote_thread_run_loop); + m_wiimote_thread_run_loop = NULL; + } +} + // Connect to a wiimote with a known address. bool Wiimote::ConnectInternal() { @@ -226,6 +242,9 @@ bool Wiimote::ConnectInternal() m_connected = true; [cbt release]; + + m_wiimote_thread_run_loop = (CFRunLoopRef) CFRetain(CFRunLoopGetCurrent()); + return true; } @@ -259,7 +278,10 @@ bool Wiimote::IsConnected() const void Wiimote::IOWakeup() { - CFRunLoopStop(m_wiimote_thread_run_loop); + if (m_wiimote_thread_run_loop) + { + CFRunLoopStop(m_wiimote_thread_run_loop); + } } int Wiimote::IORead(unsigned char *buf) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 77f84449be..ee6de5f19d 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -39,29 +39,12 @@ WiimoteScanner g_wiimote_scanner; Wiimote::Wiimote() : index() -#ifdef __APPLE__ - , btd(), ichan(), cchan(), input(), inputlen(), m_connected() -#elif defined(__linux__) && HAVE_BLUEZ - , cmd_sock(-1), int_sock(-1) -#elif defined(_WIN32) - , dev_handle(0), stack(MSBT_STACK_UNKNOWN) -#endif , m_last_input_report() , m_channel(0) , m_rumble_state() , m_need_prepare() { -#if defined(__linux__) && HAVE_BLUEZ - int fds[2]; - if (pipe(fds)) - { - ERROR_LOG(WIIMOTE, "pipe failed"); - abort(); - } - wakeup_pipe_w = fds[1]; - wakeup_pipe_r = fds[0]; - bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}}; -#endif + InitInternal(); } Wiimote::~Wiimote() @@ -69,10 +52,7 @@ Wiimote::~Wiimote() StopThread(); ClearReadQueue(); m_write_reports.Clear(); -#if defined(__linux__) && HAVE_BLUEZ - close(wakeup_pipe_w); - close(wakeup_pipe_r); -#endif + TeardownInternal(); } // to be called from CPU thread @@ -514,8 +494,6 @@ void Wiimote::StopThread() if (m_wiimote_thread.joinable()) m_wiimote_thread.join(); #if defined(__APPLE__) - CFRelease(m_wiimote_thread_run_loop); - m_wiimote_thread_run_loop = NULL; #endif } @@ -543,9 +521,6 @@ void Wiimote::WaitReady() void Wiimote::ThreadFunc() { Common::SetCurrentThreadName("Wiimote Device Thread"); -#if defined(__APPLE__) - m_wiimote_thread_run_loop = (CFRunLoopRef) CFRetain(CFRunLoopGetCurrent()); -#endif bool ok = ConnectInternal(); @@ -565,7 +540,7 @@ void Wiimote::ThreadFunc() if (!PrepareOnThread()) { ERROR_LOG(WIIMOTE, "Wiimote::PrepareOnThread failed. Disconnecting Wiimote %d.", index + 1); - DisconnectInternal(); + break; } } Write(); diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index fa94303a66..e4e965400c 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -56,6 +56,9 @@ public: bool ConnectInternal(); void DisconnectInternal(); + void InitInternal(); + void TeardownInternal(); + bool Connect(); // TODO: change to something like IsRelevant