qt_common.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <QGuiApplication>
  4. #include <QStringLiteral>
  5. #include <QWindow>
  6. #include "common/logging/log.h"
  7. #include "core/frontend/emu_window.h"
  8. #include "yuzu/qt_common.h"
  9. #if !defined(WIN32) && !defined(__APPLE__)
  10. #include <qpa/qplatformnativeinterface.h>
  11. #endif
  12. namespace QtCommon {
  13. Core::Frontend::WindowSystemType GetWindowSystemType() {
  14. // Determine WSI type based on Qt platform.
  15. QString platform_name = QGuiApplication::platformName();
  16. if (platform_name == QStringLiteral("windows"))
  17. return Core::Frontend::WindowSystemType::Windows;
  18. else if (platform_name == QStringLiteral("xcb"))
  19. return Core::Frontend::WindowSystemType::X11;
  20. else if (platform_name == QStringLiteral("wayland"))
  21. return Core::Frontend::WindowSystemType::Wayland;
  22. else if (platform_name == QStringLiteral("wayland-egl"))
  23. return Core::Frontend::WindowSystemType::Wayland;
  24. else if (platform_name == QStringLiteral("cocoa"))
  25. return Core::Frontend::WindowSystemType::Cocoa;
  26. else if (platform_name == QStringLiteral("android"))
  27. return Core::Frontend::WindowSystemType::Android;
  28. LOG_CRITICAL(Frontend, "Unknown Qt platform {}!", platform_name.toStdString());
  29. return Core::Frontend::WindowSystemType::Windows;
  30. } // namespace Core::Frontend::WindowSystemType
  31. Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) {
  32. Core::Frontend::EmuWindow::WindowSystemInfo wsi;
  33. wsi.type = GetWindowSystemType();
  34. // Our Win32 Qt external doesn't have the private API.
  35. #if defined(WIN32) || defined(__APPLE__)
  36. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  37. #else
  38. QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
  39. wsi.display_connection = pni->nativeResourceForWindow("display", window);
  40. if (wsi.type == Core::Frontend::WindowSystemType::Wayland)
  41. wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr;
  42. else
  43. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  44. #endif
  45. wsi.render_surface_scale = window ? static_cast<float>(window->devicePixelRatio()) : 1.0f;
  46. return wsi;
  47. }
  48. } // namespace QtCommon