2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "InputCommon/InputConfig.h"
|
|
|
|
|
2015-10-26 02:28:15 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
#include "Common/Config/Config.h"
|
2015-10-26 02:28:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
2016-10-07 20:55:13 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2018-04-22 21:27:10 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-04-28 18:07:26 +00:00
|
|
|
#include "Core/Core.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Wiimote.h"
|
2017-02-09 03:15:43 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
|
2017-01-30 03:32:04 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
2019-03-27 00:31:03 +00:00
|
|
|
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
|
2015-10-26 02:28:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2018-05-26 22:19:13 +00:00
|
|
|
#include "InputCommon/InputProfile.h"
|
2010-06-04 20:03:03 +00:00
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
InputConfig::InputConfig(const std::string& ini_name, const std::string& gui_name,
|
|
|
|
const std::string& profile_name)
|
|
|
|
: m_ini_name(ini_name), m_gui_name(gui_name), m_profile_name(profile_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputConfig::~InputConfig() = default;
|
|
|
|
|
2021-07-04 11:02:03 +00:00
|
|
|
bool InputConfig::LoadConfig(InputClass type)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile inifile;
|
2013-05-18 12:30:20 +00:00
|
|
|
bool useProfile[MAX_BBMOTES] = {false, false, false, false, false};
|
2019-09-24 04:32:43 +00:00
|
|
|
static constexpr std::array<std::string_view, MAX_BBMOTES> num = {"1", "2", "3", "4", "BB"};
|
2013-05-18 12:30:20 +00:00
|
|
|
std::string profile[MAX_BBMOTES];
|
2013-01-10 01:41:14 +00:00
|
|
|
std::string path;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-08-17 19:40:58 +00:00
|
|
|
m_dynamic_input_tex_config_manager.Load();
|
|
|
|
|
2016-10-29 12:42:43 +00:00
|
|
|
if (SConfig::GetInstance().GetGameID() != "00000000")
|
2013-01-09 20:03:43 +00:00
|
|
|
{
|
2021-07-04 11:02:03 +00:00
|
|
|
std::string type_str;
|
|
|
|
switch (type)
|
2013-01-10 01:41:14 +00:00
|
|
|
{
|
2021-07-04 11:02:03 +00:00
|
|
|
case InputClass::GBA:
|
|
|
|
type_str = "GBA";
|
|
|
|
path = "Profiles/GBA/";
|
|
|
|
break;
|
|
|
|
case InputClass::Wii:
|
|
|
|
type_str = "Wiimote";
|
2013-01-10 01:41:14 +00:00
|
|
|
path = "Profiles/Wiimote/";
|
2021-07-04 11:02:03 +00:00
|
|
|
break;
|
|
|
|
case InputClass::GC:
|
|
|
|
default:
|
|
|
|
type_str = "Pad";
|
|
|
|
path = "Profiles/GCPad/";
|
|
|
|
break;
|
2013-01-10 01:41:14 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini = SConfig::GetInstance().LoadGameIni();
|
|
|
|
auto* control_section = game_ini.GetOrCreateSection("Controls");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-09 20:03:43 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2021-07-04 11:02:03 +00:00
|
|
|
const auto profile_name = fmt::format("{}Profile{}", type_str, num[i]);
|
2020-11-12 07:29:21 +00:00
|
|
|
|
|
|
|
if (control_section->Exists(profile_name))
|
2013-01-09 20:03:43 +00:00
|
|
|
{
|
2018-04-22 21:27:10 +00:00
|
|
|
std::string profile_setting;
|
2020-11-12 07:29:21 +00:00
|
|
|
if (control_section->Get(profile_name, &profile_setting))
|
2013-09-07 21:02:49 +00:00
|
|
|
{
|
2018-05-26 22:19:13 +00:00
|
|
|
auto profiles = InputProfile::GetProfilesFromSetting(
|
|
|
|
profile_setting, File::GetUserPath(D_CONFIG_IDX) + path);
|
2018-04-22 21:27:10 +00:00
|
|
|
|
2018-05-26 22:19:13 +00:00
|
|
|
if (profiles.empty())
|
2014-02-08 05:50:37 +00:00
|
|
|
{
|
2015-02-07 20:27:26 +00:00
|
|
|
// TODO: PanicAlert shouldn't be used for this.
|
2020-11-16 12:28:11 +00:00
|
|
|
PanicAlertFmtT("No profiles found for game setting '{0}'", profile_setting);
|
2018-05-26 22:19:13 +00:00
|
|
|
continue;
|
2014-02-08 05:50:37 +00:00
|
|
|
}
|
2018-05-26 22:19:13 +00:00
|
|
|
|
|
|
|
// Use the first profile by default
|
|
|
|
profile[i] = profiles[0];
|
|
|
|
useProfile[i] = true;
|
2013-09-07 21:02:49 +00:00
|
|
|
}
|
2013-01-09 20:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-09-29 19:49:22 +00:00
|
|
|
if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini") &&
|
|
|
|
!inifile.GetSections().empty())
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
int n = 0;
|
2021-02-27 22:41:50 +00:00
|
|
|
|
|
|
|
std::vector<std::string> controller_names;
|
2015-10-26 02:28:15 +00:00
|
|
|
for (auto& controller : m_controllers)
|
2010-10-03 04:29:34 +00:00
|
|
|
{
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile::Section config;
|
2014-01-31 00:51:21 +00:00
|
|
|
// Load settings from ini
|
2013-01-09 20:03:43 +00:00
|
|
|
if (useProfile[n])
|
|
|
|
{
|
2018-04-28 18:07:26 +00:00
|
|
|
std::string base;
|
|
|
|
SplitPath(profile[n], nullptr, &base, nullptr);
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("Loading game specific input profile '" + base + "' for device '" +
|
|
|
|
controller->GetName() + "'",
|
2018-04-28 18:07:26 +00:00
|
|
|
6000);
|
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
inifile.Load(profile[n]);
|
|
|
|
config = *inifile.GetOrCreateSection("Profile");
|
2013-01-09 20:03:43 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2018-10-25 01:29:48 +00:00
|
|
|
config = *inifile.GetOrCreateSection(controller->GetName());
|
2013-04-15 02:53:10 +00:00
|
|
|
}
|
2018-10-25 01:29:48 +00:00
|
|
|
controller->LoadConfig(&config);
|
2015-10-26 02:28:15 +00:00
|
|
|
controller->UpdateReferences(g_controller_interface);
|
2021-02-27 22:41:50 +00:00
|
|
|
controller_names.push_back(controller->GetName());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 00:51:21 +00:00
|
|
|
// Next profile
|
|
|
|
n++;
|
2010-10-03 04:29:34 +00:00
|
|
|
}
|
2021-02-27 22:41:50 +00:00
|
|
|
|
|
|
|
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
|
2010-10-03 04:29:34 +00:00
|
|
|
return true;
|
2010-07-10 06:48:24 +00:00
|
|
|
}
|
2010-10-03 04:29:34 +00:00
|
|
|
else
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2021-11-29 22:33:11 +00:00
|
|
|
// Only load the default profile for the first controller and clear the others,
|
|
|
|
// otherwise they would all share the same mappings on the same (default) device
|
2021-06-07 16:31:38 +00:00
|
|
|
if (m_controllers.size() > 0)
|
|
|
|
{
|
|
|
|
m_controllers[0]->LoadDefaults(g_controller_interface);
|
|
|
|
m_controllers[0]->UpdateReferences(g_controller_interface);
|
|
|
|
}
|
2021-11-29 22:33:11 +00:00
|
|
|
for (size_t i = 1; i < m_controllers.size(); ++i)
|
2021-06-07 16:31:38 +00:00
|
|
|
{
|
2021-11-29 22:33:11 +00:00
|
|
|
// Calling the base version just clears all settings without overwriting them with a default
|
|
|
|
m_controllers[i]->EmulatedController::LoadDefaults(g_controller_interface);
|
|
|
|
m_controllers[i]->UpdateReferences(g_controller_interface);
|
2021-06-07 16:31:38 +00:00
|
|
|
}
|
2010-10-03 04:29:34 +00:00
|
|
|
return false;
|
2010-06-04 20:03:03 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 04:04:15 +00:00
|
|
|
void InputConfig::SaveConfig()
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2015-10-26 02:28:15 +00:00
|
|
|
std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini";
|
2010-06-04 20:03:03 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile inifile;
|
2010-07-03 08:04:10 +00:00
|
|
|
inifile.Load(ini_filename);
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
std::vector<std::string> controller_names;
|
2015-10-26 02:28:15 +00:00
|
|
|
for (auto& controller : m_controllers)
|
2021-02-27 22:41:50 +00:00
|
|
|
{
|
2015-10-26 02:28:15 +00:00
|
|
|
controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName()));
|
2021-02-27 22:41:50 +00:00
|
|
|
controller_names.push_back(controller->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
|
2013-09-07 21:02:49 +00:00
|
|
|
|
2010-06-04 20:03:03 +00:00
|
|
|
inifile.Save(ini_filename);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
2015-10-26 02:28:15 +00:00
|
|
|
|
2021-05-04 20:47:55 +00:00
|
|
|
ControllerEmu::EmulatedController* InputConfig::GetController(int index) const
|
2015-10-26 02:28:15 +00:00
|
|
|
{
|
|
|
|
return m_controllers.at(index).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputConfig::ClearControllers()
|
|
|
|
{
|
|
|
|
m_controllers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputConfig::ControllersNeedToBeCreated() const
|
|
|
|
{
|
|
|
|
return m_controllers.empty();
|
|
|
|
}
|
2016-06-21 13:33:53 +00:00
|
|
|
|
2021-02-13 01:21:48 +00:00
|
|
|
int InputConfig::GetControllerCount() const
|
2018-04-14 04:51:32 +00:00
|
|
|
{
|
2021-02-13 01:21:48 +00:00
|
|
|
return static_cast<int>(m_controllers.size());
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:02:38 +00:00
|
|
|
void InputConfig::RegisterHotplugCallback()
|
|
|
|
{
|
|
|
|
// Update control references on all controllers
|
|
|
|
// as configured devices may have been added or removed.
|
|
|
|
m_hotplug_callback_handle = g_controller_interface.RegisterDevicesChangedCallback([this] {
|
|
|
|
for (auto& controller : m_controllers)
|
|
|
|
controller->UpdateReferences(g_controller_interface);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputConfig::UnregisterHotplugCallback()
|
|
|
|
{
|
|
|
|
g_controller_interface.UnregisterDevicesChangedCallback(m_hotplug_callback_handle);
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:33:53 +00:00
|
|
|
bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
|
|
|
|
{
|
|
|
|
if (static_cast<size_t>(index) >= m_controllers.size())
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-11-04 21:08:26 +00:00
|
|
|
const auto& controller = m_controllers.at(index).get()->GetDefaultDevice();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-08-01 17:33:52 +00:00
|
|
|
// By default on Android, no device is selected
|
|
|
|
if (controller.source == "")
|
|
|
|
return false;
|
|
|
|
|
2016-06-21 13:33:53 +00:00
|
|
|
// Filter out anything which obviously not a gamepad
|
2017-08-22 16:26:44 +00:00
|
|
|
return !((controller.source == "Quartz") // OSX Quartz Keyboard/Mouse
|
2016-06-21 13:33:53 +00:00
|
|
|
|| (controller.source == "XInput2") // Linux and BSD Keyboard/Mouse
|
2023-08-01 17:33:52 +00:00
|
|
|
|| (controller.source == "Android" && controller.cid <= 0) // Android non-gamepad device
|
2016-06-21 13:33:53 +00:00
|
|
|
|| (controller.source == "DInput" &&
|
|
|
|
controller.name == "Keyboard Mouse")); // Windows Keyboard/Mouse
|
|
|
|
}
|
2021-02-27 22:41:50 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
void InputConfig::GenerateControllerTextures(const Common::IniFile& file)
|
2021-02-27 22:41:50 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> controller_names;
|
|
|
|
for (auto& controller : m_controllers)
|
|
|
|
{
|
|
|
|
controller_names.push_back(controller->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dynamic_input_tex_config_manager.GenerateTextures(file, controller_names);
|
|
|
|
}
|