| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/InputProfile.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <iterator> |
| 8 | |
| 9 | #include <fmt/format.h> |
| 10 | |
| 11 | #include "Common/FileSearch.h" |
| 12 | #include "Common/FileUtil.h" |
| 13 | #include "Common/StringUtil.h" |
| 14 | |
| 15 | #include "Core/ConfigManager.h" |
| 16 | #include "Core/Core.h" |
| 17 | #include "Core/HW/Wiimote.h" |
| 18 | |
| 19 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 20 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 21 | #include "InputCommon/InputConfig.h" |
| 22 | |
| 23 | namespace InputProfile |
| 24 | { |
| 25 | namespace |
| 26 | { |
| 27 | constexpr int display_message_ms = 3000; |
| 28 | } |
| 29 | |
| 30 | std::vector<std::string> GetProfilesFromSetting(const std::string& setting, const std::string& root) |
| 31 | { |
| 32 | const auto& setting_choices = SplitString(setting, ','); |
| 33 | |
| 34 | std::vector<std::string> result; |
| 35 | for (const std::string& setting_choice : setting_choices) |
| 36 | { |
| 37 | const std::string path = root + std::string(StripWhitespace(setting_choice)); |
| 38 | if (File::IsDirectory(path)) |
| 39 | { |
| 40 | const auto files_under_directory = Common::DoFileSearch(path, ".ini", true); |
| 41 | result.insert(result.end(), files_under_directory.begin(), files_under_directory.end()); |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | const std::string file_path = path + ".ini"; |
| 46 | if (File::Exists(file_path)) |
| 47 | { |
| 48 | result.push_back(file_path); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | std::vector<std::string> ProfileCycler::GetProfilesForDevice(InputConfig* device_configuration) |
| 57 | { |
| 58 | const std::string device_profile_root_location( |
| 59 | device_configuration->GetUserProfileDirectoryPath()); |
| 60 | return Common::DoFileSearch(device_profile_root_location, ".ini", true); |
| 61 | } |
| 62 | |
| 63 | std::string ProfileCycler::GetProfile(CycleDirection cycle_direction, int& profile_index, |
| 64 | const std::vector<std::string>& profiles) |
| 65 | { |
| 66 | // update the index and bind it to the number of available strings |
| 67 | auto positive_modulo = [](int& i, int n) { i = (i % n + n) % n; }; |
| 68 | profile_index += static_cast<int>(cycle_direction); |
| 69 | positive_modulo(profile_index, static_cast<int>(profiles.size())); |
| 70 | |
| 71 | return profiles[profile_index]; |
| 72 | } |
| 73 | |
| 74 | void ProfileCycler::UpdateToProfile(const std::string& profile_filename, |
| 75 | ControllerEmu::EmulatedController* controller, |
| 76 | InputConfig* device_configuration) |
| 77 | { |
| 78 | std::string base; |
| 79 | SplitPath(profile_filename, nullptr, &base, nullptr); |
| 80 | |
| 81 | Common::IniFile ini_file; |
| 82 | if (ini_file.Load(profile_filename)) |
| 83 | { |
| 84 | Core::DisplayMessage("Loading input profile '" + base + "' for device '" + |
| 85 | controller->GetName() + "'", |
| 86 | display_message_ms); |
| 87 | controller->LoadConfig(ini_file.GetOrCreateSection("Profile")); |
| 88 | controller->UpdateReferences(g_controller_interface); |
| 89 | device_configuration->GenerateControllerTextures(ini_file); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | Core::DisplayMessage("Unable to load input profile '" + base + "' for device '" + |
| 94 | controller->GetName() + "'", |
| 95 | display_message_ms); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> |
| 100 | ProfileCycler::GetMatchingProfilesFromSetting(const std::string& setting, |
| 101 | const std::vector<std::string>& profiles, |
| 102 | InputConfig* device_configuration) |
| 103 | { |
| 104 | const std::string device_profile_root_location( |
| 105 | device_configuration->GetUserProfileDirectoryPath()); |
| 106 | |
| 107 | const auto& profiles_from_setting = GetProfilesFromSetting(setting, device_profile_root_location); |
| 108 | if (profiles_from_setting.empty()) |
| 109 | { |
| 110 | return {}; |
| 111 | } |
| 112 | |
| 113 | std::vector<std::string> result; |
| 114 | std::ranges::set_intersection(profiles, profiles_from_setting, std::back_inserter(result)); |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | void ProfileCycler::CycleProfile(CycleDirection cycle_direction, InputConfig* device_configuration, |
| 119 | int& profile_index, int controller_index) |
| 120 | { |
| 121 | const auto& profiles = GetProfilesForDevice(device_configuration); |
| 122 | if (profiles.empty()) |
| 123 | { |
| 124 | Core::DisplayMessage("No input profiles found", display_message_ms); |
| 125 | return; |
| 126 | } |
| 127 | const std::string profile = GetProfile(cycle_direction, profile_index, profiles); |
| 128 | |
| 129 | auto* controller = device_configuration->GetController(controller_index); |
| 130 | if (controller) |
| 131 | { |
| 132 | UpdateToProfile(profile, controller, device_configuration); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index), |
| 137 | display_message_ms); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void ProfileCycler::CycleProfileForGame(CycleDirection cycle_direction, |
| 142 | InputConfig* device_configuration, int& profile_index, |
| 143 | const std::string& setting, int controller_index) |
| 144 | { |
| 145 | const auto& profiles = GetProfilesForDevice(device_configuration); |
| 146 | if (profiles.empty()) |
| 147 | { |
| 148 | Core::DisplayMessage("No input profiles found", display_message_ms); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (setting.empty()) |
| 153 | { |
| 154 | Core::DisplayMessage("No setting found for game", display_message_ms); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | const auto& profiles_for_game = |
| 159 | GetMatchingProfilesFromSetting(setting, profiles, device_configuration); |
| 160 | if (profiles_for_game.empty()) |
| 161 | { |
| 162 | Core::DisplayMessage("No input profiles found for game", display_message_ms); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | const std::string profile = GetProfile(cycle_direction, profile_index, profiles_for_game); |
| 167 | |
| 168 | auto* controller = device_configuration->GetController(controller_index); |
| 169 | if (controller) |
| 170 | { |
| 171 | UpdateToProfile(profile, controller, device_configuration); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index), |
| 176 | display_message_ms); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | std::string ProfileCycler::GetWiimoteInputProfilesForGame(int controller_index) |
| 181 | { |
| 182 | Common::IniFile game_ini = SConfig::GetInstance().LoadGameIni(); |
| 183 | const auto* const control_section = game_ini.GetOrCreateSection("Controls"); |
| 184 | |
| 185 | std::string result; |
| 186 | control_section->Get(fmt::format("WiimoteProfile{}", controller_index + 1), &result); |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | void ProfileCycler::NextWiimoteProfile(int controller_index) |
| 191 | { |
| 192 | CycleProfile(CycleDirection::Forward, Wiimote::GetConfig(), m_wiimote_profile_index, |
| 193 | controller_index); |
| 194 | } |
| 195 | |
| 196 | void ProfileCycler::PreviousWiimoteProfile(int controller_index) |
| 197 | { |
| 198 | CycleProfile(CycleDirection::Backward, Wiimote::GetConfig(), m_wiimote_profile_index, |
| 199 | controller_index); |
| 200 | } |
| 201 | |
| 202 | void ProfileCycler::NextWiimoteProfileForGame(int controller_index) |
| 203 | { |
| 204 | CycleProfileForGame(CycleDirection::Forward, Wiimote::GetConfig(), m_wiimote_profile_index, |
| 205 | GetWiimoteInputProfilesForGame(controller_index), controller_index); |
| 206 | } |
| 207 | |
| 208 | void ProfileCycler::PreviousWiimoteProfileForGame(int controller_index) |
| 209 | { |
| 210 | CycleProfileForGame(CycleDirection::Backward, Wiimote::GetConfig(), m_wiimote_profile_index, |
| 211 | GetWiimoteInputProfilesForGame(controller_index), controller_index); |
| 212 | } |
| 213 | } // namespace InputProfile |