config.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <sstream>
  6. #include <SDL.h>
  7. #include <inih/cpp/INIReader.h>
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/param_package.h"
  11. #include "core/hle/service/acc/profile_manager.h"
  12. #include "core/settings.h"
  13. #include "input_common/main.h"
  14. #include "yuzu_cmd/config.h"
  15. #include "yuzu_cmd/default_ini.h"
  16. Config::Config() {
  17. // TODO: Don't hardcode the path; let the frontend decide where to put the config files.
  18. sdl2_config_loc = FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "sdl2-config.ini";
  19. sdl2_config = std::make_unique<INIReader>(sdl2_config_loc);
  20. Reload();
  21. }
  22. Config::~Config() = default;
  23. bool Config::LoadINI(const std::string& default_contents, bool retry) {
  24. const std::string& location = this->sdl2_config_loc;
  25. if (sdl2_config->ParseError() < 0) {
  26. if (retry) {
  27. LOG_WARNING(Config, "Failed to load {}. Creating file from defaults...", location);
  28. FileUtil::CreateFullPath(location);
  29. FileUtil::WriteStringToFile(true, location, default_contents);
  30. sdl2_config = std::make_unique<INIReader>(location); // Reopen file
  31. return LoadINI(default_contents, false);
  32. }
  33. LOG_ERROR(Config, "Failed.");
  34. return false;
  35. }
  36. LOG_INFO(Config, "Successfully loaded {}", location);
  37. return true;
  38. }
  39. static const std::array<int, Settings::NativeButton::NumButtons> default_buttons = {
  40. SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_T,
  41. SDL_SCANCODE_G, SDL_SCANCODE_F, SDL_SCANCODE_H, SDL_SCANCODE_Q, SDL_SCANCODE_W,
  42. SDL_SCANCODE_M, SDL_SCANCODE_N, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_B,
  43. };
  44. static const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> default_analogs{{
  45. {
  46. SDL_SCANCODE_UP,
  47. SDL_SCANCODE_DOWN,
  48. SDL_SCANCODE_LEFT,
  49. SDL_SCANCODE_RIGHT,
  50. SDL_SCANCODE_D,
  51. },
  52. {
  53. SDL_SCANCODE_I,
  54. SDL_SCANCODE_K,
  55. SDL_SCANCODE_J,
  56. SDL_SCANCODE_L,
  57. SDL_SCANCODE_D,
  58. },
  59. }};
  60. static const std::array<int, Settings::NativeMouseButton::NumMouseButtons> default_mouse_buttons = {
  61. SDL_SCANCODE_LEFTBRACKET, SDL_SCANCODE_RIGHTBRACKET, SDL_SCANCODE_APOSTROPHE,
  62. SDL_SCANCODE_MINUS, SDL_SCANCODE_EQUALS,
  63. };
  64. static const std::array<int, 0x8A> keyboard_keys = {
  65. 0,
  66. 0,
  67. 0,
  68. 0,
  69. SDL_SCANCODE_A,
  70. SDL_SCANCODE_B,
  71. SDL_SCANCODE_C,
  72. SDL_SCANCODE_D,
  73. SDL_SCANCODE_E,
  74. SDL_SCANCODE_F,
  75. SDL_SCANCODE_G,
  76. SDL_SCANCODE_H,
  77. SDL_SCANCODE_I,
  78. SDL_SCANCODE_J,
  79. SDL_SCANCODE_K,
  80. SDL_SCANCODE_L,
  81. SDL_SCANCODE_M,
  82. SDL_SCANCODE_N,
  83. SDL_SCANCODE_O,
  84. SDL_SCANCODE_P,
  85. SDL_SCANCODE_Q,
  86. SDL_SCANCODE_R,
  87. SDL_SCANCODE_S,
  88. SDL_SCANCODE_T,
  89. SDL_SCANCODE_U,
  90. SDL_SCANCODE_V,
  91. SDL_SCANCODE_W,
  92. SDL_SCANCODE_X,
  93. SDL_SCANCODE_Y,
  94. SDL_SCANCODE_Z,
  95. SDL_SCANCODE_1,
  96. SDL_SCANCODE_2,
  97. SDL_SCANCODE_3,
  98. SDL_SCANCODE_4,
  99. SDL_SCANCODE_5,
  100. SDL_SCANCODE_6,
  101. SDL_SCANCODE_7,
  102. SDL_SCANCODE_8,
  103. SDL_SCANCODE_9,
  104. SDL_SCANCODE_0,
  105. SDL_SCANCODE_RETURN,
  106. SDL_SCANCODE_ESCAPE,
  107. SDL_SCANCODE_BACKSPACE,
  108. SDL_SCANCODE_TAB,
  109. SDL_SCANCODE_SPACE,
  110. SDL_SCANCODE_MINUS,
  111. SDL_SCANCODE_EQUALS,
  112. SDL_SCANCODE_LEFTBRACKET,
  113. SDL_SCANCODE_RIGHTBRACKET,
  114. SDL_SCANCODE_BACKSLASH,
  115. 0,
  116. SDL_SCANCODE_SEMICOLON,
  117. SDL_SCANCODE_APOSTROPHE,
  118. SDL_SCANCODE_GRAVE,
  119. SDL_SCANCODE_COMMA,
  120. SDL_SCANCODE_PERIOD,
  121. SDL_SCANCODE_SLASH,
  122. SDL_SCANCODE_CAPSLOCK,
  123. SDL_SCANCODE_F1,
  124. SDL_SCANCODE_F2,
  125. SDL_SCANCODE_F3,
  126. SDL_SCANCODE_F4,
  127. SDL_SCANCODE_F5,
  128. SDL_SCANCODE_F6,
  129. SDL_SCANCODE_F7,
  130. SDL_SCANCODE_F8,
  131. SDL_SCANCODE_F9,
  132. SDL_SCANCODE_F10,
  133. SDL_SCANCODE_F11,
  134. SDL_SCANCODE_F12,
  135. 0,
  136. SDL_SCANCODE_SCROLLLOCK,
  137. SDL_SCANCODE_PAUSE,
  138. SDL_SCANCODE_INSERT,
  139. SDL_SCANCODE_HOME,
  140. SDL_SCANCODE_PAGEUP,
  141. SDL_SCANCODE_DELETE,
  142. SDL_SCANCODE_END,
  143. SDL_SCANCODE_PAGEDOWN,
  144. SDL_SCANCODE_RIGHT,
  145. SDL_SCANCODE_LEFT,
  146. SDL_SCANCODE_DOWN,
  147. SDL_SCANCODE_UP,
  148. SDL_SCANCODE_NUMLOCKCLEAR,
  149. SDL_SCANCODE_KP_DIVIDE,
  150. SDL_SCANCODE_KP_MULTIPLY,
  151. SDL_SCANCODE_KP_MINUS,
  152. SDL_SCANCODE_KP_PLUS,
  153. SDL_SCANCODE_KP_ENTER,
  154. SDL_SCANCODE_KP_1,
  155. SDL_SCANCODE_KP_2,
  156. SDL_SCANCODE_KP_3,
  157. SDL_SCANCODE_KP_4,
  158. SDL_SCANCODE_KP_5,
  159. SDL_SCANCODE_KP_6,
  160. SDL_SCANCODE_KP_7,
  161. SDL_SCANCODE_KP_8,
  162. SDL_SCANCODE_KP_9,
  163. SDL_SCANCODE_KP_0,
  164. SDL_SCANCODE_KP_PERIOD,
  165. 0,
  166. 0,
  167. SDL_SCANCODE_POWER,
  168. SDL_SCANCODE_KP_EQUALS,
  169. SDL_SCANCODE_F13,
  170. SDL_SCANCODE_F14,
  171. SDL_SCANCODE_F15,
  172. SDL_SCANCODE_F16,
  173. SDL_SCANCODE_F17,
  174. SDL_SCANCODE_F18,
  175. SDL_SCANCODE_F19,
  176. SDL_SCANCODE_F20,
  177. SDL_SCANCODE_F21,
  178. SDL_SCANCODE_F22,
  179. SDL_SCANCODE_F23,
  180. SDL_SCANCODE_F24,
  181. 0,
  182. SDL_SCANCODE_HELP,
  183. SDL_SCANCODE_MENU,
  184. 0,
  185. 0,
  186. 0,
  187. 0,
  188. 0,
  189. 0,
  190. 0,
  191. 0,
  192. 0,
  193. 0,
  194. 0,
  195. 0,
  196. SDL_SCANCODE_KP_COMMA,
  197. SDL_SCANCODE_KP_LEFTPAREN,
  198. SDL_SCANCODE_KP_RIGHTPAREN,
  199. 0,
  200. 0,
  201. 0,
  202. 0,
  203. };
  204. static const std::array<int, 8> keyboard_mods{
  205. SDL_SCANCODE_LCTRL, SDL_SCANCODE_LSHIFT, SDL_SCANCODE_LALT, SDL_SCANCODE_LGUI,
  206. SDL_SCANCODE_RCTRL, SDL_SCANCODE_RSHIFT, SDL_SCANCODE_RALT, SDL_SCANCODE_RGUI,
  207. };
  208. void Config::ReadValues() {
  209. // Controls
  210. for (std::size_t p = 0; p < Settings::values.players.size(); ++p) {
  211. const auto group = fmt::format("ControlsP{}", p);
  212. for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
  213. std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
  214. Settings::values.players[p].buttons[i] =
  215. sdl2_config->Get(group, Settings::NativeButton::mapping[i], default_param);
  216. if (Settings::values.players[p].buttons[i].empty())
  217. Settings::values.players[p].buttons[i] = default_param;
  218. }
  219. for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
  220. std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
  221. default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
  222. default_analogs[i][3], default_analogs[i][4], 0.5f);
  223. Settings::values.players[p].analogs[i] =
  224. sdl2_config->Get(group, Settings::NativeAnalog::mapping[i], default_param);
  225. if (Settings::values.players[p].analogs[i].empty())
  226. Settings::values.players[p].analogs[i] = default_param;
  227. }
  228. }
  229. Settings::values.mouse_enabled =
  230. sdl2_config->GetBoolean("ControlsGeneral", "mouse_enabled", false);
  231. for (int i = 0; i < Settings::NativeMouseButton::NumMouseButtons; ++i) {
  232. std::string default_param = InputCommon::GenerateKeyboardParam(default_mouse_buttons[i]);
  233. Settings::values.mouse_buttons[i] = sdl2_config->Get(
  234. "ControlsGeneral", std::string("mouse_") + Settings::NativeMouseButton::mapping[i],
  235. default_param);
  236. if (Settings::values.mouse_buttons[i].empty())
  237. Settings::values.mouse_buttons[i] = default_param;
  238. }
  239. Settings::values.motion_device = sdl2_config->Get(
  240. "ControlsGeneral", "motion_device", "engine:motion_emu,update_period:100,sensitivity:0.01");
  241. Settings::values.keyboard_enabled =
  242. sdl2_config->GetBoolean("ControlsGeneral", "keyboard_enabled", false);
  243. Settings::values.debug_pad_enabled =
  244. sdl2_config->GetBoolean("ControlsGeneral", "debug_pad_enabled", false);
  245. for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
  246. std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
  247. Settings::values.debug_pad_buttons[i] = sdl2_config->Get(
  248. "ControlsGeneral", std::string("debug_pad_") + Settings::NativeButton::mapping[i],
  249. default_param);
  250. if (Settings::values.debug_pad_buttons[i].empty())
  251. Settings::values.debug_pad_buttons[i] = default_param;
  252. }
  253. for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
  254. std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
  255. default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
  256. default_analogs[i][3], default_analogs[i][4], 0.5f);
  257. Settings::values.debug_pad_analogs[i] = sdl2_config->Get(
  258. "ControlsGeneral", std::string("debug_pad_") + Settings::NativeAnalog::mapping[i],
  259. default_param);
  260. if (Settings::values.debug_pad_analogs[i].empty())
  261. Settings::values.debug_pad_analogs[i] = default_param;
  262. }
  263. Settings::values.touchscreen.enabled =
  264. sdl2_config->GetBoolean("ControlsGeneral", "touch_enabled", true);
  265. Settings::values.touchscreen.device =
  266. sdl2_config->Get("ControlsGeneral", "touch_device", "engine:emu_window");
  267. Settings::values.touchscreen.finger =
  268. sdl2_config->GetInteger("ControlsGeneral", "touch_finger", 0);
  269. Settings::values.touchscreen.rotation_angle =
  270. sdl2_config->GetInteger("ControlsGeneral", "touch_angle", 0);
  271. Settings::values.touchscreen.diameter_x =
  272. sdl2_config->GetInteger("ControlsGeneral", "touch_diameter_x", 15);
  273. Settings::values.touchscreen.diameter_y =
  274. sdl2_config->GetInteger("ControlsGeneral", "touch_diameter_y", 15);
  275. std::transform(keyboard_keys.begin(), keyboard_keys.end(),
  276. Settings::values.keyboard_keys.begin(), InputCommon::GenerateKeyboardParam);
  277. std::transform(keyboard_mods.begin(), keyboard_mods.end(),
  278. Settings::values.keyboard_keys.begin() +
  279. Settings::NativeKeyboard::LeftControlKey,
  280. InputCommon::GenerateKeyboardParam);
  281. std::transform(keyboard_mods.begin(), keyboard_mods.end(),
  282. Settings::values.keyboard_mods.begin(), InputCommon::GenerateKeyboardParam);
  283. // Data Storage
  284. Settings::values.use_virtual_sd =
  285. sdl2_config->GetBoolean("Data Storage", "use_virtual_sd", true);
  286. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir,
  287. sdl2_config->Get("Data Storage", "nand_directory",
  288. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir)));
  289. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir,
  290. sdl2_config->Get("Data Storage", "sdmc_directory",
  291. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir)));
  292. // System
  293. Settings::values.use_docked_mode = sdl2_config->GetBoolean("System", "use_docked_mode", false);
  294. const auto size = sdl2_config->GetInteger("System", "users_size", 0);
  295. Settings::values.current_user = std::clamp<int>(
  296. sdl2_config->GetInteger("System", "current_user", 0), 0, Service::Account::MAX_USERS - 1);
  297. const auto rng_seed_enabled = sdl2_config->GetBoolean("System", "rng_seed_enabled", false);
  298. if (rng_seed_enabled) {
  299. Settings::values.rng_seed = sdl2_config->GetInteger("System", "rng_seed", 0);
  300. } else {
  301. Settings::values.rng_seed = std::nullopt;
  302. }
  303. const auto custom_rtc_enabled = sdl2_config->GetBoolean("System", "custom_rtc_enabled", false);
  304. if (custom_rtc_enabled) {
  305. Settings::values.custom_rtc =
  306. std::chrono::seconds(sdl2_config->GetInteger("System", "custom_rtc", 0));
  307. } else {
  308. Settings::values.custom_rtc = std::nullopt;
  309. }
  310. // Core
  311. Settings::values.use_cpu_jit = sdl2_config->GetBoolean("Core", "use_cpu_jit", true);
  312. Settings::values.use_multi_core = sdl2_config->GetBoolean("Core", "use_multi_core", false);
  313. // Renderer
  314. Settings::values.resolution_factor =
  315. static_cast<float>(sdl2_config->GetReal("Renderer", "resolution_factor", 1.0));
  316. Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true);
  317. Settings::values.frame_limit =
  318. static_cast<u16>(sdl2_config->GetInteger("Renderer", "frame_limit", 100));
  319. Settings::values.use_compatibility_profile =
  320. sdl2_config->GetBoolean("Renderer", "use_compatibility_profile", true);
  321. Settings::values.use_disk_shader_cache =
  322. sdl2_config->GetBoolean("Renderer", "use_disk_shader_cache", false);
  323. Settings::values.use_accurate_gpu_emulation =
  324. sdl2_config->GetBoolean("Renderer", "use_accurate_gpu_emulation", false);
  325. Settings::values.use_asynchronous_gpu_emulation =
  326. sdl2_config->GetBoolean("Renderer", "use_asynchronous_gpu_emulation", false);
  327. Settings::values.bg_red = static_cast<float>(sdl2_config->GetReal("Renderer", "bg_red", 0.0));
  328. Settings::values.bg_green =
  329. static_cast<float>(sdl2_config->GetReal("Renderer", "bg_green", 0.0));
  330. Settings::values.bg_blue = static_cast<float>(sdl2_config->GetReal("Renderer", "bg_blue", 0.0));
  331. // Audio
  332. Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto");
  333. Settings::values.enable_audio_stretching =
  334. sdl2_config->GetBoolean("Audio", "enable_audio_stretching", true);
  335. Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto");
  336. Settings::values.volume = static_cast<float>(sdl2_config->GetReal("Audio", "volume", 1));
  337. Settings::values.language_index = sdl2_config->GetInteger("System", "language_index", 1);
  338. // Miscellaneous
  339. Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace");
  340. Settings::values.use_dev_keys = sdl2_config->GetBoolean("Miscellaneous", "use_dev_keys", false);
  341. // Debugging
  342. Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false);
  343. Settings::values.gdbstub_port =
  344. static_cast<u16>(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689));
  345. Settings::values.program_args = sdl2_config->Get("Debugging", "program_args", "");
  346. Settings::values.dump_exefs = sdl2_config->GetBoolean("Debugging", "dump_exefs", false);
  347. Settings::values.dump_nso = sdl2_config->GetBoolean("Debugging", "dump_nso", false);
  348. const auto title_list = sdl2_config->Get("AddOns", "title_ids", "");
  349. std::stringstream ss(title_list);
  350. std::string line;
  351. while (std::getline(ss, line, '|')) {
  352. const auto title_id = std::stoul(line, nullptr, 16);
  353. const auto disabled_list = sdl2_config->Get("AddOns", "disabled_" + line, "");
  354. std::stringstream inner_ss(disabled_list);
  355. std::string inner_line;
  356. std::vector<std::string> out;
  357. while (std::getline(inner_ss, inner_line, '|')) {
  358. out.push_back(inner_line);
  359. }
  360. Settings::values.disabled_addons.insert_or_assign(title_id, out);
  361. }
  362. // Web Service
  363. Settings::values.enable_telemetry =
  364. sdl2_config->GetBoolean("WebService", "enable_telemetry", true);
  365. Settings::values.web_api_url =
  366. sdl2_config->Get("WebService", "web_api_url", "https://api.yuzu-emu.org");
  367. Settings::values.yuzu_username = sdl2_config->Get("WebService", "yuzu_username", "");
  368. Settings::values.yuzu_token = sdl2_config->Get("WebService", "yuzu_token", "");
  369. }
  370. void Config::Reload() {
  371. LoadINI(DefaultINI::sdl2_config_file);
  372. ReadValues();
  373. }