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

source code of qtwayland/src/client/qwaylandnativeinterface.cpp