| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "surface_p.h" |
| 8 | #include "surface.h" |
| 9 | |
| 10 | #include <QGuiApplication> |
| 11 | #include <private/qwaylandwindow_p.h> |
| 12 | #include <qpa/qplatformnativeinterface.h> |
| 13 | |
| 14 | namespace KWayland |
| 15 | { |
| 16 | namespace Client |
| 17 | { |
| 18 | |
| 19 | // The file split from surface.cpp because QtWaylandClient private headers require to unset QT_NO_KEYWORDS. |
| 20 | Surface *Surface::fromWindow(QWindow *window) |
| 21 | { |
| 22 | if (!window) { |
| 23 | return nullptr; |
| 24 | } |
| 25 | QPlatformNativeInterface *native = qApp->platformNativeInterface(); |
| 26 | if (!native) { |
| 27 | return nullptr; |
| 28 | } |
| 29 | window->create(); |
| 30 | wl_surface *s = reinterpret_cast<wl_surface *>(native->nativeResourceForWindow(QByteArrayLiteral("surface" ), window)); |
| 31 | if (!s) { |
| 32 | return nullptr; |
| 33 | } |
| 34 | if (auto surface = get(native: s)) { |
| 35 | return surface; |
| 36 | } |
| 37 | Surface *surface = new Surface(window); |
| 38 | surface->d->surface.setup(pointer: s, foreign: true); |
| 39 | |
| 40 | auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle()); |
| 41 | if (waylandWindow) { |
| 42 | connect(sender: waylandWindow, signal: &QtWaylandClient::QWaylandWindow::wlSurfaceDestroyed, context: surface, slot: &QObject::deleteLater); |
| 43 | } |
| 44 | return surface; |
| 45 | } |
| 46 | |
| 47 | Surface *Surface::fromQtWinId(WId wid) |
| 48 | { |
| 49 | QWindow *window = nullptr; |
| 50 | |
| 51 | for (auto win : qApp->allWindows()) { |
| 52 | if (win->winId() == wid) { |
| 53 | window = win; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (!window) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | return fromWindow(window); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | } |
| 66 | |