summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/Host.cpp
blob: ea7b1920c6cdcd949219a09ccf947bb3cba46d56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/Host.h"

#include <functional>

#include <QAbstractEventDispatcher>
#include <QApplication>
#include <QLocale>
#include <QThread>

#include <imgui.h>

#ifdef _WIN32
#include <windows.h>
#endif

#include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/Debugger/PPCDebugInterface.h"
#include "Core/Host.h"
#include "Core/NetPlayProto.h"
#include "Core/State.h"
#include "Core/System.h"

#ifdef HAS_LIBMGBA
#include "DolphinQt/GBAWidget.h"
#endif
#include "DolphinQt/QtUtils/QueueOnObject.h"
#include "DolphinQt/Settings.h"

#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include "UICommon/DiscordPresence.h"

#include "VideoCommon/AbstractGfx.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/Present.h"
#include "VideoCommon/VideoConfig.h"

Host::Host()
{
  State::SetOnAfterLoadCallback([] { Host_UpdateDisasmDialog(); });
}

Host::~Host()
{
  State::SetOnAfterLoadCallback(nullptr);
}

Host* Host::GetInstance()
{
  static Host* s_instance = new Host();
  return s_instance;
}

void Host::SetRenderHandle(void* handle)
{
  m_render_to_main = Config::Get(Config::MAIN_RENDER_TO_MAIN);

  if (m_render_handle == handle)
    return;

  m_render_handle = handle;
  if (g_presenter)
  {
    g_presenter->ChangeSurface(handle);
    g_controller_interface.ChangeWindow(handle);
  }
}

void Host::SetMainWindowHandle(void* handle)
{
  m_main_window_handle = handle;
}

static void RunWithGPUThreadInactive(std::function<void()> f)
{
  // 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?

  if (Core::IsGPUThread())
  {
    // 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.

    f();
  }
  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.)

    auto& system = Core::System::GetInstance();
    const bool was_running = Core::GetState(system) == Core::State::Running;
    auto& fifo = system.GetFifo();
    fifo.PauseAndLock();
    f();
    fifo.RestoreState(was_running);
  }
  else
  {
    // If we reach here, we can call Core::PauseAndLock (which we do using a CPUThreadGuard).
    const Core::CPUThreadGuard guard(Core::System::GetInstance());
    f();
  }
}

bool Host::GetRenderFocus()
{
#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
  if (m_render_to_main && !m_render_fullscreen)
    return GetForegroundWindow() == (HWND)m_main_window_handle.load();
  return GetForegroundWindow() == (HWND)m_render_handle.load();
#else
  return m_render_focus;
#endif
}

bool Host::GetRenderFullFocus()
{
  return m_render_full_focus;
}

void Host::SetRenderFocus(bool focus)
{
  m_render_focus = focus;
  if (g_gfx && m_render_fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
  {
    RunWithGPUThreadInactive([focus] {
      if (!Config::Get(Config::MAIN_RENDER_TO_MAIN))
        g_gfx->SetFullscreen(focus);
    });
  }
}

void Host::SetRenderFullFocus(bool focus)
{
  m_render_full_focus = focus;
}

bool Host::GetGBAFocus()
{
#ifdef HAS_LIBMGBA
  return qobject_cast<GBAWidget*>(QApplication::activeWindow()) != nullptr;
#else
  return false;
#endif
}

bool Host::GetTASInputFocus() const
{
  return m_tas_input_focus;
}

bool Host::GetRenderFullscreen()
{
  return m_render_fullscreen;
}

void Host::SetRenderFullscreen(bool fullscreen)
{
  m_render_fullscreen = fullscreen;

  if (g_gfx && g_gfx->IsFullscreen() != fullscreen && g_ActiveConfig.ExclusiveFullscreenEnabled())
  {
    RunWithGPUThreadInactive([fullscreen] { g_gfx->SetFullscreen(fullscreen); });
  }
}

void Host::SetTASInputFocus(const bool focus)
{
  m_tas_input_focus = focus;
}

void Host::ResizeSurface(int new_width, int new_height)
{
  if (g_presenter)
    g_presenter->ResizeSurface();
}

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;
}

void Host_Message(HostMessageID id)
{
  if (id == HostMessageID::WMUserStop)
  {
    emit Host::GetInstance()->RequestStop();
  }
  else if (id == HostMessageID::WMUserJobDispatch)
  {
    // 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();
  }
}

void Host_UpdateTitle(const std::string& title)
{
  emit Host::GetInstance()->RequestTitle(QString::fromStdString(title));
}

bool Host_RendererHasFocus()
{
  return Host::GetInstance()->GetRenderFocus() || Host::GetInstance()->GetGBAFocus();
}

bool Host_RendererHasFullFocus()
{
  return Host::GetInstance()->GetRenderFullFocus();
}

bool Host_RendererIsFullscreen()
{
  return Host::GetInstance()->GetRenderFullscreen();
}

bool Host_TASInputHasFocus()
{
  return Host::GetInstance()->GetTASInputFocus();
}

void Host_YieldToUI()
{
  if (qApp->thread() == QThread::currentThread())
    qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}

void Host_UpdateDisasmDialog()
{
  if (Settings::Instance().GetIsContinuouslyFrameStepping())
    return;

  QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->UpdateDisasmDialog(); });
}

void Host_JitCacheInvalidation()
{
  QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->JitCacheInvalidation(); });
}

void Host_JitProfileDataWiped()
{
  QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->JitProfileDataWiped(); });
}

void Host_PPCSymbolsChanged()
{
  QueueOnObject(QApplication::instance(), [] { emit Host::GetInstance()->PPCSymbolsChanged(); });
}

void Host_PPCBreakpointsChanged()
{
  QueueOnObject(QApplication::instance(),
                [] { emit Host::GetInstance()->PPCBreakpointsChanged(); });
}

void Host_RequestRenderWindowSize(int w, int h)
{
  emit Host::GetInstance()->RequestRenderSize(w, h);
}

bool Host_UIBlocksControllerState()
{
  // TODO: Remove the Paused check once async presentation is implemented.
  return ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard &&
         Core::GetState(Core::System::GetInstance()) != Core::State::Paused;
}

void Host_TitleChanged()
{
#ifdef USE_DISCORD_PRESENCE
  // TODO: Not sure if the NetPlay check is needed.
  if (!NetPlay::IsNetPlayRunning())
    Discord::UpdateDiscordPresence();
#endif
}

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
}

#ifndef HAS_LIBMGBA
std::unique_ptr<GBAHostInterface> Host_CreateGBAHost(std::weak_ptr<HW::GBA::Core> core)
{
  return nullptr;
}
#endif