diff --git a/include/state.h b/include/state.h new file mode 100644 index 0000000..2f61a81 --- /dev/null +++ b/include/state.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include +#include + +bool AppRunning(); +void initState(); +void shutdownState(); \ No newline at end of file diff --git a/src/log_freetype.cpp b/src/log_freetype.cpp index 6aca56e..00fd0a9 100644 --- a/src/log_freetype.cpp +++ b/src/log_freetype.cpp @@ -49,13 +49,13 @@ static uint32_t FreetypeProcCallbackAcquired(void *context) { MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1); if (frameBufferTVSize) { - frameBufferTVFrontPtr = (uint8_t*)MEMAllocFromFrmHeapEx(heap, frameBufferTVSize, 0x100); - frameBufferTVBackPtr = (uint8_t*)frameBufferTVFrontPtr + (1*(1280*720*4)); + frameBufferTVFrontPtr = (uint8_t *) MEMAllocFromFrmHeapEx(heap, frameBufferTVSize, 0x100); + frameBufferTVBackPtr = (uint8_t *) frameBufferTVFrontPtr + (1 * (1280 * 720 * 4)); } if (frameBufferDRCSize) { - frameBufferDRCFrontPtr = (uint8_t*)MEMAllocFromFrmHeapEx(heap, frameBufferDRCSize, 0x100); - frameBufferDRCBackPtr = (uint8_t*)frameBufferDRCFrontPtr + (1*(896*480*4)); + frameBufferDRCFrontPtr = (uint8_t *) MEMAllocFromFrmHeapEx(heap, frameBufferDRCSize, 0x100); + frameBufferDRCBackPtr = (uint8_t *) frameBufferDRCFrontPtr + (1 * (896 * 480 * 4)); } OSScreenSetBufferEx(SCREEN_TV, frameBufferTVFrontPtr); @@ -65,8 +65,8 @@ static uint32_t FreetypeProcCallbackAcquired(void *context) { OSScreenPutPixelEx(SCREEN_DRC, 0, 0, 0xABCDEFFF); DCFlushRange(frameBufferTVFrontPtr, frameBufferTVSize); DCFlushRange(frameBufferDRCFrontPtr, frameBufferDRCSize); - currTVFrameBuffer = (((uint32_t*)frameBufferTVFrontPtr)[0] == 0xABCDEFFF) ? frameBufferTVFrontPtr : frameBufferTVBackPtr; - currDRCFrameBuffer = (((uint32_t*)frameBufferTVFrontPtr)[0] == 0xABCDEFFF) ? frameBufferDRCFrontPtr : frameBufferDRCBackPtr; + currTVFrameBuffer = (((uint32_t *) frameBufferTVFrontPtr)[0] == 0xABCDEFFF) ? frameBufferTVFrontPtr : frameBufferTVBackPtr; + currDRCFrameBuffer = (((uint32_t *) frameBufferTVFrontPtr)[0] == 0xABCDEFFF) ? frameBufferDRCFrontPtr : frameBufferDRCBackPtr; return 0; } @@ -148,7 +148,6 @@ void WHBLogFreetypeFree() { FT_Done_FreeType(fontLibrary); if (freetypeHasForeground) { - OSScreenShutdown(); FreetypeProcCallbackReleased(NULL); } } diff --git a/src/main.cpp b/src/main.cpp index 670b77c..e6505d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "language.h" #include "log_freetype.h" #include "savemng.h" +#include "state.h" #include "string.hpp" #define VERSION_MAJOR 1 @@ -415,6 +416,7 @@ int main() { KPADInit(); WPADEnableURCC(1); loadWiiUTitles(0); + initState(); int res = romfsInit(); if (res) { @@ -447,7 +449,7 @@ int main() { bool redraw = true; int entrycount = 0; - while (WHBProcIsRunning()) { + while (AppRunning()) { Title *titles = mode != 0 ? wiititles : wiiutitles; int count = mode != 0 ? titlesvwii : titleswiiu; @@ -1051,9 +1053,9 @@ int main() { gettextCleanUp(); romfsExit(); - OSScreenShutdown(); WHBLogFreetypeFree(); + shutdownState(); WHBProcShutdown(); - + ProcUIShutdown(); return 0; } diff --git a/src/state.cpp b/src/state.cpp new file mode 100644 index 0000000..485b2d1 --- /dev/null +++ b/src/state.cpp @@ -0,0 +1,72 @@ +#include "state.h" + +typedef enum { + APP_STATE_STOPPING = 0, + APP_STATE_STOPPED, + APP_STATE_RUNNING, + APP_STATE_BACKGROUND, + APP_STATE_RETURNING, +} APP_STATE; + +static bool aroma; +static volatile APP_STATE app = APP_STATE_RUNNING; + +bool isAroma() { + OSDynLoad_Module mod; + aroma = OSDynLoad_Acquire("homebrew_kernel", &mod) == OS_DYNLOAD_OK; + if (aroma) + OSDynLoad_Release(mod); + return aroma; +} + +static uint32_t homeButtonCallback(void *dummy) { + app = APP_STATE_STOPPING; + return 0; +} + +bool AppRunning() { + if (OSIsMainCore() && app != APP_STATE_STOPPED) { + switch (ProcUIProcessMessages(true)) { + case PROCUI_STATUS_EXITING: + // Being closed, prepare to exit + app = APP_STATE_STOPPED; + break; + case PROCUI_STATUS_RELEASE_FOREGROUND: + // Free up MEM1 to next foreground app, deinit screen, etc. + ProcUIDrawDoneRelease(); + if (app != APP_STATE_STOPPING) + app = APP_STATE_BACKGROUND; + break; + case PROCUI_STATUS_IN_FOREGROUND: + // Executed while app is in foreground + if (app == APP_STATE_STOPPING) + break; + if (app == APP_STATE_BACKGROUND) { + app = APP_STATE_RETURNING; + } else + app = APP_STATE_RUNNING; + + break; + case PROCUI_STATUS_IN_BACKGROUND: + if (app != APP_STATE_STOPPING) + app = APP_STATE_BACKGROUND; + break; + } + } + + return app; +} + +void initState() { + ProcUIRegisterCallback(PROCUI_CALLBACK_HOME_BUTTON_DENIED, &homeButtonCallback, NULL, 100); + OSEnableHomeButtonMenu(false); +} + +void shutdownState() { + if (isAroma()) { + SYSLaunchMenu(); + while (app != APP_STATE_STOPPED) { + AppRunning(); + } + } +} \ No newline at end of file