config.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "citra/config.h"
  8. #include "citra/default_ini.h"
  9. #include "common/file_util.h"
  10. #include "common/logging/log.h"
  11. #include "common/param_package.h"
  12. #include "core/settings.h"
  13. #include "input_common/main.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, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_D,
  45. },
  46. {
  47. SDL_SCANCODE_I, SDL_SCANCODE_K, SDL_SCANCODE_J, SDL_SCANCODE_L, SDL_SCANCODE_D,
  48. },
  49. }};
  50. void Config::ReadValues() {
  51. // Controls
  52. for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
  53. std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
  54. Settings::values.buttons[i] =
  55. sdl2_config->Get("Controls", Settings::NativeButton::mapping[i], default_param);
  56. if (Settings::values.buttons[i].empty())
  57. Settings::values.buttons[i] = default_param;
  58. }
  59. for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
  60. std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
  61. default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
  62. default_analogs[i][3], default_analogs[i][4], 0.5f);
  63. Settings::values.analogs[i] =
  64. sdl2_config->Get("Controls", Settings::NativeAnalog::mapping[i], default_param);
  65. if (Settings::values.analogs[i].empty())
  66. Settings::values.analogs[i] = default_param;
  67. }
  68. // Core
  69. Settings::values.use_cpu_jit = sdl2_config->GetBoolean("Core", "use_cpu_jit", true);
  70. // Renderer
  71. Settings::values.use_hw_renderer = sdl2_config->GetBoolean("Renderer", "use_hw_renderer", true);
  72. Settings::values.use_shader_jit = sdl2_config->GetBoolean("Renderer", "use_shader_jit", true);
  73. Settings::values.resolution_factor =
  74. (float)sdl2_config->GetReal("Renderer", "resolution_factor", 1.0);
  75. Settings::values.use_vsync = sdl2_config->GetBoolean("Renderer", "use_vsync", false);
  76. Settings::values.toggle_framelimit =
  77. sdl2_config->GetBoolean("Renderer", "toggle_framelimit", true);
  78. Settings::values.bg_red = (float)sdl2_config->GetReal("Renderer", "bg_red", 1.0);
  79. Settings::values.bg_green = (float)sdl2_config->GetReal("Renderer", "bg_green", 1.0);
  80. Settings::values.bg_blue = (float)sdl2_config->GetReal("Renderer", "bg_blue", 1.0);
  81. // Layout
  82. Settings::values.layout_option =
  83. static_cast<Settings::LayoutOption>(sdl2_config->GetInteger("Layout", "layout_option", 0));
  84. Settings::values.swap_screen = sdl2_config->GetBoolean("Layout", "swap_screen", false);
  85. Settings::values.custom_layout = sdl2_config->GetBoolean("Layout", "custom_layout", false);
  86. Settings::values.custom_top_left =
  87. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_top_left", 0));
  88. Settings::values.custom_top_top =
  89. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_top_top", 0));
  90. Settings::values.custom_top_right =
  91. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_top_right", 400));
  92. Settings::values.custom_top_bottom =
  93. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_top_bottom", 240));
  94. Settings::values.custom_bottom_left =
  95. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_bottom_left", 40));
  96. Settings::values.custom_bottom_top =
  97. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_bottom_top", 240));
  98. Settings::values.custom_bottom_right =
  99. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_bottom_right", 360));
  100. Settings::values.custom_bottom_bottom =
  101. static_cast<u16>(sdl2_config->GetInteger("Layout", "custom_bottom_bottom", 480));
  102. // Audio
  103. Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto");
  104. Settings::values.enable_audio_stretching =
  105. sdl2_config->GetBoolean("Audio", "enable_audio_stretching", true);
  106. Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto");
  107. // Data Storage
  108. Settings::values.use_virtual_sd =
  109. sdl2_config->GetBoolean("Data Storage", "use_virtual_sd", true);
  110. // System
  111. Settings::values.is_new_3ds = sdl2_config->GetBoolean("System", "is_new_3ds", false);
  112. Settings::values.region_value =
  113. sdl2_config->GetInteger("System", "region_value", Settings::REGION_VALUE_AUTO_SELECT);
  114. // Camera
  115. using namespace Service::CAM;
  116. Settings::values.camera_name[OuterRightCamera] =
  117. sdl2_config->Get("Camera", "camera_outer_right_name", "blank");
  118. Settings::values.camera_config[OuterRightCamera] =
  119. sdl2_config->Get("Camera", "camera_outer_right_config", "");
  120. Settings::values.camera_name[InnerCamera] =
  121. sdl2_config->Get("Camera", "camera_inner_name", "blank");
  122. Settings::values.camera_config[InnerCamera] =
  123. sdl2_config->Get("Camera", "camera_inner_config", "");
  124. Settings::values.camera_name[OuterLeftCamera] =
  125. sdl2_config->Get("Camera", "camera_outer_left_name", "blank");
  126. Settings::values.camera_config[OuterLeftCamera] =
  127. sdl2_config->Get("Camera", "camera_outer_left_config", "");
  128. // Miscellaneous
  129. Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Info");
  130. // Debugging
  131. Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false);
  132. Settings::values.gdbstub_port =
  133. static_cast<u16>(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689));
  134. }
  135. void Config::Reload() {
  136. LoadINI(DefaultINI::sdl2_config_file);
  137. ReadValues();
  138. }