1 // Copyright 2015 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4
5 #include "DolphinQt/RenderWidget.h"
6
7 #include <array>
8
9 #include <QApplication>
10 #include <QDragEnterEvent>
11 #include <QDropEvent>
12 #include <QFileInfo>
13 #include <QGuiApplication>
14 #include <QIcon>
15 #include <QKeyEvent>
16 #include <QMimeData>
17 #include <QMouseEvent>
18 #include <QPalette>
19 #include <QScreen>
20 #include <QTimer>
21 #include <QWindow>
22
23 #include "imgui.h"
24
25 #include "Core/Config/MainSettings.h"
26 #include "Core/ConfigManager.h"
27 #include "Core/Core.h"
28 #include "Core/State.h"
29
30 #include "DolphinQt/Host.h"
31 #include "DolphinQt/QtUtils/ModalMessageBox.h"
32 #include "DolphinQt/Resources.h"
33 #include "DolphinQt/Settings.h"
34
35 #include "VideoCommon/FreeLookCamera.h"
36 #include "VideoCommon/RenderBase.h"
37 #include "VideoCommon/VertexShaderManager.h"
38 #include "VideoCommon/VideoConfig.h"
39
RenderWidget(QWidget * parent)40 RenderWidget::RenderWidget(QWidget* parent) : QWidget(parent)
41 {
42 setWindowTitle(QStringLiteral("Dolphin"));
43 setWindowIcon(Resources::GetAppIcon());
44 setWindowRole(QStringLiteral("renderer"));
45 setAcceptDrops(true);
46
47 QPalette p;
48 p.setColor(QPalette::Window, Qt::black);
49 setPalette(p);
50
51 connect(Host::GetInstance(), &Host::RequestTitle, this, &RenderWidget::setWindowTitle);
52 connect(Host::GetInstance(), &Host::RequestRenderSize, this, [this](int w, int h) {
53 if (!Config::Get(Config::MAIN_RENDER_WINDOW_AUTOSIZE) || isFullScreen() || isMaximized())
54 return;
55
56 const auto dpr = window()->windowHandle()->screen()->devicePixelRatio();
57
58 resize(w / dpr, h / dpr);
59 });
60
61 connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
62 if (state == Core::State::Running)
63 SetImGuiKeyMap();
64 });
65
66 // We have to use Qt::DirectConnection here because we don't want those signals to get queued
67 // (which results in them not getting called)
68 connect(this, &RenderWidget::StateChanged, Host::GetInstance(), &Host::SetRenderFullscreen,
69 Qt::DirectConnection);
70 connect(this, &RenderWidget::HandleChanged, Host::GetInstance(), &Host::SetRenderHandle,
71 Qt::DirectConnection);
72 connect(this, &RenderWidget::SizeChanged, Host::GetInstance(), &Host::ResizeSurface,
73 Qt::DirectConnection);
74 connect(this, &RenderWidget::FocusChanged, Host::GetInstance(), &Host::SetRenderFocus,
75 Qt::DirectConnection);
76
77 m_mouse_timer = new QTimer(this);
78 connect(m_mouse_timer, &QTimer::timeout, this, &RenderWidget::HandleCursorTimer);
79 m_mouse_timer->setSingleShot(true);
80 setMouseTracking(true);
81
82 connect(&Settings::Instance(), &Settings::HideCursorChanged, this,
83 &RenderWidget::OnHideCursorChanged);
84 OnHideCursorChanged();
85 connect(&Settings::Instance(), &Settings::KeepWindowOnTopChanged, this,
86 &RenderWidget::OnKeepOnTopChanged);
87 OnKeepOnTopChanged(Settings::Instance().IsKeepWindowOnTopEnabled());
88 m_mouse_timer->start(MOUSE_HIDE_DELAY);
89
90 // We need a native window to render into.
91 setAttribute(Qt::WA_NativeWindow);
92 setAttribute(Qt::WA_PaintOnScreen);
93 }
94
paintEngine() const95 QPaintEngine* RenderWidget::paintEngine() const
96 {
97 return nullptr;
98 }
99
dragEnterEvent(QDragEnterEvent * event)100 void RenderWidget::dragEnterEvent(QDragEnterEvent* event)
101 {
102 if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1)
103 event->acceptProposedAction();
104 }
105
dropEvent(QDropEvent * event)106 void RenderWidget::dropEvent(QDropEvent* event)
107 {
108 const auto& urls = event->mimeData()->urls();
109 if (urls.empty())
110 return;
111
112 const auto& url = urls[0];
113 QFileInfo file_info(url.toLocalFile());
114
115 auto path = file_info.filePath();
116
117 if (!file_info.exists() || !file_info.isReadable())
118 {
119 ModalMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path));
120 return;
121 }
122
123 if (!file_info.isFile())
124 {
125 return;
126 }
127
128 State::LoadAs(path.toStdString());
129 }
130
OnHideCursorChanged()131 void RenderWidget::OnHideCursorChanged()
132 {
133 setCursor(Settings::Instance().GetHideCursor() ? Qt::BlankCursor : Qt::ArrowCursor);
134 }
135
OnKeepOnTopChanged(bool top)136 void RenderWidget::OnKeepOnTopChanged(bool top)
137 {
138 const bool was_visible = isVisible();
139
140 setWindowFlags(top ? windowFlags() | Qt::WindowStaysOnTopHint :
141 windowFlags() & ~Qt::WindowStaysOnTopHint);
142
143 if (was_visible)
144 show();
145 }
146
HandleCursorTimer()147 void RenderWidget::HandleCursorTimer()
148 {
149 if (isActiveWindow())
150 setCursor(Qt::BlankCursor);
151 }
152
showFullScreen()153 void RenderWidget::showFullScreen()
154 {
155 QWidget::showFullScreen();
156
157 QScreen* screen = window()->windowHandle()->screen();
158
159 const auto dpr = screen->devicePixelRatio();
160
161 emit SizeChanged(width() * dpr, height() * dpr);
162 }
163
event(QEvent * event)164 bool RenderWidget::event(QEvent* event)
165 {
166 PassEventToImGui(event);
167
168 switch (event->type())
169 {
170 case QEvent::KeyPress:
171 {
172 QKeyEvent* ke = static_cast<QKeyEvent*>(event);
173 if (ke->key() == Qt::Key_Escape)
174 emit EscapePressed();
175
176 // The render window might flicker on some platforms because Qt tries to change focus to a new
177 // element when there is none (?) Handling this event before it reaches QWidget fixes the issue.
178 if (ke->key() == Qt::Key_Tab)
179 return true;
180
181 break;
182 }
183 case QEvent::MouseMove:
184 if (g_Config.bFreeLook)
185 OnFreeLookMouseMove(static_cast<QMouseEvent*>(event));
186 [[fallthrough]];
187
188 case QEvent::MouseButtonPress:
189 if (!Settings::Instance().GetHideCursor() && isActiveWindow())
190 {
191 setCursor(Qt::ArrowCursor);
192 m_mouse_timer->start(MOUSE_HIDE_DELAY);
193 }
194 break;
195 case QEvent::WinIdChange:
196 emit HandleChanged(reinterpret_cast<void*>(winId()));
197 break;
198 case QEvent::WindowActivate:
199 if (SConfig::GetInstance().m_PauseOnFocusLost && Core::GetState() == Core::State::Paused)
200 Core::SetState(Core::State::Running);
201
202 emit FocusChanged(true);
203 break;
204 case QEvent::WindowDeactivate:
205 if (SConfig::GetInstance().m_PauseOnFocusLost && Core::GetState() == Core::State::Running)
206 {
207 // If we are declared as the CPU thread, it means that the real CPU thread is waiting
208 // for us to finish showing a panic alert (with that panic alert likely being the cause
209 // of this event), so trying to pause the real CPU thread would cause a deadlock
210 if (!Core::IsCPUThread())
211 Core::SetState(Core::State::Paused);
212 }
213
214 emit FocusChanged(false);
215 break;
216 case QEvent::Resize:
217 {
218 const QResizeEvent* se = static_cast<QResizeEvent*>(event);
219 QSize new_size = se->size();
220
221 QScreen* screen = window()->windowHandle()->screen();
222
223 const auto dpr = screen->devicePixelRatio();
224
225 emit SizeChanged(new_size.width() * dpr, new_size.height() * dpr);
226 break;
227 }
228 case QEvent::WindowStateChange:
229 emit StateChanged(isFullScreen());
230 break;
231 case QEvent::Close:
232 emit Closed();
233 break;
234 default:
235 break;
236 }
237 return QWidget::event(event);
238 }
239
OnFreeLookMouseMove(QMouseEvent * event)240 void RenderWidget::OnFreeLookMouseMove(QMouseEvent* event)
241 {
242 const auto mouse_move = event->pos() - m_last_mouse;
243 m_last_mouse = event->pos();
244
245 if (event->buttons() & Qt::RightButton)
246 {
247 // Camera Pitch and Yaw:
248 g_freelook_camera.Rotate(Common::Vec3{mouse_move.y() / 200.f, mouse_move.x() / 200.f, 0.f});
249 }
250 else if (event->buttons() & Qt::MidButton)
251 {
252 // Camera Roll:
253 g_freelook_camera.Rotate({0.f, 0.f, mouse_move.x() / 200.f});
254 }
255 }
256
PassEventToImGui(const QEvent * event)257 void RenderWidget::PassEventToImGui(const QEvent* event)
258 {
259 if (!Core::IsRunningAndStarted())
260 return;
261
262 switch (event->type())
263 {
264 case QEvent::KeyPress:
265 case QEvent::KeyRelease:
266 {
267 // As the imgui KeysDown array is only 512 elements wide, and some Qt keys which
268 // we need to track (e.g. alt) are above this value, we mask the lower 9 bits.
269 // Even masked, the key codes are still unique, so conflicts aren't an issue.
270 // The actual text input goes through AddInputCharactersUTF8().
271 const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event);
272 const bool is_down = event->type() == QEvent::KeyPress;
273 const u32 key = static_cast<u32>(key_event->key() & 0x1FF);
274 auto lock = g_renderer->GetImGuiLock();
275 if (key < std::size(ImGui::GetIO().KeysDown))
276 ImGui::GetIO().KeysDown[key] = is_down;
277
278 if (is_down)
279 {
280 auto utf8 = key_event->text().toUtf8();
281 ImGui::GetIO().AddInputCharactersUTF8(utf8.constData());
282 }
283 }
284 break;
285
286 case QEvent::MouseMove:
287 {
288 auto lock = g_renderer->GetImGuiLock();
289
290 // Qt multiplies all coordinates by the scaling factor in highdpi mode, giving us "scaled" mouse
291 // coordinates (as if the screen was standard dpi). We need to update the mouse position in
292 // native coordinates, as the UI (and game) is rendered at native resolution.
293 const float scale = devicePixelRatio();
294 ImGui::GetIO().MousePos.x = static_cast<const QMouseEvent*>(event)->x() * scale;
295 ImGui::GetIO().MousePos.y = static_cast<const QMouseEvent*>(event)->y() * scale;
296 }
297 break;
298
299 case QEvent::MouseButtonPress:
300 case QEvent::MouseButtonRelease:
301 {
302 auto lock = g_renderer->GetImGuiLock();
303 const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->buttons());
304 for (size_t i = 0; i < std::size(ImGui::GetIO().MouseDown); i++)
305 ImGui::GetIO().MouseDown[i] = (button_mask & (1u << i)) != 0;
306 }
307 break;
308
309 default:
310 break;
311 }
312 }
313
SetImGuiKeyMap()314 void RenderWidget::SetImGuiKeyMap()
315 {
316 static constexpr std::array<std::array<int, 2>, 21> key_map{{
317 {ImGuiKey_Tab, Qt::Key_Tab},
318 {ImGuiKey_LeftArrow, Qt::Key_Left},
319 {ImGuiKey_RightArrow, Qt::Key_Right},
320 {ImGuiKey_UpArrow, Qt::Key_Up},
321 {ImGuiKey_DownArrow, Qt::Key_Down},
322 {ImGuiKey_PageUp, Qt::Key_PageUp},
323 {ImGuiKey_PageDown, Qt::Key_PageDown},
324 {ImGuiKey_Home, Qt::Key_Home},
325 {ImGuiKey_End, Qt::Key_End},
326 {ImGuiKey_Insert, Qt::Key_Insert},
327 {ImGuiKey_Delete, Qt::Key_Delete},
328 {ImGuiKey_Backspace, Qt::Key_Backspace},
329 {ImGuiKey_Space, Qt::Key_Space},
330 {ImGuiKey_Enter, Qt::Key_Return},
331 {ImGuiKey_Escape, Qt::Key_Escape},
332 {ImGuiKey_A, Qt::Key_A},
333 {ImGuiKey_C, Qt::Key_C},
334 {ImGuiKey_V, Qt::Key_V},
335 {ImGuiKey_X, Qt::Key_X},
336 {ImGuiKey_Y, Qt::Key_Y},
337 {ImGuiKey_Z, Qt::Key_Z},
338 }};
339 auto lock = g_renderer->GetImGuiLock();
340
341 for (auto [imgui_key, qt_key] : key_map)
342 ImGui::GetIO().KeyMap[imgui_key] = (qt_key & 0x1FF);
343 }
344