From 846015ef2e16c4f5320d701b5fb3d0acc0243e08 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 28 Apr 2022 19:47:43 +0200 Subject: [PATCH] Add CleanUpHandles-Callbacks --- include/sdutils/sdutils.h | 34 ++++++++++++++++++++++++++++++++-- source/utils.cpp | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/include/sdutils/sdutils.h b/include/sdutils/sdutils.h index cb5ec04..2ac47b9 100644 --- a/include/sdutils/sdutils.h +++ b/include/sdutils/sdutils.h @@ -28,6 +28,8 @@ enum SDUtilsAttachStatus { typedef void (*SDAttachHandlerFn)(SDUtilsAttachStatus status); +typedef void (*SDCleanUpHandlesHandlerFn)(); + /** * Initializes the SDUtils library. This must be call before any other function can be called * @return SDUTILS_RESULT_SUCCESS on success, the functions of this lib can be used @@ -61,7 +63,7 @@ SDUtilsStatus SDUtils_DeInit(); * @return SDUTILS_RESULT_SUCCESS on success * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the * maximum amount of callback has been reached - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initalized properly + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly */ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); @@ -71,10 +73,38 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); * @param fn * @return SDUTILS_RESULT_SUCCESS on success * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initalized properly + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly */ SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn); +/** + * Registers a callback which will be called whenever a sd card will be ejected and before + * the SDUtils_AddAttachHandler. This callback is supposed to be used to clean up any open + * file handles before the sd card gets unmounted. + * This is only true for future events, if the sd card is already ejected before registering + * a callback, the callback is only called on the next ejecting. + * + * Any previously registered callbacks will be removed when the currently running application + * is closing. + * + * @param fn callback that will be called + * @return SDUTILS_RESULT_SUCCESS on success + * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the + * maximum amount of callback has been reached + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + */ +SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); + +/** + * Removed a previously registered callback + * + * @param fn + * @return SDUTILS_RESULT_SUCCESS on success + * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + */ +SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); + /** * Checks if a FAT32 formatted SD Card is inserted, mounted and available via `fs:/vol/external01` * diff --git a/source/utils.cpp b/source/utils.cpp index 8ce1a02..8171573 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -10,6 +10,9 @@ static SDUtilsVersion (*sSDUtilsGetVersion)() = nullptr; static bool (*sSDUtilsAddAttachHandler)(SDAttachHandlerFn) = nullptr; static bool (*sSDUtilsRemoveAttachHandler)(SDAttachHandlerFn) = nullptr; +static bool (*sSDUtilsAddCleanUpHandlesHandler)(SDCleanUpHandlesHandlerFn) = nullptr; +static bool (*sSDUtilsRemoveCleanUpHandlesHandler)(SDCleanUpHandlesHandlerFn) = nullptr; + SDUtilsStatus SDUtils_Init() { if (OSDynLoad_Acquire("homebrew_sdhotswap", &sModuleHandle) != OS_DYNLOAD_OK) { OSReport("SDUtils_Init: OSDynLoad_Acquire failed.\n"); @@ -35,6 +38,16 @@ SDUtilsStatus SDUtils_Init() { return SDUTILS_RESULT_MODULE_MISSING_EXPORT; } + if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddCleanUpHandlesHandler", (void **) &sSDUtilsAddCleanUpHandlesHandler) != OS_DYNLOAD_OK) { + OSReport("SDUtils_Init: SDUtilsAddCleanUpHandlesHandler failed.\n"); + return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + } + + if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveCleanUpHandlesHandler", (void **) &sSDUtilsRemoveCleanUpHandlesHandler) != OS_DYNLOAD_OK) { + OSReport("SDUtils_Init: SDUtilsRemoveCleanUpHandlesHandler failed.\n"); + return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + } + return SDUTILS_RESULT_SUCCESS; } @@ -52,7 +65,6 @@ SDUtilsStatus SDUtils_DeInit() { return SDUTILS_RESULT_SUCCESS; } - SDUtilsStatus SDUtils_IsSdCardMounted(bool *status) { if (status == nullptr) { return SDUTILS_RESULT_INVALID_ARGUMENT; @@ -77,7 +89,6 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) { return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; } - bool RemoveAttachHandler(SDAttachHandlerFn); SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { @@ -86,4 +97,24 @@ SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { } auto res = reinterpret_cast(sSDUtilsRemoveAttachHandler)(fn); return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; +} + +bool AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); + +SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { + if (sSDUtilsAddCleanUpHandlesHandler == nullptr) { + return SDUTILS_RESULT_LIB_UNINITIALIZED; + } + auto res = reinterpret_cast(sSDUtilsAddCleanUpHandlesHandler)(fn); + return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; +} + +bool RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); + +SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { + if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr) { + return SDUTILS_RESULT_LIB_UNINITIALIZED; + } + auto res = reinterpret_cast(sSDUtilsRemoveCleanUpHandlesHandler)(fn); + return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; } \ No newline at end of file