From de4a28b713ac560c4d79217e841cd5f568a3ef9e Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sat, 11 Jul 2020 13:37:34 +1000 Subject: [PATCH] daybreak: support resetting to factory settings (cherry picked from commit 1c0e196eae91cfd85f63064c36cc288a0ea0363f) --- troposphere/daybreak/source/main.cpp | 5 ++ troposphere/daybreak/source/ui.cpp | 83 ++++++++++++++++++++++++++-- troposphere/daybreak/source/ui.hpp | 15 +++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/troposphere/daybreak/source/main.cpp b/troposphere/daybreak/source/main.cpp index 268808366..0eb1d04bc 100644 --- a/troposphere/daybreak/source/main.cpp +++ b/troposphere/daybreak/source/main.cpp @@ -42,6 +42,10 @@ extern "C" { fatalThrow(rc); } + if (R_FAILED(rc = nsInitialize())) { + fatalThrow(rc); + } + if (R_FAILED(rc = hiddbgInitialize())) { fatalThrow(rc); } @@ -50,6 +54,7 @@ extern "C" { void userAppExit(void) { hiddbgExit(); + nsExit(); splExit(); plExit(); spsmExit(); diff --git a/troposphere/daybreak/source/ui.cpp b/troposphere/daybreak/source/ui.cpp index a1b4faf3a..12306a68a 100644 --- a/troposphere/daybreak/source/ui.cpp +++ b/troposphere/daybreak/source/ui.cpp @@ -60,6 +60,8 @@ namespace dbk { /* Update install state. */ char g_update_path[FS_MAX_PATH]; + bool g_reset_to_factory = false; + bool g_exfat_supported = false; bool g_use_exfat = false; constexpr u32 MaxTapMovement = 20; @@ -864,13 +866,13 @@ namespace dbk { break; } - if (m_update_info.exfat_supported) { - ChangeMenu(std::make_shared(g_current_menu)); - } else { + /* Check if exfat is supported. */ + g_exfat_supported = m_update_info.exfat_supported; + if (!g_exfat_supported) { g_use_exfat = false; - ChangeMenu(std::make_shared(g_current_menu, std::make_shared(g_current_menu), "Ready to begin update installation", "Are you sure you want to proceed?")); } + ChangeMenu(std::make_shared(g_current_menu)); return; } } @@ -890,6 +892,60 @@ namespace dbk { m_has_drawn = true; } + ChooseResetMenu::ChooseResetMenu(std::shared_ptr prev_menu) : Menu(prev_menu) { + const float x = g_screen_width / 2.0f - WindowWidth / 2.0f; + const float y = g_screen_height / 2.0f - WindowHeight / 2.0f; + const float button_width = (WindowWidth - HorizontalInset * 2.0f) / 2.0f - ButtonHorizontalGap; + + /* Add buttons. */ + this->AddButton(ResetToFactorySettingsButtonId, "Reset to factory settings", x + HorizontalInset, y + TitleGap, button_width, ButtonHeight); + this->AddButton(PreserveSettingsButtonId, "Preserve settings", x + HorizontalInset + button_width + ButtonHorizontalGap, y + TitleGap, button_width, ButtonHeight); + this->SetButtonSelected(PreserveSettingsButtonId, true); + } + + void ChooseResetMenu::Update(u64 ns) { + u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + + /* Go back if B is pressed. */ + if (k_down & KEY_B) { + ReturnToPreviousMenu(); + return; + } + + /* Take action if a button has been activated. */ + if (const Button *activated_button = this->GetActivatedButton(); activated_button != nullptr) { + switch (activated_button->id) { + case ResetToFactorySettingsButtonId: + g_reset_to_factory = true; + break; + case PreserveSettingsButtonId: + g_reset_to_factory = false; + break; + } + + if (g_exfat_supported) { + ChangeMenu(std::make_shared(g_current_menu)); + } else { + ChangeMenu(std::make_shared(g_current_menu, std::make_shared(g_current_menu), "Ready to begin update installation", "Are you sure you want to proceed?")); + } + } + + this->UpdateButtons(); + + /* Fallback on selecting the exfat button. */ + if (const Button *selected_button = this->GetSelectedButton(); k_down && selected_button == nullptr) { + this->SetButtonSelected(PreserveSettingsButtonId, true); + } + } + + void ChooseResetMenu::Draw(NVGcontext *vg, u64 ns) { + const float x = g_screen_width / 2.0f - WindowWidth / 2.0f; + const float y = g_screen_height / 2.0f - WindowHeight / 2.0f; + + DrawWindow(vg, "Select settings mode", x, y, WindowWidth, WindowHeight); + this->DrawButtons(vg, ns); + } + ChooseExfatMenu::ChooseExfatMenu(std::shared_ptr prev_menu) : Menu(prev_menu) { const float x = g_screen_width / 2.0f - WindowWidth / 2.0f; const float y = g_screen_height / 2.0f - WindowHeight / 2.0f; @@ -1045,6 +1101,25 @@ namespace dbk { } else { /* Log success. */ this->LogText("Update applied successfully.\n"); + + if (g_reset_to_factory) { + if (R_FAILED(rc = nsResetToFactorySettingsForRefurbishment())) { + /* Fallback on ResetToFactorySettings. */ + if (rc == MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer)) { + if (R_FAILED(rc = nsResetToFactorySettings())) { + this->LogText("Failed to reset to factory settings.\nResult: 0x%08x\n", rc); + this->MarkForReboot(); + return rc; + } + } else { + this->LogText("Failed to reset to factory settings for refurbishment.\nResult: 0x%08x\n", rc); + this->MarkForReboot(); + return rc; + } + } + + this->LogText("Successfully reset to factory settings.\n", rc); + } } this->MarkForReboot(); diff --git a/troposphere/daybreak/source/ui.hpp b/troposphere/daybreak/source/ui.hpp index 61ef81e50..481ad4069 100644 --- a/troposphere/daybreak/source/ui.hpp +++ b/troposphere/daybreak/source/ui.hpp @@ -197,6 +197,21 @@ namespace dbk { virtual void Draw(NVGcontext *vg, u64 ns) override; }; + class ChooseResetMenu : public Menu { + private: + static constexpr u32 ResetToFactorySettingsButtonId = 0; + static constexpr u32 PreserveSettingsButtonId = 1; + + static constexpr float WindowWidth = 600.0f; + static constexpr float WindowHeight = 170.0f; + static constexpr float TitleGap = 90.0f; + public: + ChooseResetMenu(std::shared_ptr prev_menu); + + virtual void Update(u64 ns) override; + virtual void Draw(NVGcontext *vg, u64 ns) override; + }; + class ChooseExfatMenu : public Menu { private: static constexpr u32 Fat32ButtonId = 0;