config.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(FileUtil::UserPath::ConfigDir) + "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 {}. 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 {}", 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.use_cpu_jit = sdl2_config->GetBoolean("Core", "use_cpu_jit", true);
  82. Settings::values.use_multi_core = sdl2_config->GetBoolean("Core", "use_multi_core", false);
  83. // Renderer
  84. Settings::values.resolution_factor =
  85. (float)sdl2_config->GetReal("Renderer", "resolution_factor", 1.0);
  86. Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true);
  87. Settings::values.frame_limit =
  88. static_cast<u16>(sdl2_config->GetInteger("Renderer", "frame_limit", 100));
  89. Settings::values.use_accurate_framebuffers =
  90. sdl2_config->GetBoolean("Renderer", "use_accurate_framebuffers", false);
  91. Settings::values.bg_red = (float)sdl2_config->GetReal("Renderer", "bg_red", 0.0);
  92. Settings::values.bg_green = (float)sdl2_config->GetReal("Renderer", "bg_green", 0.0);
  93. Settings::values.bg_blue = (float)sdl2_config->GetReal("Renderer", "bg_blue", 0.0);
  94. // Audio
  95. Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto");
  96. Settings::values.enable_audio_stretching =
  97. sdl2_config->GetBoolean("Audio", "enable_audio_stretching", true);
  98. Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto");
  99. Settings::values.volume = sdl2_config->GetReal("Audio", "volume", 1);
  100. // Data Storage
  101. Settings::values.use_virtual_sd =
  102. sdl2_config->GetBoolean("Data Storage", "use_virtual_sd", true);
  103. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir,
  104. sdl2_config->Get("Data Storage", "nand_directory",
  105. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir)));
  106. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir,
  107. sdl2_config->Get("Data Storage", "sdmc_directory",
  108. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir)));
  109. // System
  110. Settings::values.use_docked_mode = sdl2_config->GetBoolean("System", "use_docked_mode", false);
  111. Settings::values.username = sdl2_config->Get("System", "username", "yuzu");
  112. if (Settings::values.username.empty()) {
  113. Settings::values.username = "yuzu";
  114. }
  115. // Miscellaneous
  116. Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace");
  117. Settings::values.use_dev_keys = sdl2_config->GetBoolean("Miscellaneous", "use_dev_keys", false);
  118. // Debugging
  119. Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false);
  120. Settings::values.gdbstub_port =
  121. static_cast<u16>(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689));
  122. }
  123. void Config::Reload() {
  124. LoadINI(DefaultINI::sdl2_config_file);
  125. ReadValues();
  126. }