From 9a8e39cba1a11c47346fd747a3e34c7522982e42 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Fri, 15 Apr 2022 13:14:38 +0100 Subject: [PATCH] Slightly refactor controller code in HID Now uses ranges where possible and a function to get the number of connected controllers has been added. --- app/src/main/cpp/skyline/input/npad.cpp | 9 +-------- app/src/main/cpp/skyline/input/npad.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/src/main/cpp/skyline/input/npad.cpp b/app/src/main/cpp/skyline/input/npad.cpp index 867a2ed8..00dcefeb 100644 --- a/app/src/main/cpp/skyline/input/npad.cpp +++ b/app/src/main/cpp/skyline/input/npad.cpp @@ -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 for (auto &device : npads) { - bool connected{}; - for (const auto &controller : controllers) { - if (controller.device == &device) { - connected = true; - break; - } - } - if (!connected) + if (!ranges::any_of(controllers, [&](auto &controller) { return controller.device == &device; })) device.Disconnect(); } } diff --git a/app/src/main/cpp/skyline/input/npad.h b/app/src/main/cpp/skyline/input/npad.h index 7475f049..4fbbc085 100644 --- a/app/src/main/cpp/skyline/input/npad.h +++ b/app/src/main/cpp/skyline/input/npad.h @@ -3,6 +3,7 @@ #pragma once +#include #include "npad_device.h" namespace skyline::input { @@ -69,6 +70,16 @@ namespace skyline::input { return npads.operator[](Translate(id)); } + /** + * @brief Counts the number of currently connected controllers + */ + size_t GetConnectedControllerCount() { + std::scoped_lock lock{mutex}; + return static_cast(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 * @note If any class members were edited, the mutex shouldn't be released till this is called