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