Skip to content

Commit

Permalink
Require frontend to initialize controllers
Browse files Browse the repository at this point in the history
We currently have two different code paths for initializing controllers:
Either the frontend (DolphinQt) can do it, or if the frontend doesn't do
it, the core will do it automatically when booting. Having these two
paths has caused problems in the past due to only one frontend being
tested (see de7ef47). I would like to get rid of the latter path to
avoid further problems like this.
  • Loading branch information
JosJuice committed Jul 3, 2022
1 parent 77804ce commit c5c09a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 69 deletions.
5 changes: 2 additions & 3 deletions Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <EGL/egl.h>
#include <UICommon/GameFile.h>
#include <android/log.h>
#include <android/native_window_jni.h>
#include <cstdio>
Expand Down Expand Up @@ -56,13 +55,13 @@
#include "InputCommon/ControllerInterface/Touch/ButtonManager.h"
#include "InputCommon/GCAdapter.h"

#include "UICommon/GameFile.h"
#include "UICommon/UICommon.h"

#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/VideoBackendBase.h"

#include "../../Core/Common/WindowSystemInfo.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"

Expand Down Expand Up @@ -521,7 +520,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Initialize(J
Common::AndroidSetReportHandler(&ReportSend);
DolphinAnalytics::AndroidSetGetValFunc(&GetAnalyticValue);
UICommon::Init();
GCAdapter::Init();
UICommon::InitControllers(WindowSystemInfo(WindowSystemType::Android, nullptr, nullptr, nullptr));
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ReportStartToAnalytics(JNIEnv*,
Expand Down
72 changes: 12 additions & 60 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "AudioCommon/AudioCommon.h"

#include "Common/Assert.h"
#include "Common/CPUDetect.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
Expand Down Expand Up @@ -469,79 +470,30 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
DeclareAsCPUThread();
s_frame_step = false;

// The frontend will likely have initialized the controller interface, as it needs
// it to provide the configuration dialogs. In this case, instead of re-initializing
// entirely, we switch the window used for inputs to the render window. This way, the
// cursor position is relative to the render window, instead of the main window.
bool init_controllers = false;
if (!g_controller_interface.IsInit())
{
g_controller_interface.Initialize(wsi);
Pad::Initialize();
Pad::InitializeGBA();
Keyboard::Initialize();
init_controllers = true;
}
else
{
g_controller_interface.ChangeWindow(wsi.render_window);
Pad::LoadConfig();
Pad::LoadGBAConfig();
Keyboard::LoadConfig();
}
// Switch the window used for inputs to the render window. This way, the cursor position
// is relative to the render window, instead of the main window.
ASSERT(g_controller_interface.IsInit());
g_controller_interface.ChangeWindow(wsi.render_window);

Pad::LoadConfig();
Pad::LoadGBAConfig();
Keyboard::LoadConfig();

BootSessionData boot_session_data = std::move(boot->boot_session_data);
const std::optional<std::string>& savestate_path = boot_session_data.GetSavestatePath();
const bool delete_savestate =
boot_session_data.GetDeleteSavestate() == DeleteSavestateAfterBoot::Yes;

// Load and Init Wiimotes - only if we are booting in Wii mode
bool init_wiimotes = false;
// Load Wiimotes - only if we are booting in Wii mode
if (core_parameter.bWii && !Config::Get(Config::MAIN_BLUETOOTH_PASSTHROUGH_ENABLED))
{
if (init_controllers)
{
Wiimote::Initialize(savestate_path ? Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES :
Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
init_wiimotes = true;
}
else
{
Wiimote::LoadConfig();
}
Wiimote::LoadConfig();

if (NetPlay::IsNetPlayRunning())
NetPlay::SetupWiimotes();
}

if (init_controllers)
{
FreeLook::Initialize();
}
else
{
FreeLook::LoadInputConfig();
}

Common::ScopeGuard controller_guard{[init_controllers, init_wiimotes] {
if (!init_controllers)
return;

if (init_wiimotes)
{
Wiimote::ResetAllWiimotes();
Wiimote::Shutdown();
}

FreeLook::Shutdown();

ResetRumble();

Keyboard::Shutdown();
Pad::Shutdown();
Pad::ShutdownGBA();
g_controller_interface.Shutdown();
}};
FreeLook::LoadInputConfig();

Movie::Init(*boot);
Common::ScopeGuard movie_guard{&Movie::Shutdown};
Expand Down
19 changes: 13 additions & 6 deletions Source/Core/DolphinNoGUI/MainNoGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Windows.h>
#endif

#include "Common/ScopeGuard.h"
#include "Common/StringUtil.h"
#include "Core/Boot/Boot.h"
#include "Core/BootManager.h"
Expand Down Expand Up @@ -226,17 +227,24 @@ int main(int argc, char* argv[])
if (options.is_set("user"))
user_directory = static_cast<const char*>(options.get("user"));

UICommon::SetUserDirectory(user_directory);
UICommon::Init();
GCAdapter::Init();

s_platform = GetPlatform(options);
if (!s_platform || !s_platform->Init())
{
fprintf(stderr, "No platform found, or failed to initialize.\n");
return 1;
}

const WindowSystemInfo wsi = s_platform->GetWindowSystemInfo();

UICommon::SetUserDirectory(user_directory);
UICommon::Init();
UICommon::InitControllers(wsi);

Common::ScopeGuard ui_common_guard([] {
UICommon::ShutdownControllers();
UICommon::Shutdown();
});

if (save_state_path && !game_specified)
{
fprintf(stderr, "A save state cannot be loaded without specifying a game to launch.\n");
Expand All @@ -263,7 +271,7 @@ int main(int argc, char* argv[])

DolphinAnalytics::Instance().ReportDolphinStart("nogui");

if (!BootManager::BootCore(std::move(boot), s_platform->GetWindowSystemInfo()))
if (!BootManager::BootCore(std::move(boot), wsi))
{
fprintf(stderr, "Could not boot the specified file\n");
return 1;
Expand All @@ -278,7 +286,6 @@ int main(int argc, char* argv[])

Core::Shutdown();
s_platform.reset();
UICommon::Shutdown();

return 0;
}
Expand Down

0 comments on commit c5c09a5

Please sign in to comment.