diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp index 1fb61f7715..f56c11e248 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp @@ -195,20 +195,30 @@ void Wiimote::RealDisconnect() if (m_wiimote_thread.joinable()) m_wiimote_thread.join(); - Host_ConnectWiimote(index, false); + Close(); +} - close(out_sock); - close(in_sock); +void Wiimote::Close() +{ + if (IsOpen()) + { + Host_ConnectWiimote(index, false); - out_sock = -1; - in_sock = -1; + close(out_sock); + close(in_sock); + + out_sock = -1; + in_sock = -1; + } +} + +bool Wiimote::IsOpen() const +{ + return out_sock != -1 && in_sock != -1; } int Wiimote::IORead(unsigned char *buf) { - if (!IsConnected()) - return 0; - // Block select for 1/2000th of a second timeval tv; tv.tv_sec = 0; @@ -239,7 +249,7 @@ int Wiimote::IORead(unsigned char *buf) // This can happen if the bluetooth dongle is disconnected ERROR_LOG(WIIMOTE, "Bluetooth appears to be disconnected. " "Wiimote %i will be disconnected.", index + 1); - //RealDisconnect(); + Close(); } return 0; @@ -247,7 +257,7 @@ int Wiimote::IORead(unsigned char *buf) else if (!r) { // Disconnect - //RealDisconnect(); + Close(); } return r; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp index c22e1972dc..cc05bc10e1 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp @@ -294,6 +294,11 @@ void Wiimote::RealDisconnect() ResetEvent(&hid_overlap); } +bool Wiimote::IsOpen() const +{ + return IsConnected(); +} + int Wiimote::IORead(unsigned char* buf) { DWORD b, r; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm index 9ee39e5b92..64de880180 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm @@ -227,6 +227,11 @@ void Wiimote::RealDisconnect() ichan = NULL; } +bool Wiimote::IsOpen() const +{ + return IsConnected(); +} + int Wiimote::IORead(unsigned char *buf) { int bytes; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 204a1fc698..5ead30bdf8 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -312,7 +312,7 @@ void Wiimote::ThreadFunc() Rumble(); // main loop - while (IsConnected()) + while (IsOpen()) { #ifdef __APPLE__ while (Write()) {} diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index cd1080903f..815c23d945 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -54,6 +54,7 @@ public: bool Write(); bool Connect(); bool IsConnected(); + bool IsOpen() const; void Disconnect(); void DisableDataReporting(); void Rumble(); @@ -72,6 +73,9 @@ public: bdaddr_t bdaddr; // Bluetooth address int out_sock; // Output socket int in_sock; // Input socket + + void Close(); + #elif defined(_WIN32) char devicepath[255]; // Unique wiimote reference //ULONGLONG btaddr; // Bluetooth address