From f7f6f91a478578cd29507aea9599e5e6458fd115 Mon Sep 17 00:00:00 2001 From: J-D-K Date: Tue, 7 Oct 2025 10:42:09 -0400 Subject: [PATCH] Thread remote initialization. --- Makefile | 2 +- include/builddate.hpp | 2 +- include/remote/remote.hpp | 8 +++----- source/JKSV.cpp | 9 ++------- source/remote/remote.cpp | 36 +++++++++++++++++++++++++----------- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 6e4d6cd..b83a070 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ INCLUDES := include ./Libraries/FsLib/Switch/FsLib/include ./Libraries/SDLLib/SD EXEFS_SRC := exefs_src APP_TITLE := JKSV APP_AUTHOR := JK -APP_VERSION := 10.02.2025 +APP_VERSION := 10.07.2025 ROMFS := romfs ICON := icon.jpg diff --git a/include/builddate.hpp b/include/builddate.hpp index c0d1c5b..0c9b6db 100644 --- a/include/builddate.hpp +++ b/include/builddate.hpp @@ -3,6 +3,6 @@ namespace builddate { inline constexpr int MONTH = 10; - inline constexpr int DAY = 2; + inline constexpr int DAY = 8; inline constexpr int YEAR = 2025; } diff --git a/include/remote/remote.hpp b/include/remote/remote.hpp index df7c224..a1894a4 100644 --- a/include/remote/remote.hpp +++ b/include/remote/remote.hpp @@ -1,5 +1,6 @@ #pragma once #include "remote/Storage.hpp" +#include "sys/threadpool.hpp" #include @@ -12,11 +13,8 @@ namespace remote /// @brief Returns whether or not the console has an active internet connection. bool has_internet_connection() noexcept; - /// @brief Initializes the Storage instance to Google Drive. - void initialize_google_drive(); - - /// @brief Initializes the Storage instance to WebDav - void initialize_webdav(); + /// @brief Initializes the remote service according to the config on the sdmc. + void initialize(sys::threadpool::JobData jobData); /// @brief Returns the pointer to the Storage instance. remote::Storage *get_remote_storage() noexcept; diff --git a/source/JKSV.cpp b/source/JKSV.cpp index 4e28455..e976e54 100644 --- a/source/JKSV.cpp +++ b/source/JKSV.cpp @@ -83,6 +83,7 @@ JKSV::JKSV() JKSV::create_directories(); sys::threadpool::initialize(); + sys::threadpool::push_job(remote::initialize, nullptr); data::launch_initialization(false, finish_initialization); FadeState::create_and_push(colors::BLACK, 0xFF, 0x00, nullptr); @@ -221,10 +222,4 @@ void JKSV::exit_services() accountExit(); } -static void finish_initialization() -{ - MainMenuState::create_and_push(); - - if (fslib::file_exists(remote::PATH_GOOGLE_DRIVE_CONFIG)) { remote::initialize_google_drive(); } - else if (fslib::file_exists(remote::PATH_WEBDAV_CONFIG)) { remote::initialize_webdav(); } -} +static void finish_initialization() { MainMenuState::create_and_push(); } diff --git a/source/remote/remote.cpp b/source/remote/remote.cpp index 131c04d..708573c 100644 --- a/source/remote/remote.cpp +++ b/source/remote/remote.cpp @@ -3,6 +3,7 @@ #include "StateManager.hpp" #include "appstates/TaskState.hpp" #include "error.hpp" +#include "input.hpp" #include "logging/logger.hpp" #include "remote/GoogleDrive.hpp" #include "remote/WebDav.hpp" @@ -28,15 +29,17 @@ namespace remote::GoogleDrive *drive{}; }; // clang-format on - } // namespace +static void initialize_google_drive(); +static void initialize_webdav(); + // Declarations here. Definitions at bottom. /// @brief This is the thread function that handles logging into Google. static void drive_sign_in(sys::threadpool::JobData taskData); /// @brief This creates (if needed) the JKSV folder for Google Drive and sets it as the root. -/// @param drive Pointer to the drive instance.. +/// @param drive Pointer to the drive instance. static void drive_set_jksv_root(remote::GoogleDrive *drive); bool remote::has_internet_connection() noexcept @@ -49,17 +52,25 @@ bool remote::has_internet_connection() noexcept return true; } -void remote::initialize_google_drive() +void remote::initialize(sys::threadpool::JobData jobData) { - const int popTicks = ui::PopMessageManager::DEFAULT_TICKS; - - if (!remote::has_internet_connection()) + const bool driveExists = fslib::file_exists(remote::PATH_GOOGLE_DRIVE_CONFIG); + const bool webdavExists = fslib::file_exists(remote::PATH_WEBDAV_CONFIG); + if ((driveExists || webdavExists) && !remote::has_internet_connection()) { const char *popNoInternet = strings::get_by_name(strings::names::REMOTE_POPS, 0); - ui::PopMessageManager::push_message(popTicks, popNoInternet); + ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_TICKS, popNoInternet); return; } + if (driveExists) { initialize_google_drive(); } + else if (webdavExists) { initialize_webdav(); } +} + +void initialize_google_drive() +{ + const int popTicks = ui::PopMessageManager::DEFAULT_TICKS; + s_storage = std::make_unique(); remote::GoogleDrive *drive = static_cast(s_storage.get()); if (drive->sign_in_required()) @@ -67,7 +78,8 @@ void remote::initialize_google_drive() auto driveStruct = std::make_shared(); driveStruct->drive = drive; - TaskState::create_and_push(drive_sign_in, driveStruct); + // To do: StateManager isn't thread safe. This might/probably will cause data race randomly. + TaskState::create_push_fade(drive_sign_in, driveStruct); return; } @@ -79,10 +91,8 @@ void remote::initialize_google_drive() ui::PopMessageManager::push_message(popTicks, popDriveSuccess); } -void remote::initialize_webdav() +void initialize_webdav() { - if (!remote::has_internet_connection()) { return; } - s_storage = std::make_unique(); const int popTicks = ui::PopMessageManager::DEFAULT_TICKS; if (s_storage->is_initialized()) @@ -126,6 +136,10 @@ static void drive_sign_in(sys::threadpool::JobData taskData) while (std::time(NULL) < expiration && !drive->poll_sign_in(deviceCode)) { + const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool bHeld = input::button_held(HidNpadButton_B); + if (bPressed || bHeld) { break; } + std::this_thread::sleep_for(std::chrono::seconds(pollingInterval)); }