1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qwaylandnativeinterface_p.h"
41 #include "qwaylanddisplay_p.h"
42 #include "qwaylandwindow_p.h"
43 #include "qwaylandshellintegration_p.h"
44 #include "qwaylandsubsurface_p.h"
45 #include "qwaylandextendedsurface_p.h"
46 #include "qwaylandintegration_p.h"
47 #include "qwaylanddisplay_p.h"
48 #include "qwaylandwindowmanagerintegration_p.h"
49 #include "qwaylandscreen_p.h"
50 #include "qwaylandinputdevice_p.h"
51 #include <QtGui/private/qguiapplication_p.h>
52 #include <QtGui/QScreen>
53 #include <QtWaylandClient/private/qwaylandclientbufferintegration_p.h>
54 #if QT_CONFIG(vulkan)
55 #include <QtWaylandClient/private/qwaylandvulkanwindow_p.h>
56 #endif
57
58 #include <QtPlatformHeaders/qwaylandwindowfunctions.h>
59
60 QT_BEGIN_NAMESPACE
61
62 namespace QtWaylandClient {
63
QWaylandNativeInterface(QWaylandIntegration * integration)64 QWaylandNativeInterface::QWaylandNativeInterface(QWaylandIntegration *integration)
65 : m_integration(integration)
66 {
67 }
68
nativeResourceForIntegration(const QByteArray & resourceString)69 void *QWaylandNativeInterface::nativeResourceForIntegration(const QByteArray &resourceString)
70 {
71 QByteArray lowerCaseResource = resourceString.toLower();
72
73 if (lowerCaseResource == "display" || lowerCaseResource == "wl_display" || lowerCaseResource == "nativedisplay")
74 return m_integration->display()->wl_display();
75 if (lowerCaseResource == "compositor")
76 return const_cast<wl_compositor *>(m_integration->display()->wl_compositor());
77 if (lowerCaseResource == "server_buffer_integration")
78 return m_integration->serverBufferIntegration();
79
80 if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration())
81 return m_integration->clientBufferIntegration()->nativeResource(QWaylandClientBufferIntegration::EglDisplay);
82
83 if (lowerCaseResource == "wl_seat")
84 return m_integration->display()->defaultInputDevice()->wl_seat();
85 if (lowerCaseResource == "wl_keyboard") {
86 auto *keyboard = m_integration->display()->defaultInputDevice()->keyboard();
87 if (keyboard)
88 return keyboard->wl_keyboard();
89 return nullptr;
90 }
91 if (lowerCaseResource == "wl_pointer") {
92 auto *pointer = m_integration->display()->defaultInputDevice()->pointer();
93 if (pointer)
94 return pointer->wl_pointer();
95 return nullptr;
96 }
97 if (lowerCaseResource == "wl_touch") {
98 auto *touch = m_integration->display()->defaultInputDevice()->touch();
99 if (touch)
100 return touch->wl_touch();
101 return nullptr;
102 }
103
104 return nullptr;
105 }
106
nativeResourceForWindow(const QByteArray & resourceString,QWindow * window)107 void *QWaylandNativeInterface::nativeResourceForWindow(const QByteArray &resourceString, QWindow *window)
108 {
109 QByteArray lowerCaseResource = resourceString.toLower();
110
111 if (lowerCaseResource == "display")
112 return m_integration->display()->wl_display();
113 if (lowerCaseResource == "compositor")
114 return const_cast<wl_compositor *>(m_integration->display()->wl_compositor());
115 if (lowerCaseResource == "surface") {
116 QWaylandWindow *w = static_cast<QWaylandWindow*>(window->handle());
117 return w ? w->wlSurface() : nullptr;
118 }
119
120 if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration())
121 return m_integration->clientBufferIntegration()->nativeResource(QWaylandClientBufferIntegration::EglDisplay);
122
123 #if QT_CONFIG(vulkan)
124 if (lowerCaseResource == "vksurface") {
125 if (window->surfaceType() == QSurface::VulkanSurface && window->handle()) {
126 // return a pointer to the VkSurfaceKHR value, not the value itself
127 return static_cast<QWaylandVulkanWindow *>(window->handle())->surface();
128 }
129 }
130 #endif
131
132 if (auto shellIntegration = m_integration->shellIntegration())
133 return shellIntegration->nativeResourceForWindow(resourceString, window);
134
135 return nullptr;
136 }
137
nativeResourceForScreen(const QByteArray & resourceString,QScreen * screen)138 void *QWaylandNativeInterface::nativeResourceForScreen(const QByteArray &resourceString, QScreen *screen)
139 {
140 QByteArray lowerCaseResource = resourceString.toLower();
141
142 if (lowerCaseResource == "output")
143 return ((QWaylandScreen *) screen->handle())->output();
144
145 return nullptr;
146 }
147
148 #if QT_CONFIG(opengl)
nativeResourceForContext(const QByteArray & resource,QOpenGLContext * context)149 void *QWaylandNativeInterface::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context)
150 {
151 #if QT_CONFIG(opengl)
152 QByteArray lowerCaseResource = resource.toLower();
153
154 if (lowerCaseResource == "eglconfig" && m_integration->clientBufferIntegration())
155 return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglConfig, context->handle());
156
157 if (lowerCaseResource == "eglcontext" && m_integration->clientBufferIntegration())
158 return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglContext, context->handle());
159
160 if (lowerCaseResource == "egldisplay" && m_integration->clientBufferIntegration())
161 return m_integration->clientBufferIntegration()->nativeResourceForContext(QWaylandClientBufferIntegration::EglDisplay, context->handle());
162 #endif
163
164 return nullptr;
165 }
166 #endif // opengl
167
windowProperties(QPlatformWindow * window) const168 QVariantMap QWaylandNativeInterface::windowProperties(QPlatformWindow *window) const
169 {
170 QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window);
171 return waylandWindow->properties();
172 }
173
windowProperty(QPlatformWindow * window,const QString & name) const174 QVariant QWaylandNativeInterface::windowProperty(QPlatformWindow *window, const QString &name) const
175 {
176 QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window);
177 return waylandWindow->property(name);
178 }
179
windowProperty(QPlatformWindow * window,const QString & name,const QVariant & defaultValue) const180 QVariant QWaylandNativeInterface::windowProperty(QPlatformWindow *window, const QString &name, const QVariant &defaultValue) const
181 {
182 QWaylandWindow *waylandWindow = static_cast<QWaylandWindow *>(window);
183 return waylandWindow->property(name, defaultValue);
184 }
185
setWindowProperty(QPlatformWindow * window,const QString & name,const QVariant & value)186 void QWaylandNativeInterface::setWindowProperty(QPlatformWindow *window, const QString &name, const QVariant &value)
187 {
188 QWaylandWindow *wlWindow = static_cast<QWaylandWindow*>(window);
189 wlWindow->sendProperty(name, value);
190 }
191
emitWindowPropertyChanged(QPlatformWindow * window,const QString & name)192 void QWaylandNativeInterface::emitWindowPropertyChanged(QPlatformWindow *window, const QString &name)
193 {
194 emit windowPropertyChanged(window,name);
195 }
196
platformFunction(const QByteArray & resource) const197 QFunctionPointer QWaylandNativeInterface::platformFunction(const QByteArray &resource) const
198 {
199 if (resource == QWaylandWindowFunctions::setSyncIdentifier()) {
200 return QFunctionPointer(setSync);
201 } else if (resource == QWaylandWindowFunctions::setDeSyncIdentifier()) {
202 return QFunctionPointer(setDeSync);
203 } else if (resource == QWaylandWindowFunctions::isSyncIdentifier()) {
204 return QFunctionPointer(isSync);
205 }
206 return nullptr;
207 }
208
209
setSync(QWindow * window)210 void QWaylandNativeInterface::setSync(QWindow *window)
211 {
212 QWaylandWindow *ww = static_cast<QWaylandWindow*>(window->handle());
213 if (ww->subSurfaceWindow()) {
214 ww->subSurfaceWindow()->setSync();
215 }
216 }
217
setDeSync(QWindow * window)218 void QWaylandNativeInterface::setDeSync(QWindow *window)
219 {
220 QWaylandWindow *ww = static_cast<QWaylandWindow*>(window->handle());
221 if (ww->subSurfaceWindow()) {
222 ww->subSurfaceWindow()->setDeSync();
223 }
224 }
225
isSync(QWindow * window)226 bool QWaylandNativeInterface::isSync(QWindow *window)
227 {
228 QWaylandWindow *ww = static_cast<QWaylandWindow*>(window->handle());
229 if (ww->subSurfaceWindow()) {
230 return ww->subSurfaceWindow()->isSync();
231 }
232 return false;
233 }
234
235 }
236
237 QT_END_NAMESPACE
238