| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qwaylandnativeinterface_p.h" |
| 5 | #include "qwaylanddisplay_p.h" |
| 6 | #include "qwaylandwindow_p.h" |
| 7 | #include "qwaylandshellintegration_p.h" |
| 8 | #include "qwaylandsubsurface_p.h" |
| 9 | #include "qwaylandextendedsurface_p.h" |
| 10 | #include "qwaylandintegration_p.h" |
| 11 | #include "qwaylanddisplay_p.h" |
| 12 | #include "qwaylandwindowmanagerintegration_p.h" |
| 13 | #include "qwaylandscreen_p.h" |
| 14 | #include "qwaylandinputdevice_p.h" |
| 15 | #include <QtCore/private/qnativeinterface_p.h> |
| 16 | #include <QtGui/private/qguiapplication_p.h> |
| 17 | #include <QtGui/QScreen> |
| 18 | #include <QtWaylandClient/private/qwaylandclientbufferintegration_p.h> |
| 19 | #if QT_CONFIG(vulkan) |
| 20 | #include <QtWaylandClient/private/qwaylandvulkanwindow_p.h> |
| 21 | #endif |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | namespace QtWaylandClient { |
| 26 | |
| 27 | QWaylandNativeInterface::QWaylandNativeInterface(QWaylandIntegration *integration) |
| 28 | : m_integration(integration) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void *QWaylandNativeInterface::nativeResourceForIntegration(const QByteArray &resourceString) |
| 33 | { |
| 34 | QByteArray lowerCaseResource = resourceString.toLower(); |
| 35 | |
| 36 | if (lowerCaseResource == "display" || lowerCaseResource == "wl_display" || lowerCaseResource == "nativedisplay" ) |
| 37 | return m_integration->display()->wl_display(); |
| 38 | if (lowerCaseResource == "compositor" ) { |
| 39 | if (auto compositor = m_integration->display()->compositor()) |
| 40 | return compositor->object(); |
| 41 | } |
| 42 | if (lowerCaseResource == "server_buffer_integration" ) |
| 43 | return m_integration->serverBufferIntegration(); |
| 44 | |
| 45 | if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration()) |
| 46 | return m_integration->clientBufferIntegration()->nativeResource(QWaylandClientBufferIntegration::EglDisplay); |
| 47 | |
| 48 | if (lowerCaseResource == "wl_seat" ) |
| 49 | return m_integration->display()->defaultInputDevice()->wl_seat(); |
| 50 | if (lowerCaseResource == "wl_keyboard" ) { |
| 51 | auto *keyboard = m_integration->display()->defaultInputDevice()->keyboard(); |
| 52 | if (keyboard) |
| 53 | return keyboard->wl_keyboard(); |
| 54 | return nullptr; |
| 55 | } |
| 56 | if (lowerCaseResource == "wl_pointer" ) { |
| 57 | auto *pointer = m_integration->display()->defaultInputDevice()->pointer(); |
| 58 | if (pointer) |
| 59 | return pointer->wl_pointer(); |
| 60 | return nullptr; |
| 61 | } |
| 62 | if (lowerCaseResource == "wl_touch" ) { |
| 63 | auto *touch = m_integration->display()->defaultInputDevice()->touch(); |
| 64 | if (touch) |
| 65 | return touch->wl_touch(); |
| 66 | return nullptr; |
| 67 | } |
| 68 | if (lowerCaseResource == "serial" ) |
| 69 | return reinterpret_cast<void *>(quintptr(m_integration->display()->defaultInputDevice()->serial())); |
| 70 | |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | wl_display *QtWaylandClient::QWaylandNativeInterface::display() const |
| 75 | { |
| 76 | return m_integration->display()->wl_display(); |
| 77 | } |
| 78 | |
| 79 | wl_compositor *QtWaylandClient::QWaylandNativeInterface::compositor() const |
| 80 | { |
| 81 | if (auto compositor = m_integration->display()->compositor()) |
| 82 | return compositor->object(); |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | wl_seat *QtWaylandClient::QWaylandNativeInterface::seat() const |
| 87 | { |
| 88 | if (auto inputDevice = m_integration->display()->defaultInputDevice()) { |
| 89 | return inputDevice->wl_seat(); |
| 90 | } |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | wl_keyboard *QtWaylandClient::QWaylandNativeInterface::keyboard() const |
| 95 | { |
| 96 | if (auto inputDevice = m_integration->display()->defaultInputDevice()) |
| 97 | if (auto keyboard = inputDevice->keyboard()) |
| 98 | return keyboard->wl_keyboard(); |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | wl_pointer *QtWaylandClient::QWaylandNativeInterface::pointer() const |
| 103 | { |
| 104 | if (auto inputDevice = m_integration->display()->defaultInputDevice()) |
| 105 | if (auto pointer = inputDevice->pointer()) |
| 106 | return pointer->wl_pointer(); |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | wl_touch *QtWaylandClient::QWaylandNativeInterface::touch() const |
| 111 | { |
| 112 | if (auto inputDevice = m_integration->display()->defaultInputDevice()) |
| 113 | if (auto touch = inputDevice->touch()) |
| 114 | return touch->wl_touch(); |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | uint QtWaylandClient::QWaylandNativeInterface::lastInputSerial() const |
| 119 | { |
| 120 | return m_integration->display()->lastInputSerial(); |
| 121 | } |
| 122 | |
| 123 | wl_seat *QtWaylandClient::QWaylandNativeInterface::lastInputSeat() const |
| 124 | { |
| 125 | if (auto inputDevice = m_integration->display()->lastInputDevice()) |
| 126 | return inputDevice->wl_seat(); |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | void *QWaylandNativeInterface::nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) |
| 131 | { |
| 132 | QByteArray lowerCaseResource = resourceString.toLower(); |
| 133 | |
| 134 | if (lowerCaseResource == "display" ) |
| 135 | return m_integration->display()->wl_display(); |
| 136 | if (lowerCaseResource == "compositor" ) { |
| 137 | if (auto compositor = m_integration->display()->compositor()) |
| 138 | return compositor->object(); |
| 139 | } |
| 140 | if (lowerCaseResource == "surface" ) { |
| 141 | QWaylandWindow *w = static_cast<QWaylandWindow*>(window->handle()); |
| 142 | return w ? w->wlSurface() : nullptr; |
| 143 | } |
| 144 | |
| 145 | if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration()) |
| 146 | return m_integration->clientBufferIntegration()->nativeResource(QWaylandClientBufferIntegration::EglDisplay); |
| 147 | |
| 148 | #if QT_CONFIG(vulkan) |
| 149 | if (lowerCaseResource == "vksurface" ) { |
| 150 | if (window->surfaceType() == QSurface::VulkanSurface && window->handle()) { |
| 151 | // return a pointer to the VkSurfaceKHR value, not the value itself |
| 152 | return static_cast<QWaylandVulkanWindow *>(window->handle())->vkSurface(); |
| 153 | } |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | QWaylandWindow *platformWindow = static_cast<QWaylandWindow *>(window->handle()); |
| 158 | if (platformWindow && platformWindow->shellIntegration()) |
| 159 | return platformWindow->shellIntegration()->nativeResourceForWindow(resource: resourceString, window); |
| 160 | |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | void *QWaylandNativeInterface::nativeResourceForScreen(const QByteArray &resourceString, QScreen *screen) |
| 165 | { |
| 166 | QByteArray lowerCaseResource = resourceString.toLower(); |
| 167 | |
| 168 | if (lowerCaseResource == "output" && !screen->handle()->isPlaceholder()) |
| 169 | return ((QWaylandScreen *) screen->handle())->output(); |
| 170 | |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | #if QT_CONFIG(opengl) |
| 175 | void *QWaylandNativeInterface::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context) |
| 176 | { |
| 177 | #if QT_CONFIG(opengl) |
| 178 | QByteArray lowerCaseResource = resource.toLower(); |
| 179 | |
| 180 | if (lowerCaseResource == "eglconfig" && m_integration->clientBufferIntegration()) |
| 181 | return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglConfig, context->handle()); |
| 182 | |
| 183 | if (lowerCaseResource == "eglcontext" && m_integration->clientBufferIntegration()) |
| 184 | return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglContext, context->handle()); |
| 185 | |
| 186 | if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration()) |
| 187 | return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglDisplay, context->handle()); |
| 188 | #endif |
| 189 | |
| 190 | return nullptr; |
| 191 | } |
| 192 | #endif // opengl |
| 193 | |
| 194 | QPlatformNativeInterface::NativeResourceForWindowFunction QWaylandNativeInterface::nativeResourceFunctionForWindow(const QByteArray &resource) |
| 195 | { |
| 196 | QByteArray lowerCaseResource = resource.toLower(); |
| 197 | |
| 198 | if (lowerCaseResource == "setmargins" ) { |
| 199 | return NativeResourceForWindowFunction(reinterpret_cast<void *>(setWindowMargins)); |
| 200 | } |
| 201 | |
| 202 | return nullptr; |
| 203 | } |
| 204 | |
| 205 | QVariantMap QWaylandNativeInterface::windowProperties(QPlatformWindow *window) const |
| 206 | { |
| 207 | QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window); |
| 208 | return waylandWindow->properties(); |
| 209 | } |
| 210 | |
| 211 | QVariant QWaylandNativeInterface::windowProperty(QPlatformWindow *window, const QString &name) const |
| 212 | { |
| 213 | QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window); |
| 214 | return waylandWindow->property(name); |
| 215 | } |
| 216 | |
| 217 | QVariant QWaylandNativeInterface::windowProperty(QPlatformWindow *window, const QString &name, const QVariant &defaultValue) const |
| 218 | { |
| 219 | QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window); |
| 220 | return waylandWindow->property(name, defaultValue); |
| 221 | } |
| 222 | |
| 223 | void QWaylandNativeInterface::setWindowProperty(QPlatformWindow *window, const QString &name, const QVariant &value) |
| 224 | { |
| 225 | QWaylandWindow *wlWindow = static_cast<QWaylandWindow*>(window); |
| 226 | wlWindow->sendProperty(name, value); |
| 227 | } |
| 228 | |
| 229 | void QWaylandNativeInterface::emitWindowPropertyChanged(QPlatformWindow *window, const QString &name) |
| 230 | { |
| 231 | emit windowPropertyChanged(window,propertyName: name); |
| 232 | } |
| 233 | |
| 234 | void QWaylandNativeInterface::setWindowMargins(QWindow *window, const QMargins &margins) |
| 235 | { |
| 236 | QWaylandWindow *wlWindow = static_cast<QWaylandWindow*>(window->handle()); |
| 237 | wlWindow->setCustomMargins(margins); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | QT_END_NAMESPACE |
| 243 | |