Windows: Open wiimotes with the FILE_SHARE_WRITE flag like before.

This should fix issues introduced by real-wiimote-scanning.
This commit is contained in:
Jordan Woyak 2013-03-03 14:20:45 -06:00
parent 11e4403fb2
commit cedfa452b4
1 changed files with 31 additions and 4 deletions

View File

@ -21,6 +21,7 @@
#include <algorithm> #include <algorithm>
#include <unordered_map> #include <unordered_map>
#include <ctime> #include <ctime>
#include <unordered_set>
#include <windows.h> #include <windows.h>
#include <dbt.h> #include <dbt.h>
@ -36,6 +37,7 @@
#include <BluetoothAPIs.h> #include <BluetoothAPIs.h>
//#define AUTHENTICATE_WIIMOTES //#define AUTHENTICATE_WIIMOTES
#define SHARE_WRITE_WIIMOTES
typedef struct _HIDD_ATTRIBUTES typedef struct _HIDD_ATTRIBUTES
{ {
@ -84,6 +86,11 @@ static int initialized = 0;
std::unordered_map<BTH_ADDR, std::time_t> g_connect_times; 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() inline void init_lib()
{ {
if (!initialized) if (!initialized)
@ -259,11 +266,22 @@ bool Wiimote::Connect()
if (IsConnected()) if (IsConnected())
return false; 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(), dev_handle = CreateFile(devicepath.c_str(),
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE, open_flags,
// Having no FILE_SHARE_WRITE disallows us from connecting to the same wiimote twice.
// This is what "WiiYourself" does.
FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (dev_handle == INVALID_HANDLE_VALUE) if (dev_handle == INVALID_HANDLE_VALUE)
@ -298,6 +316,10 @@ bool Wiimote::Connect()
ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority"); ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority");
} }
*/ */
#ifdef SHARE_WRITE_WIIMOTES
g_connected_wiimotes.insert(devicepath);
#endif
return true; return true;
} }
@ -311,6 +333,11 @@ void Wiimote::Disconnect()
CloseHandle(hid_overlap_read.hEvent); CloseHandle(hid_overlap_read.hEvent);
CloseHandle(hid_overlap_write.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 bool Wiimote::IsConnected() const