Untested Windows buildfix attempt.

This commit is contained in:
Jordan Woyak 2013-01-15 22:57:25 -06:00
parent b2acae44b7
commit 54497be653
7 changed files with 139 additions and 144 deletions

View File

@ -21,9 +21,19 @@
namespace WiimoteReal namespace WiimoteReal
{ {
int FindWiimotes(Wiimote **wm, int max_wiimotes) WiimoteScanner::WiimoteScanner()
{ {
return 0; return;
}
std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
{
return std::vector<Wiimote*>()
}
bool WiimoteScanner::IsReady() const
{
return false;
} }
bool Wiimote::Connect() bool Wiimote::Connect()
@ -31,7 +41,7 @@ bool Wiimote::Connect()
return 0; return 0;
} }
void Wiimote::RealDisconnect() void Wiimote::Disconnect()
{ {
return; return;
} }

View File

@ -131,7 +131,7 @@ std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
} }
// Connect to a wiimote with a known address. // Connect to a wiimote with a known address.
bool Wiimote::Open() bool Wiimote::Connect()
{ {
sockaddr_l2 addr; sockaddr_l2 addr;
addr.l2_family = AF_BLUETOOTH; addr.l2_family = AF_BLUETOOTH;
@ -177,7 +177,7 @@ void Wiimote::StopThread()
m_wiimote_thread.join(); m_wiimote_thread.join();
} }
void Wiimote::Close() void Wiimote::Disconnect()
{ {
close(cmd_sock); close(cmd_sock);
close(int_sock); close(int_sock);
@ -186,7 +186,7 @@ void Wiimote::Close()
int_sock = -1; int_sock = -1;
} }
bool Wiimote::IsOpen() const bool Wiimote::IsConnected() const
{ {
return cmd_sock != -1;// && int_sock != -1; return cmd_sock != -1;// && int_sock != -1;
} }

View File

