Skip to content

Commit

Permalink
Merge pull request #56 from w3irDv/fix/homefreeze
Browse files Browse the repository at this point in the history
fix freeze exiting application - use OSScreen inititalization and ProcUI callbacks from  libwhb's console.c
  • Loading branch information
w3irDv authored Aug 22, 2024
2 parents 70e3b63 + a22b7af commit 46af3bf
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 40 deletions.
20 changes: 17 additions & 3 deletions include/utils/DrawUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions include/utils/StateUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class State {
static void init();
static bool AppRunning();
static void shutdown();
static void registerProcUICallbacks();

private:
static bool aroma;
Expand Down
34 changes: 12 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -412,8 +400,8 @@ int main() {
WPADInit();
KPADInit();
WPADEnableURCC(1);

loadWiiUTitles(0);
State::init();

int res = romfsInit();
if (res) {
Expand All @@ -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();
Expand All @@ -445,8 +433,9 @@ int main() {

Input input{};
std::unique_ptr<MainMenuState> state = std::make_unique<MainMenuState>(wiiutitles, wiititles, wiiuTitlesCount,
vWiiTitlesCount);
vWiiTitlesCount);
while (State::AppRunning()) {

input.read();

if (input.get(TRIGGER, PAD_BUTTON_ANY))
Expand All @@ -473,12 +462,13 @@ int main() {

unloadTitles(wiiutitles, wiiuTitlesCount);
unloadTitles(wiititles, vWiiTitlesCount);

shutdownFS();
LanguageUtils::gettextCleanUp();
romfsExit();

DrawUtils::deinitFont();
DrawUtils::LogConsoleFree();

State::shutdown();
return 0;
}
117 changes: 103 additions & 14 deletions src/utils/DrawUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,109 @@
#include <memory>
#include <tga_reader.h>
#include <utils/DrawUtils.h>
#include <utils/StateUtils.h>
#include <malloc.h>
#include <coreinit/debug.h>

#include <coreinit/memheap.h>
#include <coreinit/cache.h>
#include <coreinit/memfrmheap.h>
#include <coreinit/memory.h>
#include <proc_ui/procui.h>

// 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);
static Color black(0x000000FF);

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;

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;
}

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);

State::registerProcUICallbacks();

return FALSE;
}

void
DrawUtils::LogConsoleFree()
{
if (sConsoleHasForeground) {
OSScreenShutdown();
ConsoleProcCallbackReleased(NULL);
}
}


bool DrawUtils::redraw = true;

Expand All @@ -32,11 +120,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() {
Expand All @@ -56,8 +142,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);
Expand All @@ -73,9 +162,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;
Expand All @@ -90,7 +179,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;
}
Expand All @@ -99,9 +188,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;
Expand Down
18 changes: 17 additions & 1 deletion src/utils/StateUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
#include <coreinit/foreground.h>
#include <proc_ui/procui.h>
#include <utils/StateUtils.h>
#include <utils/DrawUtils.h>
#include <coreinit/screen.h>
#include <whb/proc.h>

#include <string.h>

#include <utils/DrawUtils.h>

bool State::aroma = false;

void State::init() {
Expand All @@ -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;
Expand Down Expand Up @@ -50,4 +64,6 @@ void State::shutdown() {
if (!aroma)
WHBProcShutdown();
ProcUIShutdown();
}
}


0 comments on commit 46af3bf

Please sign in to comment.