configure_system.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <chrono>
  6. #include <optional>
  7. #include <QFileDialog>
  8. #include <QGraphicsItem>
  9. #include <QMessageBox>
  10. #include "common/assert.h"
  11. #include "common/file_util.h"
  12. #include "core/core.h"
  13. #include "core/hle/service/time/time.h"
  14. #include "core/settings.h"
  15. #include "ui_configure_system.h"
  16. #include "yuzu/configuration/configuration_shared.h"
  17. #include "yuzu/configuration/configure_system.h"
  18. ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) {
  19. ui->setupUi(this);
  20. connect(ui->button_regenerate_console_id, &QPushButton::clicked, this,
  21. &ConfigureSystem::RefreshConsoleID);
  22. connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  23. ui->rng_seed_edit->setEnabled(state == Qt::Checked);
  24. if (state != Qt::Checked) {
  25. ui->rng_seed_edit->setText(QStringLiteral("00000000"));
  26. }
  27. });
  28. connect(ui->custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  29. ui->custom_rtc_edit->setEnabled(state == Qt::Checked);
  30. if (state != Qt::Checked) {
  31. ui->custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
  32. }
  33. });
  34. ui->label_console_id->setVisible(Settings::configuring_global);
  35. ui->button_regenerate_console_id->setVisible(Settings::configuring_global);
  36. SetupPerGameUI();
  37. SetConfiguration();
  38. }
  39. ConfigureSystem::~ConfigureSystem() = default;
  40. void ConfigureSystem::changeEvent(QEvent* event) {
  41. if (event->type() == QEvent::LanguageChange) {
  42. RetranslateUI();
  43. }
  44. QWidget::changeEvent(event);
  45. }
  46. void ConfigureSystem::RetranslateUI() {
  47. ui->retranslateUi(this);
  48. }
  49. void ConfigureSystem::SetConfiguration() {
  50. enabled = !Core::System::GetInstance().IsPoweredOn();
  51. const auto rng_seed =
  52. QStringLiteral("%1")
  53. .arg(Settings::values.rng_seed.GetValue().value_or(0), 8, 16, QLatin1Char{'0'})
  54. .toUpper();
  55. const auto rtc_time = Settings::values.custom_rtc.GetValue().value_or(
  56. std::chrono::seconds(QDateTime::currentSecsSinceEpoch()));
  57. ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.GetValue().has_value());
  58. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value() &&
  59. Settings::values.rng_seed.UsingGlobal());
  60. ui->rng_seed_edit->setText(rng_seed);
  61. ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.GetValue().has_value());
  62. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.GetValue().has_value() &&
  63. Settings::values.rng_seed.UsingGlobal());
  64. ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
  65. if (Settings::configuring_global) {
  66. ui->combo_language->setCurrentIndex(Settings::values.language_index.GetValue());
  67. ui->combo_region->setCurrentIndex(Settings::values.region_index.GetValue());
  68. ui->combo_time_zone->setCurrentIndex(Settings::values.time_zone_index.GetValue());
  69. ui->combo_sound->setCurrentIndex(Settings::values.sound_index.GetValue());
  70. } else {
  71. ConfigurationShared::SetPerGameSetting(ui->combo_language,
  72. &Settings::values.language_index);
  73. ConfigurationShared::SetPerGameSetting(ui->combo_region, &Settings::values.region_index);
  74. ConfigurationShared::SetPerGameSetting(ui->combo_time_zone,
  75. &Settings::values.time_zone_index);
  76. ConfigurationShared::SetPerGameSetting(ui->combo_sound, &Settings::values.sound_index);
  77. ConfigurationShared::SetHighlight(ui->label_language,
  78. !Settings::values.language_index.UsingGlobal());
  79. ConfigurationShared::SetHighlight(ui->label_region,
  80. !Settings::values.region_index.UsingGlobal());
  81. ConfigurationShared::SetHighlight(ui->label_timezone,
  82. !Settings::values.time_zone_index.UsingGlobal());
  83. ConfigurationShared::SetHighlight(ui->label_sound,
  84. !Settings::values.sound_index.UsingGlobal());
  85. }
  86. }
  87. void ConfigureSystem::ReadSystemSettings() {}
  88. void ConfigureSystem::ApplyConfiguration() {
  89. // Allow setting custom RTC even if system is powered on, to allow in-game time to be fast
  90. // forwared
  91. if (Settings::values.custom_rtc.UsingGlobal()) {
  92. if (ui->custom_rtc_checkbox->isChecked()) {
  93. Settings::values.custom_rtc.SetValue(
  94. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  95. if (Core::System::GetInstance().IsPoweredOn()) {
  96. const s64 posix_time{Settings::values.custom_rtc.GetValue()->count() +
  97. Service::Time::TimeManager::GetExternalTimeZoneOffset()};
  98. Core::System::GetInstance().GetTimeManager().UpdateLocalSystemClockTime(posix_time);
  99. }
  100. } else {
  101. Settings::values.custom_rtc.SetValue(std::nullopt);
  102. }
  103. }
  104. if (!enabled) {
  105. return;
  106. }
  107. if (Settings::configuring_global) {
  108. // Guard if during game and set to game-specific value
  109. if (Settings::values.language_index.UsingGlobal()) {
  110. Settings::values.language_index.SetValue(ui->combo_language->currentIndex());
  111. }
  112. if (Settings::values.region_index.UsingGlobal()) {
  113. Settings::values.region_index.SetValue(ui->combo_region->currentIndex());
  114. }
  115. if (Settings::values.time_zone_index.UsingGlobal()) {
  116. Settings::values.time_zone_index.SetValue(ui->combo_time_zone->currentIndex());
  117. }
  118. if (Settings::values.sound_index.UsingGlobal()) {
  119. Settings::values.sound_index.SetValue(ui->combo_sound->currentIndex());
  120. }
  121. if (Settings::values.rng_seed.UsingGlobal()) {
  122. if (ui->rng_seed_checkbox->isChecked()) {
  123. Settings::values.rng_seed.SetValue(
  124. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  125. } else {
  126. Settings::values.rng_seed.SetValue(std::nullopt);
  127. }
  128. }
  129. } else {
  130. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index,
  131. ui->combo_language);
  132. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  133. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  134. ui->combo_time_zone);
  135. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  136. switch (use_rng_seed) {
  137. case ConfigurationShared::CheckState::On:
  138. case ConfigurationShared::CheckState::Off:
  139. Settings::values.rng_seed.SetGlobal(false);
  140. if (ui->rng_seed_checkbox->isChecked()) {
  141. Settings::values.rng_seed.SetValue(
  142. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  143. } else {
  144. Settings::values.rng_seed.SetValue(std::nullopt);
  145. }
  146. break;
  147. case ConfigurationShared::CheckState::Global:
  148. Settings::values.rng_seed.SetGlobal(false);
  149. Settings::values.rng_seed.SetValue(std::nullopt);
  150. Settings::values.rng_seed.SetGlobal(true);
  151. break;
  152. case ConfigurationShared::CheckState::Count:
  153. break;
  154. }
  155. switch (use_custom_rtc) {
  156. case ConfigurationShared::CheckState::On:
  157. case ConfigurationShared::CheckState::Off:
  158. Settings::values.custom_rtc.SetGlobal(false);
  159. if (ui->custom_rtc_checkbox->isChecked()) {
  160. Settings::values.custom_rtc.SetValue(
  161. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  162. } else {
  163. Settings::values.custom_rtc.SetValue(std::nullopt);
  164. }
  165. break;
  166. case ConfigurationShared::CheckState::Global:
  167. Settings::values.custom_rtc.SetGlobal(false);
  168. Settings::values.custom_rtc.SetValue(std::nullopt);
  169. Settings::values.custom_rtc.SetGlobal(true);
  170. break;
  171. case ConfigurationShared::CheckState::Count:
  172. break;
  173. }
  174. }
  175. Settings::Apply();
  176. }
  177. void ConfigureSystem::RefreshConsoleID() {
  178. QMessageBox::StandardButton reply;
  179. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  180. "Your current virtual Switch will not be recoverable. "
  181. "This might have unexpected effects in games. This might fail, "
  182. "if you use an outdated config savegame. Continue?");
  183. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  184. QMessageBox::No | QMessageBox::Yes);
  185. if (reply == QMessageBox::No) {
  186. return;
  187. }
  188. u64 console_id{};
  189. ui->label_console_id->setText(
  190. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  191. }
  192. void ConfigureSystem::SetupPerGameUI() {
  193. if (Settings::configuring_global) {
  194. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  195. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  196. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  197. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  198. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  199. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  200. ui->custom_rtc_checkbox->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  201. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  202. return;
  203. }
  204. ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
  205. Settings::values.language_index.GetValue(true));
  206. ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region,
  207. Settings::values.region_index.GetValue(true));
  208. ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
  209. Settings::values.time_zone_index.GetValue(true));
  210. ConfigurationShared::SetColoredComboBox(ui->combo_sound, ui->label_sound,
  211. Settings::values.sound_index.GetValue(true));
  212. ConfigurationShared::SetColoredTristate(
  213. ui->rng_seed_checkbox, Settings::values.rng_seed.UsingGlobal(),
  214. Settings::values.rng_seed.GetValue().has_value(),
  215. Settings::values.rng_seed.GetValue(true).has_value(), use_rng_seed);
  216. ConfigurationShared::SetColoredTristate(
  217. ui->custom_rtc_checkbox, Settings::values.custom_rtc.UsingGlobal(),
  218. Settings::values.custom_rtc.GetValue().has_value(),
  219. Settings::values.custom_rtc.GetValue(true).has_value(), use_custom_rtc);
  220. }