2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2015-10-26 03:28:15 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2021-02-27 23:41:50 +01:00
|
|
|
#include "Common/Config/Config.h"
|
2015-10-26 03:28:15 +01:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
2016-10-07 22:55:13 +02:00
|
|
|
#include "Common/MsgHandler.h"
|
2018-04-22 23:27:10 +02:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-04-28 20:07:26 +02:00
|
|
|
#include "Core/Core.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Core/HW/Wiimote.h"
|
2017-02-09 04:15:43 +01:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
|
2017-01-30 04:32:04 +01:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
2019-03-27 01:31:03 +01:00
|
|
|
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
|
2015-10-26 03:28:15 +01:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "InputCommon/InputConfig.h"
|
2018-05-27 00:19:13 +02:00
|
|
|
#include "InputCommon/InputProfile.h"
|
2010-06-04 22:03:03 +02:00
|
|
|
|
2017-02-09 04:15:43 +01: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;
|
|
|
|
|
2014-08-31 06:04:15 +02:00
|
|
|
bool InputConfig::LoadConfig(bool isGC)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
|
|
|
IniFile inifile;
|
2013-05-18 14:30:20 +02:00
|
|
|
bool useProfile[MAX_BBMOTES] = {false, false, false, false, false};
|
2019-09-24 06:32:43 +02:00
|
|
|
static constexpr std::array<std::string_view, MAX_BBMOTES> num = {"1", "2", "3", "4", "BB"};
|
2013-05-18 14:30:20 +02:00
|
|
|
std::string profile[MAX_BBMOTES];
|
2013-01-10 02:41:14 +01:00
|
|
|
std::string path;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-10-25 03:29:48 +02:00
|
|
|
#if defined(ANDROID)
|
|
|
|
bool use_ir_config = false;
|
|
|
|
std::string ir_values[3];
|
|
|
|
#endif
|
|
|
|
|
2019-08-17 21:40:58 +02:00
|
|
|
m_dynamic_input_tex_config_manager.Load();
|
|
|
|
|
2016-10-29 14:42:43 +02:00
|
|
|
if (SConfig::GetInstance().GetGameID() != "00000000")
|
2013-01-09 21:03:43 +01:00
|
|
|
{
|
|
|
|
std::string type;
|
|
|
|
if (isGC)
|
2013-01-10 02:41:14 +01:00
|
|
|
{
|
2013-01-10 03:56:17 +01:00
|
|
|
type = "Pad";
|
2013-01-10 02:41:14 +01:00
|
|
|
path = "Profiles/GCPad/";
|
|
|
|
}
|
2013-01-09 21:03:43 +01:00
|
|
|
else
|
2013-01-10 02:41:14 +01:00
|
|
|
{
|
2013-01-10 03:56:17 +01:00
|
|
|
type = "Wiimote";
|
2013-01-10 02:41:14 +01:00
|
|
|
path = "Profiles/Wiimote/";
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-12 13:56:53 +02:00
|
|
|
IniFile game_ini = SConfig::GetInstance().LoadGameIni();
|
2014-06-16 07:12:43 +02:00
|
|
|
IniFile::Section* control_section = game_ini.GetOrCreateSection("Controls");
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-09 21:03:43 +01:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2020-11-12 08:29:21 +01:00
|
|
|
const auto profile_name = fmt::format("{}Profile{}", type, num[i]);
|
|
|
|
|
|
|
|
if (control_section->Exists(profile_name))
|
2013-01-09 21:03:43 +01:00
|
|
|
{
|
2018-04-22 23:27:10 +02:00
|
|
|
std::string profile_setting;
|
2020-11-12 08:29:21 +01:00
|
|
|
if (control_section->Get(profile_name, &profile_setting))
|
2013-09-07 23:02:49 +02:00
|
|
|
{
|
2018-05-27 00:19:13 +02:00
|
|
|
auto profiles = InputProfile::GetProfilesFromSetting(
|
|
|
|
profile_setting, File::GetUserPath(D_CONFIG_IDX) + path);
|
2018-04-22 23:27:10 +02:00
|
|
|
|
2018-05-27 00:19:13 +02:00
|
|
|
if (profiles.empty())
|
2014-02-08 06:50:37 +01:00
|
|
|
{
|
2015-02-07 21:27:26 +01:00
|
|
|
// TODO: PanicAlert shouldn't be used for this.
|
2020-11-16 13:28:11 +01:00
|
|
|
PanicAlertFmtT("No profiles found for game setting '{0}'", profile_setting);
|
2018-05-27 00:19:13 +02:00
|
|
|
continue;
|
2014-02-08 06:50:37 +01:00
|
|
|
}
|
2018-05-27 00:19:13 +02:00
|
|
|
|
|
|
|
// Use the first profile by default
|
|
|
|
profile[i] = profiles[0];
|
|
|
|
useProfile[i] = true;
|
2013-09-07 23:02:49 +02:00
|
|
|
}
|
2013-01-09 21:03:43 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 03:29:48 +02:00
|
|
|
#if defined(ANDROID)
|
|
|
|
// For use on android touchscreen IR pointer
|
|
|
|
// Check for IR values
|
2019-03-27 01:31:03 +01:00
|
|
|
if (control_section->Exists("IRTotalYaw") && control_section->Exists("IRTotalPitch") &&
|
|
|
|
control_section->Exists("IRVerticalOffset"))
|
2018-10-25 03:29:48 +02:00
|
|
|
{
|
|
|
|
use_ir_config = true;
|
2019-03-27 01:31:03 +01:00
|
|
|
control_section->Get("IRTotalYaw", &ir_values[0]);
|
|
|
|
control_section->Get("IRTotalPitch", &ir_values[1]);
|
|
|
|
control_section->Get("IRVerticalOffset", &ir_values[2]);
|
2018-10-25 03:29:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
2013-01-09 21:03:43 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-09-29 21:49:22 +02:00
|
|
|
if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini") &&
|
|
|
|
!inifile.GetSections().empty())
|
2010-07-10 08:48:24 +02:00
|
|
|
{
|
2014-01-31 01:51:21 +01:00
|
|
|
int n = 0;
|
2021-02-27 23:41:50 +01:00
|
|
|
|
|
|
|
std::vector<std::string> controller_names;
|
2015-10-26 03:28:15 +01:00
|
|
|
for (auto& controller : m_controllers)
|
2010-10-03 06:29:34 +02:00
|
|
|
{
|
2018-10-25 03:29:48 +02:00
|
|
|
IniFile::Section config;
|
2014-01-31 01:51:21 +01:00
|
|
|
// Load settings from ini
|
2013-01-09 21:03:43 +01:00
|
|
|
if (useProfile[n])
|
|
|
|
{
|
2018-04-28 20:07:26 +02:00
|
|
|
std::string base;
|
|
|
|
SplitPath(profile[n], nullptr, &base, nullptr);
|
2018-05-24 05:20:58 +02:00
|
|
|
Core::DisplayMessage("Loading game specific input profile '" + base + "' for device '" +
|
|
|
|
controller->GetName() + "'",
|
2018-04-28 20:07:26 +02:00
|
|
|
6000);
|
|
|
|
|
2021-02-27 23:41:50 +01:00
|
|
|
inifile.Load(profile[n]);
|
|
|
|
config = *inifile.GetOrCreateSection("Profile");
|
2013-01-09 21:03:43 +01:00
|
|
|
}
|
|
|
|
else
|
2013-04-15 04:53:10 +02:00
|
|
|
{
|
2018-10-25 03:29:48 +02:00
|
|
|
config = *inifile.GetOrCreateSection(controller->GetName());
|
2013-04-15 04:53:10 +02:00
|
|
|
}
|
2018-10-25 03:29:48 +02:00
|
|
|
#if defined(ANDROID)
|
|
|
|
// Only set for wii pads
|
|
|
|
if (!isGC && use_ir_config)
|
|
|
|
{
|
2019-03-27 01:31:03 +01:00
|
|
|
config.Set("IR/Total Yaw", ir_values[0]);
|
|
|
|
config.Set("IR/Total Pitch", ir_values[1]);
|
|
|
|
config.Set("IR/Vertical Offset", ir_values[2]);
|
2018-10-25 03:29:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
controller->LoadConfig(&config);
|
2015-10-26 03:28:15 +01:00
|
|
|
controller->UpdateReferences(g_controller_interface);
|
2021-02-27 23:41:50 +01:00
|
|
|
controller_names.push_back(controller->GetName());
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-01-31 01:51:21 +01:00
|
|
|
// Next profile
|
|
|
|
n++;
|
2010-10-03 06:29:34 +02:00
|
|
|
}
|
2021-02-27 23:41:50 +01:00
|
|
|
|
|
|
|
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
|
2010-10-03 06:29:34 +02:00
|
|
|
return true;
|
2010-07-10 08:48:24 +02:00
|
|
|
}
|
2010-10-03 06:29:34 +02:00
|
|
|
else
|
2010-07-10 08:48:24 +02:00
|
|
|
{
|
2021-06-07 18:31:38 +02:00
|
|
|
// Only load the default profile for the first controller,
|
|
|
|
// otherwise they would all share the same mappings and default device
|
|
|
|
if (m_controllers.size() > 0)
|
|
|
|
{
|
|
|
|
m_controllers[0]->LoadDefaults(g_controller_interface);
|
|
|
|
m_controllers[0]->UpdateReferences(g_controller_interface);
|
|
|
|
}
|
|
|
|
// Set the "default" default device for all other controllers, or they would end up
|
|
|
|
// having no default device (which is fine, but might be confusing for some users)
|
|
|
|
const std::string& default_device_string = g_controller_interface.GetDefaultDeviceString();
|
|
|
|
if (!default_device_string.empty())
|
|
|
|
{
|
|
|
|
for (size_t i = 1; i < m_controllers.size(); ++i)
|
|
|
|
{
|
|
|
|
m_controllers[i]->SetDefaultDevice(default_device_string);
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 06:29:34 +02:00
|
|
|
return false;
|
2010-06-04 22:03:03 +02:00
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 06:04:15 +02:00
|
|
|
void InputConfig::SaveConfig()
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2015-10-26 03:28:15 +01:00
|
|
|
std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini";
|
2010-06-04 22:03:03 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
IniFile inifile;
|
2010-07-03 10:04:10 +02:00
|
|
|
inifile.Load(ini_filename);
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2021-02-27 23:41:50 +01:00
|
|
|
std::vector<std::string> controller_names;
|
2015-10-26 03:28:15 +01:00
|
|
|
for (auto& controller : m_controllers)
|
2021-02-27 23:41:50 +01:00
|
|
|
{
|
2015-10-26 03:28:15 +01:00
|
|
|
controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName()));
|
2021-02-27 23:41:50 +01:00
|
|
|
controller_names.push_back(controller->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
|
2013-09-07 23:02:49 +02:00
|
|
|
|
2010-06-04 22:03:03 +02:00
|
|
|
inifile.Save(ini_filename);
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
2015-10-26 03:28:15 +01:00
|
|
|
|
2021-05-04 22:47:55 +02:00
|
|
|
ControllerEmu::EmulatedController* InputConfig::GetController(int index) const
|
2015-10-26 03:28:15 +01:00
|
|
|
{
|
|
|
|
return m_controllers.at(index).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputConfig::ClearControllers()
|
|
|
|
{
|
|
|
|
m_controllers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputConfig::ControllersNeedToBeCreated() const
|
|
|
|
{
|
|
|
|
return m_controllers.empty();
|
|
|
|
}
|
2016-06-21 15:33:53 +02:00
|
|
|
|
2021-02-13 02:21:48 +01:00
|
|
|
int InputConfig::GetControllerCount() const
|
2018-04-14 06:51:32 +02:00
|
|
|
{
|
2021-02-13 02:21:48 +01:00
|
|
|
return static_cast<int>(m_controllers.size());
|
2018-04-14 06:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:02:38 +01: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 15:33:53 +02:00
|
|
|
bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
|
|
|
|
{
|
|
|
|
if (static_cast<size_t>(index) >= m_controllers.size())
|
|
|
|
return false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-11-04 22:08:26 +01:00
|
|
|
const auto& controller = m_controllers.at(index).get()->GetDefaultDevice();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-06-21 15:33:53 +02:00
|
|
|
// Filter out anything which obviously not a gamepad
|
2017-08-22 18:26:44 +02:00
|
|
|
return !((controller.source == "Quartz") // OSX Quartz Keyboard/Mouse
|
2016-06-21 15:33:53 +02:00
|
|
|
|| (controller.source == "XInput2") // Linux and BSD Keyboard/Mouse
|
|
|
|
|| (controller.source == "Android" &&
|
|
|
|
controller.name == "Touchscreen") // Android Touchscreen
|
|
|
|
|| (controller.source == "DInput" &&
|
|
|
|
controller.name == "Keyboard Mouse")); // Windows Keyboard/Mouse
|
|
|
|
}
|
2021-02-27 23:41:50 +01:00
|
|
|
|
|
|
|
void InputConfig::GenerateControllerTextures(const IniFile& file)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|