forked from dolphin-emu/dolphin
-
Notifications
You must be signed in to change notification settings - Fork 44
/
RenderWidget.cpp
585 lines (505 loc) · 18.3 KB
/
RenderWidget.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/RenderWidget.h"
#include <array>
#include <QApplication>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileInfo>
#include <QGuiApplication>
#include <QIcon>
#include <QKeyEvent>
#include <QMimeData>
#include <QMouseEvent>
#include <QPalette>
#include <QScreen>
#include <QTimer>
#include <QWindow>
#include <imgui.h>
#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/State.h"
#include "DolphinQt/Host.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/VideoConfig.h"
#include "InputCommon/GenericMouse.h"
#ifdef _WIN32
#include <Windows.h>
#endif
RenderWidget::RenderWidget(QWidget* parent) : QWidget(parent)
{
setWindowTitle(QStringLiteral("Dolphin"));
setWindowIcon(Resources::GetAppIcon());
setWindowRole(QStringLiteral("renderer"));
setAcceptDrops(true);
QPalette p;
p.setColor(QPalette::Window, Qt::black);
setPalette(p);
connect(Host::GetInstance(), &Host::RequestTitle, this, &RenderWidget::setWindowTitle);
connect(Host::GetInstance(), &Host::RequestRenderSize, this, [this](int w, int h) {
if (!Config::Get(Config::MAIN_RENDER_WINDOW_AUTOSIZE) || isFullScreen() || isMaximized())
return;
const auto dpr = window()->windowHandle()->screen()->devicePixelRatio();
resize(w / dpr, h / dpr);
});
connect(Host::GetInstance(), &Host::UpdateAndRecenterCursor, this, [this](bool locked) {
if (locked) {
QRect render_rect = geometry();
QPoint center = mapToGlobal(QPoint(render_rect.width() / 2, render_rect.height() / 2));
cursor().setPos(windowHandle()->screen(), center);
}
setCursor((locked && Settings::Instance().GetCursorVisibility() == Config::Get(Config::MAIN_SHOW_CURSOR) ? Qt::BlankCursor :
Qt::ArrowCursor));
});
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
if (state == Core::State::Running)
SetImGuiKeyMap();
});
// We have to use Qt::DirectConnection here because we don't want those signals to get queued
// (which results in them not getting called)
connect(this, &RenderWidget::StateChanged, Host::GetInstance(), &Host::SetRenderFullscreen,
Qt::DirectConnection);
connect(this, &RenderWidget::HandleChanged, Host::GetInstance(), &Host::SetRenderHandle,
Qt::DirectConnection);
connect(this, &RenderWidget::SizeChanged, Host::GetInstance(), &Host::ResizeSurface,
Qt::DirectConnection);
connect(this, &RenderWidget::FocusChanged, Host::GetInstance(), &Host::SetRenderFocus,
Qt::DirectConnection);
m_mouse_timer = new QTimer(this);
connect(m_mouse_timer, &QTimer::timeout, this, &RenderWidget::HandleCursorTimer);
m_mouse_timer->setSingleShot(true);
setMouseTracking(true);
connect(&Settings::Instance(), &Settings::CursorVisibilityChanged, this,
&RenderWidget::OnHideCursorChanged);
connect(&Settings::Instance(), &Settings::LockCursorChanged, this,
&RenderWidget::OnLockCursorChanged);
OnHideCursorChanged();
OnLockCursorChanged();
connect(&Settings::Instance(), &Settings::KeepWindowOnTopChanged, this,
&RenderWidget::OnKeepOnTopChanged);
OnKeepOnTopChanged(Settings::Instance().IsKeepWindowOnTopEnabled());
m_mouse_timer->start(MOUSE_HIDE_DELAY);
// We need a native window to render into.
setAttribute(Qt::WA_NativeWindow);
setAttribute(Qt::WA_PaintOnScreen);
}
QPaintEngine* RenderWidget::paintEngine() const
{
return nullptr;
}
void RenderWidget::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1)
event->acceptProposedAction();
}
void RenderWidget::dropEvent(QDropEvent* event)
{
const auto& urls = event->mimeData()->urls();
if (urls.empty())
return;
const auto& url = urls[0];
QFileInfo file_info(url.toLocalFile());
auto path = file_info.filePath();
if (!file_info.exists() || !file_info.isReadable())
{
ModalMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path));
return;
}
if (!file_info.isFile())
{
return;
}
State::LoadAs(path.toStdString());
}
void RenderWidget::OnHideCursorChanged()
{
UpdateCursor();
}
void RenderWidget::OnLockCursorChanged()
{
SetCursorLocked(false);
UpdateCursor();
}
// Calling this at any time will set the cursor (image) to the correct state
void RenderWidget::UpdateCursor()
{
if (!Settings::Instance().GetLockCursor())
{
// Only hide if the cursor is automatically locking (it will hide on lock).
// "Unhide" the cursor if we lost focus, otherwise it will disappear when hovering
// on top of the game window in the background
const bool keep_on_top = (windowFlags() & Qt::WindowStaysOnTopHint) != 0;
const bool should_hide =
(Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) &&
(keep_on_top || Config::Get(Config::MAIN_INPUT_BACKGROUND_INPUT) || isActiveWindow());
setCursor(should_hide ? Qt::BlankCursor : Qt::ArrowCursor);
}
else
{
setCursor((m_cursor_locked &&
Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) ?
Qt::BlankCursor :
Qt::ArrowCursor);
}
}
void RenderWidget::OnKeepOnTopChanged(bool top)
{
const bool was_visible = isVisible();
setWindowFlags(top ? windowFlags() | Qt::WindowStaysOnTopHint :
windowFlags() & ~Qt::WindowStaysOnTopHint);
m_dont_lock_cursor_on_show = true;
if (was_visible)
show();
m_dont_lock_cursor_on_show = false;
UpdateCursor();
}
void RenderWidget::HandleCursorTimer()
{
if (!isActiveWindow())
return;
if ((!Settings::Instance().GetLockCursor() || m_cursor_locked) &&
Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement)
{
setCursor(Qt::BlankCursor);
}
}
void RenderWidget::showFullScreen()
{
QWidget::showFullScreen();
QScreen* screen = window()->windowHandle()->screen();
const auto dpr = screen->devicePixelRatio();
emit SizeChanged(width() * dpr, height() * dpr);
}
// Lock the cursor within the window/widget internal borders, including the aspect ratio if wanted
void RenderWidget::SetCursorLocked(bool locked, bool follow_aspect_ratio)
{
// It seems like QT doesn't scale the window frame correctly with some DPIs
// so it might happen that the locked cursor can be on the frame of the window,
// being able to resize it, but that is a minor problem.
// As a hack, if necessary, we could always scale down the size by 2 pixel, to a min of 1 given
// that the size can be 0 already. We probably shouldn't scale axes already scaled by aspect ratio
QRect render_rect = geometry();
if (parentWidget())
{
render_rect.moveTopLeft(parentWidget()->mapToGlobal(render_rect.topLeft()));
}
auto scale = devicePixelRatioF(); // Seems to always be rounded on Win. Should we round results?
QPoint screen_offset = QPoint(0, 0);
if (window()->windowHandle() && window()->windowHandle()->screen())
{
screen_offset = window()->windowHandle()->screen()->geometry().topLeft();
}
render_rect.moveTopLeft(((render_rect.topLeft() - screen_offset) * scale) + screen_offset);
render_rect.setSize(render_rect.size() * scale);
if (follow_aspect_ratio)
{
// TODO: SetCursorLocked() should be re-called every time this value is changed?
// This might cause imprecisions of one pixel (but it won't cause the cursor to go over borders)
Common::Vec2 aspect_ratio = g_controller_interface.GetWindowInputScale();
if (aspect_ratio.x > 1.f)
{
const float new_half_width = float(render_rect.width()) / (aspect_ratio.x * 2.f);
// Only ceil if it was >= 0.25
const float ceiled_new_half_width = std::ceil(std::round(new_half_width * 2.f) / 2.f);
const int x_center = render_rect.center().x();
// Make a guess on which one to floor and ceil.
// For more precision, we should have kept the rounding point scale from above as well.
render_rect.setLeft(x_center - std::floor(new_half_width));
render_rect.setRight(x_center + ceiled_new_half_width);
}
if (aspect_ratio.y > 1.f)
{
const float new_half_height = render_rect.height() / (aspect_ratio.y * 2.f);
const float ceiled_new_half_height = std::ceil(std::round(new_half_height * 2.f) / 2.f);
const int y_center = render_rect.center().y();
render_rect.setTop(y_center - std::floor(new_half_height));
render_rect.setBottom(y_center + ceiled_new_half_height);
}
}
if (locked)
{
#ifdef _WIN32
// This will prevent the mouse from interacting with the task bar
// while in windowed / borderless fullscreen.
SetWindowPos((HWND) winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
RECT rect;
rect.left = render_rect.left();
rect.right = render_rect.right();
rect.top = render_rect.top();
rect.bottom = render_rect.bottom();
if (ClipCursor(&rect))
#else
// TODO: implement on other platforms. Probably XGrabPointer on Linux.
// The setting is hidden in the UI if not implemented
if (false)
#endif
{
m_cursor_locked = true;
if (Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly)
{
setCursor(Qt::BlankCursor);
}
Host::GetInstance()->SetRenderFullFocus(true);
}
}
else
{
#ifdef _WIN32
SetWindowPos((HWND) winId(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
ClipCursor(nullptr);
#endif
if (m_cursor_locked)
{
m_cursor_locked = false;
if (!Settings::Instance().GetLockCursor())
{
return;
}
// Center the mouse in the window if it's still active
// Leave it where it was otherwise, e.g. a prompt has opened or we alt tabbed.
if (isActiveWindow())
{
cursor().setPos(render_rect.left() + render_rect.width() / 2,
render_rect.top() + render_rect.height() / 2);
}
// Show the cursor or the user won't know the mouse is now unlocked
setCursor(Qt::ArrowCursor);
Host::GetInstance()->SetRenderFullFocus(false);
}
}
}
void RenderWidget::SetCursorLockedOnNextActivation(bool locked)
{
if (Settings::Instance().GetLockCursor())
{
m_lock_cursor_on_next_activation = locked;
return;
}
m_lock_cursor_on_next_activation = false;
}
void RenderWidget::SetWaitingForMessageBox(bool waiting_for_message_box)
{
if (m_waiting_for_message_box == waiting_for_message_box)
{
return;
}
m_waiting_for_message_box = waiting_for_message_box;
if (!m_waiting_for_message_box && m_lock_cursor_on_next_activation && isActiveWindow())
{
if (Settings::Instance().GetLockCursor())
{
SetCursorLocked(true);
}
m_lock_cursor_on_next_activation = false;
}
}
bool RenderWidget::event(QEvent* event)
{
PassEventToImGui(event);
switch (event->type())
{
case QEvent::KeyPress:
{
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
if (ke->key() == Qt::Key_Escape)
emit EscapePressed();
// The render window might flicker on some platforms because Qt tries to change focus to a new
// element when there is none (?) Handling this event before it reaches QWidget fixes the issue.
if (ke->key() == Qt::Key_Tab)
return true;
break;
}
// Needed in case a new window open and it moves the mouse
case QEvent::WindowBlocked:
SetCursorLocked(false);
break;
case QEvent::MouseButtonPress:
if (isActiveWindow())
{
// Lock the cursor with any mouse button click (behave the same as window focus change).
// This event is occasionally missed because isActiveWindow is laggy
if (Settings::Instance().GetLockCursor())
{
SetCursorLocked(true);
}
}
break;
case QEvent::MouseMove:
// Unhide on movement
if (Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement)
{
setCursor(Qt::ArrowCursor);
m_mouse_timer->start(MOUSE_HIDE_DELAY);
}
break;
case QEvent::WinIdChange:
emit HandleChanged(reinterpret_cast<void*>(winId()));
break;
case QEvent::Show:
// Don't do if "stay on top" changed (or was true)
if (Settings::Instance().GetLockCursor() &&
Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly &&
!m_dont_lock_cursor_on_show)
{
// Auto lock when this window is shown (it was hidden)
if (isActiveWindow())
SetCursorLocked(true);
else
SetCursorLockedOnNextActivation();
}
break;
// Note that this event in Windows is not always aligned to the window that is highlighted,
// it's the window that has keyboard and mouse focus
case QEvent::WindowActivate:
if (Config::Get(Config::MAIN_PAUSE_ON_FOCUS_LOST) && Core::GetState() == Core::State::Paused)
Core::SetState(Core::State::Running);
UpdateCursor();
// Avoid "race conditions" with message boxes
if (m_lock_cursor_on_next_activation && !m_waiting_for_message_box)
{
if (Settings::Instance().GetLockCursor())
{
SetCursorLocked(true);
}
m_lock_cursor_on_next_activation = false;
}
emit FocusChanged(true);
break;
case QEvent::WindowDeactivate:
SetCursorLocked(false);
UpdateCursor();
if (Config::Get(Config::MAIN_PAUSE_ON_FOCUS_LOST) && Core::GetState() == Core::State::Running)
{
// If we are declared as the CPU thread, it means that the real CPU thread is waiting
// for us to finish showing a panic alert (with that panic alert likely being the cause
// of this event), so trying to pause the real CPU thread would cause a deadlock
if (!Core::IsCPUThread())
Core::SetState(Core::State::Paused);
}
emit FocusChanged(false);
break;
case QEvent::Move:
SetCursorLocked(m_cursor_locked);
win_w = size().width() / 2;
win_h = size().height() / 2;
break;
case QEvent::Resize:
{
#if defined(CIFACE_USE_WIN32) || defined(CIFACE_USE_XLIB)
const QResizeEvent* se = static_cast<QResizeEvent*>(event);
QSize new_size = se->size();
QScreen* screen = window()->windowHandle()->screen();
const auto dpr = screen->devicePixelRatio();
// Window Centre for PrimeHack mouselock.
win_w = (new_size.width() * dpr) / 2;
win_h = (new_size.height() * dpr) / 2;
emit SizeChanged(new_size.width() * dpr, new_size.height() * dpr);
#endif
break;
}
// Happens when we add/remove the widget from the main window instead of the dedicated one
case QEvent::ParentChange:
SetCursorLocked(false);
break;
case QEvent::WindowStateChange:
// Lock the mouse again when fullscreen changes (we might have missed some events)
SetCursorLocked(m_cursor_locked || (isFullScreen() && Settings::Instance().GetLockCursor()));
emit StateChanged(isFullScreen());
break;
case QEvent::Close:
emit Closed();
break;
default:
break;
}
return QWidget::event(event);
}
void RenderWidget::PassEventToImGui(const QEvent* event)
{
if (!Core::IsRunningAndStarted())
return;
switch (event->type())
{
case QEvent::KeyPress:
case QEvent::KeyRelease:
{
// As the imgui KeysDown array is only 512 elements wide, and some Qt keys which
// we need to track (e.g. alt) are above this value, we mask the lower 9 bits.
// Even masked, the key codes are still unique, so conflicts aren't an issue.
// The actual text input goes through AddInputCharactersUTF8().
const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event);
const bool is_down = event->type() == QEvent::KeyPress;
const u32 key = static_cast<u32>(key_event->key() & 0x1FF);
auto lock = g_renderer->GetImGuiLock();
if (key < std::size(ImGui::GetIO().KeysDown))
ImGui::GetIO().KeysDown[key] = is_down;
if (is_down)
{
auto utf8 = key_event->text().toUtf8();
ImGui::GetIO().AddInputCharactersUTF8(utf8.constData());
}
}
break;
case QEvent::MouseMove:
{
auto lock = g_renderer->GetImGuiLock();
// Qt multiplies all coordinates by the scaling factor in highdpi mode, giving us "scaled" mouse
// coordinates (as if the screen was standard dpi). We need to update the mouse position in
// native coordinates, as the UI (and game) is rendered at native resolution.
const float scale = devicePixelRatio();
ImGui::GetIO().MousePos.x = static_cast<const QMouseEvent*>(event)->x() * scale;
ImGui::GetIO().MousePos.y = static_cast<const QMouseEvent*>(event)->y() * scale;
}
break;
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
auto lock = g_renderer->GetImGuiLock();
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->buttons());
for (size_t i = 0; i < std::size(ImGui::GetIO().MouseDown); i++)
ImGui::GetIO().MouseDown[i] = (button_mask & (1u << i)) != 0;
if (Config::Get(Config::PRIMEHACK_ENABLE))
{
int x = static_cast<const QMouseEvent*>(event)->x();
int y = static_cast<const QMouseEvent*>(event)->y();
prime::g_mouse_input->mousePressEvent(x, y);
}
}
break;
default:
break;
}
}
void RenderWidget::SetImGuiKeyMap()
{
static constexpr std::array<std::array<int, 2>, 21> key_map{{
{ImGuiKey_Tab, Qt::Key_Tab},
{ImGuiKey_LeftArrow, Qt::Key_Left},
{ImGuiKey_RightArrow, Qt::Key_Right},
{ImGuiKey_UpArrow, Qt::Key_Up},
{ImGuiKey_DownArrow, Qt::Key_Down},
{ImGuiKey_PageUp, Qt::Key_PageUp},
{ImGuiKey_PageDown, Qt::Key_PageDown},
{ImGuiKey_Home, Qt::Key_Home},
{ImGuiKey_End, Qt::Key_End},
{ImGuiKey_Insert, Qt::Key_Insert},
{ImGuiKey_Delete, Qt::Key_Delete},
{ImGuiKey_Backspace, Qt::Key_Backspace},
{ImGuiKey_Space, Qt::Key_Space},
{ImGuiKey_Enter, Qt::Key_Return},
{ImGuiKey_Escape, Qt::Key_Escape},
{ImGuiKey_A, Qt::Key_A},
{ImGuiKey_C, Qt::Key_C},
{ImGuiKey_V, Qt::Key_V},
{ImGuiKey_X, Qt::Key_X},
{ImGuiKey_Y, Qt::Key_Y},
{ImGuiKey_Z, Qt::Key_Z},
}};
auto lock = g_renderer->GetImGuiLock();
for (auto [imgui_key, qt_key] : key_map)
ImGui::GetIO().KeyMap[imgui_key] = (qt_key & 0x1FF);
}