Skip to content

Commit

Permalink
Merge pull request dolphin-emu#8655 from Techjar/fix-hotkey-groups
Browse files Browse the repository at this point in the history
Core/HotkeyManager: Fix group names in config
  • Loading branch information
leoetlino authored Mar 18, 2020
2 parents bb430fb + ae0c918 commit 4711b76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Source/Core/Common/IniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ bool IniFile::DeleteSection(std::string_view section_name)
return false;
}

bool IniFile::Exists(std::string_view section_name) const
{
return GetSection(section_name) != nullptr;
}

bool IniFile::Exists(std::string_view section_name, std::string_view key) const
{
const Section* section = GetSection(section_name);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/IniFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class IniFile

bool Save(const std::string& filename);

bool Exists(std::string_view section_name) const;
// Returns true if key exists in section
bool Exists(std::string_view section_name, std::string_view key) const;

Expand Down
46 changes: 43 additions & 3 deletions Source/Core/Core/HotkeyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "Common/Common.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/StringUtil.h"

#include "InputCommon/ControllerEmu/Control/Input.h"
Expand Down Expand Up @@ -238,6 +240,43 @@ bool IsPressed(int id, bool held)
return false;
}

// This function exists to load the old "Keys" group so pre-existing configs don't break.
// TODO: Remove this at a future date when we're confident most configs are migrated.
static void LoadLegacyConfig(ControllerEmu::EmulatedController* controller)
{
IniFile inifile;
if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + "Hotkeys.ini"))
{
if (!inifile.Exists("Hotkeys") && inifile.Exists("Hotkeys1"))
{
auto sec = inifile.GetOrCreateSection("Hotkeys1");

{
std::string defdev;
sec->Get("Device", &defdev, "");
controller->SetDefaultDevice(defdev);
}

for (auto& group : controller->groups)
{
for (auto& control : group->controls)
{
std::string key("Keys/" + control->name);

if (sec->Exists(key))
{
std::string expression;
sec->Get(key, &expression, "");
control->control_ref->SetExpression(std::move(expression));
}
}
}

controller->UpdateReferences(g_controller_interface);
}
}
}

void Initialize()
{
if (s_config.ControllersNeedToBeCreated())
Expand All @@ -246,7 +285,7 @@ void Initialize()
s_config.RegisterHotplugCallback();

// load the saved controller config
s_config.LoadConfig(true);
LoadConfig();

s_hotkey_down = {};

Expand All @@ -256,6 +295,7 @@ void Initialize()
void LoadConfig()
{
s_config.LoadConfig(true);
LoadLegacyConfig(s_config.GetController(0));
}

ControllerEmu::ControlGroup* GetHotkeyGroup(HotkeyGroup group)
Expand Down Expand Up @@ -307,7 +347,7 @@ HotkeyManager::HotkeyManager()
for (std::size_t group = 0; group < m_hotkey_groups.size(); group++)
{
m_hotkey_groups[group] =
(m_keys[group] = new ControllerEmu::Buttons("Keys", s_groups_info[group].name));
(m_keys[group] = new ControllerEmu::Buttons(s_groups_info[group].name));
groups.emplace_back(m_hotkey_groups[group]);
for (int key = s_groups_info[group].first; key <= s_groups_info[group].last; key++)
{
Expand All @@ -322,7 +362,7 @@ HotkeyManager::~HotkeyManager()

std::string HotkeyManager::GetName() const
{
return std::string("Hotkeys") + char('1' + 0);
return "Hotkeys";
}

void HotkeyManager::GetInput(HotkeyStatus* const kb)
Expand Down

0 comments on commit 4711b76

Please sign in to comment.