Skip to content

Commit

Permalink
Add a bool to ProcessEvents to signal program exit.
Browse files Browse the repository at this point in the history
Calling Player::Exit in the Ui can crash because the Ui is deleted by it.
Also the normal teardown codepath is too slow on WiiU and crashes the console when the home button is used.
  • Loading branch information
Ghabry committed Aug 27, 2024
1 parent 7b4887e commit 4b09236
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/baseui.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ class BaseUi {

/**
* Processes events queue.
*
* @return When false requests an immediate Player shutdown
*/
virtual void ProcessEvents() = 0;
virtual bool ProcessEvents() = 0;

/**
* Cleans video buffer.
Expand Down
6 changes: 3 additions & 3 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ static void HandleErrorOutput(const std::string& err) {
#if !defined(USE_LIBRETRO)
Game_Clock::SleepFor(1ms);
#endif
DisplayUi->ProcessEvents();

if (Player::exit_flag) break;
if (!DisplayUi->ProcessEvents() || Player::exit_flag) {
break;
}

Input::Update();
}
Expand Down
9 changes: 6 additions & 3 deletions src/platform/3ds/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ CtrUi::~CtrUi() {
ToggleBottomScreen(true);
}

void CtrUi::ProcessEvents() {
if (!aptMainLoop())
Player::Exit();
bool CtrUi::ProcessEvents() {
if (!aptMainLoop()) {
return false;
}

hidScanInput();
u32 input = hidKeysHeld();
Expand Down Expand Up @@ -324,6 +325,8 @@ void CtrUi::ProcessEvents() {
}
}
#endif

return true;
}

void CtrUi::UpdateDisplay() {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/3ds/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CtrUi final : public BaseUi {
*/
/** @{ */
void UpdateDisplay() override;
void ProcessEvents() override;
bool ProcessEvents() override;
void ToggleStretch() override;
void ToggleTouchUi() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;
Expand Down
6 changes: 4 additions & 2 deletions src/platform/libretro/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ bool LibretroUi::vChangeDisplaySurfaceResolution(int new_width, int new_height)
return true;
}

void LibretroUi::ProcessEvents() {
bool LibretroUi::ProcessEvents() {
# if defined(USE_JOYSTICK) && defined(SUPPORT_JOYSTICK)
if (CheckInputState == nullptr) {
return;
return true;
}

LibretroUi::input_poll_cb();
Expand Down Expand Up @@ -184,6 +184,8 @@ void LibretroUi::ProcessEvents() {
analog_input.trigger_right = normalize(CheckInputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_BUTTON, RETRO_DEVICE_ID_JOYPAD_R2));
# endif
# endif

return true;
}

retro_video_refresh_t LibretroUi::UpdateWindow = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/libretro/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class LibretroUi final : public BaseUi {
/** @{ */
bool vChangeDisplaySurfaceResolution(int new_width, int new_height) override;
void UpdateDisplay() override;
void ProcessEvents() override;
bool ProcessEvents() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;

#ifdef SUPPORT_AUDIO
Expand Down
4 changes: 3 additions & 1 deletion src/platform/psvita/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Psp2Ui::~Psp2Ui() {
vita2d_fini();
}

void Psp2Ui::ProcessEvents() {
bool Psp2Ui::ProcessEvents() {
SceCtrlData input;
SceTouchData touch;

Expand Down Expand Up @@ -261,6 +261,8 @@ void Psp2Ui::ProcessEvents() {
}
}
}

return true;
}

void Psp2Ui::UpdateDisplay() {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/psvita/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Psp2Ui final : public BaseUi {
*/
/** @{ */
void UpdateDisplay() override;
void ProcessEvents() override;
bool ProcessEvents() override;
void SetScalingMode(ConfigEnum::ScalingMode) override;
void ToggleStretch() override;
void ToggleTouchUi() override;
Expand Down
7 changes: 4 additions & 3 deletions src/platform/sdl/sdl2_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,10 @@ void Sdl2Ui::ToggleZoom() {
#endif
}

void Sdl2Ui::ProcessEvents() {
bool Sdl2Ui::ProcessEvents() {
#if defined(__WIIU__)
if (!WiiU_ProcessProcUI()) {
Player::Exit();
return;
return false;
}
#endif

Expand All @@ -555,6 +554,8 @@ void Sdl2Ui::ProcessEvents() {
if (Player::exit_flag)
break;
}

return true;
}

void Sdl2Ui::SetScalingMode(ConfigEnum::ScalingMode mode) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/sdl/sdl2_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Sdl2Ui final : public BaseUi {
void UpdateDisplay() override;
void SetTitle(const std::string &title) override;
bool ShowCursor(bool flag) override;
void ProcessEvents() override;
bool ProcessEvents() override;
void SetScalingMode(ConfigEnum::ScalingMode) override;
void ToggleStretch() override;
void ToggleVsync() override;
Expand Down
4 changes: 3 additions & 1 deletion src/platform/sdl/sdl_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void SdlUi::ToggleZoom() {
EndDisplayModeChange();
}

void SdlUi::ProcessEvents() {
bool SdlUi::ProcessEvents() {
SDL_Event evnt;

// Poll SDL events and process them
Expand All @@ -414,6 +414,8 @@ void SdlUi::ProcessEvents() {
if (Player::exit_flag)
break;
}

return true;
}

void SdlUi::UpdateDisplay() {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/sdl/sdl_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SdlUi final : public BaseUi {
void UpdateDisplay() override;
void SetTitle(const std::string &title) override;
bool ShowCursor(bool flag) override;
void ProcessEvents() override;
bool ProcessEvents() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;

#ifdef SUPPORT_AUDIO
Expand Down
4 changes: 3 additions & 1 deletion src/platform/switch/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ NxUi::~NxUi() {
appletUnhook(&applet_hook_cookie);
}

void NxUi::ProcessEvents() {
bool NxUi::ProcessEvents() {
// handle system events
appletMainLoop();

Expand Down Expand Up @@ -453,6 +453,8 @@ void NxUi::ProcessEvents() {
}
}
}

return true;
}

void NxUi::UpdateDisplay() {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/switch/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NxUi final : public BaseUi {
*/
/** @{ */
void UpdateDisplay() override;
void ProcessEvents() override;
bool ProcessEvents() override;
void ToggleStretch() override;
void ToggleTouchUi() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;
Expand Down
15 changes: 12 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,22 @@ void Player::MainLoop() {

Player::UpdateInput();

if (!DisplayUi->ProcessEvents()) {
Scene::PopUntil(Scene::Null);
Player::Exit();
return;
}

int num_updates = 0;
while (Game_Clock::NextGameTimeStep()) {
if (num_updates > 0) {
Player::UpdateInput();

if (!DisplayUi->ProcessEvents()) {
Scene::PopUntil(Scene::Null);
Player::Exit();
return;
}
}

Scene::old_instances.clear();
Expand Down Expand Up @@ -306,9 +318,6 @@ void Player::UpdateInput() {
if (Main_Data::game_quit) {
reset_flag |= Main_Data::game_quit->ShouldQuit();
}

// Update Logic:
DisplayUi->ProcessEvents();
}

void Player::Update(bool update_scene) {
Expand Down

0 comments on commit 4b09236

Please sign in to comment.