Skip to content

Commit

Permalink
(WiiU) Implement ProcUI-loop, add home menu support when not running …
Browse files Browse the repository at this point in the history
…an app

* WIP: Implement ProcUI loop properly, allows home menu when no game is running. Some stuff is copy pasted from wut, so variables/functions need be adjusted I guess. We loose access to MEM1 when opening the HOME Menu, so for now I replaced all MEM1 usages with MEM2, we need figure out how to handle this properly.

* Fix setting setvbuf on WII_U

* Preserve MEM1 and bucket via proc_ui

* Revert procui callbacks

* Avoid accessing MEM1 and Bucket memory when not in foreground

* the exception handler are broken...

* Clean up and formatting

* Formatting
  • Loading branch information
Maschell authored and ashquarky committed Jun 6, 2024
1 parent cd105b6 commit 0f516d9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 78 deletions.
143 changes: 82 additions & 61 deletions frontend/drivers/platform_wiiu.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
#include <coreinit/dynload.h>
#include <coreinit/ios.h>
#include <coreinit/foreground.h>
#include <coreinit/memory.h>
#include <coreinit/time.h>
#include <coreinit/title.h>
#include <proc_ui/procui.h>
#include <proc_ui/memory.h>
#include <padscore/wpad.h>
#include <padscore/kpad.h>
#include <sysapp/launch.h>
Expand Down Expand Up @@ -228,6 +230,25 @@ static int frontend_wiiu_parse_drive_list(void *data, bool load_content)
return 0;
}

static bool check_proc() {
switch (ProcUIProcessMessages(true)) {
case PROCUI_STATUS_EXITING: {
return false;
}
case PROCUI_STATUS_RELEASE_FOREGROUND: {
ProcUIDrawDoneRelease();
break;
}
case PROCUI_STATUS_IN_FOREGROUND: {
break;
}
case PROCUI_STATUS_IN_BACKGROUND:
default:
break;
}
return true;
}

static void frontend_wiiu_exec(const char *path, bool should_load_content)
{
/* goal: make one big buffer with all the argv's, seperated by NUL. we can
Expand Down Expand Up @@ -322,24 +343,26 @@ static void frontend_wiiu_exec(const char *path, bool should_load_content)
argv_buf = NULL;
}

#ifdef IS_SALAMANDER
static void frontend_wiiu_exitspawn(char *s, size_t len, char *args)
{
bool should_load_content = false;
#ifndef IS_SALAMANDER
if (wiiu_fork_mode == FRONTEND_FORK_NONE)
return;

switch (wiiu_fork_mode)
{
case FRONTEND_FORK_CORE_WITH_ARGS:
should_load_content = true;
break;
default:
break;
frontend_wiiu_exec(s, false);
while (check_proc());
}
#else /* ifndef IS_SALAMANDER */
static void frontend_wiiu_exitspawn(char *s, size_t len, char *args)
{
if (wiiu_fork_mode == FRONTEND_FORK_NONE) {
/* Exit to menu */
SYSLaunchMenu();
} else {
/* Load a core */
frontend_wiiu_exec(s, wiiu_fork_mode == FRONTEND_FORK_CORE_WITH_ARGS);
}
#endif
frontend_wiiu_exec(s, should_load_content);

while (check_proc());
}
#endif /* IS_SALAMANDER */

#ifndef IS_SALAMANDER
static bool frontend_wiiu_set_fork(enum frontend_fork fork_mode)
Expand Down Expand Up @@ -451,7 +474,7 @@ int main(int argc, char **argv)
static void main_setup(void)
{
memoryInitialize();
init_os_exceptions();
//init_os_exceptions();
init_logging();
init_filesystems();
init_pad_libraries();
Expand All @@ -464,35 +487,16 @@ static void main_teardown(void)
deinit_pad_libraries();
deinit_filesystems();
deinit_logging();
deinit_os_exceptions();
//deinit_os_exceptions();
memoryRelease();
}

// https://github.com/devkitPro/wut/blob/7d9fa9e416bffbcd747f1a8e5701fd6342f9bc3d/libraries/libwhb/src/proc.c

#define HBL_TITLE_ID (0x0005000013374842)
#define MII_MAKER_JPN_TITLE_ID (0x000500101004A000)
#define MII_MAKER_USA_TITLE_ID (0x000500101004A100)
#define MII_MAKER_EUR_TITLE_ID (0x000500101004A200)

static bool in_hbl = false;
static bool in_aroma = false;
static void* procui_mem1Storage = NULL;
static void* procui_bucketStorage = NULL;

static void proc_setup(void)
{
uint64_t titleID = OSGetTitleID();

// Homebrew Launcher does not like the standard ProcUI application loop, sad!
if (titleID == HBL_TITLE_ID ||
titleID == MII_MAKER_JPN_TITLE_ID ||
titleID == MII_MAKER_USA_TITLE_ID ||
titleID == MII_MAKER_EUR_TITLE_ID)
{
// Important: OSEnableHomeButtonMenu must come before ProcUIInitEx.
OSEnableHomeButtonMenu(FALSE);
in_hbl = TRUE;
}

/* Detect Aroma explicitly (it's possible to run under H&S while using Tiramisu) */
OSDynLoad_Module rpxModule;
if (OSDynLoad_Acquire("homebrew_rpx_loader", &rpxModule) == OS_DYNLOAD_OK)
Expand All @@ -502,33 +506,33 @@ static void proc_setup(void)
}

ProcUIInit(&proc_save_callback);

uint32_t addr = 0;
uint32_t size = 0;
if (OSGetMemBound(OS_MEM1, &addr, &size) == 0) {
procui_mem1Storage = malloc(size);
if (procui_mem1Storage) {
ProcUISetMEM1Storage(procui_mem1Storage, size);
}
}
if (OSGetForegroundBucketFreeArea(&addr, &size)) {
procui_bucketStorage = malloc(size);
if (procui_bucketStorage) {
ProcUISetBucketStorage(procui_bucketStorage, size);
}
}
}

