From 54497be653dfdf2de558426cf460a8b8a96bd524 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 15 Jan 2013 22:57:25 -0600 Subject: [PATCH] Untested Windows buildfix attempt. --- .../Core/Core/Src/HW/WiimoteReal/IODummy.cpp | 16 +- Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp | 6 +- Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp | 213 ++++++++---------- .../Core/Core/Src/HW/WiimoteReal/IOdarwin.mm | 19 +- .../Core/Src/HW/WiimoteReal/WiimoteReal.cpp | 14 +- .../Core/Src/HW/WiimoteReal/WiimoteReal.h | 13 +- .../Core/DolphinWX/Src/WiimoteConfigDiag.cpp | 2 +- 7 files changed, 139 insertions(+), 144 deletions(-) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp index 49afee8ad1..b22f1c7795 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp @@ -21,9 +21,19 @@ namespace WiimoteReal { -int FindWiimotes(Wiimote **wm, int max_wiimotes) +WiimoteScanner::WiimoteScanner() { - return 0; + return; +} + +std::vector WiimoteScanner::FindWiimotes(size_t max_wiimotes) +{ + return std::vector() +} + +bool WiimoteScanner::IsReady() const +{ + return false; } bool Wiimote::Connect() @@ -31,7 +41,7 @@ bool Wiimote::Connect() return 0; } -void Wiimote::RealDisconnect() +void Wiimote::Disconnect() { return; } diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp index 82630aaebb..24e25e9a2b 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp @@ -131,7 +131,7 @@ std::vector WiimoteScanner::FindWiimotes(size_t max_wiimotes) } // Connect to a wiimote with a known address. -bool Wiimote::Open() +bool Wiimote::Connect() { sockaddr_l2 addr; addr.l2_family = AF_BLUETOOTH; @@ -177,7 +177,7 @@ void Wiimote::StopThread() m_wiimote_thread.join(); } -void Wiimote::Close() +void Wiimote::Disconnect() { close(cmd_sock); close(int_sock); @@ -186,7 +186,7 @@ void Wiimote::Close() int_sock = -1; } -bool Wiimote::IsOpen() const +bool Wiimote::IsConnected() const { return cmd_sock != -1;// && int_sock != -1; } diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp index cc05bc10e1..f862193ff9 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp @@ -127,12 +127,21 @@ inline void init_lib() namespace WiimoteReal { +WiimoteScanner::WiimoteScanner() +{ + init_lib(); +} + // Find and connect wiimotes. // Does not replace already found wiimotes even if they are disconnected. // wm is an array of max_wiimotes wiimotes // Returns the total number of found and connected wiimotes. -int FindWiimotes(Wiimote** wm, int max_wiimotes) +std::vector WiimoteScanner::FindWiimotes(size_t max_wiimotes) { + PairUp(); + + std::vector wiimotes; + GUID device_id; HANDLE dev; HDEVINFO device_info; @@ -142,8 +151,6 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes) PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = NULL; HIDD_ATTRIBUTES attr; - init_lib(); - // Count the number of already found wiimotes for (int i = 0; i < MAX_WIIMOTES; ++i) { @@ -207,22 +214,11 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes) // Find an unused slot unsigned int k = 0; - for (; k < MAX_WIIMOTES && !(WIIMOTE_SRC_REAL & g_wiimote_sources[k] && !wm[k]); ++k); - wm[k] = new Wiimote(k); - wm[k]->dev_handle = dev; - memcpy(wm[k]->devicepath, detail_data->DevicePath, 197); + auto const wm = new Wiimote; + wm->dev_handle = dev; + memcpy(wm->devicepath, detail_data->DevicePath, 197); - if (!wm[k]->Connect()) - { - ERROR_LOG(WIIMOTE, "Unable to connect to wiimote %i.", wm[k]->index + 1); - delete wm[k]; - wm[k] = NULL; - CloseHandle(dev); - } - else - { - ++found_wiimotes; - } + found_wiimotes.push_back(wm); } if (detail_data) @@ -233,87 +229,64 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes) return found_wiimotes; } +bool WiimoteScanner::IsReady() const +{ + // TODO: impl + return true; +} + // Connect to a wiimote with a known device path. 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, - (GENERIC_READ | GENERIC_WRITE), - (FILE_SHARE_READ | FILE_SHARE_WRITE), - NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - if (dev_handle == INVALID_HANDLE_VALUE) - return false; + dev_handle = 0; + return false; } hid_overlap.hEvent = CreateEvent(NULL, 1, 1, _T("")); hid_overlap.Offset = 0; hid_overlap.OffsetHigh = 0; - m_connected = true; - - // 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); - + // TODO: do this elsewhere // 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 +/* if (!SetThreadPriority(m_wiimote_thread.native_handle(), THREAD_PRIORITY_TIME_CRITICAL)) { ERROR_LOG(WIIMOTE, "Failed to set wiimote thread priority"); } - - NOTICE_LOG(WIIMOTE, "Connected to wiimote %i.", index + 1); +*/ 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); 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) { - DWORD b, r; - - init_lib(); - - if (!IsConnected()) - return 0; - - *buf = 0; + //*buf = 0; if (!ReadFile(dev_handle, buf, MAX_PAYLOAD, &b, &hid_overlap)) { // Partial read - b = GetLastError(); - + auto const b = GetLastError(); if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED)) { // Remote disconnect @@ -321,7 +294,7 @@ int Wiimote::IORead(unsigned char* buf) return 0; } - r = WaitForSingleObject(hid_overlap.hEvent, WIIMOTE_DEFAULT_TIMEOUT); + auto const r = WaitForSingleObject(hid_overlap.hEvent, WIIMOTE_DEFAULT_TIMEOUT); if (r == WAIT_TIMEOUT) { // Timeout - cancel and continue @@ -357,77 +330,71 @@ int Wiimote::IORead(unsigned char* buf) int Wiimote::IOWrite(unsigned char* buf, int len) { - DWORD bytes, dw; - int i; - - init_lib(); - - if (!IsConnected()) - return 0; - switch (stack) { - case MSBT_STACK_UNKNOWN: - { - // Try to auto-detect the stack type - if (i = WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap)) - { - // Bluesoleil will always return 1 here, even if it's not connected - stack = MSBT_STACK_BLUESOLEIL; - return i; - } - - 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; - } + case MSBT_STACK_UNKNOWN: + { + // Try to auto-detect the stack type + DWORD bytes = 0; + auto i = WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap); + if (i) + { + // Bluesoleil will always return 1 here, even if it's not connected + stack = MSBT_STACK_BLUESOLEIL; return i; + } - case MSBT_STACK_BLUESOLEIL: - return WriteFile(dev_handle, buf + 1, 22, &bytes, &hid_overlap); + i = HidD_SetOutputReport(dev_handle, buf + 1, len - 1); + 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; } -int UnPair() -{ - // TODO: - return 0; -} - // WiiMote Pair-Up, function will return amount of either new paired or unpaired devices // negative number on failure int PairUp(bool unpair) { - init_lib(); - // 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})?"); diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm index 64de880180..95068fe650 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm @@ -10,6 +10,19 @@ } @end +WiimoteScanner::WiimoteScanner() +{} + +std::vector WiimoteScanner::FindWiimotes(size_t max_wiimotes) +{ + return std::vector(); +} + +bool WiimoteScanner::IsReady() const +{ + return false; +} + @implementation SearchBT - (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender error: (IOReturn) error @@ -92,7 +105,7 @@ WARN_LOG(WIIMOTE, "Lost channel to wiimote %i", wm->index + 1); - wm->RealDisconnect(); + wm->Disconnect(); } @end @@ -182,7 +195,7 @@ bool Wiimote::Connect() if (ichan == NULL || cchan == NULL) { ERROR_LOG(WIIMOTE, "Unable to open L2CAP channels " "for wiimote %i", index + 1); - RealDisconnect(); + Disconnect(); return false; } @@ -207,7 +220,7 @@ bool Wiimote::Connect() } // Disconnect a wiimote. -void Wiimote::RealDisconnect() +void Wiimote::Disconnect() { if (!IsConnected()) return; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 2735c1a312..d25a78490d 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -69,8 +69,8 @@ Wiimote::~Wiimote() { StopThread(); - if (IsOpen()) - Close(); + if (IsConnected()) + Disconnect(); ClearReadQueue(); @@ -178,7 +178,7 @@ bool Wiimote::Read() rpt.second = IORead(rpt.first); if (0 == rpt.second) - Close(); + Disconnect(); if (rpt.second > 0 && m_channel > 0) { // Add it to queue @@ -348,7 +348,7 @@ void WiimoteScanner::ThreadFunc() // TODO: this code here is ugly std::lock_guard lk(g_refresh_lock); 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); } #endif @@ -365,14 +365,14 @@ void Wiimote::ThreadFunc() Rumble(); // main loop - while (m_run_thread && IsOpen()) + while (m_run_thread && IsConnected()) { #ifdef __APPLE__ while (Write()) {} Common::SleepCurrentThread(1); #else // TODO: this is all a mess - while (m_run_thread && IsOpen()) + while (m_run_thread && IsConnected()) { bool const did_write = Write(); @@ -512,7 +512,7 @@ void HandleFoundWiimotes(const std::vector& wiimotes) { std::for_each(wiimotes.begin(), wiimotes.end(), [](Wiimote* const wm) { - if (wm->Open()) + if (wm->Connect()) HandleWiimoteConnect(wm); else delete wm; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index 5f8206bf14..4c65768666 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -62,11 +62,11 @@ public: void EmuStop(); // connecting and disconnecting from physical devices - bool Open(); - void Close(); + bool Connect(); + void Disconnect(); // TODO: change to something like IsRelevant - bool IsOpen() const; + bool IsConnected() const; void SetLEDs(int leds); @@ -141,8 +141,13 @@ private: // TODO: this should probably be atomic volatile size_t want_wiimotes; +#if defined(_WIN32) + + +#elif defined(__linux__) && HAVE_BLUEZ int device_id; - int device_sock; + int device_sock; +#endif }; extern std::recursive_mutex g_refresh_lock; diff --git a/Source/Core/DolphinWX/Src/WiimoteConfigDiag.cpp b/Source/Core/DolphinWX/Src/WiimoteConfigDiag.cpp index ba2186d17a..b259cde2b9 100644 --- a/Source/Core/DolphinWX/Src/WiimoteConfigDiag.cpp +++ b/Source/Core/DolphinWX/Src/WiimoteConfigDiag.cpp @@ -245,7 +245,7 @@ void WiimoteConfigDiag::UpdateWiimoteStatus() if (WIIMOTE_SRC_EMU & g_wiimote_sources[index]) CFrame::ConnectWiimote(index, true); 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()); } } }