// Connect to a Wiimote with a known device path. bool WiimoteWindows::ConnectInternal() { if (IsConnected()) return true; if (!IsNewWiimote(UTF16ToUTF8(m_devicepath))) return false; auto const open_flags = FILE_SHARE_READ | FILE_SHARE_WRITE; m_dev_handle = CreateFile(m_devicepath.c_str(), GENERIC_READ | GENERIC_WRITE, open_flags, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr); if (m_dev_handle == INVALID_HANDLE_VALUE) { m_dev_handle = nullptr; return false; } #if 0 TCHAR name[128] = {}; pHidD_GetProductString(dev_handle, name, 128); //ERROR_LOG(WIIMOTE, "Product string: %s", TStrToUTF8(name).c_str()); if (!IsValidBluetoothName(TStrToUTF8(name))) { CloseHandle(dev_handle); dev_handle = 0; return false; } #endif #if 0 HIDD_ATTRIBUTES attr; attr.Size = sizeof(attr); if (!pHidD_GetAttributes(dev_handle, &attr)) { CloseHandle(dev_handle); dev_handle = 0; return false; } #endif // TODO: thread isn't started here now, 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"); } */ return true; }
// Connect to a Wiimote with a known device path. bool WiimoteWindows::ConnectInternal() { if (IsConnected()) return false; #ifdef SHARE_WRITE_WIIMOTES std::lock_guard<std::mutex> lk(g_connected_wiimotes_lock); if (g_connected_wiimotes.count(m_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 m_dev_handle = CreateFile(m_devicepath.c_str(), GENERIC_READ | GENERIC_WRITE, open_flags, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr); if (m_dev_handle == INVALID_HANDLE_VALUE) { m_dev_handle = 0; return false; } #if 0 TCHAR name[128] = {}; pHidD_GetProductString(dev_handle, name, 128); //ERROR_LOG(WIIMOTE, "Product string: %s", TStrToUTF8(name).c_str()); if (!IsValidBluetoothName(TStrToUTF8(name))) { CloseHandle(dev_handle); dev_handle = 0; return false; } #endif #if 0 HIDD_ATTRIBUTES attr; attr.Size = sizeof(attr); if (!pHidD_GetAttributes(dev_handle, &attr)) { CloseHandle(dev_handle); dev_handle = 0; return false; } #endif // TODO: thread isn't started here now, 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"); } */ #ifdef SHARE_WRITE_WIIMOTES g_connected_wiimotes.insert(m_devicepath); #endif return true; }