| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/InputConfig.h" |
| 5 | |
| 6 | #include <utility> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "Common/FileUtil.h" |
| 10 | #include "Common/IniFile.h" |
| 11 | #include "Common/MsgHandler.h" |
| 12 | #include "Common/StringUtil.h" |
| 13 | #include "Core/ConfigManager.h" |
| 14 | #include "Core/Core.h" |
| 15 | #include "Core/HW/Wiimote.h" |
| 16 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 17 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 18 | #include "InputCommon/InputProfile.h" |
| 19 | |
| 20 | InputConfig::InputConfig(std::string ini_name, std::string gui_name, |
| 21 | std::string profile_directory_name, std::string profile_key) |
| 22 | : m_ini_name(std::move(ini_name)), m_gui_name(std::move(gui_name)), |
| 23 | m_profile_directory_name(std::move(profile_directory_name)), |
| 24 | m_profile_key(std::move(profile_key)) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | InputConfig::~InputConfig() = default; |
| 29 | |
| 30 | bool InputConfig::LoadConfig() |
| 31 | { |
| 32 | Common::IniFile inifile; |
| 33 | bool useProfile[MAX_BBMOTES] = {false, false, false, false, false}; |
| 34 | static constexpr std::array<std::string_view, MAX_BBMOTES> num = {"1", "2", "3", "4", "BB"}; |
| 35 | std::string profile[MAX_BBMOTES]; |
| 36 | |
| 37 | if (SConfig::GetInstance().GetGameID() != "00000000") |
| 38 | { |
| 39 | const std::string profile_directory = GetUserProfileDirectoryPath(); |
| 40 | |
| 41 | Common::IniFile game_ini = SConfig::GetInstance().LoadGameIni(); |
| 42 | auto* control_section = game_ini.GetOrCreateSection("Controls"); |
| 43 | |
| 44 | for (int i = 0; i < 4; i++) |
| 45 | { |
| 46 | const auto profile_name = fmt::format("{}Profile{}", GetProfileKey(), num[i]); |
| 47 | |
| 48 | if (control_section->Exists(profile_name)) |
| 49 | { |
| 50 | std::string profile_setting; |
| 51 | if (control_section->Get(profile_name, &profile_setting)) |
| 52 | { |
| 53 | auto profiles = InputProfile::GetProfilesFromSetting(profile_setting, profile_directory); |
| 54 | |
| 55 | if (profiles.empty()) |
| 56 | { |
| 57 | // TODO: PanicAlert shouldn't be used for this. |
| 58 | PanicAlertFmtT("No profiles found for game setting '{0}'", profile_setting); |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | // Use the first profile by default |
| 63 | profile[i] = profiles[0]; |
| 64 | useProfile[i] = true; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini") && |
| 71 | !inifile.GetSections().empty()) |
| 72 | { |
| 73 | int n = 0; |
| 74 | |
| 75 | for (auto& controller : m_controllers) |
| 76 | { |
| 77 | Common::IniFile::Section config; |
| 78 | // Load settings from ini |
| 79 | if (useProfile[n]) |
| 80 | { |
| 81 | std::string base; |
| 82 | SplitPath(profile[n], nullptr, &base, nullptr); |
| 83 | Core::DisplayMessage("Loading game specific input profile '" + base + "' for device '" + |
| 84 | controller->GetName() + "'", |
| 85 | 6000); |
| 86 | |
| 87 | inifile.Load(profile[n]); |
| 88 | config = *inifile.GetOrCreateSection("Profile"); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | config = *inifile.GetOrCreateSection(controller->GetName()); |
| 93 | } |
| 94 | controller->LoadConfig(&config); |
| 95 | controller->UpdateReferences(g_controller_interface); |
| 96 | |
| 97 | // Next profile |
| 98 | n++; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | // Only load the default profile for the first controller and clear the others, |
| 105 | // otherwise they would all share the same mappings on the same (default) device |
| 106 | if (m_controllers.size() > 0) |
| 107 | { |
| 108 | m_controllers[0]->LoadDefaults(g_controller_interface); |
| 109 | m_controllers[0]->UpdateReferences(g_controller_interface); |
| 110 | } |
| 111 | for (size_t i = 1; i < m_controllers.size(); ++i) |
| 112 | { |
| 113 | // Calling the base version just clears all settings without overwriting them with a default |
| 114 | m_controllers[i]->EmulatedController::LoadDefaults(g_controller_interface); |
| 115 | m_controllers[i]->UpdateReferences(g_controller_interface); |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void InputConfig::SaveConfig() |
| 122 | { |
| 123 | std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini"; |
| 124 | |
| 125 | Common::IniFile inifile; |
| 126 | inifile.Load(ini_filename); |
| 127 | |
| 128 | for (auto& controller : m_controllers) |
| 129 | { |
| 130 | controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName())); |
| 131 | } |
| 132 | |
| 133 | inifile.Save(ini_filename); |
| 134 | } |
| 135 | |
| 136 | ControllerEmu::EmulatedController* InputConfig::GetController(int index) const |
| 137 | { |
| 138 | return m_controllers.at(index).get(); |
| 139 | } |
| 140 | |
| 141 | void InputConfig::ClearControllers() |
| 142 | { |
| 143 | m_controllers.clear(); |
| 144 | } |
| 145 | |
| 146 | bool InputConfig::ControllersNeedToBeCreated() const |
| 147 | { |
| 148 | return m_controllers.empty(); |
| 149 | } |
| 150 | |
| 151 | std::string InputConfig::GetUserProfileDirectoryPath() const |
| 152 | { |
| 153 | return fmt::format("{}Profiles/{}/", File::GetUserPath(D_CONFIG_IDX), GetProfileDirectoryName()); |
| 154 | } |
| 155 | |
| 156 | std::string InputConfig::GetSysProfileDirectoryPath() const |
| 157 | { |
| 158 | return fmt::format("{}Profiles/{}/", File::GetSysDirectory(), GetProfileDirectoryName()); |
| 159 | } |
| 160 | |
| 161 | int InputConfig::GetControllerCount() const |
| 162 | { |
| 163 | return static_cast<int>(m_controllers.size()); |
| 164 | } |
| 165 | |
| 166 | void InputConfig::RegisterHotplugCallback() |
| 167 | { |
| 168 | // Update control references on all controllers |
| 169 | // as configured devices may have been added or removed. |
| 170 | m_hotplug_event_hook = g_controller_interface.RegisterDevicesChangedCallback([this] { |
| 171 | for (auto& controller : m_controllers) |
| 172 | controller->UpdateReferences(g_controller_interface); |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | void InputConfig::UnregisterHotplugCallback() |
| 177 | { |
| 178 | m_hotplug_event_hook.reset(); |
| 179 | } |
| 180 | |
| 181 | bool InputConfig::IsControllerControlledByGamepadDevice(int index) const |
| 182 | { |
| 183 | if (static_cast<size_t>(index) >= m_controllers.size()) |
| 184 | return false; |
| 185 | |
| 186 | const auto& controller = m_controllers.at(index).get()->GetDefaultDevice(); |
| 187 | |
| 188 | // By default on Android, no device is selected |
| 189 | if (controller.source == "") |
| 190 | return false; |
| 191 | |
| 192 | // Filter out anything which obviously not a gamepad |
| 193 | return !((controller.source == "Quartz") // OSX Quartz Keyboard/Mouse |
| 194 | || (controller.source == "XInput2") // Linux and BSD Keyboard/Mouse |
| 195 | || (controller.source == "Android" && controller.cid <= 0) // Android non-gamepad device |
| 196 | || (controller.source == "DInput" && |
| 197 | controller.name == "Keyboard Mouse")); // Windows Keyboard/Mouse |
| 198 | } |
| 199 | |
| 200 | void InputConfig::GenerateControllerTextures(const Common::IniFile& file) |
| 201 | { |
| 202 | m_dynamic_input_tex_config_manager.Load(); |
| 203 | |
| 204 | std::vector<std::string> controller_names; |
| 205 | for (auto& controller : m_controllers) |
| 206 | { |
| 207 | controller_names.push_back(controller->GetName()); |
| 208 | } |
| 209 | |
| 210 | m_dynamic_input_tex_config_manager.GenerateTextures(file, controller_names); |
| 211 | } |
| 212 | |
| 213 | void InputConfig::GenerateControllerTextures() |
| 214 | { |
| 215 | const std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini"; |
| 216 | |
| 217 | Common::IniFile inifile; |
| 218 | inifile.Load(ini_filename); |
| 219 | |
| 220 | for (auto& controller : m_controllers) |
| 221 | { |
| 222 | controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName())); |
| 223 | } |
| 224 | |
| 225 | GenerateControllerTextures(inifile); |
| 226 | } |