KWayland

qtsurfaceextension_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "qtsurfaceextension_interface.h"
7 #include "global_p.h"
8 #include "resource_p.h"
9 #include "display.h"
10 #include "surface_interface.h"
11 
12 #include <QTimer>
13 #include <QVariant>
14 
15 #include <wayland-server.h>
16 #include <wayland-qt-surface-extension-server-protocol.h>
17 
18 namespace KWayland
19 {
20 namespace Server
21 {
22 
23 class QtSurfaceExtensionInterface::Private : public Global::Private
24 {
25 public:
26  Private(QtSurfaceExtensionInterface *q, Display *d);
27 
29 
30 private:
31  static void createSurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
32  void bind(wl_client *client, uint32_t version, uint32_t id) override;
33  void createSurface(wl_client *client, uint32_t version, uint32_t id, SurfaceInterface *surface, wl_resource *parentResource);
34 
35  QtSurfaceExtensionInterface *q;
36  static const struct qt_surface_extension_interface s_interface;
37  static const quint32 s_version;
38 };
39 
40 const quint32 QtSurfaceExtensionInterface::Private::s_version = 1;
41 
42 QtSurfaceExtensionInterface::Private::Private(QtSurfaceExtensionInterface *q, Display *d)
43  : Global::Private(d, &qt_surface_extension_interface, s_version)
44  , q(q)
45 {
46 }
47 
48 #ifndef K_DOXYGEN
49 const struct qt_surface_extension_interface QtSurfaceExtensionInterface::Private::s_interface = {
50  createSurfaceCallback
51 };
52 #endif
53 
54 
55 class QtExtendedSurfaceInterface::Private : public Resource::Private
56 {
57 public:
58  Private(QtExtendedSurfaceInterface *q, QtSurfaceExtensionInterface *shell, SurfaceInterface *surface, wl_resource *parentResource);
59 
60  SurfaceInterface *surface;
61 
62 private:
63  // interface callbacks
64  static void updateGenericPropertyCallback(wl_client *client, wl_resource *resource, const char *name, wl_array *value);
65  static void setContentOrientationMaskCallback(wl_client *client, wl_resource *resource, int32_t orientation);
66  static void setWindowFlagsCallback(wl_client *client, wl_resource *resource, int32_t flags);
67  static void raiseCallback(wl_client *client, wl_resource *resource);
68  static void lowerCallback(wl_client *client, wl_resource *resource);
69 
70  QtExtendedSurfaceInterface *q_func() {
71  return reinterpret_cast<QtExtendedSurfaceInterface *>(q);
72  }
73 
74  static const struct qt_extended_surface_interface s_interface;
75 };
76 
77 QtSurfaceExtensionInterface::QtSurfaceExtensionInterface(Display *display, QObject *parent)
78  : Global(new Private(this, display), parent)
79 {
80 }
81 
82 QtSurfaceExtensionInterface::~QtSurfaceExtensionInterface() = default;
83 
84 void QtSurfaceExtensionInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
85 {
86  auto c = display->getConnection(client);
87  wl_resource *shell = c->createResource(&qt_surface_extension_interface, qMin(version, s_version), id);
88  if (!shell) {
89  wl_client_post_no_memory(client);
90  return;
91  }
92  wl_resource_set_implementation(shell, &s_interface, this, nullptr);
93 }
94 
95 void QtSurfaceExtensionInterface::Private::createSurfaceCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
96 {
97  auto s = reinterpret_cast<QtSurfaceExtensionInterface::Private*>(wl_resource_get_user_data(resource));
98  s->createSurface(client, wl_resource_get_version(resource), id, SurfaceInterface::get(surface), resource);
99 }
100 
101 void QtSurfaceExtensionInterface::Private::createSurface(wl_client *client, uint32_t version, uint32_t id, SurfaceInterface *surface, wl_resource *parentResource)
102 {
103  auto it = std::find_if(surfaces.constBegin(), surfaces.constEnd(),
104  [surface](QtExtendedSurfaceInterface *s) {
105  return surface == s->surface();
106  }
107  );
108  if (it != surfaces.constEnd()) {
109  wl_resource_post_error(surface->resource(), WL_DISPLAY_ERROR_INVALID_OBJECT, "Qt Surface Extension already created");
110  return;
111  }
112  QtExtendedSurfaceInterface *shellSurface = new QtExtendedSurfaceInterface(q, surface, parentResource);
113  surfaces << shellSurface;
115  [this, shellSurface] {
116  surfaces.removeAll(shellSurface);
117  }
118  );
119  shellSurface->d->create(display->getConnection(client), version, id);
120  emit q->surfaceCreated(shellSurface);
121 }
122 
123 /*********************************
124  * ShellSurfaceInterface
125  *********************************/
126 QtExtendedSurfaceInterface::Private::Private(QtExtendedSurfaceInterface *q, QtSurfaceExtensionInterface *shell, SurfaceInterface *surface, wl_resource *parentResource)
127  : Resource::Private(q, shell, parentResource, &qt_extended_surface_interface, &s_interface)
128  , surface(surface)
129 {
130 }
131 
132 #ifndef K_DOXYGEN
133 const struct qt_extended_surface_interface QtExtendedSurfaceInterface::Private::s_interface = {
134  updateGenericPropertyCallback,
135  setContentOrientationMaskCallback,
136  setWindowFlagsCallback,
137  raiseCallback,
138  lowerCallback
139 };
140 #endif
141 
142 void QtExtendedSurfaceInterface::Private::lowerCallback(wl_client *client, wl_resource *resource)
143 {
144  Q_UNUSED(client)
145  emit cast<Private>(resource)->q_func()->lowerRequested();
146 }
147 
148 void QtExtendedSurfaceInterface::Private::raiseCallback(wl_client *client, wl_resource *resource)
149 {
150  Q_UNUSED(client)
151  emit cast<Private>(resource)->q_func()->raiseRequested();
152 }
153 
154 void QtExtendedSurfaceInterface::Private::setContentOrientationMaskCallback(wl_client *client, wl_resource *resource, int32_t orientation)
155 {
156  Q_UNUSED(client)
157  Q_UNUSED(resource)
158  Q_UNUSED(orientation)
159 }
160 
161 void QtExtendedSurfaceInterface::Private::setWindowFlagsCallback(wl_client *client, wl_resource *resource, int32_t flags)
162 {
163  Q_UNUSED(client)
164  Q_UNUSED(resource)
165  Q_UNUSED(flags)
166 }
167 
168 void QtExtendedSurfaceInterface::Private::updateGenericPropertyCallback(wl_client *client, wl_resource *resource, const char *name, wl_array *value)
169 {
170  Q_UNUSED(client)
171  QByteArray data = QByteArray::fromRawData(static_cast<char *>(value->data), value->size);
172 
173  QVariant variantValue;
174  QDataStream ds(data);
175  ds >> variantValue;
176  cast<Private>(resource)->q_func()->setProperty(name, variantValue);
177 }
178 
179 QtExtendedSurfaceInterface::QtExtendedSurfaceInterface(QtSurfaceExtensionInterface *shell, SurfaceInterface *parent, wl_resource *parentResource)
180  : Resource(new Private(this, shell, parent, parentResource))
181 {
182  auto unsetSurface = [this] {
183  Q_D();
184  d->surface = nullptr;
185  };
186  connect(parent, &Resource::unbound, this, unsetSurface);
187  connect(parent, &QObject::destroyed, this, unsetSurface);
188 }
189 
190 QtExtendedSurfaceInterface::~QtExtendedSurfaceInterface() = default;
191 
192 SurfaceInterface *QtExtendedSurfaceInterface::surface() const
193 {
194  Q_D();
195  return d->surface;
196 }
197 
198 QtSurfaceExtensionInterface *QtExtendedSurfaceInterface::shell() const
199 {
200  Q_D();
201  return reinterpret_cast<QtSurfaceExtensionInterface*>(d->global);
202 }
203 
204 QtExtendedSurfaceInterface::Private *QtExtendedSurfaceInterface::d_func() const
205 {
206  return reinterpret_cast<QtExtendedSurfaceInterface::Private*>(d.data());
207 }
208 
209 void QtExtendedSurfaceInterface::close()
210 {
211  Q_D();
212  if (!d->resource) {
213  return;
214  }
215  qt_extended_surface_send_close(d->resource);
216  d->client->flush();
217 }
218 
219 }
220 }
ClientConnection * getConnection(wl_client *client)
Gets the ClientConnection for the given client.
Definition: display.cpp:592
static SurfaceInterface * get(wl_resource *native)
void unbound()
This signal is emitted when the client unbound this Resource.
wl_resource * createResource(const wl_interface *interface, quint32 version, quint32 id)
Creates a new wl_resource for the provided interface.
bool setProperty(const char *name, const QVariant &value)
Display * display()
Definition: global.cpp:79
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 22:48:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.