Skip to content

Commit

Permalink
Wii U: Manually handle ProcUI without the WHB wrapper
Browse files Browse the repository at this point in the history
The ProcUI API is very unstable and querying it crashes when a shutdown was requested.

For more control use it directly.
  • Loading branch information
Ghabry committed Jun 3, 2024
1 parent 956eb50 commit 6467523
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ if(${PLAYER_TARGET_PLATFORM} STREQUAL "SDL2")
if(NINTENDO_WIIU)
target_compile_definitions(${PROJECT_NAME} PUBLIC PLAYER_NINTENDO)
target_sources(${PROJECT_NAME} PRIVATE
src/platform/wiiu/main.h
src/platform/wiiu/input_buttons.cpp)
endif()

Expand Down
10 changes: 2 additions & 8 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
#elif defined(__vita__)
# include <psp2/kernel/processmgr.h>
#elif defined(__WIIU__)
# include <whb/proc.h>
# include <sysapp/launch.h>
# include "platform/wiiu/main.h"
#endif

#include "output.h"
Expand Down Expand Up @@ -323,12 +322,7 @@ void Output::ErrorStr(std::string const& err) {
#ifdef __vita__
sceKernelExitProcess(EXIT_FAILURE);
#elif defined(__WIIU__)
// cleanup procui (see FIXME in platform/wiiu/main.cpp)
SYSLaunchMenu();
while(WHBProcIsRunning()) {
Output::Debug("Waiting for shutdown...");
Game_Clock::SleepFor(1s);
}
WiiU_Exit();
#endif
exit(EXIT_FAILURE);
}
Expand Down
8 changes: 0 additions & 8 deletions src/platform/sdl/sdl2_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/

#include <algorithm>
#include <cstdlib>
#include <cstring>
Expand All @@ -31,8 +30,6 @@
# include <SDL_system.h>
#elif defined(EMSCRIPTEN)
# include <emscripten.h>
#elif defined(__WIIU__)
# include <whb/proc.h>
#endif
#include "icon.h"

Expand Down Expand Up @@ -520,11 +517,6 @@ void Sdl2Ui::ToggleZoom() {
void Sdl2Ui::ProcessEvents() {
SDL_Event evnt;

#ifdef __WIIU__
if (!WHBProcIsRunning())
Player::Exit();
#endif

#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
// Reset Mouse scroll
keys[Input::Keys::MOUSE_SCROLLUP] = false;
Expand Down
53 changes: 38 additions & 15 deletions src/platform/wiiu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#ifdef USE_SDL // SDL might wrap main()
# include <SDL.h>
#endif

#include <coreinit/debug.h>
#include <whb/proc.h>
#include <coreinit/foreground.h>
#include <proc_ui/procui.h>
#include <sysapp/launch.h>

using namespace std::chrono_literals;
Expand All @@ -46,6 +48,7 @@ static void deinitLogging() {}
static bool moduleLogInit = false;
static bool cafeLogInit = false;
static bool udpLogInit = false;
static bool prodUiExited = false;

static void initLogging() {
if (!(moduleLogInit = WHBLogModuleInit())) {
Expand Down Expand Up @@ -81,6 +84,38 @@ static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserDat
#endif
}

static uint32_t SaveCallback(void*) {
OSSavesDone_ReadyToRelease();
return 0;
}

bool WiiU_ProcessProcUI() {
ProcUIStatus status = ProcUIProcessMessages(TRUE);
if (status == PROCUI_STATUS_EXITING) {
prodUiExited = true;
return false;
} else if (status == PROCUI_STATUS_RELEASE_FOREGROUND) {
ProcUIDrawDoneRelease();
}
return true;
}

void WiiU_Exit() {
Output::Debug("Shutdown Reason: {}", prodUiExited ? "HOME Menu" : "Player Exit");

if (!prodUiExited) {
// Exit was not through the Home Menu
// Manually launch the system menu
SYSLaunchMenu();
Game_Clock::SleepFor(std::chrono::milliseconds(10));
while (WiiU_ProcessProcUI()) {}
}

ProcUIShutdown();

deinitLogging();
}

/**
* If the main function ever needs to change, be sure to update the `main()`
* functions of the other platforms as well.
Expand All @@ -89,7 +124,7 @@ extern "C" int main(int argc, char* argv[]) {
std::vector<std::string> args{argv, argv + argc};

initLogging();
WHBProcInit();
ProcUIInitEx(SaveCallback, nullptr);

Output::SetLogCallback(LogCallback);

Expand Down Expand Up @@ -118,19 +153,7 @@ extern "C" int main(int argc, char* argv[]) {
Player::Init(std::move(args));
Player::Run();

// FIXME: somehow the wiiu sdl2 port does not clean up procui
// without launching the menu manually
#if 1
SYSLaunchMenu();
while(WHBProcIsRunning()) {
Output::Debug("Waiting for shutdown...");
Game_Clock::SleepFor(1s);
}
#else
WHBProcShutdown();
#endif

deinitLogging();
WiiU_Exit();

return EXIT_SUCCESS;
}
19 changes: 19 additions & 0 deletions src/platform/wiiu/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/

bool WiiU_ProcessProcUI();
void WiiU_Exit();
8 changes: 8 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
# include <windows.h>
#elif defined(EMSCRIPTEN)
# include <emscripten.h>
#elif defined(__WIIU__)
# include "platform/wiiu/main.h"
#endif

#include "async_handler.h"
Expand Down Expand Up @@ -219,6 +221,12 @@ void Player::Run() {
// libretro invokes the MainLoop through a retro_run-callback
#else
while (Transition::instance().IsActive() || (Scene::instance && Scene::instance->type != Scene::Null)) {
#if defined(__WIIU__)
if (!WiiU_ProcessProcUI()) {
Player::Exit();
return;
}
#endif
MainLoop();
}
#endif
Expand Down

0 comments on commit 6467523

Please sign in to comment.