Debian Codegraph @

Source/Core/DolphinQt/Host.cpp

raw symbols SCIP
1 // Copyright 2015 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Host.h"
5
6 #include <functional>
7
8 #include <QAbstractEventDispatcher>
9 #include <QApplication>
10 #include <QLocale>
11 #include <QThread>
12
13 #include <imgui.h>
14
15 #ifdef _WIN32
16 #include <windows.h>
17 #endif
18
19 #include "Core/Config/MainSettings.h"
20 #include "Core/ConfigManager.h"
21 #include "Core/Core.h"
22 #include "Core/Debugger/PPCDebugInterface.h"
23 #include "Core/Host.h"
24 #include "Core/NetPlayProto.h"
25 #include "Core/State.h"
26 #include "Core/System.h"
27
28 #ifdef HAS_LIBMGBA
29 #include "DolphinQt/GBAWidget.h"
30 #endif
31 #include "DolphinQt/QtUtils/QueueOnObject.h"
32 #include "DolphinQt/Settings.h"
33
34 #include "InputCommon/ControllerInterface/ControllerInterface.h"
35
36 #include "UICommon/DiscordPresence.h"
37
38 #include "VideoCommon/AbstractGfx.h"
39 #include "VideoCommon/Fifo.h"
40 #include "VideoCommon/Present.h"
41 #include "VideoCommon/VideoConfig.h"
42
43 Host::Host()
44 {
45 State::SetOnAfterLoadCallback([] { Host_UpdateDisasmDialog(); });
46 }
47
48 Host::~Host()
49 {
50 State::SetOnAfterLoadCallback(nullptr);
51 }
52
53 Host* Host::GetInstance()
54 {
55 static Host* s_instance = new Host();
56 return s_instance;
57 }
58
59 void Host::SetRenderHandle(void* handle)
60 {
61 m_render_to_main = Config::Get(Config::MAIN_RENDER_TO_MAIN);
62
63 if (m_render_handle == handle)
64 return;
65
66 m_render_handle = handle;
67 if (g_presenter)
68 {
69 g_presenter->ChangeSurface(handle);
70 g_controller_interface.ChangeWindow(handle);
71 }
72 }
73
74 void Host::SetMainWindowHandle(void* handle)
75 {
76 m_main_window_handle = handle;
77 }
78
79 static void RunWithGPUThreadInactive(const std::function<void()>& f)
80 {
81 // Potentially any thread which shows panic alerts can be blocked on this returning.
82 // This means that, in order to avoid deadlocks, we need to be careful with how we
83 // synchronize with other threads. Note that the panic alert handler temporarily declares
84 // us as the CPU and/or GPU thread if the panic alert was requested by that thread.
85
86 // TODO: What about the unlikely case where the GPU thread calls the panic alert handler
87 // while the panic alert handler is processing a panic alert from the CPU thread?
88
89 if (Core::IsGPUThread())
90 {
91 // If we are the GPU thread, we can't call Core::PauseAndLock without getting a deadlock,
92 // since it would try to pause the GPU thread while that thread is waiting for us.
93 // However, since we know that the GPU thread is inactive, we can just run f directly.
94
95 f();
96 }
97 else if (Core::IsCPUThread())
98 {
99 // If we are the CPU thread in dual core mode, we can't call Core::PauseAndLock, for the
100 // same reason as above. Instead, we use Fifo::PauseAndLock to pause the GPU thread only.
101 // (Note that this case cannot be reached in single core mode, because in single core mode,
102 // the CPU and GPU threads are the same thread, and we already checked for the GPU thread.)
103
104 auto& system = Core::System::GetInstance();
105 const bool was_running = Core::GetState(system) == Core::State::Running;
106 auto& fifo = system.GetFifo();
107 fifo.PauseAndLock();
108 f();
109 fifo.RestoreState(was_running);
110 }
111 else
112 {
113 // If we reach here, we can call Core::PauseAndLock (which we do using a CPUThreadGuard).
114 const Core::CPUThreadGuard guard(Core::System::GetInstance());
115 f();
116 }
117 }
118
119 bool Host::GetRenderFocus()
120 {
121 #ifdef _WIN32
122 // Unfortunately Qt calls SetRenderFocus() with a slight delay compared to what we actually need
123 // to avoid inputs that cause a focus loss to be processed by the emulation
124 if (m_render_to_main && !m_render_fullscreen)
125 return GetForegroundWindow() == (HWND)m_main_window_handle.load();
126 return GetForegroundWindow() == (HWND)m_render_handle.load();
127 #else
128 return m_render_focus;
129 #endif
130 }
131
132 bool Host::GetRenderFullFocus()
133 {
134 return m_render_full_focus;
135 }
136
137 void Host::SetRenderFocus(bool focus)
138 {
139 m_render_focus = focus;
140 if (g_gfx && m_render_fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
141 {
142 RunWithGPUThreadInactive([focus] {
143 if (!Config::Get(Config::MAIN_RENDER_TO_MAIN))
144 g_gfx->SetFullscreen(focus);
145 });
146 }
147 }
148
149 void Host::SetRenderFullFocus(bool focus)
150 {
151 m_render_full_focus = focus;
152 }
153
154 bool Host::GetGBAFocus()
155 {
156 #ifdef HAS_LIBMGBA
157 return qobject_cast<GBAWidget*>(QApplication::activeWindow()) != nullptr;
158 #else
159 return false;
160 #endif
161 }
162
163 bool Host::GetTASInputFocus() const
164 {
165 return m_tas_input_focus;
166 }
167
168 bool Host::GetRenderFullscreen()
169 {
170 return m_render_fullscreen;
171 }
172
173 void Host::SetRenderFullscreen(bool fullscreen)
174 {
175 m_render_fullscreen = fullscreen;
176
177 if (g_gfx && g_gfx->IsFullscreen() != fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
178 {
179 RunWithGPUThreadInactive([fullscreen] { g_gfx->SetFullscreen(fullscreen); });
180 }
181 }
182
183 void Host::SetTASInputFocus(const bool focus)
184 {
185 m_tas_input_focus = focus;
186 }
187
188 void Host::ResizeSurface(int new_width, int new_height)
189 {
190 if (g_presenter)
191 g_presenter->ResizeSurface();
192 }
193
194 std::vector<std::string> Host_GetPreferredLocales()
195 {
196 const QStringList ui_languages = QLocale::system().uiLanguages();
197 std::vector<std::string> converted_languages(ui_languages.size());
198
199 for (int i = 0; i < ui_languages.size(); ++i)
200 converted_languages[i] = ui_languages[i].toStdString();
201
202 return converted_languages;
203 }
204
205 void Host_Message(HostMessageID id)
206 {
207 if (id == HostMessageID::WMUserStop)
208 {
209 emit Host::GetInstance()->RequestStop();
210 }
211 else if (id == HostMessageID::WMUserJobDispatch)
212 {
213 // Just poke the main thread to get it to wake up, job dispatch
214 // will happen automatically before it goes back to sleep again.
215 QAbstractEventDispatcher::instance(qApp->thread())->wakeUp();
216 }
217 }
218
219 void Host_UpdateTitle(const std::string& title)
220 {
221 emit Host::GetInstance()->RequestTitle(QString::fromStdString(title));
222 }
223
224 bool Host_RendererHasFocus()
225 {
226 return Host::GetInstance()->GetRenderFocus() || Host::GetInstance()->GetGBAFocus();
227 }
228
229 bool Host_RendererHasFullFocus()
230 {
231 return Host::GetInstance()->GetRenderFullFocus();
232 }
233
234 bool Host_RendererIsFullscreen()
235 {
236 return Host::GetInstance()->GetRenderFullscreen();
237 }
238
239 bool Host_TASInputHasFocus()
240 {
241 return Host::GetInstance()->GetTASInputFocus();
242 }
243
244 void Host_YieldToUI()
245 {
246 if (qApp->thread() == QThread::currentThread())
247 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
248 }
249
250 void Host_UpdateDisasmDialog()
251 {
252 if (Settings::Instance().GetIsContinuouslyFrameStepping())
253 return;
254
255 QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->UpdateDisasmDialog(); });
256 }
257
258 void Host_JitCacheInvalidation()
259 {
260 QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->JitCacheInvalidation(); });
261 }
262
263 void Host_JitProfileDataWiped()
264 {
265 QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->JitProfileDataWiped(); });
266 }
267
268 void Host_PPCSymbolsChanged()
269 {
270 QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->PPCSymbolsChanged(); });
271 }
272
273 void Host_PPCBreakpointsChanged()
274 {
275 QueueOnObject(QApplication::instance(),
276 [] { emit Host::GetInstance()->PPCBreakpointsChanged(); });
277 }
278
279 void Host_RequestRenderWindowSize(int w, int h)
280 {
281 emit Host::GetInstance()->RequestRenderSize(w, h);
282 }
283
284 bool Host_UIBlocksControllerState()
285 {
286 // TODO: Remove the Paused check once async presentation is implemented.
287 return ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard &&
288 Core::GetState(Core::System::GetInstance()) != Core::State::Paused;
289 }
290
291 void Host_TitleChanged()
292 {
293 #ifdef USE_DISCORD_PRESENCE
294 // TODO: Not sure if the NetPlay check is needed.
295 if (!NetPlay::IsNetPlayRunning())
296 Discord::UpdateDiscordPresence();
297 #endif
298 }
299
300 void Host_UpdateDiscordClientID(const std::string& client_id)
301 {
302 #ifdef USE_DISCORD_PRESENCE
303 Discord::UpdateClientID(client_id);
304 #endif
305 }
306
307 bool Host_UpdateDiscordPresenceRaw(const std::string& details, const std::string& state,
308 const std::string& large_image_key,
309 const std::string& large_image_text,
310 const std::string& small_image_key,
311 const std::string& small_image_text,
312 const int64_t start_timestamp, const int64_t end_timestamp,
313 const int party_size, const int party_max)
314 {
315 #ifdef USE_DISCORD_PRESENCE
316 return Discord::UpdateDiscordPresenceRaw(details, state, large_image_key, large_image_text,
317 small_image_key, small_image_text, start_timestamp,
318 end_timestamp, party_size, party_max);
319 #else
320 return false;
321 #endif
322 }
323
324 #ifndef HAS_LIBMGBA
325 std::unique_ptr<GBAHostInterface> Host_CreateGBAHost(std::weak_ptr<HW::GBA::Core> core)
326 {
327 return nullptr;
328 }
329 #endif