From 841fd73fbf76d8dcdff2c7431b326cb5aefbd1fe Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Thu, 29 Jul 2021 12:48:32 -0400 Subject: [PATCH] utils: implement utilsSetLongRunningProcessState(). Other changes include: * DownloadTask: use utilsSetLongRunningProcessState() to control HOME button block status, screen dimming and auto sleep. * Add missing utilsCommitSdCardFileSystemChanges() calls throughout the codebase. --- include/core/nxdt_utils.h | 5 ++-- include/download_task.hpp | 11 ++++++++- source/core/http.c | 3 +++ source/core/nxdt_bfsar.c | 8 ++++++- source/core/nxdt_utils.c | 49 ++++++++++++++++++++++----------------- source/core/save.c | 7 +++++- 6 files changed, 57 insertions(+), 26 deletions(-) diff --git a/include/core/nxdt_utils.h b/include/core/nxdt_utils.h index 7168055..3856e16 100644 --- a/include/core/nxdt_utils.h +++ b/include/core/nxdt_utils.h @@ -85,8 +85,9 @@ FsStorage *utilsGetEmmcBisSystemPartitionStorage(void); /// Enables/disables CPU/MEM overclocking. void utilsOverclockSystem(bool overclock); -/// (Un)blocks HOME button presses. -void utilsChangeHomeButtonBlockStatus(bool block); +/// (Un)blocks HOME button presses and (un)sets screen dimming and auto sleep. +/// Must be called before starting long-running processes. +void utilsSetLongRunningProcessState(bool state); /// Thread management functions. bool utilsCreateThread(Thread *out_thread, ThreadFunc func, void *arg, int cpu_id); diff --git a/include/download_task.hpp b/include/download_task.hpp index db10902..6a1ba1f 100644 --- a/include/download_task.hpp +++ b/include/download_task.hpp @@ -26,7 +26,7 @@ #include -#include "defines.h" +#include "core/nxdt_utils.h" #include "async_task.hpp" namespace nxdt::tasks @@ -177,6 +177,9 @@ namespace nxdt::tasks /* Pause task handler. */ this->task_handler->pause(); + + /* Unset long running process state. */ + utilsSetLongRunningProcessState(false); } template @@ -190,11 +193,17 @@ namespace nxdt::tasks /* Update progress one last time. */ this->onProgressUpdate(this->getProgress()); + + /* Unset long running process state. */ + utilsSetLongRunningProcessState(false); } template void DownloadTask::onPreExecute(void) { + /* Set long running process state. */ + utilsSetLongRunningProcessState(true); + /* Start task handler. */ this->task_handler->start(); diff --git a/source/core/http.c b/source/core/http.c index bd0b71b..4bd3105 100644 --- a/source/core/http.c +++ b/source/core/http.c @@ -209,6 +209,9 @@ bool httpDownloadFile(const char *path, const char *url, bool force_https, HttpP /* Delete output file if the request failed. */ if (!ret) remove(path); + /* Commit SD card filesystem changes. */ + utilsCommitSdCardFileSystemChanges(); + return ret; } diff --git a/source/core/nxdt_bfsar.c b/source/core/nxdt_bfsar.c index 934f5a7..911a797 100644 --- a/source/core/nxdt_bfsar.c +++ b/source/core/nxdt_bfsar.c @@ -168,7 +168,13 @@ bool bfsarInitialize(void) ret = g_bfsarInterfaceInit = true; } - if (bfsar_file) fclose(bfsar_file); + if (bfsar_file) + { + fclose(bfsar_file); + + /* Commit SD card filesystem changes. */ + utilsCommitSdCardFileSystemChanges(); + } if (bfsar_data) free(bfsar_data); diff --git a/source/core/nxdt_utils.c b/source/core/nxdt_utils.c index ad60ecf..54c7be4 100644 --- a/source/core/nxdt_utils.c +++ b/source/core/nxdt_utils.c @@ -55,7 +55,7 @@ static FATFS *g_emmcBisSystemPartitionFatFsObj = NULL; static AppletHookCookie g_systemOverclockCookie = {0}; -static bool g_homeButtonBlocked = false; +static bool g_longRunningProcess = false; static int g_nxLinkSocketFd = -1; @@ -94,6 +94,8 @@ static void utilsUnmountEmmcBisSystemPartitionStorage(void); static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param); +static void utilsChangeHomeButtonBlockStatus(bool block); + static void utilsPrintConsoleError(void); static size_t utilsGetUtf8CodepointCount(const char *str, size_t str_size, size_t cp_limit, size_t *last_cp_pos); @@ -210,10 +212,6 @@ bool utilsInitializeResources(const int program_argc, const char **program_argv) if (R_SUCCEEDED(rc) && flag) appletInitializeGamePlayRecording(); } - /* Disable screen dimming and auto sleep. */ - /* TODO: only use this function while dealing with a dump process - make sure to handle power button presses as well. */ - appletSetMediaPlaybackState(true); - /* Redirect stdout and stderr over network to nxlink. */ g_nxLinkSocketFd = nxlinkConnectToHost(true, true); @@ -237,12 +235,8 @@ void utilsCloseResources(void) g_nxLinkSocketFd = -1; } - /* Enable screen dimming and auto sleep. */ - /* TODO: only use this function while dealing with a dump process - make sure to handle power button presses as well. */ - appletSetMediaPlaybackState(false); - - /* Unblock HOME button presses. */ - utilsChangeHomeButtonBlockStatus(false); + /* Unset long running process state. */ + utilsSetLongRunningProcessState(false); /* Unset our overclock applet hook. */ appletUnhook(&g_systemOverclockCookie); @@ -335,21 +329,21 @@ void utilsOverclockSystem(bool overclock) servicesChangeHardwareClockRates(cpu_rate, mem_rate); } -void utilsChangeHomeButtonBlockStatus(bool block) +void utilsSetLongRunningProcessState(bool state) { SCOPED_LOCK(&g_resourcesMutex) { - /* Only change HOME button blocking status if we're running as a regular application or a system application, and if its current blocking status is different than the requested one. */ - if (_utilsAppletModeCheck() || block == g_homeButtonBlocked) break; + /* Don't proceed if the requested state matches the current one. */ + if (state == g_longRunningProcess) break; - if (block) - { - appletBeginBlockingHomeButtonShortAndLongPressed(0); - } else { - appletEndBlockingHomeButtonShortAndLongPressed(); - } + /* Change HOME button block status. */ + utilsChangeHomeButtonBlockStatus(state); - g_homeButtonBlocked = block; + /* (Un)set screen dimming and auto sleep. */ + appletSetMediaPlaybackState(state); + + /* Update flag. */ + g_longRunningProcess = state; } } @@ -906,6 +900,19 @@ static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param) utilsOverclockSystem(configGetBoolean("overclock")); } +static void utilsChangeHomeButtonBlockStatus(bool block) +{ + /* Only change HOME button blocking status if we're running as a regular application or a system application. */ + if (_utilsAppletModeCheck()) return; + + if (block) + { + appletBeginBlockingHomeButtonShortAndLongPressed(0); + } else { + appletEndBlockingHomeButtonShortAndLongPressed(); + } +} + static void utilsPrintConsoleError(void) { PadState pad = {0}; diff --git a/source/core/save.c b/source/core/save.c index cd8fc60..43e86c2 100644 --- a/source/core/save.c +++ b/source/core/save.c @@ -1770,7 +1770,12 @@ save_ctx_t *save_open_savefile(const char *path, u32 action) f_rewind(save_fd); } - if (fd) fclose(fd); + if (fd) + { + fclose(fd); + utilsCommitSdCardFileSystemChanges(); + } + if (buf) free(buf);*/ save_ctx = calloc(1, sizeof(save_ctx_t));