mirror of
https://github.com/cemu-project/Cemu.git
synced 2026-06-08 07:34:57 -06:00
Merge 7889b37370 into 553990a25e
This commit is contained in:
commit
2b3bcfeb93
@ -28,9 +28,6 @@ void NativeWiimoteController::load(const pugi::xml_node& node)
|
|||||||
|
|
||||||
bool NativeWiimoteController::connect()
|
bool NativeWiimoteController::connect()
|
||||||
{
|
{
|
||||||
if (is_connected())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (!m_provider->is_registered_device(m_index))
|
if (!m_provider->is_registered_device(m_index))
|
||||||
{
|
{
|
||||||
m_provider->get_controllers();
|
m_provider->get_controllers();
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "input/api/Wiimote/WiimoteControllerProvider.h"
|
#include "input/api/Wiimote/WiimoteControllerProvider.h"
|
||||||
#include "input/api/Wiimote/NativeWiimoteController.h"
|
#include "input/api/Wiimote/NativeWiimoteController.h"
|
||||||
#include "input/api/Wiimote/WiimoteMessages.h"
|
#include "input/api/Wiimote/WiimoteMessages.h"
|
||||||
|
#include "input/InputManager.h"
|
||||||
|
|
||||||
#ifdef HAS_HIDAPI
|
#ifdef HAS_HIDAPI
|
||||||
#include "input/api/Wiimote/hidapi/HidapiWiimote.h"
|
#include "input/api/Wiimote/hidapi/HidapiWiimote.h"
|
||||||
@ -37,14 +38,21 @@ std::vector<std::shared_ptr<ControllerBase>> WiimoteControllerProvider::get_cont
|
|||||||
auto devices = m_connectedDevices;
|
auto devices = m_connectedDevices;
|
||||||
m_connectedDeviceMutex.unlock();
|
m_connectedDeviceMutex.unlock();
|
||||||
|
|
||||||
std::scoped_lock lock(m_device_mutex);
|
// probe devices outside the exclusive lock to avoid blocking reader/writer threads during I/O
|
||||||
|
std::vector<WiimoteDevicePtr> reachableDevices;
|
||||||
for (auto& device : devices)
|
for (auto& device : devices)
|
||||||
{
|
{
|
||||||
const auto writeable = device->write_data({kStatusRequest, 0x00});
|
const auto writeable = device->write_data({kStatusRequest, 0x00});
|
||||||
if (!writeable)
|
if (writeable)
|
||||||
continue;
|
reachableDevices.push_back(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<size_t> newIndices;
|
||||||
|
{
|
||||||
|
std::scoped_lock lock(m_device_mutex);
|
||||||
|
|
||||||
|
for (auto& device : reachableDevices)
|
||||||
|
{
|
||||||
bool isDuplicate = false;
|
bool isDuplicate = false;
|
||||||
ssize_t lowestReplaceableIndex = -1;
|
ssize_t lowestReplaceableIndex = -1;
|
||||||
for (ssize_t i = m_wiimotes.size() - 1; i >= 0; --i)
|
for (ssize_t i = m_wiimotes.size() - 1; i >= 0; --i)
|
||||||
@ -68,7 +76,13 @@ std::vector<std::shared_ptr<ControllerBase>> WiimoteControllerProvider::get_cont
|
|||||||
m_wiimotes.replace(lowestReplaceableIndex, std::make_unique<Wiimote>(device));
|
m_wiimotes.replace(lowestReplaceableIndex, std::make_unique<Wiimote>(device));
|
||||||
else
|
else
|
||||||
m_wiimotes.push_back(std::make_unique<Wiimote>(device));
|
m_wiimotes.push_back(std::make_unique<Wiimote>(device));
|
||||||
|
m_device_changed = true;
|
||||||
|
newIndices.push_back(lowestReplaceableIndex != -1 ? lowestReplaceableIndex : m_wiimotes.size() - 1);
|
||||||
}
|
}
|
||||||
|
} // lock released here, now safe to call send_packet
|
||||||
|
|
||||||
|
for (const size_t idx : newIndices)
|
||||||
|
send_packet(idx, {kStatusRequest, 0x00});
|
||||||
|
|
||||||
std::vector<std::shared_ptr<ControllerBase>> result;
|
std::vector<std::shared_ptr<ControllerBase>> result;
|
||||||
result.reserve(m_wiimotes.size());
|
result.reserve(m_wiimotes.size());
|
||||||
@ -164,6 +178,22 @@ void WiimoteControllerProvider::connectionThread()
|
|||||||
const auto& l2capDevices = L2CapWiimote::get_devices();
|
const auto& l2capDevices = L2CapWiimote::get_devices();
|
||||||
std::ranges::move(l2capDevices, std::back_inserter(devices));
|
std::ranges::move(l2capDevices, std::back_inserter(devices));
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
// Replace newly opened handles with existing ones for already-active devices
|
||||||
|
// to avoid closing and reopening handles every poll cycle
|
||||||
|
std::shared_lock deviceLock(m_device_mutex);
|
||||||
|
for (auto& device : devices)
|
||||||
|
{
|
||||||
|
for (auto& wiimote : m_wiimotes)
|
||||||
|
{
|
||||||
|
if (wiimote.device && *wiimote.device == *device)
|
||||||
|
{
|
||||||
|
device = wiimote.device;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
{
|
{
|
||||||
std::scoped_lock lock(m_connectedDeviceMutex);
|
std::scoped_lock lock(m_connectedDeviceMutex);
|
||||||
m_connectedDevices.clear();
|
m_connectedDevices.clear();
|
||||||
@ -181,7 +211,7 @@ void WiimoteControllerProvider::reader_thread()
|
|||||||
while (m_running.load(std::memory_order_relaxed))
|
while (m_running.load(std::memory_order_relaxed))
|
||||||
{
|
{
|
||||||
const auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastCheck) > std::chrono::milliseconds(500))
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastCheck) > std::chrono::milliseconds(500))
|
||||||
{
|
{
|
||||||
// check for new connected wiimotes
|
// check for new connected wiimotes
|
||||||
get_controllers();
|
get_controllers();
|
||||||
@ -200,6 +230,7 @@ void WiimoteControllerProvider::reader_thread()
|
|||||||
if (!read_data)
|
if (!read_data)
|
||||||
{
|
{
|
||||||
wiimote.device.reset();
|
wiimote.device.reset();
|
||||||
|
m_device_changed = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (read_data->empty())
|
if (read_data->empty())
|
||||||
@ -698,6 +729,10 @@ void WiimoteControllerProvider::reader_thread()
|
|||||||
}
|
}
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
if (m_device_changed.exchange(false))
|
||||||
|
{
|
||||||
|
InputManager::instance().on_device_changed();
|
||||||
|
}
|
||||||
if (!receivedAnyPacket)
|
if (!receivedAnyPacket)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
@ -972,6 +1007,7 @@ void WiimoteControllerProvider::writer_thread()
|
|||||||
{
|
{
|
||||||
wiimote.device.reset();
|
wiimote.device.reset();
|
||||||
wiimote.rumble = false;
|
wiimote.rumble = false;
|
||||||
|
m_device_changed = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
wiimote.data_ts = std::chrono::high_resolution_clock::now();
|
wiimote.data_ts = std::chrono::high_resolution_clock::now();
|
||||||
|
|||||||
@ -76,6 +76,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::atomic_bool m_running = false;
|
std::atomic_bool m_running = false;
|
||||||
|
std::atomic_bool m_device_changed = false;
|
||||||
std::thread m_reader_thread, m_writer_thread;
|
std::thread m_reader_thread, m_writer_thread;
|
||||||
std::shared_mutex m_device_mutex;
|
std::shared_mutex m_device_mutex;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user