qt_common.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #elif defined(__APPLE__)
  12. #include <objc/message.h>
  13. #endif
  14. namespace QtCommon {
  15. Core::Frontend::WindowSystemType GetWindowSystemType() {
  16. // Determine WSI type based on Qt platform.
  17. QString platform_name = QGuiApplication::platformName();
  18. if (platform_name == QStringLiteral("windows"))
  19. return Core::Frontend::WindowSystemType::Windows;
  20. else if (platform_name == QStringLiteral("xcb"))
  21. return Core::Frontend::WindowSystemType::X11;
  22. else if (platform_name == QStringLiteral("wayland"))
  23. return Core::Frontend::WindowSystemType::Wayland;
  24. else if (platform_name == QStringLiteral("wayland-egl"))
  25. return Core::Frontend::WindowSystemType::Wayland;
  26. else if (platform_name == QStringLiteral("cocoa"))
  27. return Core::Frontend::WindowSystemType::Cocoa;
  28. else if (platform_name == QStringLiteral("android"))
  29. return Core::Frontend::WindowSystemType::Android;
  30. LOG_CRITICAL(Frontend, "Unknown Qt platform {}!", platform_name.toStdString());
  31. return Core::Frontend::WindowSystemType::Windows;
  32. } // namespace Core::Frontend::WindowSystemType
  33. Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) {
  34. Core::Frontend::EmuWindow::WindowSystemInfo wsi;
  35. wsi.type = GetWindowSystemType();
  36. #if defined(WIN32)
  37. // Our Win32 Qt external doesn't have the private API.
  38. wsi.render_surface = reinterpret_cast<void*>(window->winId());
  39. #elif defined(__APPLE__)
  40. wsi.render_surface = reinterpret_cast<void* (*)(id, SEL)>(objc_msgSend)(
  41. reinterpret_cast<id>(window->winId()), sel_registerName("layer"));
  42. #else
  43. QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
  44. wsi.display_connection = pni->nativeResourceForWindow("display", window);
  45. if (wsi.type == Core::Frontend::WindowSystemType::Wayland)
  46. wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr;
  47. else
  48. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  49. #endif
  50. wsi.render_surface_scale = window ? static_cast<float>(window->devicePixelRatio()) : 1.0f;
  51. return wsi;
  52. }
  53. } // namespace QtCommon