From 50a4ea814d985431cd2566298374d99bdb209d33 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Mon, 6 Feb 2023 23:13:01 +0100 Subject: [PATCH] Add auto controller count detection and make it default Note: Existing users have to manually adjust this setting --- GlosSIConfig/UIModel.cpp | 2 +- GlosSIConfig/qml/AdvancedTargetSettings.qml | 15 +++++++++-- GlosSITarget/InputRedirector.cpp | 30 +++++++++++++++++---- GlosSITarget/InputRedirector.h | 1 + common/Settings.h | 2 +- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/GlosSIConfig/UIModel.cpp b/GlosSIConfig/UIModel.cpp index 219cff9..2ba051e 100644 --- a/GlosSIConfig/UIModel.cpp +++ b/GlosSIConfig/UIModel.cpp @@ -404,7 +404,7 @@ QVariantMap UIModel::getDefaultConf() const QJsonValue::fromVariant(QString::fromStdWString(getSteamUserId(false)))}, {"globalModeGameId", ""}, {"globalModeUseGamepadUI", false}, - {"controller", QJsonObject{{"maxControllers", 1}, {"emulateDS4", false}, {"allowDesktopConfig", false}}}, + {"controller", QJsonObject{{"maxControllers", -1}, {"emulateDS4", false}, {"allowDesktopConfig", false}}}, {"devices", QJsonObject{ {"hideDevices", true}, diff --git a/GlosSIConfig/qml/AdvancedTargetSettings.qml b/GlosSIConfig/qml/AdvancedTargetSettings.qml index c6327cd..1c536af 100644 --- a/GlosSIConfig/qml/AdvancedTargetSettings.qml +++ b/GlosSIConfig/qml/AdvancedTargetSettings.qml @@ -261,8 +261,14 @@ CollapsiblePane { width: 128 editable: true value: shortcutInfo.controller.maxControllers - from: 0 + from: -1 to: 4 + textFromValue: function(value) { + if (value == -1) { + return qsTr("auto"); + } + return Number(value); + } onValueChanged: shortcutInfo.controller.maxControllers = value } RoundButton { @@ -271,7 +277,12 @@ CollapsiblePane { helpInfoDialog.text = qsTr("GlosSI will only provide [NUMBER] of controllers") + "\n" - + qsTr("Required to set to actually connected controller count when using \"real device IDs\" ") + + qsTr("-1 ^= auto-detect") + + "\n" + + qsTr("auto detection only works upon launch") + + "\n" + + "\n" + + qsTr("Required to manuelly set to actually connected controller count when using \"real device IDs\" ") helpInfoDialog.open() } width: 48 diff --git a/GlosSITarget/InputRedirector.cpp b/GlosSITarget/InputRedirector.cpp index 1854861..1631766 100644 --- a/GlosSITarget/InputRedirector.cpp +++ b/GlosSITarget/InputRedirector.cpp @@ -49,6 +49,22 @@ InputRedirector::~InputRedirector() void InputRedirector::run() { run_ = vigem_connected_; + max_controllers_ = Settings::controller.maxControllers; + if (max_controllers_ < 0) { + for (int i = 0; i < XUSER_MAX_COUNT; i++) { + XINPUT_STATE state{}; + if (XInputGetState(i, &state) == ERROR_SUCCESS) { + max_controllers_ = i + 1; + } + } + if (max_controllers_ < 0) { + max_controllers_ = 1; + spdlog::error("Failed to auto detect controller count. Defaulting to 1"); + } + else { + spdlog::info("Auto detected {} controllers", max_controllers_); + } + } controller_thread_ = std::thread(&InputRedirector::runLoop, this); #ifdef _WIN32 Overlay::AddOverlayElem([this](bool window_has_focus, ImGuiID dockspace_id) { @@ -58,13 +74,17 @@ void InputRedirector::run() ImGui::Text("Max. controller count"); ImGui::SameLine(); ImGui::InputInt("##Max. controller count", &countcopy, 1, 1); + ImGui::Text("-1 = Auto-detection (auto-detection only works on launch"); if (countcopy > XUSER_MAX_COUNT) { countcopy = XUSER_MAX_COUNT; } - if (countcopy < 0) { - countcopy = 0; + if (countcopy < -1) { + countcopy = -1; } Settings::controller.maxControllers = countcopy; + if (Settings::controller.maxControllers > -1) { + max_controllers_ = countcopy; + } if (ImGui::Checkbox("Emulate DS4 (instead of Xbox360 controller)", &Settings::controller.emulateDS4)) { controller_settings_changed_ = true; @@ -131,12 +151,12 @@ void InputRedirector::runLoop() unplugVigemPad(i); } } - if (Settings::controller.maxControllers < XUSER_MAX_COUNT) { - for (int i = Settings::controller.maxControllers; i < XUSER_MAX_COUNT; i++) { + if (max_controllers_ < XUSER_MAX_COUNT) { + for (int i = max_controllers_; i < XUSER_MAX_COUNT; i++) { unplugVigemPad(i); } } - for (int i = 0; i < XUSER_MAX_COUNT && i < Settings::controller.maxControllers; i++) { + for (int i = 0; i < XUSER_MAX_COUNT && i < max_controllers_; i++) { XINPUT_STATE state{}; if (XInputGetState(i, &state) == ERROR_SUCCESS) { if (vt_pad_[i] != nullptr) { diff --git a/GlosSITarget/InputRedirector.h b/GlosSITarget/InputRedirector.h index cc456a9..94b1f10 100644 --- a/GlosSITarget/InputRedirector.h +++ b/GlosSITarget/InputRedirector.h @@ -35,6 +35,7 @@ class InputRedirector { private: void runLoop(); + int max_controllers_ = -1; static constexpr int start_delay_ms_ = 2000; bool run_ = false; int overlay_elem_id_ = -1; diff --git a/common/Settings.h b/common/Settings.h index ece94ac..2d83b58 100644 --- a/common/Settings.h +++ b/common/Settings.h @@ -65,7 +65,7 @@ namespace Settings inline struct Controller { - int maxControllers = 1; + int maxControllers = -1; bool allowDesktopConfig = false; bool emulateDS4 = false; } controller;