config.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <SDL.h>
  6. #include <inih/cpp/INIReader.h>
  7. #include "common/file_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/param_package.h"
  10. #include "core/settings.h"
  11. #include "input_common/main.h"
  12. #include "yuzu_cmd/config.h"
  13. #include "yuzu_cmd/default_ini.h"
  14. Config::Config() {
  15. // TODO: Don't hardcode the path; let the frontend decide where to put the config files.
  16. sdl2_config_loc = FileUtil::GetUserPath(D_CONFIG_IDX) + "sdl2-config.ini";
  17. sdl2_config = std::make_unique<INIReader>(sdl2_config_loc);
  18. Reload();
  19. }
  20. Config::~Config() = default;
  21. bool Config::LoadINI(const std::string& default_contents, bool retry) {
  22. const char* location = this->sdl2_config_loc.c_str();
  23. if (sdl2_config->ParseError() < 0) {
  24. if (retry) {
  25. LOG_WARNING(Config, "Failed to load %s. Creating file from defaults...", location);
  26. FileUtil::CreateFullPath(location);
  27. FileUtil::WriteStringToFile(true, default_contents, location);
  28. sdl2_config = std::make_unique<INIReader>(location); // Reopen file
  29. return LoadINI(default_contents, false);
  30. }
  31. LOG_ERROR(Config, "Failed.");
  32. return false;
  33. }
  34. LOG_INFO(Config, "Successfully loaded %s", location);
  35. return true;
  36. }
  37. static const std::array<int, Settings::NativeButton::NumButtons> default_buttons = {
  38. SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_T,
  39. SDL_SCANCODE_G, SDL_SCANCODE_F, SDL_SCANCODE_H, SDL_SCANCODE_Q, SDL_SCANCODE_W,
  40. SDL_SCANCODE_M, SDL_SCANCODE_N, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_B,
  41. };
  42. static const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> default_analogs{{
  43. {
  44. SDL_SCANCODE_UP,
  45. SDL_SCANCODE_DOWN,
  46. SDL_SCANCODE_LEFT,
  47. SDL_SCANCODE_RIGHT,
  48. SDL_SCANCODE_D,
  49. },
  50. {
  51. SDL_SCANCODE_I,
  52. SDL_SCANCODE_K,
  53. SDL_SCANCODE_J,
  54. SDL_SCANCODE_L,
  55. SDL_SCANCODE_D,
  56. },
  57. }};
  58. void Config::ReadValues() {
  59. // Controls
  60. for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
  61. std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
  62. Settings::values.buttons[i] =
  63. sdl2_config->Get("Controls", Settings::NativeButton::mapping[i], default_param);
  64. if (Settings::values.buttons[i].empty())
  65. Settings::values.buttons[i] = default_param;
  66. }
  67. for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
  68. std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
  69. default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
  70. default_analogs[i][3], default_analogs[i][4], 0.5f);
  71. Settings::values.analogs[i] =
  72. sdl2_config->Get("Controls", Settings::NativeAnalog::mapping[i], default_param);
  73. if (Settings::values.analogs[i].empty())
  74. Settings::values.analogs[i] = default_param;
  75. }
  76. Settings::values.motion_device = sdl2_config->Get(
  77. "Controls", "motion_device", "engine:motion_emu,update_period:100,sensitivity:0.01");
  78. Settings::values.touch_device =
  79. sdl2_config->Get("Controls", "touch_device", "engine:emu_window");
  80. // Core
  81. Settings::values.cpu_core =
  82. static_cast<Settings::CpuCore>(sdl2_config->GetInteger("Core", "cpu_core", 0));
  83. // Renderer
  84. Settings::values.resolution_factor =
  85. (float)sdl2_config->GetReal("Renderer", "resolution_factor", 1.0);
  86. Settings::values.toggle_framelimit =
  87. sdl2_config->GetBoolean("Renderer", "toggle_framelimit", true);
  88. Settings::values.bg_red = (float)sdl2_config->GetReal("Renderer", "bg_red", 0.0);
  89. Settings::values.bg_green = (float)sdl2_config->GetReal("Renderer", "bg_green", 0.0);
  90. Settings::values.bg_blue = (float)sdl2_config->GetReal("Renderer", "bg_blue", 0.0);
  91. // Data Storage
  92. Settings::values.use_virtual_sd =
  93. sdl2_config->GetBoolean("Data Storage", "use_virtual_sd", true);
  94. // Miscellaneous
  95. Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace");
  96. // Debugging
  97. Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false);
  98. Settings::values.gdbstub_port =
  99. static_cast<u16>(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689));
  100. }
  101. void Config::Reload() {
  102. LoadINI(DefaultINI::sdl2_config_file);
  103. ReadValues();
  104. }