| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/RenderWidget.h" |
| 5 | |
| 6 | #include <QApplication> |
| 7 | #include <QDragEnterEvent> |
| 8 | #include <QDropEvent> |
| 9 | #include <QFileInfo> |
| 10 | #include <QGuiApplication> |
| 11 | #include <QIcon> |
| 12 | #include <QKeyEvent> |
| 13 | #include <QMimeData> |
| 14 | #include <QMouseEvent> |
| 15 | #include <QPalette> |
| 16 | #include <QScreen> |
| 17 | #include <QTimer> |
| 18 | #include <QWindow> |
| 19 | |
| 20 | #include "Core/Config/MainSettings.h" |
| 21 | #include "Core/Core.h" |
| 22 | #include "Core/State.h" |
| 23 | #include "Core/System.h" |
| 24 | |
| 25 | #include "DolphinQt/Host.h" |
| 26 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 27 | #include "DolphinQt/Resources.h" |
| 28 | #include "DolphinQt/Settings.h" |
| 29 | |
| 30 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 31 | |
| 32 | #include "VideoCommon/Present.h" |
| 33 | |
| 34 | #ifdef _WIN32 |
| 35 | #include <Windows.h> |
| 36 | #include <dwmapi.h> |
| 37 | #endif |
| 38 | |
| 39 | RenderWidget::RenderWidget(QWidget* parent) : QWidget(parent) |
| 40 | { |
| 41 | setWindowTitle(QStringLiteral("Dolphin")); |
| 42 | setWindowIcon(Resources::GetAppIcon()); |
| 43 | setWindowRole(QStringLiteral("renderer")); |
| 44 | setAcceptDrops(true); |
| 45 | |
| 46 | QPalette p; |
| 47 | p.setColor(QPalette::Window, Qt::black); |
| 48 | setPalette(p); |
| 49 | |
| 50 | connect(Host::GetInstance(), &Host::RequestTitle, this, &RenderWidget::setWindowTitle); |
| 51 | connect(Host::GetInstance(), &Host::RequestRenderSize, this, [this](int w, int h) { |
| 52 | if (!Config::Get(Config::MAIN_RENDER_WINDOW_AUTOSIZE) || isFullScreen() || isMaximized()) |
| 53 | return; |
| 54 | |
| 55 | const auto dpr = window()->windowHandle()->screen()->devicePixelRatio(); |
| 56 | |
| 57 | resize(w / dpr, h / dpr); |
| 58 | }); |
| 59 | |
| 60 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { |
| 61 | if (state == Core::State::Running) |
| 62 | SetPresenterKeyMap(); |
| 63 | }); |
| 64 | |
| 65 | // We have to use Qt::DirectConnection here because we don't want those signals to get queued |
| 66 | // (which results in them not getting called) |
| 67 | connect(this, &RenderWidget::StateChanged, Host::GetInstance(), &Host::SetRenderFullscreen, |
| 68 | Qt::DirectConnection); |
| 69 | connect(this, &RenderWidget::HandleChanged, this, &RenderWidget::OnHandleChanged, |
| 70 | Qt::DirectConnection); |
| 71 | connect(this, &RenderWidget::SizeChanged, Host::GetInstance(), &Host::ResizeSurface, |
| 72 | Qt::DirectConnection); |
| 73 | connect(this, &RenderWidget::FocusChanged, Host::GetInstance(), &Host::SetRenderFocus, |
| 74 | Qt::DirectConnection); |
| 75 | |
| 76 | m_mouse_timer = new QTimer(this); |
| 77 | connect(m_mouse_timer, &QTimer::timeout, this, &RenderWidget::HandleCursorTimer); |
| 78 | m_mouse_timer->setSingleShot(true); |
| 79 | setMouseTracking(true); |
| 80 | |
| 81 | connect(&Settings::Instance(), &Settings::CursorVisibilityChanged, this, |
| 82 | &RenderWidget::OnHideCursorChanged); |
| 83 | connect(&Settings::Instance(), &Settings::LockCursorChanged, this, |
| 84 | &RenderWidget::OnLockCursorChanged); |
| 85 | OnHideCursorChanged(); |
| 86 | OnLockCursorChanged(); |
| 87 | connect(&Settings::Instance(), &Settings::KeepWindowOnTopChanged, this, |
| 88 | &RenderWidget::OnKeepOnTopChanged); |
| 89 | OnKeepOnTopChanged(Settings::Instance().IsKeepWindowOnTopEnabled()); |
| 90 | m_mouse_timer->start(MOUSE_HIDE_DELAY); |
| 91 | |
| 92 | // We need a native window to render into. |
| 93 | setAttribute(Qt::WA_NativeWindow); |
| 94 | setAttribute(Qt::WA_PaintOnScreen); |
| 95 | } |
| 96 | |
| 97 | QPaintEngine* RenderWidget::paintEngine() const |
| 98 | { |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | void RenderWidget::dragEnterEvent(QDragEnterEvent* event) |
| 103 | { |
| 104 | if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) |
| 105 | event->acceptProposedAction(); |
| 106 | } |
| 107 | |
| 108 | void RenderWidget::dropEvent(QDropEvent* event) |
| 109 | { |
| 110 | const auto& urls = event->mimeData()->urls(); |
| 111 | if (urls.empty()) |
| 112 | return; |
| 113 | |
| 114 | const auto& url = urls[0]; |
| 115 | QFileInfo file_info(url.toLocalFile()); |
| 116 | |
| 117 | auto path = file_info.filePath(); |
| 118 | |
| 119 | if (!file_info.exists() || !file_info.isReadable()) |
| 120 | { |
| 121 | ModalMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path)); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if (!file_info.isFile()) |
| 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | State::LoadAs(Core::System::GetInstance(), path.toStdString()); |
| 131 | } |
| 132 | |
| 133 | void RenderWidget::OnHandleChanged(void* handle) |
| 134 | { |
| 135 | if (handle) |
| 136 | { |
| 137 | #ifdef _WIN32 |
| 138 | // Remove rounded corners from the render window on Windows 11 |
| 139 | const DWM_WINDOW_CORNER_PREFERENCE corner_preference = DWMWCP_DONOTROUND; |
| 140 | DwmSetWindowAttribute(static_cast<HWND>(handle), DWMWA_WINDOW_CORNER_PREFERENCE, |
| 141 | &corner_preference, sizeof(corner_preference)); |
| 142 | #endif |
| 143 | } |
| 144 | Host::GetInstance()->SetRenderHandle(handle); |
| 145 | } |
| 146 | |
| 147 | void RenderWidget::OnHideCursorChanged() |
| 148 | { |
| 149 | UpdateCursor(); |
| 150 | } |
| 151 | |
| 152 | void RenderWidget::OnLockCursorChanged() |
| 153 | { |
| 154 | SetCursorLocked(false); |
| 155 | UpdateCursor(); |
| 156 | } |
| 157 | |
| 158 | // Calling this at any time will set the cursor (image) to the correct state |
| 159 | void RenderWidget::UpdateCursor() |
| 160 | { |
| 161 | if (!Settings::Instance().GetLockCursor()) |
| 162 | { |
| 163 | // Only hide if the cursor is automatically locking (it will hide on lock). |
| 164 | // "Unhide" the cursor if we lost focus, otherwise it will disappear when hovering |
| 165 | // on top of the game window in the background |
| 166 | const bool keep_on_top = (windowFlags() & Qt::WindowStaysOnTopHint) != 0; |
| 167 | const bool should_hide = |
| 168 | (Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) && |
| 169 | (keep_on_top || Config::Get(Config::MAIN_INPUT_BACKGROUND_INPUT) || isActiveWindow()); |
| 170 | setCursor(should_hide ? Qt::BlankCursor : Qt::ArrowCursor); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | setCursor((m_cursor_locked && |
| 175 | Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) ? |
| 176 | Qt::BlankCursor : |
| 177 | Qt::ArrowCursor); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void RenderWidget::OnKeepOnTopChanged(bool top) |
| 182 | { |
| 183 | const bool was_visible = isVisible(); |
| 184 | |
| 185 | setWindowFlags(top ? windowFlags() | Qt::WindowStaysOnTopHint : |
| 186 | windowFlags() & ~Qt::WindowStaysOnTopHint); |
| 187 | |
| 188 | m_dont_lock_cursor_on_show = true; |
| 189 | if (was_visible) |
| 190 | show(); |
| 191 | m_dont_lock_cursor_on_show = false; |
| 192 | |
| 193 | UpdateCursor(); |
| 194 | } |
| 195 | |
| 196 | void RenderWidget::HandleCursorTimer() |
| 197 | { |
| 198 | if (!isActiveWindow()) |
| 199 | return; |
| 200 | if ((!Settings::Instance().GetLockCursor() || m_cursor_locked) && |
| 201 | Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement) |
| 202 | { |
| 203 | setCursor(Qt::BlankCursor); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void RenderWidget::showFullScreen() |
| 208 | { |
| 209 | QWidget::showFullScreen(); |
| 210 | |
| 211 | QScreen* screen = window()->windowHandle()->screen(); |
| 212 | |
| 213 | const auto dpr = screen->devicePixelRatio(); |
| 214 | |
| 215 | emit SizeChanged(width() * dpr, height() * dpr); |
| 216 | } |
| 217 | |
| 218 | // Lock the cursor within the window/widget internal borders, including the aspect ratio if wanted |
| 219 | void RenderWidget::SetCursorLocked(bool locked, bool follow_aspect_ratio) |
| 220 | { |
| 221 | // It seems like QT doesn't scale the window frame correctly with some DPIs |
| 222 | // so it might happen that the locked cursor can be on the frame of the window, |
| 223 | // being able to resize it, but that is a minor problem. |
| 224 | // As a hack, if necessary, we could always scale down the size by 2 pixel, to a min of 1 given |
| 225 | // that the size can be 0 already. We probably shouldn't scale axes already scaled by aspect ratio |
| 226 | QRect render_rect = geometry(); |
| 227 | if (parentWidget()) |
| 228 | { |
| 229 | render_rect.moveTopLeft(parentWidget()->mapToGlobal(render_rect.topLeft())); |
| 230 | } |
| 231 | auto scale = devicePixelRatioF(); // Seems to always be rounded on Win. Should we round results? |
| 232 | QPoint screen_offset = QPoint(0, 0); |
| 233 | if (window()->windowHandle() && window()->windowHandle()->screen()) |
| 234 | { |
| 235 | screen_offset = window()->windowHandle()->screen()->geometry().topLeft(); |
| 236 | } |
| 237 | render_rect.moveTopLeft(((render_rect.topLeft() - screen_offset) * scale) + screen_offset); |
| 238 | render_rect.setSize(render_rect.size() * scale); |
| 239 | |
| 240 | if (follow_aspect_ratio) |
| 241 | { |
| 242 | // TODO: SetCursorLocked() should be re-called every time this value is changed? |
| 243 | // This might cause imprecisions of one pixel (but it won't cause the cursor to go over borders) |
| 244 | Common::Vec2 aspect_ratio = g_controller_interface.GetWindowInputScale(); |
| 245 | if (aspect_ratio.x > 1.f) |
| 246 | { |
| 247 | const float new_half_width = float(render_rect.width()) / (aspect_ratio.x * 2.f); |
| 248 | // Only ceil if it was >= 0.25 |
| 249 | const float ceiled_new_half_width = std::ceil(std::round(new_half_width * 2.f) / 2.f); |
| 250 | const int x_center = render_rect.center().x(); |
| 251 | // Make a guess on which one to floor and ceil. |
| 252 | // For more precision, we should have kept the rounding point scale from above as well. |
| 253 | render_rect.setLeft(x_center - std::floor(new_half_width)); |
| 254 | render_rect.setRight(x_center + ceiled_new_half_width); |
| 255 | } |
| 256 | if (aspect_ratio.y > 1.f) |
| 257 | { |
| 258 | const float new_half_height = render_rect.height() / (aspect_ratio.y * 2.f); |
| 259 | const float ceiled_new_half_height = std::ceil(std::round(new_half_height * 2.f) / 2.f); |
| 260 | const int y_center = render_rect.center().y(); |
| 261 | render_rect.setTop(y_center - std::floor(new_half_height)); |
| 262 | render_rect.setBottom(y_center + ceiled_new_half_height); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (locked) |
| 267 | { |
| 268 | #ifdef _WIN32 |
| 269 | RECT rect; |
| 270 | rect.left = render_rect.left(); |
| 271 | rect.right = render_rect.right(); |
| 272 | rect.top = render_rect.top(); |
| 273 | rect.bottom = render_rect.bottom(); |
| 274 | |
| 275 | if (ClipCursor(&rect)) |
| 276 | #else |
| 277 | // TODO: Implement on other platforms. XGrabPointer on Linux X11 should be equivalent to |
| 278 | // ClipCursor on Windows, though XFixesCreatePointerBarrier and XFixesDestroyPointerBarrier |
| 279 | // may also work. On Wayland zwp_pointer_constraints_v1::confine_pointer and |
| 280 | // zwp_pointer_constraints_v1::destroy provide this functionality. |
| 281 | // More info: |
| 282 | // https://stackoverflow.com/a/36269507 |
| 283 | // https://tronche.com/gui/x/xlib/input/XGrabPointer.html |
| 284 | // https://www.x.org/releases/X11R7.7/doc/fixesproto/fixesproto.txt |
| 285 | // https://wayland.app/protocols/pointer-constraints-unstable-v1 |
| 286 | |
| 287 | // The setting is hidden in the UI if not implemented |
| 288 | if (false) |
| 289 | #endif |
| 290 | { |
| 291 | m_cursor_locked = true; |
| 292 | |
| 293 | if (Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly) |
| 294 | { |
| 295 | setCursor(Qt::BlankCursor); |
| 296 | } |
| 297 | |
| 298 | Host::GetInstance()->SetRenderFullFocus(true); |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | #ifdef _WIN32 |
| 304 | ClipCursor(nullptr); |
| 305 | #endif |
| 306 | |
| 307 | if (m_cursor_locked) |
| 308 | { |
| 309 | m_cursor_locked = false; |
| 310 | |
| 311 | if (!Settings::Instance().GetLockCursor()) |
| 312 | { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // Center the mouse in the window if it's still active |
| 317 | // Leave it where it was otherwise, e.g. a prompt has opened or we alt tabbed. |
| 318 | if (isActiveWindow()) |
| 319 | { |
| 320 | cursor().setPos(render_rect.left() + render_rect.width() / 2, |
| 321 | render_rect.top() + render_rect.height() / 2); |
| 322 | } |
| 323 | |
| 324 | // Show the cursor or the user won't know the mouse is now unlocked |
| 325 | setCursor(Qt::ArrowCursor); |
| 326 | |
| 327 | Host::GetInstance()->SetRenderFullFocus(false); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void RenderWidget::SetCursorLockedOnNextActivation(bool locked) |
| 333 | { |
| 334 | if (Settings::Instance().GetLockCursor()) |
| 335 | { |
| 336 | m_lock_cursor_on_next_activation = locked; |
| 337 | return; |
| 338 | } |
| 339 | m_lock_cursor_on_next_activation = false; |
| 340 | } |
| 341 | |
| 342 | void RenderWidget::SetWaitingForMessageBox(bool waiting_for_message_box) |
| 343 | { |
| 344 | if (m_waiting_for_message_box == waiting_for_message_box) |
| 345 | { |
| 346 | return; |
| 347 | } |
| 348 | m_waiting_for_message_box = waiting_for_message_box; |
| 349 | if (!m_waiting_for_message_box && m_lock_cursor_on_next_activation && isActiveWindow()) |
| 350 | { |
| 351 | if (Settings::Instance().GetLockCursor()) |
| 352 | { |
| 353 | SetCursorLocked(true); |
| 354 | } |
| 355 | m_lock_cursor_on_next_activation = false; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool RenderWidget::event(QEvent* event) |
| 360 | { |
| 361 | PassEventToPresenter(event); |
| 362 | |
| 363 | switch (event->type()) |
| 364 | { |
| 365 | case QEvent::KeyPress: |
| 366 | { |
| 367 | QKeyEvent* ke = static_cast<QKeyEvent*>(event); |
| 368 | if (ke->key() == Qt::Key_Escape) |
| 369 | emit EscapePressed(); |
| 370 | |
| 371 | // The render window might flicker on some platforms because Qt tries to change focus to a new |
| 372 | // element when there is none (?) Handling this event before it reaches QWidget fixes the issue. |
| 373 | if (ke->key() == Qt::Key_Tab) |
| 374 | return true; |
| 375 | |
| 376 | break; |
| 377 | } |
| 378 | // Needed in case a new window open and it moves the mouse |
| 379 | case QEvent::WindowBlocked: |
| 380 | SetCursorLocked(false); |
| 381 | break; |
| 382 | case QEvent::MouseButtonPress: |
| 383 | |
| 384 | // Grab focus to stop unwanted keyboard input UI interaction. |
| 385 | setFocus(); |
| 386 | |
| 387 | if (isActiveWindow()) |
| 388 | { |
| 389 | // Lock the cursor with any mouse button click (behave the same as window focus change). |
| 390 | // This event is occasionally missed because isActiveWindow is laggy |
| 391 | if (Settings::Instance().GetLockCursor()) |
| 392 | { |
| 393 | SetCursorLocked(true); |
| 394 | } |
| 395 | } |
| 396 | break; |
| 397 | case QEvent::MouseMove: |
| 398 | // Unhide on movement |
| 399 | if (Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement) |
| 400 | { |
| 401 | setCursor(Qt::ArrowCursor); |
| 402 | m_mouse_timer->start(MOUSE_HIDE_DELAY); |
| 403 | } |
| 404 | break; |
| 405 | case QEvent::WinIdChange: |
| 406 | emit HandleChanged(reinterpret_cast<void*>(winId())); |
| 407 | break; |
| 408 | case QEvent::Show: |
| 409 | // Don't do if "stay on top" changed (or was true) |
| 410 | if (Settings::Instance().GetLockCursor() && |
| 411 | Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly && |
| 412 | !m_dont_lock_cursor_on_show) |
| 413 | { |
| 414 | // Auto lock when this window is shown (it was hidden) |
| 415 | if (isActiveWindow()) |
| 416 | SetCursorLocked(true); |
| 417 | else |
| 418 | SetCursorLockedOnNextActivation(); |
| 419 | } |
| 420 | break; |
| 421 | // Note that this event in Windows is not always aligned to the window that is highlighted, |
| 422 | // it's the window that has keyboard and mouse focus |
| 423 | case QEvent::WindowActivate: |
| 424 | if (m_should_unpause_on_focus && |
| 425 | Core::GetState(Core::System::GetInstance()) == Core::State::Paused) |
| 426 | { |
| 427 | Core::SetState(Core::System::GetInstance(), Core::State::Running); |
| 428 | } |
| 429 | |
| 430 | m_should_unpause_on_focus = false; |
| 431 | |
| 432 | UpdateCursor(); |
| 433 | |
| 434 | // Avoid "race conditions" with message boxes |
| 435 | if (m_lock_cursor_on_next_activation && !m_waiting_for_message_box) |
| 436 | { |
| 437 | if (Settings::Instance().GetLockCursor()) |
| 438 | { |
| 439 | SetCursorLocked(true); |
| 440 | } |
| 441 | m_lock_cursor_on_next_activation = false; |
| 442 | } |
| 443 | |
| 444 | emit FocusChanged(true); |
| 445 | break; |
| 446 | case QEvent::WindowDeactivate: |
| 447 | SetCursorLocked(false); |
| 448 | |
| 449 | UpdateCursor(); |
| 450 | |
| 451 | if (Config::Get(Config::MAIN_PAUSE_ON_FOCUS_LOST) && |
| 452 | Core::GetState(Core::System::GetInstance()) == Core::State::Running) |
| 453 | { |
| 454 | // If we are declared as the CPU or GPU thread, it means that the real CPU or GPU thread |
| 455 | // is waiting for us to finish showing a panic alert (with that panic alert likely being |
| 456 | // the cause of this event), so trying to pause the core would cause a deadlock |
| 457 | if (!Core::IsCPUThread() && !Core::IsGPUThread()) |
| 458 | { |
| 459 | m_should_unpause_on_focus = true; |
| 460 | Core::SetState(Core::System::GetInstance(), Core::State::Paused); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | emit FocusChanged(false); |
| 465 | break; |
| 466 | case QEvent::Move: |
| 467 | SetCursorLocked(m_cursor_locked); |
| 468 | break; |
| 469 | |
| 470 | // According to https://bugreports.qt.io/browse/QTBUG-95925 the recommended practice for |
| 471 | // handling DPI change is responding to paint events |
| 472 | case QEvent::Paint: |
| 473 | case QEvent::Resize: |
| 474 | { |
| 475 | SetCursorLocked(m_cursor_locked); |
| 476 | |
| 477 | const QResizeEvent* se = static_cast<QResizeEvent*>(event); |
| 478 | QSize new_size = se->size(); |
| 479 | |
| 480 | QScreen* screen = window()->windowHandle()->screen(); |
| 481 | |
| 482 | const float dpr = screen->devicePixelRatio(); |
| 483 | const int width = new_size.width() * dpr; |
| 484 | const int height = new_size.height() * dpr; |
| 485 | |
| 486 | if (m_last_window_width != width || m_last_window_height != height || |
| 487 | m_last_window_scale != dpr) |
| 488 | { |
| 489 | m_last_window_width = width; |
| 490 | m_last_window_height = height; |
| 491 | m_last_window_scale = dpr; |
| 492 | emit SizeChanged(width, height); |
| 493 | } |
| 494 | break; |
| 495 | } |
| 496 | // Happens when we add/remove the widget from the main window instead of the dedicated one |
| 497 | case QEvent::ParentChange: |
| 498 | SetCursorLocked(false); |
| 499 | break; |
| 500 | case QEvent::WindowStateChange: |
| 501 | // Lock the mouse again when fullscreen changes (we might have missed some events) |
| 502 | SetCursorLocked(m_cursor_locked || (isFullScreen() && Settings::Instance().GetLockCursor())); |
| 503 | emit StateChanged(isFullScreen()); |
| 504 | break; |
| 505 | case QEvent::Close: |
| 506 | emit Closed(); |
| 507 | break; |
| 508 | default: |
| 509 | break; |
| 510 | } |
| 511 | return QWidget::event(event); |
| 512 | } |
| 513 | |
| 514 | void RenderWidget::PassEventToPresenter(const QEvent* event) |
| 515 | { |
| 516 | if (!Core::IsRunning(Core::System::GetInstance())) |
| 517 | return; |
| 518 | |
| 519 | switch (event->type()) |
| 520 | { |
| 521 | case QEvent::KeyPress: |
| 522 | case QEvent::KeyRelease: |
| 523 | { |
| 524 | // As the imgui KeysDown array is only 512 elements wide, and some Qt keys which |
| 525 | // we need to track (e.g. alt) are above this value, we mask the lower 9 bits. |
| 526 | // Even masked, the key codes are still unique, so conflicts aren't an issue. |
| 527 | // The actual text input goes through AddInputCharactersUTF8(). |
| 528 | const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event); |
| 529 | const bool is_down = event->type() == QEvent::KeyPress; |
| 530 | const u32 key = static_cast<u32>(key_event->key() & 0x1FF); |
| 531 | |
| 532 | const char* chars = nullptr; |
| 533 | QByteArray utf8; |
| 534 | |
| 535 | if (is_down) |
| 536 | { |
| 537 | utf8 = key_event->text().toUtf8(); |
| 538 | |
| 539 | if (utf8.size()) |
| 540 | chars = utf8.constData(); |
| 541 | } |
| 542 | |
| 543 | // Pass the key onto Presenter (for the imgui UI) |
| 544 | g_presenter->SetKey(key, is_down, chars); |
| 545 | } |
| 546 | break; |
| 547 | |
| 548 | case QEvent::MouseMove: |
| 549 | { |
| 550 | // Qt multiplies all coordinates by the scaling factor in highdpi mode, giving us "scaled" mouse |
| 551 | // coordinates (as if the screen was standard dpi). We need to update the mouse position in |
| 552 | // native coordinates, as the UI (and game) is rendered at native resolution. |
| 553 | const float scale = devicePixelRatio(); |
| 554 | float x = static_cast<const QMouseEvent*>(event)->pos().x() * scale; |
| 555 | float y = static_cast<const QMouseEvent*>(event)->pos().y() * scale; |
| 556 | |
| 557 | g_presenter->SetMousePos(x, y); |
| 558 | } |
| 559 | break; |
| 560 | |
| 561 | case QEvent::MouseButtonPress: |
| 562 | case QEvent::MouseButtonRelease: |
| 563 | { |
| 564 | const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->buttons()); |
| 565 | g_presenter->SetMousePress(button_mask); |
| 566 | } |
| 567 | break; |
| 568 | |
| 569 | default: |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void RenderWidget::SetPresenterKeyMap() |
| 575 | { |
| 576 | static constexpr DolphinKeyMap key_map = { |
| 577 | Qt::Key_Tab, Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, Qt::Key_Down, |
| 578 | Qt::Key_PageUp, Qt::Key_PageDown, Qt::Key_Home, Qt::Key_End, Qt::Key_Insert, |
| 579 | Qt::Key_Delete, Qt::Key_Backspace, Qt::Key_Space, Qt::Key_Return, Qt::Key_Escape, |
| 580 | Qt::Key_Enter, // Keypad enter |
| 581 | Qt::Key_A, Qt::Key_C, Qt::Key_V, Qt::Key_X, Qt::Key_Y, |
| 582 | Qt::Key_Z, |
| 583 | }; |
| 584 | |
| 585 | g_presenter->SetKeyMap(key_map); |
| 586 | } |