From 3cd28f433b4f1651c1a09012e9ddeee9c520c797 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 20 Sep 2023 17:10:41 +1000 Subject: [PATCH] Controller: Use std::span --- src/core/analog_controller.cpp | 2 - src/core/analog_joystick.cpp | 2 - src/core/controller.cpp | 48 ++----------------- src/core/controller.h | 18 ++++--- src/core/digital_controller.cpp | 2 - src/core/fullscreen_ui.cpp | 26 ++++------ src/core/guncon.cpp | 2 - src/core/imgui_overlays.cpp | 3 +- src/core/negcon.cpp | 2 - src/core/playstation_mouse.cpp | 2 - .../controllerbindingwidgets.cpp | 32 ++++++------- src/util/input_manager.cpp | 26 ++++------ 12 files changed, 45 insertions(+), 120 deletions(-) diff --git a/src/core/analog_controller.cpp b/src/core/analog_controller.cpp index 36d1ab861..11389ba1c 100644 --- a/src/core/analog_controller.cpp +++ b/src/core/analog_controller.cpp @@ -875,9 +875,7 @@ const Controller::ControllerInfo AnalogController::INFO = {ControllerType::Analo "AnalogController", TRANSLATE_NOOP("ControllerType", "Analog Controller"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::LargeSmallMotors}; void AnalogController::LoadSettings(SettingsInterface& si, const char* section) diff --git a/src/core/analog_joystick.cpp b/src/core/analog_joystick.cpp index 85f838a4c..5bfc3bd35 100644 --- a/src/core/analog_joystick.cpp +++ b/src/core/analog_joystick.cpp @@ -407,9 +407,7 @@ const Controller::ControllerInfo AnalogJoystick::INFO = {ControllerType::AnalogJ "AnalogJoystick", TRANSLATE_NOOP("ControllerType", "Analog Joystick"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::NoVibration}; void AnalogJoystick::LoadSettings(SettingsInterface& si, const char* section) diff --git a/src/core/controller.cpp b/src/core/controller.cpp index d92278ff5..98ef5dcab 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -15,10 +15,8 @@ static const Controller::ControllerInfo s_none_info = {ControllerType::None, "None", TRANSLATE_NOOP("ControllerType", "Not Connected"), - nullptr, - 0, - nullptr, - 0, + {}, + {}, Controller::VibrationCapabilities::NoVibration}; static const Controller::ControllerInfo* s_controller_info[] = { @@ -144,53 +142,13 @@ std::vector> Controller::GetControllerTypeNa return ret; } -std::vector Controller::GetControllerBinds(const std::string_view& type) -{ - std::vector ret; - - const ControllerInfo* info = GetControllerInfo(type); - if (info) - { - for (u32 i = 0; i < info->num_bindings; i++) - { - const ControllerBindingInfo& bi = info->bindings[i]; - if (bi.type == InputBindingInfo::Type::Unknown || bi.type == InputBindingInfo::Type::Motor) - continue; - - ret.emplace_back(info->bindings[i].name); - } - } - - return ret; -} - -std::vector Controller::GetControllerBinds(ControllerType type) -{ - std::vector ret; - - const ControllerInfo* info = GetControllerInfo(type); - if (info) - { - for (u32 i = 0; i < info->num_bindings; i++) - { - const ControllerBindingInfo& bi = info->bindings[i]; - if (bi.type == InputBindingInfo::Type::Unknown || bi.type == InputBindingInfo::Type::Motor) - continue; - - ret.emplace_back(info->bindings[i].name); - } - } - - return ret; -} - std::optional Controller::GetBindIndex(ControllerType type, const std::string_view& bind_name) { const ControllerInfo* info = GetControllerInfo(type); if (!info) return std::nullopt; - for (u32 i = 0; i < info->num_bindings; i++) + for (u32 i = 0; i < static_cast(info->bindings.size()); i++) { if (bind_name == info->bindings[i].name) return i; diff --git a/src/core/controller.h b/src/core/controller.h index 50823bde2..f7670e9e5 100644 --- a/src/core/controller.h +++ b/src/core/controller.h @@ -1,13 +1,17 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #pragma once -#include "common/image.h" + #include "input_types.h" #include "settings.h" #include "types.h" + +#include "common/image.h" + #include #include +#include #include #include #include @@ -42,10 +46,8 @@ public: ControllerType type; const char* name; const char* display_name; - const ControllerBindingInfo* bindings; - u32 num_bindings; - const SettingInfo* settings; - u32 num_settings; + std::span bindings; + std::span settings; VibrationCapabilities vibration_caps; }; @@ -96,10 +98,6 @@ public: /// Returns a list of controller type names. Pair of [name, display name]. static std::vector> GetControllerTypeNames(); - /// Returns the list of binds for the specified controller type. - static std::vector GetControllerBinds(const std::string_view& type); - static std::vector GetControllerBinds(ControllerType type); - /// Gets the integer code for an axis in the specified controller type. static std::optional GetBindIndex(ControllerType type, const std::string_view& bind_name); diff --git a/src/core/digital_controller.cpp b/src/core/digital_controller.cpp index cbc84e2b2..a1657549c 100644 --- a/src/core/digital_controller.cpp +++ b/src/core/digital_controller.cpp @@ -178,9 +178,7 @@ const Controller::ControllerInfo DigitalController::INFO = {ControllerType::Digi "DigitalController", TRANSLATE_NOOP("ControllerType", "Digital Controller"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::NoVibration}; void DigitalController::LoadSettings(SettingsInterface& si, const char* section) diff --git a/src/core/fullscreen_ui.cpp b/src/core/fullscreen_ui.cpp index f443b0a50..e4d3a70b4 100644 --- a/src/core/fullscreen_ui.cpp +++ b/src/core/fullscreen_ui.cpp @@ -96,9 +96,9 @@ using ImGuiFullscreen::UIPrimaryLightColor; using ImGuiFullscreen::UIPrimaryLineColor; using ImGuiFullscreen::UIPrimaryTextColor; using ImGuiFullscreen::UISecondaryColor; -using ImGuiFullscreen::UISecondaryWeakColor; using ImGuiFullscreen::UISecondaryStrongColor; using ImGuiFullscreen::UISecondaryTextColor; +using ImGuiFullscreen::UISecondaryWeakColor; using ImGuiFullscreen::UITextHighlightColor; using ImGuiFullscreen::ActiveButton; @@ -1114,9 +1114,8 @@ void FullscreenUI::DoToggleAnalogMode() if (!cinfo) continue; - for (u32 j = 0; j < cinfo->num_bindings; j++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[j]; if (std::strcmp(bi.name, "Analog") == 0) { ctrl->SetBindState(bi.bind_index, 1.0f); @@ -3314,7 +3313,7 @@ void FullscreenUI::DrawControllerSettingsPage() }); } - if (!ci || ci->num_bindings == 0) + if (!ci || ci->bindings.empty() == 0) continue; if (MenuButton(FSUI_ICONSTR(ICON_FA_MAGIC, "Automatic Mapping"), @@ -3323,11 +3322,8 @@ void FullscreenUI::DrawControllerSettingsPage() StartAutomaticBinding(global_slot); } - for (u32 i = 0; i < ci->num_bindings; i++) - { - const Controller::ControllerBindingInfo& bi = ci->bindings[i]; + for (const Controller::ControllerBindingInfo& bi : ci->bindings) DrawInputBindingButton(bsi, bi.type, section.c_str(), bi.name, bi.display_name, true); - } if (mtap_enabled[mtap_port]) { @@ -3354,9 +3350,8 @@ void FullscreenUI::DrawControllerSettingsPage() { std::vector buttons_split(StringUtil::SplitString(binds_string, '&', true)); ImGuiFullscreen::ChoiceDialogOptions options; - for (u32 i = 0; i < ci->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : ci->bindings) { - const Controller::ControllerBindingInfo& bi = ci->bindings[i]; if (bi.type != InputBindingInfo::Type::Button && bi.type != InputBindingInfo::Type::Axis && bi.type != InputBindingInfo::Type::HalfAxis) { @@ -3372,9 +3367,8 @@ void FullscreenUI::DrawControllerSettingsPage() [game_settings, section, macro_index, ci](s32 index, const std::string& title, bool checked) { // convert display name back to bind name std::string_view to_modify; - for (u32 j = 0; j < ci->num_bindings; j++) + for (const Controller::ControllerBindingInfo& bi : ci->bindings) { - const Controller::ControllerBindingInfo& bi = ci->bindings[j]; if (bi.display_name == title) { to_modify = bi.name; @@ -3458,7 +3452,7 @@ void FullscreenUI::DrawControllerSettingsPage() ImGui::PopFont(); } - if (ci->num_settings > 0) + if (!ci->settings.empty()) { if (mtap_enabled[mtap_port]) { @@ -3471,9 +3465,8 @@ void FullscreenUI::DrawControllerSettingsPage() mtap_port + 1)); } - for (u32 i = 0; i < ci->num_settings; i++) + for (const SettingInfo& si : ci->settings) { - const SettingInfo& si = ci->settings[i]; TinyString title; title.Fmt(ICON_FA_COG "{}", si.display_name); switch (si.type) @@ -4715,8 +4708,7 @@ void FullscreenUI::DrawPauseMenu() subtitle_pos.x -= rp_height; subtitle_pos.y -= rp_height; - DrawShadowedText(dl, g_medium_font, rp_pos, text_color, rp.data(), rp.data() + rp.size(), - wrap_width); + DrawShadowedText(dl, g_medium_font, rp_pos, text_color, rp.data(), rp.data() + rp.size(), wrap_width); } } diff --git a/src/core/guncon.cpp b/src/core/guncon.cpp index 046ce3f68..28e88742a 100644 --- a/src/core/guncon.cpp +++ b/src/core/guncon.cpp @@ -304,9 +304,7 @@ const Controller::ControllerInfo GunCon::INFO = {ControllerType::GunCon, "GunCon", TRANSLATE_NOOP("ControllerType", "GunCon"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::NoVibration}; void GunCon::LoadSettings(SettingsInterface& si, const char* section) diff --git a/src/core/imgui_overlays.cpp b/src/core/imgui_overlays.cpp index 9484ac508..df9e740ef 100644 --- a/src/core/imgui_overlays.cpp +++ b/src/core/imgui_overlays.cpp @@ -627,9 +627,8 @@ void ImGuiManager::DrawInputsOverlay() text.Fmt("P{} |", port + 1u); - for (u32 bind = 0; bind < cinfo->num_bindings; bind++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[bind]; switch (bi.type) { case InputBindingInfo::Type::Axis: diff --git a/src/core/negcon.cpp b/src/core/negcon.cpp index a7b654a0c..ae88f5103 100644 --- a/src/core/negcon.cpp +++ b/src/core/negcon.cpp @@ -269,9 +269,7 @@ const Controller::ControllerInfo NeGcon::INFO = {ControllerType::NeGcon, "NeGcon", TRANSLATE_NOOP("ControllerType", "NeGcon"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::NoVibration}; void NeGcon::LoadSettings(SettingsInterface& si, const char* section) diff --git a/src/core/playstation_mouse.cpp b/src/core/playstation_mouse.cpp index fba57ffca..cc4b32f7f 100644 --- a/src/core/playstation_mouse.cpp +++ b/src/core/playstation_mouse.cpp @@ -219,7 +219,5 @@ const Controller::ControllerInfo PlayStationMouse::INFO = {ControllerType::PlayS "PlayStationMouse", TRANSLATE_NOOP("ControllerType", "PlayStation Mouse"), s_binding_info, - countof(s_binding_info), s_settings, - countof(s_settings), Controller::VibrationCapabilities::NoVibration}; \ No newline at end of file diff --git a/src/duckstation-qt/controllerbindingwidgets.cpp b/src/duckstation-qt/controllerbindingwidgets.cpp index b605dd3ef..365342483 100644 --- a/src/duckstation-qt/controllerbindingwidgets.cpp +++ b/src/duckstation-qt/controllerbindingwidgets.cpp @@ -98,8 +98,8 @@ void ControllerBindingWidget::populateWidgets() } const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_controller_type); - const bool has_settings = (cinfo && cinfo->num_settings > 0); - const bool has_macros = (cinfo && cinfo->num_bindings > 0); + const bool has_settings = (cinfo && !cinfo->settings.empty()); + const bool has_macros = (cinfo && !cinfo->bindings.empty()); m_ui.settings->setEnabled(has_settings); m_ui.macros->setEnabled(has_macros); @@ -360,20 +360,19 @@ ControllerMacroEditWidget::ControllerMacroEditWidget(ControllerMacroWidget* pare for (const std::string_view& button : buttons_split) { - for (u32 i = 0; i < cinfo->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - if (button == cinfo->bindings[i].name) + if (button == bi.name) { - m_binds.push_back(&cinfo->bindings[i]); + m_binds.push_back(&bi); break; } } } // populate list view - for (u32 i = 0; i < cinfo->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[i]; if (bi.type == InputBindingInfo::Type::Motor) continue; @@ -455,9 +454,9 @@ void ControllerMacroEditWidget::updateBinds() return; std::vector new_binds; - for (u32 i = 0, bind_index = 0; i < cinfo->num_bindings; i++) + u32 bind_index = 0; + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[i]; if (bi.type == InputBindingInfo::Type::Motor) continue; @@ -471,7 +470,7 @@ void ControllerMacroEditWidget::updateBinds() } if (item->checkState() == Qt::Checked) - new_binds.push_back(&cinfo->bindings[i]); + new_binds.push_back(&bi); } if (m_binds == new_binds) return; @@ -502,7 +501,7 @@ ControllerCustomSettingsWidget::ControllerCustomSettingsWidget(ControllerBinding : QWidget(parent), m_parent(parent) { const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(parent->getControllerType()); - if (!cinfo || cinfo->num_settings == 0) + if (!cinfo || cinfo->settings.empty()) return; QGroupBox* gbox = new QGroupBox(tr("%1 Settings").arg(qApp->translate("ControllerType", cinfo->display_name)), this); @@ -534,9 +533,8 @@ void ControllerCustomSettingsWidget::createSettingWidgets(ControllerBindingWidge SettingsInterface* sif = parent->getDialog()->getProfileSettingsInterface(); int current_row = 0; - for (u32 i = 0; i < cinfo->num_settings; i++) + for (const SettingInfo& si : cinfo->settings) { - const SettingInfo& si = cinfo->settings[i]; std::string key_name = si.name; switch (si.type) @@ -658,12 +656,11 @@ void ControllerCustomSettingsWidget::createSettingWidgets(ControllerBindingWidge void ControllerCustomSettingsWidget::restoreDefaults() { const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_parent->getControllerType()); - if (!cinfo || cinfo->num_settings == 0) + if (!cinfo || cinfo->settings.empty()) return; - for (u32 i = 0; i < cinfo->num_settings; i++) + for (const SettingInfo& si : cinfo->settings) { - const SettingInfo& si = cinfo->settings[i]; const QString key(QString::fromStdString(si.name)); switch (si.type) @@ -748,9 +745,8 @@ void ControllerBindingWidget_Base::initBindingWidgets() return; const std::string& config_section = getConfigSection(); - for (u32 i = 0; i < cinfo->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[i]; if (bi.type == InputBindingInfo::Type::Axis || bi.type == InputBindingInfo::Type::HalfAxis || bi.type == InputBindingInfo::Type::Button || bi.type == InputBindingInfo::Type::Pointer) { diff --git a/src/util/input_manager.cpp b/src/util/input_manager.cpp index 8df88ec63..b125e8ed7 100644 --- a/src/util/input_manager.cpp +++ b/src/util/input_manager.cpp @@ -698,9 +698,8 @@ void InputManager::AddHotkeyBindings(SettingsInterface& si) void InputManager::AddPadBindings(SettingsInterface& si, const std::string& section, u32 pad_index, const Controller::ControllerInfo* cinfo) { - for (u32 i = 0; i < cinfo->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - const Controller::ControllerBindingInfo& bi = cinfo->bindings[i]; const std::vector bindings(si.GetStringList(section.c_str(), bi.name)); switch (bi.type) @@ -1170,8 +1169,8 @@ void InputManager::ClearPortBindings(SettingsInterface& si, u32 port) if (!info) return; - for (u32 i = 0; i < info->num_bindings; i++) - si.DeleteValue(section.c_str(), info->bindings[i].name); + for (const Controller::ControllerBindingInfo& bi : info->bindings) + si.DeleteValue(section.c_str(), bi.name); } void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface& src_si, @@ -1201,11 +1200,8 @@ void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsI if (copy_pad_bindings) { - for (u32 i = 0; i < info->num_bindings; i++) - { - const Controller::ControllerBindingInfo& bi = info->bindings[i]; + for (const Controller::ControllerBindingInfo& bi : info->bindings) dest_si->CopyStringListValue(src_si, section.c_str(), bi.name); - } for (u32 i = 0; i < NUM_MACRO_BUTTONS_PER_CONTROLLER; i++) { @@ -1217,9 +1213,8 @@ void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsI if (copy_pad_config) { - for (u32 i = 0; i < info->num_settings; i++) + for (const SettingInfo& csi : info->settings) { - const SettingInfo& csi = info->settings[i]; switch (csi.type) { case SettingInfo::Type::Boolean: @@ -1289,9 +1284,8 @@ bool InputManager::MapController(SettingsInterface& si, u32 controller, return false; u32 num_mappings = 0; - for (u32 i = 0; i < info->num_bindings; i++) + for (const Controller::ControllerBindingInfo& bi : info->bindings) { - const Controller::ControllerBindingInfo& bi = info->bindings[i]; if (bi.generic_mapping == GenericInputBinding::Unknown) continue; @@ -1464,7 +1458,7 @@ void InputManager::LoadMacroButtonConfig(SettingsInterface& si, const std::strin const Controller::ControllerInfo* cinfo) { s_macro_buttons[pad] = {}; - if (cinfo->num_bindings == 0) + if (cinfo->bindings.empty()) return; for (u32 i = 0; i < NUM_MACRO_BUTTONS_PER_CONTROLLER; i++) @@ -1483,11 +1477,11 @@ void InputManager::LoadMacroButtonConfig(SettingsInterface& si, const std::strin for (const std::string_view& button : buttons_split) { const Controller::ControllerBindingInfo* binding = nullptr; - for (u32 j = 0; j < cinfo->num_bindings; j++) + for (const Controller::ControllerBindingInfo& bi : cinfo->bindings) { - if (button == cinfo->bindings[j].name) + if (button == bi.name) { - binding = &cinfo->bindings[j]; + binding = &bi; break; } }