static void proc_exit(void)
{
/* If we're doing a normal exit while running under HBL, we must SYSRelaunchTitle.
* If we're in an exec (i.e. launching mocha homebrew wrapper) we must *not* do that. yay! */
if (in_hbl && !in_exec)
SYSRelaunchTitle(0, NULL);

/* Similar deal for Aroma, but exit to menu. */
if (!in_hbl && !in_exec)
SYSLaunchMenu();

/* Now just tell the OS that we really are ok to exit */
if (!ProcUIInShutdown())
{
for (;;)
{
ProcUIStatus status;
status = ProcUIProcessMessages(TRUE);
if (status == PROCUI_STATUS_EXITING)
break;
else if (status == PROCUI_STATUS_RELEASE_FOREGROUND)
ProcUIDrawDoneRelease();
}
if (procui_mem1Storage) {
free(procui_mem1Storage);
procui_mem1Storage = NULL;
}
if (procui_bucketStorage) {
free(procui_bucketStorage);
procui_bucketStorage = NULL;
}

ProcUIShutdown();
}

Expand Down Expand Up @@ -600,7 +604,10 @@ static void main_loop(void)
OSTime start_time;
int status;

for (;;)
bool home_menu_allowed = true;
OSEnableHomeButtonMenu(TRUE);

while (check_proc())
{
if (video_driver_get_ptr())
{
Expand All @@ -612,8 +619,22 @@ static void main_loop(void)

status = runloop_iterate();

if (status == -1)
// TODO: make this less ugly...
if ((runloop_get_flags() & RUNLOOP_FLAG_CORE_RUNNING)) {
if (home_menu_allowed) {
OSEnableHomeButtonMenu(FALSE);
home_menu_allowed = false;
}
} else {
if (!home_menu_allowed) {
OSEnableHomeButtonMenu(TRUE);
home_menu_allowed = true;
}
}

if (status == -1) {
break;
}
}
}
#endif
Expand Down
25 changes: 14 additions & 11 deletions gfx/drivers/gx2_gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <formats/image.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#include <proc_ui/procui.h>

#include "../../driver.h"
#include "../../configuration.h"
Expand Down Expand Up @@ -1493,19 +1494,21 @@ static void gx2_free(void *data)
if (!wiiu)
return;

/* clear leftover image */
GX2ClearColor(&wiiu->color_buffer, 0.0f, 0.0f, 0.0f, 1.0f);
GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_DRC);
GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_TV);
if (ProcUIInForeground()) {
/* clear leftover image */
GX2ClearColor(&wiiu->color_buffer, 0.0f, 0.0f, 0.0f, 1.0f);
GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_DRC);
GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_TV);

GX2SwapScanBuffers();
GX2Flush();
GX2DrawDone();
GX2WaitForVsync();
GX2Shutdown();
GX2SwapScanBuffers();
GX2Flush();
GX2DrawDone();
GX2WaitForVsync();

GX2SetTVEnable(GX2_DISABLE);
GX2SetDRCEnable(GX2_DISABLE);
GX2SetTVEnable(GX2_DISABLE);
GX2SetDRCEnable(GX2_DISABLE);
}
GX2Shutdown();

GX2DestroyShader(&frame_shader);
GX2DestroyShader(&tex_shader);
Expand Down
3 changes: 0 additions & 3 deletions libretro-common/vfs/vfs_implementation.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,6 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(
stream->buf = (char*)memalign(0x40, bufsize);
if (stream->fp)
setvbuf(stream->fp, stream->buf, _IOFBF, bufsize);
stream->buf = (char*)calloc(1, 0x4000);
if (stream->fp)
setvbuf(stream->fp, stream->buf, _IOFBF, 0x4000);
}
#endif
}
Expand Down
5 changes: 2 additions & 3 deletions wiiu/system/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <malloc.h>
#include <string.h>
#include "memory.h"
#include <coreinit/memheap.h>
#include <coreinit/memexpheap.h>
Expand Down Expand Up @@ -81,7 +80,7 @@ void * MEM1_alloc(unsigned int size, unsigned int align)

void MEM1_free(void *ptr)
{
if (ptr)
if (ptr && ProcUIInForeground())
MEMFreeToExpHeap(mem1_heap, ptr);
}

Expand All @@ -94,6 +93,6 @@ void * MEMBucket_alloc(unsigned int size, unsigned int align)

void MEMBucket_free(void *ptr)
{
if (ptr)
if (ptr && ProcUIInForeground())
MEMFreeToExpHeap(bucket_heap, ptr);
}

0 comments on commit 0f516d9

Please sign in to comment.