Slightly refactor controller code in HID

Now uses ranges where possible and a function to get the number of connected controllers has been added.
This commit is contained in:
Billy Laws 2022-04-15 13:14:38 +01:00 committed by PixelyIon
parent 2873f11baa
commit 9a8e39cba1
2 changed files with 12 additions and 8 deletions

View File

@ -69,14 +69,7 @@ namespace skyline::input {
// We do this to prevent triggering the event unless there's a real change in a device's style, which would be caused if we disconnected all controllers then reconnected them // We do this to prevent triggering the event unless there's a real change in a device's style, which would be caused if we disconnected all controllers then reconnected them
for (auto &device : npads) { for (auto &device : npads) {
bool connected{}; if (!ranges::any_of(controllers, [&](auto &controller) { return controller.device == &device; }))
for (const auto &controller : controllers) {
if (controller.device == &device) {
connected = true;
break;
}
}
if (!connected)
device.Disconnect(); device.Disconnect();
} }
} }

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include <range/v3/algorithm.hpp>
#include "npad_device.h" #include "npad_device.h"
namespace skyline::input { namespace skyline::input {
@ -69,6 +70,16 @@ namespace skyline::input {
return npads.operator[](Translate(id)); return npads.operator[](Translate(id));
} }
/**
* @brief Counts the number of currently connected controllers
*/
size_t GetConnectedControllerCount() {
std::scoped_lock lock{mutex};
return static_cast<size_t>(ranges::count_if(controllers, [](const auto &controller) {
return controller.device != nullptr && controller.device->connectionState.connected;
}));
}
/** /**
* @brief Deduces all the mappings from guest controllers -> players based on the configuration supplied by HID services and available controllers * @brief Deduces all the mappings from guest controllers -> players based on the configuration supplied by HID services and available controllers
* @note If any class members were edited, the mutex shouldn't be released till this is called * @note If any class members were edited, the mutex shouldn't be released till this is called