2015-11-27 01:33:07 -07:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Host.h"
|
2018-01-20 13:00:28 -07:00
|
|
|
|
2020-04-24 16:26:51 -06:00
|
|
|
#include <functional>
|
|
|
|
|
2016-05-11 19:18:30 -06:00
|
|
|
#include <QAbstractEventDispatcher>
|
|
|
|
#include <QApplication>
|
2020-12-27 15:11:22 -07:00
|
|
|
#include <QLocale>
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2019-03-16 18:09:06 -06:00
|
|
|
#include <imgui.h>
|
|
|
|
|
2021-05-09 04:28:04 -06:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
#include "Common/Common.h"
|
2018-05-27 19:48:04 -06:00
|
|
|
|
2019-03-02 21:41:50 -07:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-01-20 13:00:28 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-02-14 15:25:01 -07:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/Debugger/PPCDebugInterface.h"
|
2015-11-27 01:33:07 -07:00
|
|
|
#include "Core/Host.h"
|
2019-02-13 19:31:31 -07:00
|
|
|
#include "Core/NetPlayProto.h"
|
2018-02-14 15:25:01 -07:00
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
2020-04-14 16:12:35 -06:00
|
|
|
#include "Core/State.h"
|
2023-12-14 03:24:06 -07:00
|
|
|
#include "Core/System.h"
|
2018-05-27 19:48:04 -06:00
|
|
|
|
2021-07-04 05:23:30 -06:00
|
|
|
#ifdef HAS_LIBMGBA
|
|
|
|
#include "DolphinQt/GBAWidget.h"
|
|
|
|
#endif
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/QtUtils/QueueOnObject.h"
|
|
|
|
#include "DolphinQt/Settings.h"
|
2018-05-27 19:48:04 -06:00
|
|
|
|
2018-10-27 05:22:55 -06:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
|
2019-02-13 19:31:31 -07:00
|
|
|
#include "UICommon/DiscordPresence.h"
|
|
|
|
|
2023-01-26 15:34:59 -07:00
|
|
|
#include "VideoCommon/AbstractGfx.h"
|
2023-12-14 03:24:06 -07:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2023-01-26 21:03:15 -07:00
|
|
|
#include "VideoCommon/Present.h"
|
2018-05-03 06:16:21 -06:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2020-04-14 16:12:35 -06:00
|
|
|
Host::Host()
|
|
|
|
{
|
2021-03-03 14:18:17 -07:00
|
|
|
State::SetOnAfterLoadCallback([] { Host_UpdateDisasmDialog(); });
|
2020-04-14 16:12:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Host::~Host()
|
|
|
|
{
|
|
|
|
State::SetOnAfterLoadCallback(nullptr);
|
|
|
|
}
|
2015-11-27 01:33:07 -07:00
|
|
|
|
|
|
|
Host* Host::GetInstance()
|
|
|
|
{
|
2017-07-13 13:58:26 -06:00
|
|
|
static Host* s_instance = new Host();
|
|
|
|
return s_instance;
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Host::SetRenderHandle(void* handle)
|
|
|
|
{
|
2021-05-09 04:28:04 -06:00
|
|
|
m_render_to_main = Config::Get(Config::MAIN_RENDER_TO_MAIN);
|
|
|
|
|
2018-10-27 05:18:54 -06:00
|
|
|
if (m_render_handle == handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_render_handle = handle;
|
2023-01-26 21:03:15 -07:00
|
|
|
if (g_presenter)
|
2018-10-27 05:22:55 -06:00
|
|
|
{
|
2023-01-26 21:03:15 -07:00
|
|
|
g_presenter->ChangeSurface(handle);
|
2021-05-20 16:33:38 -06:00
|
|
|
g_controller_interface.ChangeWindow(handle);
|
2018-10-27 05:22:55 -06:00
|
|
|
}
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 04:28:04 -06:00
|
|
|
void Host::SetMainWindowHandle(void* handle)
|
|
|
|
{
|
|
|
|
m_main_window_handle = handle;
|
|
|
|
}
|
|
|
|
|
2020-04-24 16:26:51 -06:00
|
|
|
static void RunWithGPUThreadInactive(std::function<void()> f)
|
|
|
|
{
|
2020-04-25 02:35:14 -06:00
|
|
|
// Potentially any thread which shows panic alerts can be blocked on this returning.
|
|
|
|
// This means that, in order to avoid deadlocks, we need to be careful with how we
|
|
|
|
// synchronize with other threads. Note that the panic alert handler temporarily declares
|
|
|
|
// us as the CPU and/or GPU thread if the panic alert was requested by that thread.
|
|
|
|
|
|
|
|
// TODO: What about the unlikely case where the GPU thread calls the panic alert handler
|
|
|
|
// while the panic alert handler is processing a panic alert from the CPU thread?
|
2020-04-24 16:26:51 -06:00
|
|
|
|
|
|
|
if (Core::IsGPUThread())
|
2020-04-25 02:35:14 -06:00
|
|
|
{
|
|
|
|
// If we are the GPU thread, we can't call Core::PauseAndLock without getting a deadlock,
|
|
|
|
// since it would try to pause the GPU thread while that thread is waiting for us.
|
|
|
|
// However, since we know that the GPU thread is inactive, we can just run f directly.
|
|
|
|
|
2020-04-24 16:26:51 -06:00
|
|
|
f();
|
2020-04-25 02:35:14 -06:00
|
|
|
}
|
|
|
|
else if (Core::IsCPUThread())
|
|
|
|
{
|
|
|
|
// If we are the CPU thread in dual core mode, we can't call Core::PauseAndLock, for the
|
|
|
|
// same reason as above. Instead, we use Fifo::PauseAndLock to pause the GPU thread only.
|
|
|
|
// (Note that this case cannot be reached in single core mode, because in single core mode,
|
|
|
|
// the CPU and GPU threads are the same thread, and we already checked for the GPU thread.)
|
|
|
|
|
2022-12-09 14:59:11 -07:00
|
|
|
auto& system = Core::System::GetInstance();
|
2024-03-28 12:35:13 -06:00
|
|
|
const bool was_running = Core::GetState(system) == Core::State::Running;
|
2022-12-09 14:59:11 -07:00
|
|
|
auto& fifo = system.GetFifo();
|
2023-12-18 19:31:32 -07:00
|
|
|
fifo.PauseAndLock(true, was_running);
|
2020-04-25 02:35:14 -06:00
|
|
|
f();
|
2023-12-18 19:31:32 -07:00
|
|
|
fifo.PauseAndLock(false, was_running);
|
2020-04-25 02:35:14 -06:00
|
|
|
}
|
2020-04-24 16:26:51 -06:00
|
|
|
else
|
2020-04-25 02:35:14 -06:00
|
|
|
{
|
2024-03-22 01:43:04 -06:00
|
|
|
// If we reach here, we can call Core::PauseAndLock (which we do using a CPUThreadGuard).
|
|
|
|
const Core::CPUThreadGuard guard(Core::System::GetInstance());
|
|
|
|
f();
|
2020-04-25 02:35:14 -06:00
|
|
|
}
|
2020-04-24 16:26:51 -06:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
bool Host::GetRenderFocus()
|
|
|
|
{
|
2021-05-09 04:28:04 -06:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Unfortunately Qt calls SetRenderFocus() with a slight delay compared to what we actually need
|
|
|
|
// to avoid inputs that cause a focus loss to be processed by the emulation
|
2021-06-07 05:35:59 -06:00
|
|
|
if (m_render_to_main && !m_render_fullscreen)
|
2021-05-09 04:28:04 -06:00
|
|
|
return GetForegroundWindow() == (HWND)m_main_window_handle.load();
|
|
|
|
return GetForegroundWindow() == (HWND)m_render_handle.load();
|
|
|
|
#else
|
2015-11-27 01:33:07 -07:00
|
|
|
return m_render_focus;
|
2021-05-09 04:28:04 -06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Host::GetRenderFullFocus()
|
|
|
|
{
|
|
|
|
return m_render_full_focus;
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Host::SetRenderFocus(bool focus)
|
|
|
|
{
|
|
|
|
m_render_focus = focus;
|
2023-01-26 15:34:59 -07:00
|
|
|
if (g_gfx && m_render_fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
|
2020-04-24 16:26:51 -06:00
|
|
|
{
|
|
|
|
RunWithGPUThreadInactive([focus] {
|
2019-03-02 21:41:50 -07:00
|
|
|
if (!Config::Get(Config::MAIN_RENDER_TO_MAIN))
|
2023-01-26 15:34:59 -07:00
|
|
|
g_gfx->SetFullscreen(focus);
|
2018-05-15 10:22:26 -06:00
|
|
|
});
|
2020-04-24 16:26:51 -06:00
|
|
|
}
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 04:28:04 -06:00
|
|
|
void Host::SetRenderFullFocus(bool focus)
|
|
|
|
{
|
|
|
|
m_render_full_focus = focus;
|
|
|
|
}
|
|
|
|
|
2021-07-04 05:23:30 -06:00
|
|
|
bool Host::GetGBAFocus()
|
|
|
|
{
|
|
|
|
#ifdef HAS_LIBMGBA
|
|
|
|
return qobject_cast<GBAWidget*>(QApplication::activeWindow()) != nullptr;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-05-26 17:50:12 -06:00
|
|
|
bool Host::GetTASInputFocus() const
|
|
|
|
{
|
|
|
|
return m_tas_input_focus;
|
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
bool Host::GetRenderFullscreen()
|
|
|
|
{
|
|
|
|
return m_render_fullscreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Host::SetRenderFullscreen(bool fullscreen)
|
|
|
|
{
|
|
|
|
m_render_fullscreen = fullscreen;
|
2018-05-03 06:16:21 -06:00
|
|
|
|
2023-01-26 15:34:59 -07:00
|
|
|
if (g_gfx && g_gfx->IsFullscreen() != fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
|
2020-04-24 16:26:51 -06:00
|
|
|
{
|
2023-01-26 15:34:59 -07:00
|
|
|
RunWithGPUThreadInactive([fullscreen] { g_gfx->SetFullscreen(fullscreen); });
|
2020-04-24 16:26:51 -06:00
|
|
|
}
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2024-05-26 17:50:12 -06:00
|
|
|
void Host::SetTASInputFocus(const bool focus)
|
|
|
|
{
|
|
|
|
m_tas_input_focus = focus;
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:23:24 -07:00
|
|
|
void Host::ResizeSurface(int new_width, int new_height)
|
2018-01-20 13:00:28 -07:00
|
|
|
{
|
2023-01-26 21:03:15 -07:00
|
|
|
if (g_presenter)
|
|
|
|
g_presenter->ResizeSurface();
|
2018-01-20 13:00:28 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 15:11:22 -07:00
|
|
|
std::vector<std::string> Host_GetPreferredLocales()
|
|
|
|
{
|
|
|
|
const QStringList ui_languages = QLocale::system().uiLanguages();
|
|
|
|
std::vector<std::string> converted_languages(ui_languages.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < ui_languages.size(); ++i)
|
|
|
|
converted_languages[i] = ui_languages[i].toStdString();
|
|
|
|
|
|
|
|
return converted_languages;
|
|
|
|
}
|
|
|
|
|
2018-05-28 11:03:29 -06:00
|
|
|
void Host_Message(HostMessageID id)
|
2015-11-27 01:33:07 -07:00
|
|
|
{
|
2018-05-28 11:03:29 -06:00
|
|
|
if (id == HostMessageID::WMUserStop)
|
2016-05-11 19:18:30 -06:00
|
|
|
{
|
2015-11-27 01:33:07 -07:00
|
|
|
emit Host::GetInstance()->RequestStop();
|
2016-05-11 19:18:30 -06:00
|
|
|
}
|
2018-05-28 11:03:29 -06:00
|
|
|
else if (id == HostMessageID::WMUserJobDispatch)
|
2016-05-11 19:18:30 -06:00
|
|
|
{
|
|
|
|
// Just poke the main thread to get it to wake up, job dispatch
|
|
|
|
// will happen automatically before it goes back to sleep again.
|
|
|
|
QAbstractEventDispatcher::instance(qApp->thread())->wakeUp();
|
|
|
|
}
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Host_UpdateTitle(const std::string& title)
|
|
|
|
{
|
|
|
|
emit Host::GetInstance()->RequestTitle(QString::fromStdString(title));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Host_RendererHasFocus()
|
|
|
|
{
|
2021-07-04 05:23:30 -06:00
|
|
|
return Host::GetInstance()->GetRenderFocus() || Host::GetInstance()->GetGBAFocus();
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 04:28:04 -06:00
|
|
|
bool Host_RendererHasFullFocus()
|
|
|
|
{
|
|
|
|
return Host::GetInstance()->GetRenderFullFocus();
|
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
bool Host_RendererIsFullscreen()
|
|
|
|
{
|
|
|
|
return Host::GetInstance()->GetRenderFullscreen();
|
|
|
|
}
|
2018-02-14 15:25:01 -07:00
|
|
|
|
2024-05-26 17:50:12 -06:00
|
|
|
bool Host_TASInputHasFocus()
|
|
|
|
{
|
|
|
|
return Host::GetInstance()->GetTASInputFocus();
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:55:21 -07:00
|
|
|
void Host_YieldToUI()
|
|
|
|
{
|
2016-11-11 05:39:50 -07:00
|
|
|
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
|
2016-11-10 09:55:21 -07:00
|
|
|
}
|
2018-02-14 15:25:01 -07:00
|
|
|
|
|
|
|
void Host_UpdateDisasmDialog()
|
|
|
|
{
|
2024-05-17 22:16:59 -06:00
|
|
|
if (Settings::Instance().GetIsContinuouslyFrameStepping())
|
|
|
|
return;
|
|
|
|
|
2018-05-15 22:10:40 -06:00
|
|
|
QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->UpdateDisasmDialog(); });
|
2018-02-14 15:25:01 -07:00
|
|
|
}
|
|
|
|
|
2024-03-17 00:05:56 -06:00
|
|
|
void Host_PPCSymbolsChanged()
|
2018-05-16 19:43:18 -06:00
|
|
|
{
|
2024-03-17 00:05:56 -06:00
|
|
|
QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->PPCSymbolsChanged(); });
|
2018-05-16 19:43:18 -06:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
// We ignore these, and their purpose should be questioned individually.
|
|
|
|
// In particular, RequestRenderWindowSize, RequestFullscreen, and
|
|
|
|
// UpdateMainFrame should almost certainly be removed.
|
|
|
|
void Host_UpdateMainFrame()
|
|
|
|
{
|
|
|
|
}
|
2018-03-24 09:40:47 -06:00
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
void Host_RequestRenderWindowSize(int w, int h)
|
|
|
|
{
|
2018-03-24 09:40:47 -06:00
|
|
|
emit Host::GetInstance()->RequestRenderSize(w, h);
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
2018-03-24 09:40:47 -06:00
|
|
|
|
2019-03-18 10:30:33 -06:00
|
|
|
bool Host_UIBlocksControllerState()
|
|
|
|
{
|
|
|
|
return ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard;
|
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
void Host_RefreshDSPDebuggerWindow()
|
|
|
|
{
|
|
|
|
}
|
2019-02-13 19:31:31 -07:00
|
|
|
|
|
|
|
void Host_TitleChanged()
|
|
|
|
{
|
|
|
|
#ifdef USE_DISCORD_PRESENCE
|
|
|
|
// TODO: Not sure if the NetPlay check is needed.
|
|
|
|
if (!NetPlay::IsNetPlayRunning())
|
|
|
|
Discord::UpdateDiscordPresence();
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-04 05:09:46 -06:00
|
|
|
|
2022-08-02 22:46:11 -06:00
|
|
|
void Host_UpdateDiscordClientID(const std::string& client_id)
|
|
|
|
{
|
|
|
|
#ifdef USE_DISCORD_PRESENCE
|
|
|
|
Discord::UpdateClientID(client_id);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Host_UpdateDiscordPresenceRaw(const std::string& details, const std::string& state,
|
|
|
|
const std::string& large_image_key,
|
|
|
|
const std::string& large_image_text,
|
|
|
|
const std::string& small_image_key,
|
|
|
|
const std::string& small_image_text,
|
|
|
|
const int64_t start_timestamp, const int64_t end_timestamp,
|
|
|
|
const int party_size, const int party_max)
|
|
|
|
{
|
|
|
|
#ifdef USE_DISCORD_PRESENCE
|
|
|
|
return Discord::UpdateDiscordPresenceRaw(details, state, large_image_key, large_image_text,
|
|
|
|
small_image_key, small_image_text, start_timestamp,
|
|
|
|
end_timestamp, party_size, party_max);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-04 05:23:30 -06:00
|
|
|
#ifndef HAS_LIBMGBA
|
2021-07-04 05:09:46 -06:00
|
|
|
std::unique_ptr<GBAHostInterface> Host_CreateGBAHost(std::weak_ptr<HW::GBA::Core> core)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-07-04 05:23:30 -06:00
|
|
|
#endif
|