From 9efdad6a2733e701b24e9edcbad1851692ca8863 Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Thu, 17 Nov 2022 19:23:48 -0500 Subject: [PATCH] Configuration: Add per-game input profiles --- src/common/settings_input.h | 1 + src/core/hid/emulated_controller.cpp | 3 +- src/yuzu/CMakeLists.txt | 3 + src/yuzu/configuration/config.cpp | 24 +- .../configure_input_per_game.cpp | 76 +++++ .../configuration/configure_input_per_game.h | 44 +++ .../configuration/configure_input_per_game.ui | 298 ++++++++++++++++++ .../configuration/configure_input_player.h | 2 +- src/yuzu/configuration/configure_per_game.cpp | 5 +- src/yuzu/configuration/configure_per_game.h | 6 + src/yuzu/main.cpp | 17 +- 11 files changed, 465 insertions(+), 14 deletions(-) create mode 100644 src/yuzu/configuration/configure_input_per_game.cpp create mode 100644 src/yuzu/configuration/configure_input_per_game.h create mode 100644 src/yuzu/configuration/configure_input_per_game.ui diff --git a/src/common/settings_input.h b/src/common/settings_input.h index 485e4ad223..46f38c7030 100644 --- a/src/common/settings_input.h +++ b/src/common/settings_input.h @@ -391,6 +391,7 @@ struct PlayerInput { u32 body_color_right; u32 button_color_left; u32 button_color_right; + std::string profile_name; }; struct TouchscreenInput { diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index ec13644528..a2d1062458 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -107,10 +107,9 @@ void EmulatedController::ReloadFromSettings() { original_npad_type = npad_type; } + Disconnect(); if (player.connected) { Connect(); - } else { - Disconnect(); } ReloadInput(); diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index 5cc1fbf326..8a252bd5d5 100644 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt @@ -85,6 +85,9 @@ add_executable(yuzu configuration/configure_input_advanced.cpp configuration/configure_input_advanced.h configuration/configure_input_advanced.ui + configuration/configure_input_per_game.cpp + configuration/configure_input_per_game.h + configuration/configure_input_per_game.ui configuration/configure_input_player.cpp configuration/configure_input_player.h configuration/configure_input_player.ui diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 343f3b8e50..4067ea6075 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -186,7 +186,7 @@ void Config::WriteGlobalSetting(const Settings::SwitchableSetting& void Config::ReadPlayerValue(std::size_t player_index) { const QString player_prefix = [this, player_index] { - if (type == ConfigType::InputProfile) { + if (type == ConfigType::InputProfile && global) { return QString{}; } else { return QStringLiteral("player_%1_").arg(player_index); @@ -244,6 +244,14 @@ void Config::ReadPlayerValue(std::size_t player_index) { ->value(QStringLiteral("%1button_color_right").arg(player_prefix), Settings::JOYCON_BUTTONS_NEON_RED) .toUInt(); + + // This only applies to per-game configs + if (!global) { + player.profile_name = + qt_config->value(QStringLiteral("%1profile_name").arg(player_prefix), QString{}) + .toString() + .toStdString(); + } } for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { @@ -386,6 +394,7 @@ void Config::ReadAudioValues() { } void Config::ReadControlValues() { + Settings::values.players.SetGlobal(global); qt_config->beginGroup(QStringLiteral("Controls")); for (std::size_t p = 0; p < Settings::values.players.GetValue().size(); ++p) { @@ -904,7 +913,6 @@ void Config::ReadMultiplayerValues() { void Config::ReadValues() { if (global) { - ReadControlValues(); ReadDataStorageValues(); ReadDebuggingValues(); ReadDisabledAddOnValues(); @@ -913,6 +921,7 @@ void Config::ReadValues() { ReadWebServiceValues(); ReadMiscellaneousValues(); } + ReadControlValues(); ReadCoreValues(); ReadCpuValues(); ReadRendererValues(); @@ -923,7 +932,7 @@ void Config::ReadValues() { void Config::SavePlayerValue(std::size_t player_index) { const QString player_prefix = [this, player_index] { - if (type == ConfigType::InputProfile) { + if (type == ConfigType::InputProfile && global) { return QString{}; } else { return QStringLiteral("player_%1_").arg(player_index); @@ -951,6 +960,12 @@ void Config::SavePlayerValue(std::size_t player_index) { player.button_color_left, Settings::JOYCON_BUTTONS_NEON_BLUE); WriteSetting(QStringLiteral("%1button_color_right").arg(player_prefix), player.button_color_right, Settings::JOYCON_BUTTONS_NEON_RED); + + // This only applies to per-game configs + if (!global) { + WriteSetting(QStringLiteral("%1profile_name").arg(player_prefix), + QString::fromStdString(player.profile_name), QString{}); + } } for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) { @@ -1054,7 +1069,6 @@ void Config::SaveIrCameraValues() { void Config::SaveValues() { if (global) { - SaveControlValues(); SaveDataStorageValues(); SaveDebuggingValues(); SaveDisabledAddOnValues(); @@ -1063,6 +1077,7 @@ void Config::SaveValues() { SaveWebServiceValues(); SaveMiscellaneousValues(); } + SaveControlValues(); SaveCoreValues(); SaveCpuValues(); SaveRendererValues(); @@ -1085,6 +1100,7 @@ void Config::SaveAudioValues() { } void Config::SaveControlValues() { + Settings::values.players.SetGlobal(global); qt_config->beginGroup(QStringLiteral("Controls")); for (std::size_t p = 0; p < Settings::values.players.GetValue().size(); ++p) { diff --git a/src/yuzu/configuration/configure_input_per_game.cpp b/src/yuzu/configuration/configure_input_per_game.cpp new file mode 100644 index 0000000000..5773c268de --- /dev/null +++ b/src/yuzu/configuration/configure_input_per_game.cpp @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "common/settings.h" +#include "core/core.h" +#include "core/hid/emulated_controller.h" +#include "core/hid/hid_core.h" +#include "ui_configure_input_per_game.h" +#include "yuzu/configuration/configure_input_per_game.h" +#include "yuzu/configuration/input_profiles.h" + +ConfigureInputPerGame::ConfigureInputPerGame(Core::System& system_, QWidget* parent) + : QWidget(parent), ui(std::make_unique()), + profiles(std::make_unique()), system{system_} { + ui->setupUi(this); + + Settings::values.players.SetGlobal(false); + const auto previous_profile = Settings::values.players.GetValue()[0].profile_name; + + const auto& profile_names = profiles->GetInputProfileNames(); + + ui->profile_player_1->addItem(QString::fromStdString("Use global configuration")); + for (size_t index = 0; index < profile_names.size(); ++index) { + const auto& profile_name = profile_names[index]; + ui->profile_player_1->addItem(QString::fromStdString(profile_name)); + if (profile_name == previous_profile) { + // offset by 1 since the first element is the global config + ui->profile_player_1->setCurrentIndex(static_cast(index + 1)); + } + } + LoadConfiguration(); +} + +void ConfigureInputPerGame::ApplyConfiguration() { + LoadConfiguration(); + + auto& hid_core = system.HIDCore(); + auto* emulated_controller = hid_core.GetEmulatedControllerByIndex(0); + + const auto selection_index = ui->profile_player_1->currentIndex(); + if (selection_index == 0) { + Settings::values.players.SetGlobal(true); + emulated_controller->ReloadFromSettings(); + return; + } else { + Settings::values.players.SetGlobal(false); + } + const QString profile_name = ui->profile_player_1->itemText(selection_index); + if (profile_name.isEmpty()) { + return; + } + profiles->SaveProfile(Settings::values.players.GetValue()[0].profile_name, 0); + emulated_controller->ReloadFromSettings(); +} + +void ConfigureInputPerGame::LoadConfiguration() { + auto& hid_core = system.HIDCore(); + auto* emulated_controller = hid_core.GetEmulatedControllerByIndex(0); + + Settings::values.players.SetGlobal(false); + + const auto selection_index = ui->profile_player_1->currentIndex(); + if (selection_index == 0) { + Settings::values.players.GetValue()[0].profile_name = ""; + Settings::values.players.SetGlobal(true); + emulated_controller->ReloadFromSettings(); + return; + } + const QString profile_name = ui->profile_player_1->itemText(selection_index); + if (profile_name.isEmpty()) { + return; + } + profiles->LoadProfile(profile_name.toStdString(), 0); + Settings::values.players.GetValue()[0].profile_name = profile_name.toStdString(); + emulated_controller->ReloadFromSettings(); +} diff --git a/src/yuzu/configuration/configure_input_per_game.h b/src/yuzu/configuration/configure_input_per_game.h new file mode 100644 index 0000000000..6feb608b7a --- /dev/null +++ b/src/yuzu/configuration/configure_input_per_game.h @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include + +namespace Core { +class System; +} + +namespace InputCommon { +class InputSubsystem; +} + +namespace Ui { +class ConfigureInputPerGame; +} + +class InputProfiles; + +class ConfigureInputPerGame : public QWidget { + Q_OBJECT + +public: + explicit ConfigureInputPerGame(Core::System& system_, QWidget* parent = nullptr); + + /// Initializes the input dialog with the given input subsystem. + // void Initialize(InputCommon::InputSubsystem* input_subsystem_, std::size_t max_players = 8); + + /// Save configurations to settings file. + void ApplyConfiguration(); + +private: + /// Load configuration from settings file. + void LoadConfiguration(); + + std::unique_ptr ui; + std::unique_ptr profiles; + + Core::System& system; +}; diff --git a/src/yuzu/configuration/configure_input_per_game.ui b/src/yuzu/configuration/configure_input_per_game.ui new file mode 100644 index 0000000000..8a384c0df7 --- /dev/null +++ b/src/yuzu/configuration/configure_input_per_game.ui @@ -0,0 +1,298 @@ + + + ConfigureInputPerGame + + + + 0 + 0 + 541 + 759 + + + + Form + + + Graphics + + + + + + 0 + + + + + Input Profiles + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 1 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 3 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 4 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 5 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 6 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 7 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Player 8 Profile + + + + + + + + 0 + 0 + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + diff --git a/src/yuzu/configuration/configure_input_player.h b/src/yuzu/configuration/configure_input_player.h index 79434fdd8d..26f60d121c 100644 --- a/src/yuzu/configuration/configure_input_player.h +++ b/src/yuzu/configuration/configure_input_player.h @@ -38,7 +38,7 @@ enum class InputType; namespace Ui { class ConfigureInputPlayer; -} +} // namespace Ui namespace Core::HID { class HIDCore; diff --git a/src/yuzu/configuration/configure_per_game.cpp b/src/yuzu/configuration/configure_per_game.cpp index c3cb8f61d1..cf0a6dc0e9 100644 --- a/src/yuzu/configuration/configure_per_game.cpp +++ b/src/yuzu/configuration/configure_per_game.cpp @@ -28,7 +28,7 @@ #include "yuzu/configuration/configure_general.h" #include "yuzu/configuration/configure_graphics.h" #include "yuzu/configuration/configure_graphics_advanced.h" -#include "yuzu/configuration/configure_input.h" +#include "yuzu/configuration/configure_input_per_game.h" #include "yuzu/configuration/configure_per_game.h" #include "yuzu/configuration/configure_per_game_addons.h" #include "yuzu/configuration/configure_system.h" @@ -50,6 +50,7 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const std::st general_tab = std::make_unique(system_, this); graphics_tab = std::make_unique(system_, this); graphics_advanced_tab = std::make_unique(system_, this); + input_tab = std::make_unique(system_, this); system_tab = std::make_unique(system_, this); ui->setupUi(this); @@ -61,6 +62,7 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const std::st ui->tabWidget->addTab(graphics_tab.get(), tr("Graphics")); ui->tabWidget->addTab(graphics_advanced_tab.get(), tr("Adv. Graphics")); ui->tabWidget->addTab(audio_tab.get(), tr("Audio")); + ui->tabWidget->addTab(input_tab.get(), tr("Input Profiles")); setFocusPolicy(Qt::ClickFocus); setWindowTitle(tr("Properties")); @@ -91,6 +93,7 @@ void ConfigurePerGame::ApplyConfiguration() { graphics_tab->ApplyConfiguration(); graphics_advanced_tab->ApplyConfiguration(); audio_tab->ApplyConfiguration(); + input_tab->ApplyConfiguration(); system.ApplySettings(); Settings::LogSettings(); diff --git a/src/yuzu/configuration/configure_per_game.h b/src/yuzu/configuration/configure_per_game.h index 17a98a0f3d..4ecc435417 100644 --- a/src/yuzu/configuration/configure_per_game.h +++ b/src/yuzu/configuration/configure_per_game.h @@ -16,12 +16,17 @@ namespace Core { class System; } +namespace InputCommon { +class InputSubsystem; +} + class ConfigurePerGameAddons; class ConfigureAudio; class ConfigureCpu; class ConfigureGeneral; class ConfigureGraphics; class ConfigureGraphicsAdvanced; +class ConfigureInputPerGame; class ConfigureSystem; class QGraphicsScene; @@ -72,5 +77,6 @@ private: std::unique_ptr general_tab; std::unique_ptr graphics_tab; std::unique_ptr graphics_advanced_tab; + std::unique_ptr input_tab; std::unique_ptr system_tab; }; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 72498f52a7..e0c3537887 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -124,6 +124,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual #include "yuzu/compatibility_list.h" #include "yuzu/configuration/config.h" #include "yuzu/configuration/configure_dialog.h" +#include "yuzu/configuration/configure_input_per_game.h" #include "yuzu/debugger/console.h" #include "yuzu/debugger/controller.h" #include "yuzu/debugger/profiler.h" @@ -1647,6 +1648,11 @@ void GMainWindow::BootGame(const QString& filename, u64 program_id, std::size_t LOG_INFO(Frontend, "yuzu starting..."); StoreRecentFile(filename); // Put the filename on top of the list + // Save configurations + UpdateUISettings(); + game_list->SaveInterfaceLayout(); + config->Save(); + u64 title_id{0}; last_filename_booted = filename; @@ -1666,11 +1672,6 @@ void GMainWindow::BootGame(const QString& filename, u64 program_id, std::size_t system->ApplySettings(); } - // Save configurations - UpdateUISettings(); - game_list->SaveInterfaceLayout(); - config->Save(); - Settings::LogSettings(); if (UISettings::values.select_user_on_boot) { @@ -2790,6 +2791,7 @@ void GMainWindow::OnStopGame() { ShutdownGame(); Settings::RestoreGlobalState(system->IsPoweredOn()); + system->HIDCore().ReloadInputDevices(); UpdateStatusButtons(); } @@ -3250,6 +3252,7 @@ void GMainWindow::OpenPerGameConfiguration(u64 title_id, const std::string& file // Do not cause the global config to write local settings into the config file const bool is_powered_on = system->IsPoweredOn(); Settings::RestoreGlobalState(is_powered_on); + system->HIDCore().ReloadInputDevices(); UISettings::values.configuration_applied = false; @@ -3709,6 +3712,7 @@ void GMainWindow::OnCoreError(Core::SystemResultStatus result, std::string detai ShutdownGame(); Settings::RestoreGlobalState(system->IsPoweredOn()); + system->HIDCore().ReloadInputDevices(); UpdateStatusButtons(); } } else { @@ -3860,18 +3864,19 @@ void GMainWindow::closeEvent(QCloseEvent* event) { // Unload controllers early controller_dialog->UnloadController(); game_list->UnloadController(); - system->HIDCore().UnloadInputDevices(); // Shutdown session if the emu thread is active... if (emu_thread != nullptr) { ShutdownGame(); Settings::RestoreGlobalState(system->IsPoweredOn()); + system->HIDCore().ReloadInputDevices(); UpdateStatusButtons(); } render_window->close(); multiplayer_state->Close(); + system->HIDCore().UnloadInputDevices(); system->GetRoomNetwork().Shutdown(); QWidget::closeEvent(event);