whb: Fix WHB console for foreground releasing and acquire.

This commit is contained in:
James Benton 2019-11-24 12:19:01 +00:00
parent cd6e74fd17
commit b895e7b56d

View File

@ -4,80 +4,24 @@
#include <coreinit/memheap.h>
#include <coreinit/cache.h>
#include <coreinit/memfrmheap.h>
#include <coreinit/memory.h>
#include <coreinit/screen.h>
#include <proc_ui/procui.h>
#include <string.h>
#define NUM_LINES (16)
#define LINE_LENGTH (128)
#define FRAME_HEAP_TAG (0x000DECAF)
#define CONSOLE_FRAME_HEAP_TAG (0x000DECAF)
static char sConsoleBuffer[NUM_LINES][LINE_LENGTH];
static int sLineNum = 0;
static void *sBufferTV, *sBufferDRC;
static uint32_t sBufferSizeTV, sBufferSizeDRC;
static void *sBufferTV = NULL, *sBufferDRC = NULL;
static uint32_t sBufferSizeTV = 0, sBufferSizeDRC = 0;
static BOOL sConsoleHasForeground = TRUE;
static void
consoleAddLine(const char *line);
BOOL
WHBLogConsoleInit()
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
MEMRecordStateForFrmHeap(heap, FRAME_HEAP_TAG);
OSScreenInit();
sBufferSizeTV = OSScreenGetBufferSizeEx(SCREEN_TV);
sBufferSizeDRC = OSScreenGetBufferSizeEx(SCREEN_DRC);
sBufferTV = MEMAllocFromFrmHeapEx(heap, sBufferSizeTV, 4);
if (!sBufferTV) {
WHBLogPrintf("sBufferTV = MEMAllocFromFrmHeapEx(heap, 0x%X, 4) returned NULL", sBufferSizeTV);
return FALSE;
}
sBufferDRC = MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4);
if (!sBufferDRC) {
WHBLogPrintf("sBufferDRC = MEMAllocFromFrmHeapEx(heap, 0x%X, 4) returned NULL", sBufferSizeDRC);
return FALSE;
}
OSScreenSetBufferEx(SCREEN_TV, sBufferTV);
OSScreenSetBufferEx(SCREEN_DRC, sBufferDRC);
OSScreenEnableEx(SCREEN_TV, 1);
OSScreenEnableEx(SCREEN_DRC, 1);
WHBAddLogHandler(consoleAddLine);
return FALSE;
}
void
WHBLogConsoleFree()
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
OSScreenShutdown();
MEMFreeByStateToFrmHeap(heap, FRAME_HEAP_TAG);
}
void
WHBLogConsoleDraw()
{
OSScreenClearBufferEx(SCREEN_TV, 0x993333FF);
OSScreenClearBufferEx(SCREEN_DRC, 0x993333FF);
for (int y = 0; y < NUM_LINES; ++y) {
OSScreenPutFontEx(SCREEN_TV, 0, y, sConsoleBuffer[y]);
OSScreenPutFontEx(SCREEN_DRC, 0, y, sConsoleBuffer[y]);
}
DCFlushRange(sBufferTV, sBufferSizeTV);
DCFlushRange(sBufferDRC, sBufferSizeDRC);
OSScreenFlipBuffersEx(SCREEN_TV);
OSScreenFlipBuffersEx(SCREEN_DRC);
}
static void
consoleAddLine(const char *line)
ConsoleAddLine(const char *line)
{
int length = strlen(line);
@ -98,3 +42,77 @@ consoleAddLine(const char *line)
++sLineNum;
}
}
static uint32_t
ConsoleProcCallbackAcquired(void *context)
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
if (sBufferSizeTV) {
sBufferTV = MEMAllocFromFrmHeapEx(heap, sBufferSizeTV, 4);
}
if (sBufferSizeDRC) {
sBufferDRC = MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4);
}
sConsoleHasForeground = TRUE;
OSScreenSetBufferEx(SCREEN_TV, sBufferTV);
OSScreenSetBufferEx(SCREEN_DRC, sBufferDRC);
return 0;
}
static uint32_t
ConsoleProcCallbackReleased(void *context)
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
MEMFreeByStateToFrmHeap(heap, CONSOLE_FRAME_HEAP_TAG);
sConsoleHasForeground = FALSE;
return 0;
}
BOOL
WHBLogConsoleInit()
{
OSScreenInit();
sBufferSizeTV = OSScreenGetBufferSizeEx(SCREEN_TV);
sBufferSizeDRC = OSScreenGetBufferSizeEx(SCREEN_DRC);
ConsoleProcCallbackAcquired(NULL);
OSScreenEnableEx(SCREEN_TV, 1);
OSScreenEnableEx(SCREEN_DRC, 1);
ProcUIRegisterCallback(PROCUI_CALLBACK_ACQUIRE, ConsoleProcCallbackAcquired, NULL, 100);
ProcUIRegisterCallback(PROCUI_CALLBACK_RELEASE, ConsoleProcCallbackReleased, NULL, 100);
WHBAddLogHandler(ConsoleAddLine);
return FALSE;
}
void
WHBLogConsoleFree()
{
if (sConsoleHasForeground) {
OSScreenShutdown();
ConsoleProcCallbackReleased(NULL);
}
}
void
WHBLogConsoleDraw()
{
if (!sConsoleHasForeground) {
return;
}
OSScreenClearBufferEx(SCREEN_TV, 0x993333FF);
OSScreenClearBufferEx(SCREEN_DRC, 0x993333FF);
for (int y = 0; y < NUM_LINES; ++y) {
OSScreenPutFontEx(SCREEN_TV, 0, y, sConsoleBuffer[y]);
OSScreenPutFontEx(SCREEN_DRC, 0, y, sConsoleBuffer[y]);
}
DCFlushRange(sBufferTV, sBufferSizeTV);
DCFlushRange(sBufferDRC, sBufferSizeDRC);
OSScreenFlipBuffersEx(SCREEN_TV);
OSScreenFlipBuffersEx(SCREEN_DRC);
}