Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Windows: Open wiimotes with the FILE_SHARE_WRITE flag like before.
This should fix issues introduced by real-wiimote-scanning.
  • Loading branch information
jordan-woyak committed Mar 3, 2013
1 parent 11e4403 commit cedfa45
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <unordered_map>
#include <ctime>
#include <unordered_set>

#include <windows.h>
#include <dbt.h>
Expand All @@ -36,6 +37,7 @@
#include <BluetoothAPIs.h>

//#define AUTHENTICATE_WIIMOTES
#define SHARE_WRITE_WIIMOTES

typedef struct _HIDD_ATTRIBUTES
{
Expand Down Expand Up @@ -84,6 +86,11 @@ static int initialized = 0;

std::unordered_map<BTH_ADDR, std::time_t> g_connect_times;

#ifdef SHARE_WRITE_WIIMOTES
std::unordered_set<std::string> g_connected_wiimotes;
std::mutex g_connected_wiimotes_lock;
#endif

inline void init_lib()
{
if (!initialized)
Expand Down Expand Up @@ -259,11 +266,22 @@ bool Wiimote::Connect()
if (IsConnected())
return false;

#ifdef SHARE_WRITE_WIIMOTES
std::lock_guard<std::mutex> 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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -311,6 +333,11 @@ void Wiimote::Disconnect()

CloseHandle(hid_overlap_read.hEvent);
CloseHandle(hid_overlap_write.hEvent);

#ifdef SHARE_WRITE_WIIMOTES
std::lock_guard<std::mutex> lk(g_connected_wiimotes_lock);
g_connected_wiimotes.erase(devicepath);
#endif
}

bool Wiimote::IsConnected() const
Expand Down

0 comments on commit cedfa45

Please sign in to comment.