From 103c5057425ac3a69996863e532244acb8b53f5b Mon Sep 17 00:00:00 2001 From: w3irDv <170813473+w3irDv@users.noreply.github.com> Date: Sun, 16 Jun 2024 15:19:03 +0200 Subject: [PATCH 1/3] - use OSScreen inititalization and ProcUI callbacks from libwhb's console.c --- include/utils/DrawUtils.h | 20 +++++-- include/utils/StateUtils.h | 1 + src/main.cpp | 34 +++++------- src/utils/DrawUtils.cpp | 105 ++++++++++++++++++++++++++++++++----- src/utils/StateUtils.cpp | 18 ++++++- 5 files changed, 138 insertions(+), 40 deletions(-) diff --git a/include/utils/DrawUtils.h b/include/utils/DrawUtils.h index b55ec29..44e829b 100644 --- a/include/utils/DrawUtils.h +++ b/include/utils/DrawUtils.h @@ -35,7 +35,11 @@ class DrawUtils { static bool getRedraw() { return redraw; } - static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize); + static BOOL LogConsoleInit(); + + static void LogConsoleFree(); + + static void initBuffers(void *tvBuffer, void *drcBuffer); static void beginDraw(); @@ -73,12 +77,22 @@ class DrawUtils { static void drawRGB5A3(int x, int y, float scale, uint8_t *fileContent); + static uint32_t ConsoleProcCallbackAcquired(void *context); + + static uint32_t ConsoleProcCallbackReleased(void *context); + + + + private: static bool redraw; static bool isBackBuffer; + static uint8_t *tvBuffer; - static uint32_t tvSize; static uint8_t *drcBuffer; - static uint32_t drcSize; + + static void *sBufferTV, *sBufferDRC; + static uint32_t sBufferSizeTV, sBufferSizeDRC; + static BOOL sConsoleHasForeground; }; diff --git a/include/utils/StateUtils.h b/include/utils/StateUtils.h index e788778..a469b9e 100644 --- a/include/utils/StateUtils.h +++ b/include/utils/StateUtils.h @@ -5,6 +5,7 @@ class State { static void init(); static bool AppRunning(); static void shutdown(); + static void registerProcUICallbacks(); private: static bool aroma; diff --git a/src/main.cpp b/src/main.cpp index 0f54f5b..80c0bfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -386,24 +386,12 @@ static void unloadTitles(Title *titles, int count) { int main() { AXInit(); AXQuit(); - OSScreenInit(); - - uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV); - uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC); - - auto *screenBuffer = (uint8_t *) memalign(0x100, tvBufferSize + drcBufferSize); - if (!screenBuffer) { - OSFatal("Fail to allocate screenBuffer"); + + State::init(); + + if (DrawUtils::LogConsoleInit()) { + OSFatal("Failed to initialize OSSCreen"); } - memset(screenBuffer, 0, tvBufferSize + drcBufferSize); - - OSScreenSetBufferEx(SCREEN_TV, screenBuffer); - OSScreenSetBufferEx(SCREEN_DRC, screenBuffer + tvBufferSize); - - OSScreenEnableEx(SCREEN_TV, TRUE); - OSScreenEnableEx(SCREEN_DRC, TRUE); - - DrawUtils::initBuffers(screenBuffer, tvBufferSize, screenBuffer + tvBufferSize, drcBufferSize); if (!DrawUtils::initFont()) { OSFatal("Failed to init font"); @@ -412,8 +400,8 @@ int main() { WPADInit(); KPADInit(); WPADEnableURCC(1); + loadWiiUTitles(0); - State::init(); int res = romfsInit(); if (res) { @@ -422,20 +410,20 @@ int main() { State::shutdown(); return 0; } - Swkbd_LanguageType systemLanguage = LanguageUtils::getSystemLanguage(); LanguageUtils::loadLanguage(systemLanguage); if (!initFS()) { promptError(LanguageUtils::gettext("initFS failed. Please make sure your MochaPayload is up-to-date")); DrawUtils::endDraw(); - State::shutdown(); + State::shutdown(); return 0; } DrawUtils::beginDraw(); DrawUtils::clear(COLOR_BLACK); DrawUtils::endDraw(); + Title *wiiutitles = loadWiiUTitles(1); Title *wiititles = loadWiiTitles(); getAccountsWiiU(); @@ -445,8 +433,9 @@ int main() { Input input{}; std::unique_ptr state = std::make_unique(wiiutitles, wiititles, wiiuTitlesCount, - vWiiTitlesCount); + vWiiTitlesCount); while (State::AppRunning()) { + input.read(); if (input.get(TRIGGER, PAD_BUTTON_ANY)) @@ -473,12 +462,13 @@ int main() { unloadTitles(wiiutitles, wiiuTitlesCount); unloadTitles(wiititles, vWiiTitlesCount); - shutdownFS(); LanguageUtils::gettextCleanUp(); romfsExit(); DrawUtils::deinitFont(); + DrawUtils::LogConsoleFree(); + State::shutdown(); return 0; } diff --git a/src/utils/DrawUtils.cpp b/src/utils/DrawUtils.cpp index 1b8bdec..9c5681d 100644 --- a/src/utils/DrawUtils.cpp +++ b/src/utils/DrawUtils.cpp @@ -5,22 +5,98 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include // buffer width #define TV_WIDTH 0x500 #define DRC_WIDTH 0x380 +#define CONSOLE_FRAME_HEAP_TAG (0x000DECAF) + + bool DrawUtils::isBackBuffer; uint8_t *DrawUtils::tvBuffer = nullptr; -uint32_t DrawUtils::tvSize = 0; uint8_t *DrawUtils::drcBuffer = nullptr; -uint32_t DrawUtils::drcSize = 0; +/* +uint32_t DrawUtils::sBufferSizeTV = 0; +uint32_t DrawUtils::sBufferSizeDRC = 0; +*/ static SFT pFont = {}; static Color font_col(0xFFFFFFFF); +void *DrawUtils::sBufferTV = NULL, *DrawUtils::sBufferDRC = NULL; +uint32_t DrawUtils::sBufferSizeTV = 0, DrawUtils::sBufferSizeDRC = 0; +BOOL DrawUtils::sConsoleHasForeground = TRUE; + +uint32_t +DrawUtils::ConsoleProcCallbackAcquired(void *context) +{ + MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1); + MEMRecordStateForFrmHeap(heap, CONSOLE_FRAME_HEAP_TAG); + + if (sBufferSizeTV) { + sBufferTV = MEMAllocFromFrmHeapEx(heap, sBufferSizeTV, 4); + } + + if (sBufferSizeDRC) { + sBufferDRC = MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4); + } + + sConsoleHasForeground = TRUE; + DrawUtils::setRedraw(true); + OSScreenSetBufferEx(SCREEN_TV, sBufferTV); + OSScreenSetBufferEx(SCREEN_DRC, sBufferDRC); + return 0; +} + +uint32_t +DrawUtils::ConsoleProcCallbackReleased(void *context) +{ + MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1); + MEMFreeByStateToFrmHeap(heap, CONSOLE_FRAME_HEAP_TAG); + sConsoleHasForeground = FALSE; + return 0; +} + + +BOOL +DrawUtils::LogConsoleInit() +{ + OSScreenInit(); + sBufferSizeTV = OSScreenGetBufferSizeEx(SCREEN_TV); + sBufferSizeDRC = OSScreenGetBufferSizeEx(SCREEN_DRC); + + ConsoleProcCallbackAcquired(NULL); + OSScreenEnableEx(SCREEN_TV, 1); + OSScreenEnableEx(SCREEN_DRC, 1); + + State::registerProcUICallbacks(); + + DrawUtils::initBuffers(sBufferTV, sBufferDRC); + + return FALSE; +} + +void +DrawUtils::LogConsoleFree() +{ + if (sConsoleHasForeground) { + OSScreenShutdown(); + ConsoleProcCallbackReleased(NULL); + } +} + + bool DrawUtils::redraw = true; template @@ -32,11 +108,9 @@ void DrawUtils::setRedraw(bool value) { redraw = value; } -void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_, uint32_t drcSize_) { - DrawUtils::tvBuffer = (uint8_t *) tvBuffer_; - DrawUtils::tvSize = tvSize_; - DrawUtils::drcBuffer = (uint8_t *) drcBuffer_; - DrawUtils::drcSize = drcSize_; +void DrawUtils::initBuffers(void *sBufferTV_, void *sBufferDRC_) { + DrawUtils::tvBuffer = (uint8_t *) sBufferTV_; + DrawUtils::drcBuffer = (uint8_t *) sBufferDRC_; } void DrawUtils::beginDraw() { @@ -56,8 +130,11 @@ void DrawUtils::beginDraw() { void DrawUtils::endDraw() { // OSScreenFlipBuffersEx already flushes the cache? - // DCFlushRange(tvBuffer, tvSize); - // DCFlushRange(drcBuffer, drcSize); + // DCFlushRange(tvBuffer, sBufferSizeTV); + // DCFlushRange(drcBuffer, sBufferSizeDRC); + + DCFlushRange(sBufferTV, sBufferSizeTV); + DCFlushRange(sBufferDRC, sBufferSizeDRC); OSScreenFlipBuffersEx(SCREEN_DRC); OSScreenFlipBuffersEx(SCREEN_TV); @@ -73,9 +150,9 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t // put pixel in the drc buffer uint32_t i = (x + y * DRC_WIDTH) * 4; - if (i + 3 < drcSize / 2) { + if (i + 3 < sBufferSizeDRC / 2) { if (isBackBuffer) { - i += drcSize / 2; + i += sBufferSizeDRC / 2; } if (a == 0xFF) { drcBuffer[i] = r; @@ -90,7 +167,7 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t uint32_t USED_TV_WIDTH = TV_WIDTH; float scale = 1.5f; - if (DrawUtils::tvSize == 0x00FD2000) { + if (DrawUtils::sBufferSizeTV == 0x00FD2000) { USED_TV_WIDTH = 1920; scale = 2.25f; } @@ -99,9 +176,9 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t for (uint32_t yy = (y * scale); yy < ((y * scale) + (uint32_t) scale); yy++) { for (uint32_t xx = (x * scale); xx < ((x * scale) + (uint32_t) scale); xx++) { uint32_t i = (xx + yy * USED_TV_WIDTH) * 4; - if (i + 3 < tvSize / 2) { + if (i + 3 < sBufferSizeTV / 2) { if (isBackBuffer) { - i += tvSize / 2; + i += sBufferSizeTV / 2; } if (a == 0xFF) { tvBuffer[i] = r; diff --git a/src/utils/StateUtils.cpp b/src/utils/StateUtils.cpp index 9cf4796..6cd1164 100644 --- a/src/utils/StateUtils.cpp +++ b/src/utils/StateUtils.cpp @@ -3,8 +3,14 @@ #include #include #include +#include +#include #include +#include + +#include + bool State::aroma = false; void State::init() { @@ -18,6 +24,14 @@ void State::init() { WHBProcInit(); } + +void State::registerProcUICallbacks() { + if (aroma) { + ProcUIRegisterCallback(PROCUI_CALLBACK_ACQUIRE, DrawUtils::ConsoleProcCallbackAcquired, NULL, 100); + ProcUIRegisterCallback(PROCUI_CALLBACK_RELEASE, DrawUtils::ConsoleProcCallbackReleased, NULL, 100); + } +} + bool State::AppRunning() { if (aroma) { bool app = true; @@ -50,4 +64,6 @@ void State::shutdown() { if (!aroma) WHBProcShutdown(); ProcUIShutdown(); -} \ No newline at end of file +} + + From 8eb1ec300d581beeeabb41de79985f367f864370 Mon Sep 17 00:00:00 2001 From: w3irDv <170813473+w3irDv@users.noreply.github.com> Date: Mon, 29 Jul 2024 09:53:58 +0200 Subject: [PATCH 2/3] fix crash when scanning titles due to duplicate data nand/usb --- src/main.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 80c0bfc..b13c3cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -91,10 +91,11 @@ static Title *loadWiiUTitles(int run) { path = StringUtils::stringFormat("%s/usr/save/%08x/%s/meta/meta.xml", (i == 0) ? getUSB().c_str() : "storage_mlc01:", highIDs[a], data->d_name); if (checkEntry(path.c_str()) == 1) { - for (int i = 0; i < usable; i++) { - if (contains(highIDs, savesl[i].highID) && - (strtoul(data->d_name, nullptr, 16) == savesl[i].lowID)) { - savesl[i].found = true; + for (int ii = 0; ii < usable; ii++) { + if (contains(highIDs, savesl[ii].highID) && + (strtoul(data->d_name, nullptr, 16) == savesl[ii].lowID) && + savesl[ii].dev == i ) { + savesl[ii].found = true; tNoSave--; break; } @@ -109,7 +110,7 @@ static Title *loadWiiUTitles(int run) { } foundCount += tNoSave; - auto *saves = (Saves *) malloc((foundCount + tNoSave) * sizeof(Saves)); + auto *saves = (Saves *) malloc((foundCount) * sizeof(Saves)); if (saves == nullptr) { promptError(LanguageUtils::gettext("Out of memory.")); return nullptr; From a22b7af86d4aadff06da10da8cd88af010587367 Mon Sep 17 00:00:00 2001 From: w3irDv <170813473+w3irDv@users.noreply.github.com> Date: Sat, 3 Aug 2024 13:06:38 +0200 Subject: [PATCH 3/3] fix - no black screen when reentering app --- src/utils/DrawUtils.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/utils/DrawUtils.cpp b/src/utils/DrawUtils.cpp index 9c5681d..2cda6b1 100644 --- a/src/utils/DrawUtils.cpp +++ b/src/utils/DrawUtils.cpp @@ -33,6 +33,7 @@ uint32_t DrawUtils::sBufferSizeDRC = 0; static SFT pFont = {}; static Color font_col(0xFFFFFFFF); +static Color black(0x000000FF); void *DrawUtils::sBufferTV = NULL, *DrawUtils::sBufferDRC = NULL; uint32_t DrawUtils::sBufferSizeTV = 0, DrawUtils::sBufferSizeDRC = 0; @@ -52,10 +53,25 @@ DrawUtils::ConsoleProcCallbackAcquired(void *context) sBufferDRC = MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4); } + sConsoleHasForeground = TRUE; - DrawUtils::setRedraw(true); + OSScreenSetBufferEx(SCREEN_TV, sBufferTV); OSScreenSetBufferEx(SCREEN_DRC, sBufferDRC); + DrawUtils::initBuffers(sBufferTV, sBufferDRC); + + OSScreenEnableEx(SCREEN_TV, 1); + OSScreenEnableEx(SCREEN_DRC, 1); + + + for (int i = 0; i<2; i++) // both buffers to black + { + DrawUtils::clear(black); + DrawUtils::endDraw(); + } + + DrawUtils::setRedraw(true); // force a redraw when reentering + return 0; } @@ -77,13 +93,9 @@ DrawUtils::LogConsoleInit() sBufferSizeDRC = OSScreenGetBufferSizeEx(SCREEN_DRC); ConsoleProcCallbackAcquired(NULL); - OSScreenEnableEx(SCREEN_TV, 1); - OSScreenEnableEx(SCREEN_DRC, 1); - + State::registerProcUICallbacks(); - DrawUtils::initBuffers(sBufferTV, sBufferDRC); - return FALSE; }