2018-04-14 04:51:32 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2019-11-22 19:10:28 +00:00
|
|
|
#include "InputCommon/InputProfile.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2018-04-14 04:51:32 +00:00
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2018-04-22 21:27:10 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-04-22 21:27:10 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-04-14 04:51:32 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/HW/Wiimote.h"
|
|
|
|
#include "Core/HotkeyManager.h"
|
|
|
|
|
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
#include "InputCommon/InputConfig.h"
|
2018-04-17 05:43:56 +00:00
|
|
|
|
2018-04-14 04:51:32 +00:00
|
|
|
namespace InputProfile
|
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
constexpr int display_message_ms = 3000;
|
|
|
|
}
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-26 22:19:13 +00:00
|
|
|
std::vector<std::string> GetProfilesFromSetting(const std::string& setting, const std::string& root)
|
|
|
|
{
|
|
|
|
const auto& setting_choices = SplitString(setting, ',');
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
for (const std::string& setting_choice : setting_choices)
|
|
|
|
{
|
2019-07-08 11:35:53 +00:00
|
|
|
const std::string path = root + std::string(StripSpaces(setting_choice));
|
2018-05-26 22:19:13 +00:00
|
|
|
if (File::IsDirectory(path))
|
|
|
|
{
|
|
|
|
const auto files_under_directory = Common::DoFileSearch({path}, {".ini"}, true);
|
|
|
|
result.insert(result.end(), files_under_directory.begin(), files_under_directory.end());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string file_path = path + ".ini";
|
|
|
|
if (File::Exists(file_path))
|
|
|
|
{
|
|
|
|
result.push_back(file_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
std::vector<std::string> ProfileCycler::GetProfilesForDevice(InputConfig* device_configuration)
|
|
|
|
{
|
|
|
|
const std::string device_profile_root_location(File::GetUserPath(D_CONFIG_IDX) + "Profiles/" +
|
|
|
|
device_configuration->GetProfileName());
|
2018-05-26 22:19:13 +00:00
|
|
|
return Common::DoFileSearch({device_profile_root_location}, {".ini"}, true);
|
2018-05-24 03:20:58 +00:00
|
|
|
}
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
std::string ProfileCycler::GetProfile(CycleDirection cycle_direction, int& profile_index,
|
|
|
|
const std::vector<std::string>& profiles)
|
|
|
|
{
|
|
|
|
// update the index and bind it to the number of available strings
|
|
|
|
auto positive_modulo = [](int& i, int n) { i = (i % n + n) % n; };
|
|
|
|
profile_index += static_cast<int>(cycle_direction);
|
|
|
|
positive_modulo(profile_index, static_cast<int>(profiles.size()));
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
return profiles[profile_index];
|
|
|
|
}
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
void ProfileCycler::UpdateToProfile(const std::string& profile_filename,
|
2021-02-27 22:41:50 +00:00
|
|
|
ControllerEmu::EmulatedController* controller,
|
|
|
|
InputConfig* device_configuration)
|
2018-05-24 03:20:58 +00:00
|
|
|
{
|
|
|
|
std::string base;
|
|
|
|
SplitPath(profile_filename, nullptr, &base, nullptr);
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
IniFile ini_file;
|
|
|
|
if (ini_file.Load(profile_filename))
|
2018-04-14 04:51:32 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("Loading input profile '" + base + "' for device '" +
|
|
|
|
controller->GetName() + "'",
|
|
|
|
display_message_ms);
|
|
|
|
controller->LoadConfig(ini_file.GetOrCreateSection("Profile"));
|
|
|
|
controller->UpdateReferences(g_controller_interface);
|
2021-02-27 22:41:50 +00:00
|
|
|
device_configuration->GenerateControllerTextures(ini_file);
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
2018-05-24 03:20:58 +00:00
|
|
|
else
|
2018-04-17 05:43:56 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("Unable to load input profile '" + base + "' for device '" +
|
|
|
|
controller->GetName() + "'",
|
|
|
|
display_message_ms);
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 05:43:56 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
std::vector<std::string>
|
|
|
|
ProfileCycler::GetMatchingProfilesFromSetting(const std::string& setting,
|
|
|
|
const std::vector<std::string>& profiles,
|
|
|
|
InputConfig* device_configuration)
|
|
|
|
{
|
2018-05-26 22:19:13 +00:00
|
|
|
const std::string device_profile_root_location(File::GetUserPath(D_CONFIG_IDX) + "Profiles/" +
|
|
|
|
device_configuration->GetProfileName() + "/");
|
|
|
|
|
|
|
|
const auto& profiles_from_setting = GetProfilesFromSetting(setting, device_profile_root_location);
|
2018-05-24 03:20:58 +00:00
|
|
|
if (profiles_from_setting.empty())
|
2018-04-17 05:43:56 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
return {};
|
2018-04-17 05:43:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
std::set_intersection(profiles.begin(), profiles.end(), profiles_from_setting.begin(),
|
|
|
|
profiles_from_setting.end(), std::back_inserter(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileCycler::CycleProfile(CycleDirection cycle_direction, InputConfig* device_configuration,
|
|
|
|
int& profile_index, int controller_index)
|
|
|
|
{
|
|
|
|
const auto& profiles = GetProfilesForDevice(device_configuration);
|
|
|
|
if (profiles.empty())
|
2018-04-14 04:51:32 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No input profiles found", display_message_ms);
|
|
|
|
return;
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
2018-05-24 03:20:58 +00:00
|
|
|
const std::string profile = GetProfile(cycle_direction, profile_index, profiles);
|
2018-04-14 04:51:32 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
auto* controller = device_configuration->GetController(controller_index);
|
|
|
|
if (controller)
|
2018-04-17 05:43:56 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
UpdateToProfile(profile, controller, device_configuration);
|
2018-04-17 05:43:56 +00:00
|
|
|
}
|
2018-05-24 03:20:58 +00:00
|
|
|
else
|
2018-04-22 21:27:10 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index),
|
|
|
|
display_message_ms);
|
2018-04-22 21:27:10 +00:00
|
|
|
}
|
2018-05-24 03:20:58 +00:00
|
|
|
}
|
2018-04-22 21:27:10 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
void ProfileCycler::CycleProfileForGame(CycleDirection cycle_direction,
|
|
|
|
InputConfig* device_configuration, int& profile_index,
|
|
|
|
const std::string& setting, int controller_index)
|
|
|
|
{
|
|
|
|
const auto& profiles = GetProfilesForDevice(device_configuration);
|
|
|
|
if (profiles.empty())
|
2018-04-14 04:51:32 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No input profiles found", display_message_ms);
|
|
|
|
return;
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
if (setting.empty())
|
2018-04-14 04:51:32 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No setting found for game", display_message_ms);
|
|
|
|
return;
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
2018-04-17 05:43:56 +00:00
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
const auto& profiles_for_game =
|
|
|
|
GetMatchingProfilesFromSetting(setting, profiles, device_configuration);
|
|
|
|
if (profiles_for_game.empty())
|
2018-04-17 05:43:56 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No input profiles found for game", display_message_ms);
|
|
|
|
return;
|
2018-04-17 05:43:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 03:20:58 +00:00
|
|
|
const std::string profile = GetProfile(cycle_direction, profile_index, profiles_for_game);
|
|
|
|
|
|
|
|
auto* controller = device_configuration->GetController(controller_index);
|
|
|
|
if (controller)
|
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
UpdateToProfile(profile, controller, device_configuration);
|
2018-05-24 03:20:58 +00:00
|
|
|
}
|
|
|
|
else
|
2018-04-17 05:43:56 +00:00
|
|
|
{
|
2018-05-24 03:20:58 +00:00
|
|
|
Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index),
|
|
|
|
display_message_ms);
|
2018-04-17 05:43:56 +00:00
|
|
|
}
|
2018-04-14 04:51:32 +00:00
|
|
|
}
|
2018-05-24 03:20:58 +00:00
|
|
|
|
|
|
|
std::string ProfileCycler::GetWiimoteInputProfilesForGame(int controller_index)
|
|
|
|
{
|
|
|
|
IniFile game_ini = SConfig::GetInstance().LoadGameIni();
|
|
|
|
const IniFile::Section* const control_section = game_ini.GetOrCreateSection("Controls");
|
|
|
|
|
|
|
|
std::string result;
|
2019-11-22 19:10:28 +00:00
|
|
|
control_section->Get(fmt::format("WiimoteProfile{}", controller_index + 1), &result);
|
2018-05-24 03:20:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileCycler::NextWiimoteProfile(int controller_index)
|
|
|
|
{
|
|
|
|
CycleProfile(CycleDirection::Forward, Wiimote::GetConfig(), m_wiimote_profile_index,
|
|
|
|
controller_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileCycler::PreviousWiimoteProfile(int controller_index)
|
|
|
|
{
|
|
|
|
CycleProfile(CycleDirection::Backward, Wiimote::GetConfig(), m_wiimote_profile_index,
|
|
|
|
controller_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileCycler::NextWiimoteProfileForGame(int controller_index)
|
|
|
|
{
|
|
|
|
CycleProfileForGame(CycleDirection::Forward, Wiimote::GetConfig(), m_wiimote_profile_index,
|
|
|
|
GetWiimoteInputProfilesForGame(controller_index), controller_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileCycler::PreviousWiimoteProfileForGame(int controller_index)
|
|
|
|
{
|
|
|
|
CycleProfileForGame(CycleDirection::Backward, Wiimote::GetConfig(), m_wiimote_profile_index,
|
|
|
|
GetWiimoteInputProfilesForGame(controller_index), controller_index);
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace InputProfile
|