Add CleanUpHandles-Callbacks

This commit is contained in:
Maschell 2022-04-28 19:47:43 +02:00
parent c9c3ba3f71
commit 846015ef2e
2 changed files with 65 additions and 4 deletions

View File

@ -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`
*

View File

@ -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<decltype(&RemoveAttachHandler)>(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<decltype(&AddCleanUpHandlesHandler)>(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<decltype(&RemoveCleanUpHandlesHandler)>(sSDUtilsRemoveCleanUpHandlesHandler)(fn);
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND;
}