@ -127,12 +127,21 @@ inline void init_lib()
namespace WiimoteReal namespace WiimoteReal
{ {
WiimoteScanner::WiimoteScanner()
{
init_lib();
}
// Find and connect wiimotes. // Find and connect wiimotes.
// Does not replace already found wiimotes even if they are disconnected. // Does not replace already found wiimotes even if they are disconnected.
// wm is an array of max_wiimotes wiimotes // wm is an array of max_wiimotes wiimotes
// Returns the total number of found and connected wiimotes. // Returns the total number of found and connected wiimotes.
int FindWiimotes(Wiimote** wm, int max_wiimotes) std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
{ {
PairUp();
std::vector<Wiimote*> wiimotes;
GUID device_id; GUID device_id;
HANDLE dev; HANDLE dev;
HDEVINFO device_info; HDEVINFO device_info;
@ -142,8 +151,6 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes)
PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = NULL; PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = NULL;
HIDD_ATTRIBUTES attr; HIDD_ATTRIBUTES attr;
init_lib();
// Count the number of already found wiimotes // Count the number of already found wiimotes
for (int i = 0; i < MAX_WIIMOTES; ++i) for (int i = 0; i < MAX_WIIMOTES; ++i)
{ {
@ -207,22 +214,11 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes)
// Find an unused slot // Find an unused slot
unsigned int k = 0; unsigned int k = 0;
for (; k < MAX_WIIMOTES && !(WIIMOTE_SRC_REAL & g_wiimote_sources[k] && !wm[k]); ++k); auto const wm = new Wiimote;
wm[k] = new Wiimote(k); wm->dev_handle = dev;
wm[k]->dev_handle = dev; memcpy(wm->devicepath, detail_data->DevicePath, 197);
memcpy(wm[k]->devicepath, detail_data->DevicePath, 197);
if (!wm[k]->Connect()) found_wiimotes.push_back(wm);
{
ERROR_LOG(WIIMOTE, "Unable to connect to wiimote %i.", wm[k]->index + 1);
delete wm[k];
wm[k] = NULL;
CloseHandle(dev);
}
else
{
++found_wiimotes;
}
} }
if (detail_data) if (detail_data)
@ -233,87 +229,64 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes)
return found_wiimotes; return found_wiimotes;
} }
bool WiimoteScanner::IsReady() const
{
// TODO: impl
return true;
}
// Connect to a wiimote with a known device path. // Connect to a wiimote with a known device path.
bool Wiimote::Connect() bool Wiimote::Connect()
{ {
if (IsConnected()) return false; dev_handle = CreateFile(devicepath,
(GENERIC_READ | GENERIC_WRITE),
(FILE_SHARE_READ | FILE_SHARE_WRITE),
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (!dev_handle) if (dev_handle == INVALID_HANDLE_VALUE)
{ {
dev_handle = CreateFile(devicepath, dev_handle = 0;
(GENERIC_READ | GENERIC_WRITE), return false;
(FILE_SHARE_READ | FILE_SHARE_WRITE),
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (dev_handle == INVALID_HANDLE_VALUE)
return false;
} }
hid_overlap.hEvent = CreateEvent(NULL, 1, 1, _T("")); hid_overlap.hEvent = CreateEvent(NULL, 1, 1, _T(""));
hid_overlap.Offset = 0; hid_overlap.Offset = 0;
hid_overlap.OffsetHigh = 0; hid_overlap.OffsetHigh = 0;
m_connected = true; // TODO: do this elsewhere
// Try a handshake to see if the device is actually connected
if (!Handshake())
{
m_connected = false;
return false;
}
// Set LEDs
SetLEDs(WIIMOTE_LED_1 << index);
m_wiimote_thread = std::thread(std::mem_fun(&Wiimote::ThreadFunc), this);
// This isn't as drastic as it sounds, since the process in which the threads // 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 // reside is normal priority. Needed for keeping audio reports at a decent rate
/*
if (!SetThreadPriority(m_wiimote_thread.native_handle(), THREAD_PRIORITY_TIME_CRITICAL)) if (!SetThreadPriority(m_wiimote_thread.native_handle(), THREAD_PRIORITY_TIME_CRITICAL))
{ {
ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority"); ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority");
} }
*/
NOTICE_LOG(WIIMOTE, "Connected to wiimote %i.", index + 1);
return true; return true;
} }
void Wiimote::RealDisconnect() void Wiimote::Disconnect()
{ {
if (!IsConnected())
return;
m_connected = false;
if (m_wiimote_thread.joinable())
m_wiimote_thread.join();
CloseHandle(dev_handle); CloseHandle(dev_handle);
dev_handle = 0; dev_handle = 0;
ResetEvent(&hid_overlap); //ResetEvent(&hid_overlap);
CloseHandle(hid_overlap.hEvent);
} }
bool Wiimote::IsOpen() const bool Wiimote::IsConnected() const
{ {
return IsConnected(); return dev_handle != 0;
} }
int Wiimote::IORead(unsigned char* buf) int Wiimote::IORead(unsigned char* buf)
{ {
DWORD b, r; //*buf = 0;
init_lib();
if (!IsConnected())
return 0;
*buf = 0;
if (!ReadFile(dev_handle, buf, MAX_PAYLOAD, &b, &hid_overlap)) if (!ReadFile(dev_handle, buf, MAX_PAYLOAD, &b, &hid_overlap))
{ {
// Partial read // Partial read
b = GetLastError(); auto const b = GetLastError();
if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED)) if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED))
{ {
// Remote disconnect // Remote disconnect
@ -321,7 +294,7 @@ int Wiimote::IORead(unsigned char* buf)
return 0; return 0;
} }
r = WaitForSingleObject(hid_overlap.hEvent, WIIMOTE_DEFAULT_TIMEOUT); auto const r = WaitForSingleObject(hid_overlap.hEvent, WIIMOTE_DEFAULT_TIMEOUT);
if (r == WAIT_TIMEOUT) if (r == WAIT_TIMEOUT)
{ {
// Timeout - cancel and continue // Timeout - cancel and continue
@ -357,77 +330,71 @@ int Wiimote::IORead(unsigned char* buf)
int Wiimote::IOWrite(unsigned char* buf, int len) int Wiimote::IOWrite(unsigned char* buf, int len)
{ {
DWORD bytes, dw;
int i;
init_lib();
if (!IsConnected())
return 0;
switch (stack) switch (stack)
{ {
case MSBT_STACK_UNKNOWN: case MSBT_STACK_UNKNOWN:
{ {
// Try to auto-detect the stack type // Try to auto-detect the stack type
if (i = WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap)) DWORD bytes = 0;
{ auto i = WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap);
// Bluesoleil will always return 1 here, even if it's not connected if (i)
stack = MSBT_STACK_BLUESOLEIL; {
return i; // Bluesoleil will always return 1 here, even if it's not connected
} stack = MSBT_STACK_BLUESOLEIL;
if (i = HidD_SetOutputReport(dev_handle, buf + 1, len - 1))
{
stack = MSBT_STACK_MS;
return i;
}
dw = GetLastError();
// Checking for 121 = timeout on semaphore/device off/disconnected to
// avoid trouble with other stacks toshiba/widcomm
if (dw == 121)
{
NOTICE_LOG(WIIMOTE, "IOWrite[MSBT_STACK_UNKNOWN]: Timeout");
RealDisconnect();
}
else ERROR_LOG(WIIMOTE,
"IOWrite[MSBT_STACK_UNKNOWN]: ERROR: %08x", dw);
return 0;
}
case MSBT_STACK_MS:
i = HidD_SetOutputReport(dev_handle, buf + 1, len - 1);
dw = GetLastError();
if (dw == 121)
{
// Semaphore timeout
NOTICE_LOG(WIIMOTE, "WiimoteIOWrite[MSBT_STACK_MS]: Unable to send data to wiimote");
RealDisconnect();
return 0;
}
return i; return i;
}
case MSBT_STACK_BLUESOLEIL: i = HidD_SetOutputReport(dev_handle, buf + 1, len - 1);
return WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap); if (i)
{
stack = MSBT_STACK_MS;
return i;
}
auto const dw = GetLastError();
// Checking for 121 = timeout on semaphore/device off/disconnected to
// avoid trouble with other stacks toshiba/widcomm
if (dw == 121)
{
NOTICE_LOG(WIIMOTE, "IOWrite[MSBT_STACK_UNKNOWN]: Timeout");
return 0;
}
else
{
ERROR_LOG(WIIMOTE, "IOWrite[MSBT_STACK_UNKNOWN]: ERROR: %08x", dw);
// Correct?
return -1
}
break;
}
case MSBT_STACK_MS:
{
i = HidD_SetOutputReport(dev_handle, buf + 1, len - 1);
dw = GetLastError();
if (dw == 121)
{
// Semaphore timeout
NOTICE_LOG(WIIMOTE, "WiimoteIOWrite[MSBT_STACK_MS]: Unable to send data to wiimote");
RealDisconnect();
return 0;
}
return i;
break;
}
case MSBT_STACK_BLUESOLEIL:
return WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap);
break;
} }
return 0; return 0;
} }
int UnPair()
{
// TODO:
return 0;
}
// WiiMote Pair-Up, function will return amount of either new paired or unpaired devices // WiiMote Pair-Up, function will return amount of either new paired or unpaired devices
// negative number on failure // negative number on failure
int PairUp(bool unpair) int PairUp(bool unpair)
{ {
init_lib();
// match strings like "Nintendo RVL-WBC-01", "Nintendo RVL-CNT-01", "Nintendo RVL-CNT-01-TR" // match strings like "Nintendo RVL-WBC-01", "Nintendo RVL-CNT-01", "Nintendo RVL-CNT-01-TR"
const std::wregex wiimote_device_name(L"Nintendo RVL-\\w{3}-\\d{2}(-\\w{2})?"); const std::wregex wiimote_device_name(L"Nintendo RVL-\\w{3}-\\d{2}(-\\w{2})?");

View File

@ -10,6 +10,19 @@
} }
@end @end
WiimoteScanner::WiimoteScanner()
{}
std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
{
return std::vector<Wiimote*>();
}
bool WiimoteScanner::IsReady() const
{
return false;
}
@implementation SearchBT @implementation SearchBT
- (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender - (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender
error: (IOReturn) error error: (IOReturn) error
@ -92,7 +105,7 @@
WARN_LOG(WIIMOTE, "Lost channel to wiimote %i", wm->index + 1); WARN_LOG(WIIMOTE, "Lost channel to wiimote %i", wm->index + 1);
wm->RealDisconnect(); wm->Disconnect();
} }
@end @end
@ -182,7 +195,7 @@ bool Wiimote::Connect()
if (ichan == NULL || cchan == NULL) { if (ichan == NULL || cchan == NULL) {
ERROR_LOG(WIIMOTE, "Unable to open L2CAP channels " ERROR_LOG(WIIMOTE, "Unable to open L2CAP channels "
"for wiimote %i", index + 1); "for wiimote %i", index + 1);
RealDisconnect(); Disconnect();
return false; return false;
} }
@ -207,7 +220,7 @@ bool Wiimote::Connect()
} }
// Disconnect a wiimote. // Disconnect a wiimote.
void Wiimote::RealDisconnect() void Wiimote::Disconnect()
{ {
if (!IsConnected()) if (!IsConnected())
return; return;

View File

@ -69,8 +69,8 @@ Wiimote::~Wiimote()
{ {
StopThread(); StopThread();
if (IsOpen()) if (IsConnected())
Close(); Disconnect();
ClearReadQueue(); ClearReadQueue();
@ -178,7 +178,7 @@ bool Wiimote::Read()
rpt.second = IORead(rpt.first); rpt.second = IORead(rpt.first);
if (0 == rpt.second) if (0 == rpt.second)
Close(); Disconnect();
if (rpt.second > 0 && m_channel > 0) { if (rpt.second > 0 && m_channel > 0) {
// Add it to queue // Add it to queue
@ -348,7 +348,7 @@ void WiimoteScanner::ThreadFunc()
// TODO: this code here is ugly // TODO: this code here is ugly
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock); std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
for (unsigned int i = 0; i != MAX_WIIMOTES; ++i) for (unsigned int i = 0; i != MAX_WIIMOTES; ++i)
if (g_wiimotes[i] && !g_wiimotes[i]->IsOpen()) if (g_wiimotes[i] && !g_wiimotes[i]->IsConnected())
HandleWiimoteDisconnect(i); HandleWiimoteDisconnect(i);
} }
#endif #endif
@ -365,14 +365,14 @@ void Wiimote::ThreadFunc()
Rumble(); Rumble();
// main loop // main loop
while (m_run_thread && IsOpen()) while (m_run_thread && IsConnected())
{ {
#ifdef __APPLE__ #ifdef __APPLE__
while (Write()) {} while (Write()) {}
Common::SleepCurrentThread(1); Common::SleepCurrentThread(1);
#else #else
// TODO: this is all a mess // TODO: this is all a mess
while (m_run_thread && IsOpen()) while (m_run_thread && IsConnected())
{ {
bool const did_write = Write(); bool const did_write = Write();
@ -512,7 +512,7 @@ void HandleFoundWiimotes(const std::vector<Wiimote*>& wiimotes)
{ {
std::for_each(wiimotes.begin(), wiimotes.end(), [](Wiimote* const wm) std::for_each(wiimotes.begin(), wiimotes.end(), [](Wiimote* const wm)
{ {
if (wm->Open()) if (wm->Connect())
HandleWiimoteConnect(wm); HandleWiimoteConnect(wm);
else else
delete wm; delete wm;

View File

@ -62,11 +62,11 @@ public:
void EmuStop(); void EmuStop();
// connecting and disconnecting from physical devices // connecting and disconnecting from physical devices
bool Open(); bool Connect();
void Close(); void Disconnect();
// TODO: change to something like IsRelevant // TODO: change to something like IsRelevant
bool IsOpen() const; bool IsConnected() const;
void SetLEDs(int leds); void SetLEDs(int leds);
@ -141,8 +141,13 @@ private:
// TODO: this should probably be atomic // TODO: this should probably be atomic
volatile size_t want_wiimotes; volatile size_t want_wiimotes;
#if defined(_WIN32)
#elif defined(__linux__) && HAVE_BLUEZ
int device_id; int device_id;
int device_sock; int device_sock;
#endif
}; };
extern std::recursive_mutex g_refresh_lock; extern std::recursive_mutex g_refresh_lock;

View File

@ -245,7 +245,7 @@ void WiimoteConfigDiag::UpdateWiimoteStatus()
if (WIIMOTE_SRC_EMU & g_wiimote_sources[index]) if (WIIMOTE_SRC_EMU & g_wiimote_sources[index])
CFrame::ConnectWiimote(index, true); CFrame::ConnectWiimote(index, true);
else if (WIIMOTE_SRC_REAL & g_wiimote_sources[index] && WiimoteReal::g_wiimotes[index]) else if (WIIMOTE_SRC_REAL & g_wiimote_sources[index] && WiimoteReal::g_wiimotes[index])
CFrame::ConnectWiimote(index, WiimoteReal::g_wiimotes[index]->IsOpen()); CFrame::ConnectWiimote(index, WiimoteReal::g_wiimotes[index]->IsConnected());
} }
} }
} }