diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp index 1119ae3f1d..c3824e21be 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,7 @@ #include //#define AUTHENTICATE_WIIMOTES +#define SHARE_WRITE_WIIMOTES typedef struct _HIDD_ATTRIBUTES { @@ -84,6 +86,11 @@ static int initialized = 0; std::unordered_map g_connect_times; +#ifdef SHARE_WRITE_WIIMOTES +std::unordered_set g_connected_wiimotes; +std::mutex g_connected_wiimotes_lock; +#endif + inline void init_lib() { if (!initialized) @@ -259,11 +266,22 @@ bool Wiimote::Connect() if (IsConnected()) return false; +#ifdef SHARE_WRITE_WIIMOTES + std::lock_guard lk(g_connected_wiimotes_lock); + if (g_connected_wiimotes.count(devicepath) != 0) + return false; + + auto const open_flags = FILE_SHARE_READ | FILE_SHARE_WRITE; +#else + // Having no FILE_SHARE_WRITE disallows us from connecting to the same wiimote twice. + // (And disallows using wiimotes in use by other programs) + // This is what "WiiYourself" does. + // Apparently this doesn't work for everyone. It might be their fault. + auto const open_flags = FILE_SHARE_READ; +#endif + dev_handle = CreateFile(devicepath.c_str(), - GENERIC_READ | GENERIC_WRITE, - // Having no FILE_SHARE_WRITE disallows us from connecting to the same wiimote twice. - // This is what "WiiYourself" does. - FILE_SHARE_READ, + GENERIC_READ | GENERIC_WRITE, open_flags, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (dev_handle == INVALID_HANDLE_VALUE) @@ -298,6 +316,10 @@ bool Wiimote::Connect() ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority"); } */ +#ifdef SHARE_WRITE_WIIMOTES + g_connected_wiimotes.insert(devicepath); +#endif + return true; } @@ -311,6 +333,11 @@ void Wiimote::Disconnect() 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 } bool Wiimote::IsConnected() const