summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/InputProfile.cpp
blob: 0d439c48e10f723b646d1607c484f21ac09c80cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "InputCommon/InputProfile.h"

#include <algorithm>
#include <iterator>

#include <fmt/format.h>

#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"

#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/Wiimote.h"
#include "Core/HotkeyManager.h"

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/InputConfig.h"

namespace InputProfile
{
namespace
{
constexpr int display_message_ms = 3000;
}

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)
  {
    const std::string path = root + std::string(StripWhitespace(setting_choice));
    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;
}

std::vector<std::string> ProfileCycler::GetProfilesForDevice(InputConfig* device_configuration)
{
  const std::string device_profile_root_location(
      device_configuration->GetUserProfileDirectoryPath());
  return Common::DoFileSearch({device_profile_root_location}, {".ini"}, true);
}

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()));

  return profiles[profile_index];
}

void ProfileCycler::UpdateToProfile(const std::string& profile_filename,
                                    ControllerEmu::EmulatedController* controller,
                                    InputConfig* device_configuration)
{
  std::string base;
  SplitPath(profile_filename, nullptr, &base, nullptr);

  Common::IniFile ini_file;
  if (ini_file.Load(profile_filename))
  {
    Core::DisplayMessage("Loading input profile '" + base + "' for device '" +
                             controller->GetName() + "'",
                         display_message_ms);
    controller->LoadConfig(ini_file.GetOrCreateSection("Profile"));
    controller->UpdateReferences(g_controller_interface);
    device_configuration->GenerateControllerTextures(ini_file);
  }
  else
  {
    Core::DisplayMessage("Unable to load input profile '" + base + "' for device '" +
                             controller->GetName() + "'",
                         display_message_ms);
  }
}

std::vector<std::string>
ProfileCycler::GetMatchingProfilesFromSetting(const std::string& setting,
                                              const std::vector<std::string>& profiles,
                                              InputConfig* device_configuration)
{
  const std::string device_profile_root_location(
      device_configuration->GetUserProfileDirectoryPath());

  const auto& profiles_from_setting = GetProfilesFromSetting(setting, device_profile_root_location);
  if (profiles_from_setting.empty())
  {
    return {};
  }

  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())
  {
    Core::DisplayMessage("No input profiles found", display_message_ms);
    return;
  }
  const std::string profile = GetProfile(cycle_direction, profile_index, profiles);

  auto* controller = device_configuration->GetController(controller_index);
  if (controller)
  {
    UpdateToProfile(profile, controller, device_configuration);
  }
  else
  {
    Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index),
                         display_message_ms);
  }
}

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())
  {
    Core::DisplayMessage("No input profiles found", display_message_ms);
    return;
  }

  if (setting.empty())
  {
    Core::DisplayMessage("No setting found for game", display_message_ms);
    return;
  }

  const auto& profiles_for_game =
      GetMatchingProfilesFromSetting(setting, profiles, device_configuration);
  if (profiles_for_game.empty())
  {
    Core::DisplayMessage("No input profiles found for game", display_message_ms);
    return;
  }

  const std::string profile = GetProfile(cycle_direction, profile_index, profiles_for_game);

  auto* controller = device_configuration->GetController(controller_index);
  if (controller)
  {
    UpdateToProfile(profile, controller, device_configuration);
  }
  else
  {
    Core::DisplayMessage("No controller found for index: " + std::to_string(controller_index),
                         display_message_ms);
  }
}

std::string ProfileCycler::GetWiimoteInputProfilesForGame(int controller_index)
{
  Common::IniFile game_ini = SConfig::GetInstance().LoadGameIni();
  const auto* const control_section = game_ini.GetOrCreateSection("Controls");

  std::string result;
  control_section->Get(fmt::format("WiimoteProfile{}", controller_index + 1), &result);
  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);
}
}  // namespace InputProfile