From 9baa09aaced83480168eadbf9c84baf3c4c9c63f Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 22 Sep 2025 15:26:47 +1000 Subject: [PATCH] InputManager: Use TinyString for key-to-string function --- src/core/fullscreen_ui.cpp | 8 +-- src/duckstation-qt/inputbindingdialog.cpp | 8 +-- src/duckstation-qt/inputbindingwidgets.cpp | 6 +- src/duckstation-qt/qthost.cpp | 6 +- src/util/input_manager.cpp | 65 +++++++++++----------- src/util/input_manager.h | 8 +-- 6 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/core/fullscreen_ui.cpp b/src/core/fullscreen_ui.cpp index 7ec22b3b3..71fe23dbc 100644 --- a/src/core/fullscreen_ui.cpp +++ b/src/core/fullscreen_ui.cpp @@ -2587,8 +2587,8 @@ void FullscreenUI::InputBindingDialog::Start(SettingsInterface* bsi, InputBindin other_key.modifier = InputModifier::FullAxis; SettingsInterface* bsi = GetEditingSettingsInterface(game_settings); - const std::string new_binding(InputManager::ConvertInputBindingKeysToString( - m_binding_type, m_new_bindings.data(), m_new_bindings.size())); + const SmallString new_binding = + InputManager::ConvertInputBindingKeysToString(m_binding_type, m_new_bindings.data(), m_new_bindings.size()); bsi->SetStringValue(m_binding_section.c_str(), m_binding_key.c_str(), new_binding.c_str()); SetSettingsChanged(bsi); @@ -2684,10 +2684,10 @@ void FullscreenUI::BeginVibrationMotorBinding(SettingsInterface* bsi, InputBindi options.reserve(motors.size() + 1); for (size_t i = 0; i < motors.size(); i++) { - std::string text = InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motors[i]); + const TinyString text = InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motors[i]); const bool this_index = (current_binding.view() == text); current_index = this_index ? i : current_index; - options.emplace_back(std::move(text), this_index); + options.emplace_back(text, this_index); } // empty/no mapping value diff --git a/src/duckstation-qt/inputbindingdialog.cpp b/src/duckstation-qt/inputbindingdialog.cpp index 56302afd0..7289e4e46 100644 --- a/src/duckstation-qt/inputbindingdialog.cpp +++ b/src/duckstation-qt/inputbindingdialog.cpp @@ -221,15 +221,15 @@ void InputBindingDialog::addNewBinding() if (m_new_bindings.empty()) return; - std::string new_binding = + SmallString new_binding = InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size()); if (!new_binding.empty()) { - if (std::find(m_bindings.begin(), m_bindings.end(), new_binding) != m_bindings.end()) + if (std::find(m_bindings.begin(), m_bindings.end(), new_binding.view()) != m_bindings.end()) return; - m_ui.bindingList->addItem(QString::fromStdString(new_binding)); - m_bindings.push_back(std::move(new_binding)); + m_ui.bindingList->addItem(QtUtils::StringViewToQString(new_binding)); + m_bindings.emplace_back(new_binding); saveListToSettings(); } } diff --git a/src/duckstation-qt/inputbindingwidgets.cpp b/src/duckstation-qt/inputbindingwidgets.cpp index c17a123e7..eac47a3c4 100644 --- a/src/duckstation-qt/inputbindingwidgets.cpp +++ b/src/duckstation-qt/inputbindingwidgets.cpp @@ -218,8 +218,8 @@ void InputBindingWidget::setNewBinding() if (m_new_bindings.empty()) return; - std::string new_binding( - InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size())); + const SmallString new_binding = + InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size()); if (!new_binding.empty()) { if (m_sif) @@ -239,7 +239,7 @@ void InputBindingWidget::setNewBinding() } m_bindings.clear(); - m_bindings.push_back(std::move(new_binding)); + m_bindings.emplace_back(new_binding); } void InputBindingWidget::clearBinding() diff --git a/src/duckstation-qt/qthost.cpp b/src/duckstation-qt/qthost.cpp index 84d6ebd34..f47951375 100644 --- a/src/duckstation-qt/qthost.cpp +++ b/src/duckstation-qt/qthost.cpp @@ -2607,7 +2607,7 @@ void InputDeviceListModel::enumerateDevices() for (const auto& key : motors) { new_motors.push_back( - QString::fromStdString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, key))); + QtUtils::StringViewToQString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, key))); } QMetaObject::invokeMethod(this, &InputDeviceListModel::resetLists, Qt::QueuedConnection, new_devices, new_motors); @@ -2676,8 +2676,8 @@ void Host::OnInputDeviceConnected(InputBindingKey key, std::string_view identifi vibration_motor_list.reserve(im_vibration_motor_list.size()); for (const InputBindingKey& motor_key : im_vibration_motor_list) { - vibration_motor_list.push_back( - QString::fromStdString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motor_key))); + vibration_motor_list.push_back(QtUtils::StringViewToQString( + InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motor_key))); } } diff --git a/src/util/input_manager.cpp b/src/util/input_manager.cpp index 6955a850b..3417897e9 100644 --- a/src/util/input_manager.cpp +++ b/src/util/input_manager.cpp @@ -362,84 +362,88 @@ bool InputManager::ParseBindingAndGetSource(std::string_view binding, InputBindi return false; } -std::string InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key) +TinyString InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key) { + TinyString ret; + if (binding_type == InputBindingInfo::Type::Pointer || binding_type == InputBindingInfo::Type::RelativePointer || binding_type == InputBindingInfo::Type::Device) { // pointer and device bindings don't have a data part if (key.source_type == InputSourceType::Pointer) { - return GetPointerDeviceName(key.source_index); + ret = GetPointerDeviceName(key.source_index); } else if (key.source_type < InputSourceType::Count && s_state.input_sources[static_cast(key.source_type)]) { // This assumes that it always follows the Type/Binding form. - std::string keystr(s_state.input_sources[static_cast(key.source_type)]->ConvertKeyToString(key)); - std::string::size_type pos = keystr.find('/'); - if (pos != std::string::npos) - keystr.erase(pos); - return keystr; + ret = s_state.input_sources[static_cast(key.source_type)]->ConvertKeyToString(key); + + if (const s32 pos = ret.find('/'); pos > 0) + ret.erase(pos); } } else { if (key.source_type == InputSourceType::Keyboard) { - const std::optional str(ConvertHostKeyboardCodeToString(key.data)); - if (str.has_value() && !str->empty()) - return fmt::format("Keyboard/{}", str->c_str()); + if (const char* key_code_string = ConvertHostKeyboardCodeToString(key.data)) + ret.format("Keyboard/{}", key_code_string); } else if (key.source_type == InputSourceType::Pointer) { if (key.source_subtype == InputSubclass::PointerButton) { if (key.data < s_pointer_button_names.size()) - return fmt::format("Pointer-{}/{}", u32{key.source_index}, s_pointer_button_names[key.data]); + ret.format("Pointer-{}/{}", u32{key.source_index}, s_pointer_button_names[key.data]); else - return fmt::format("Pointer-{}/Button{}", u32{key.source_index}, key.data); + ret.format("Pointer-{}/Button{}", u32{key.source_index}, key.data); } else if (key.source_subtype == InputSubclass::PointerAxis) { - return fmt::format("Pointer-{}/{}{:c}", u32{key.source_index}, s_pointer_axis_names[key.data], - key.modifier == InputModifier::Negate ? '-' : '+'); + ret.format("Pointer-{}/{}{:c}", u32{key.source_index}, s_pointer_axis_names[key.data], + key.modifier == InputModifier::Negate ? '-' : '+'); } } else if (key.source_type < InputSourceType::Count && s_state.input_sources[static_cast(key.source_type)]) { - return std::string(s_state.input_sources[static_cast(key.source_type)]->ConvertKeyToString(key)); + ret = s_state.input_sources[static_cast(key.source_type)]->ConvertKeyToString(key); } } - return {}; + return ret; } -std::string InputManager::ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, +SmallString InputManager::ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys, size_t num_keys) { + SmallString ret; + // can't have a chord of devices/pointers if (binding_type == InputBindingInfo::Type::Pointer || binding_type == InputBindingInfo::Type::RelativePointer || binding_type == InputBindingInfo::Type::Device) { // so only take the first if (num_keys > 0) - return ConvertInputBindingKeyToString(binding_type, keys[0]); + { + ret = ConvertInputBindingKeyToString(binding_type, keys[0]); + return ret; + } } - std::stringstream ss; for (size_t i = 0; i < num_keys; i++) { - const std::string keystr(ConvertInputBindingKeyToString(binding_type, keys[i])); + const TinyString keystr = ConvertInputBindingKeyToString(binding_type, keys[i]); if (keystr.empty()) - return std::string(); + return ret; if (i > 0) - ss << " & "; + ret.append(" & "); - ss << keystr; + ret.append(keystr); } - return std::move(ss).str(); + return ret; } bool InputManager::PrettifyInputBinding(SmallStringBase& binding, BindingIconMappingFunction mapper /*= nullptr*/) @@ -683,15 +687,10 @@ std::optional InputManager::ConvertHostKeyboardStringToCode(std::string_vie return std::nullopt; } -std::optional InputManager::ConvertHostKeyboardCodeToString(u32 code) +const char* InputManager::ConvertHostKeyboardCodeToString(u32 code) { - std::optional ret; - const KeyCodeData* key_data = FindKeyCodeData(code); - if (key_data) - ret.emplace(key_data->name); - - return ret; + return key_data ? key_data->name : nullptr; } const InputManager::KeyCodeData* InputManager::FindKeyCodeData(u32 usb_code) @@ -874,9 +873,9 @@ std::optional InputManager::GetIndexFromPointerBinding(std::string_view sou return static_cast(pointer_index.value()); } -std::string InputManager::GetPointerDeviceName(u32 pointer_index) +TinyString InputManager::GetPointerDeviceName(u32 pointer_index) { - return fmt::format("Pointer-{}", pointer_index); + return TinyString::from_format("Pointer-{}", pointer_index); } std::optional InputManager::ParseSensorKey(std::string_view source, std::string_view sub_binding) diff --git a/src/util/input_manager.h b/src/util/input_manager.h index 365786e1f..31fc0577f 100644 --- a/src/util/input_manager.h +++ b/src/util/input_manager.h @@ -226,13 +226,13 @@ std::optional ParseInputSourceString(std::string_view str); std::optional GetIndexFromPointerBinding(std::string_view str); /// Returns the device name for a pointer index (e.g. Pointer-0). -std::string GetPointerDeviceName(u32 pointer_index); +TinyString GetPointerDeviceName(u32 pointer_index); /// Converts a key code from a human-readable string to an identifier. std::optional ConvertHostKeyboardStringToCode(std::string_view str); /// Converts a key code from an identifier to a human-readable string. -std::optional ConvertHostKeyboardCodeToString(u32 code); +const char* ConvertHostKeyboardCodeToString(u32 code); /// Converts a key code from an identifier to an icon which can be drawn. const char* ConvertHostKeyboardCodeToIcon(u32 code); @@ -257,10 +257,10 @@ InputBindingKey MakeSensorAxisKey(InputSubclass sensor, u32 axis); std::optional ParseInputBindingKey(std::string_view binding); /// Converts a input key to a string. -std::string ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key); +TinyString ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key); /// Converts a chord of binding keys to a string. -std::string ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys, +SmallString ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys, size_t num_keys); /// Represents a binding with icon fonts, if available.