KWaylandServer

server_decoration_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 "server_decoration_interface.h"
7 #include "display.h"
8 #include "logging.h"
9 #include "surface_interface.h"
10 
11 #include <QVector>
12 
13 #include <qwayland-server-server-decoration.h>
14 
15 namespace KWaylandServer
16 {
17 static const quint32 s_version = 1;
18 
19 class ServerSideDecorationManagerInterfacePrivate : public QtWaylandServer::org_kde_kwin_server_decoration_manager
20 {
21 public:
22  ServerSideDecorationManagerInterfacePrivate(ServerSideDecorationManagerInterface *_q, Display *display);
23  void setDefaultMode(ServerSideDecorationManagerInterface::Mode mode);
24 
26  ServerSideDecorationManagerInterface *q;
27 
28 protected:
29  void org_kde_kwin_server_decoration_manager_bind_resource(Resource *resource) override;
30  void org_kde_kwin_server_decoration_manager_create(Resource *resource, uint32_t id, wl_resource *surface) override;
31 };
32 
33 static uint32_t modeWayland(ServerSideDecorationManagerInterface::Mode mode)
34 {
35  switch (mode) {
37  return ServerSideDecorationManagerInterfacePrivate::mode_None;
39  return ServerSideDecorationManagerInterfacePrivate::mode_Client;
41  return ServerSideDecorationManagerInterfacePrivate::mode_Server;
42  default:
43  Q_UNREACHABLE();
44  }
45 }
46 
47 void ServerSideDecorationManagerInterfacePrivate::org_kde_kwin_server_decoration_manager_create(Resource *resource, uint32_t id, wl_resource *surface)
48 {
49  SurfaceInterface *s = SurfaceInterface::get(surface);
50  if (!s) {
51  wl_resource_post_error(resource->handle, 0, "Invalid surface");
52  return;
53  }
54 
55  wl_resource *decorationResource = wl_resource_create(resource->client(), &org_kde_kwin_server_decoration_interface, resource->version(), id);
56  if (!decorationResource) {
57  wl_client_post_no_memory(resource->client());
58  return;
59  }
60  auto decoration = new ServerSideDecorationInterface(s, decorationResource);
61  decoration->setMode(defaultMode);
62  Q_EMIT q->decorationCreated(decoration);
63 }
64 
65 void ServerSideDecorationManagerInterfacePrivate::setDefaultMode(ServerSideDecorationManagerInterface::Mode mode)
66 {
67  defaultMode = mode;
68  const uint32_t wlMode = modeWayland(mode);
69 
70  const auto clientResources = resourceMap();
71  for (Resource *resource : clientResources) {
72  send_default_mode(resource->handle, wlMode);
73  }
74 }
75 
76 ServerSideDecorationManagerInterfacePrivate::ServerSideDecorationManagerInterfacePrivate(ServerSideDecorationManagerInterface *_q, Display *display)
77  : QtWaylandServer::org_kde_kwin_server_decoration_manager(*display, s_version)
78  , q(_q)
79 {
80 }
81 
82 void ServerSideDecorationManagerInterfacePrivate::org_kde_kwin_server_decoration_manager_bind_resource(Resource *resource)
83 {
84  send_default_mode(resource->handle, modeWayland(defaultMode));
85 }
86 
87 ServerSideDecorationManagerInterface::ServerSideDecorationManagerInterface(Display *display, QObject *parent)
88  : QObject(parent)
89  , d(new ServerSideDecorationManagerInterfacePrivate(this, display))
90 {
91 }
92 
93 ServerSideDecorationManagerInterface::~ServerSideDecorationManagerInterface() = default;
94 
96 {
97  d->setDefaultMode(mode);
98 }
99 
101 {
102  return d->defaultMode;
103 }
104 
105 class ServerSideDecorationInterfacePrivate : public QtWaylandServer::org_kde_kwin_server_decoration
106 {
107 public:
108  ServerSideDecorationInterfacePrivate(ServerSideDecorationInterface *_q, SurfaceInterface *surface, wl_resource *resource);
109  ~ServerSideDecorationInterfacePrivate();
110 
111  static ServerSideDecorationInterface *get(SurfaceInterface *surface);
113 
115  SurfaceInterface *surface;
116 
117 private:
120 
121 protected:
122  void org_kde_kwin_server_decoration_destroy_resource(Resource *resource) override;
123  void org_kde_kwin_server_decoration_release(Resource *resource) override;
124  void org_kde_kwin_server_decoration_request_mode(Resource *resource, uint32_t mode) override;
125 };
126 
127 QVector<ServerSideDecorationInterfacePrivate *> ServerSideDecorationInterfacePrivate::s_all;
128 
129 void ServerSideDecorationInterfacePrivate::org_kde_kwin_server_decoration_request_mode(Resource *resource, uint32_t mode)
130 {
131  Q_UNUSED(resource)
133  switch (mode) {
134  case ServerSideDecorationManagerInterfacePrivate::mode_None:
136  break;
137  case ServerSideDecorationManagerInterfacePrivate::mode_Client:
139  break;
140  case ServerSideDecorationManagerInterfacePrivate::mode_Server:
142  break;
143  default:
144  // invalid mode
145  qCWarning(KWAYLAND_SERVER) << "Invalid mode:" << mode;
146  return;
147  }
148  Q_EMIT q->modeRequested(m);
149 }
150 
151 void ServerSideDecorationInterfacePrivate::org_kde_kwin_server_decoration_release(Resource *resource)
152 {
153  wl_resource_destroy(resource->handle);
154 }
155 
156 void ServerSideDecorationInterfacePrivate::org_kde_kwin_server_decoration_destroy_resource(Resource *resource)
157 {
158  Q_UNUSED(resource)
159  delete q;
160 }
161 
162 ServerSideDecorationInterface *ServerSideDecorationInterfacePrivate::get(SurfaceInterface *surface)
163 {
164  for (ServerSideDecorationInterfacePrivate *decoration : qAsConst(s_all)) {
165  if (decoration->surface == surface) {
166  return decoration->q;
167  }
168  }
169  return nullptr;
170 }
171 
172 ServerSideDecorationInterfacePrivate::ServerSideDecorationInterfacePrivate(ServerSideDecorationInterface *_q, SurfaceInterface *surface, wl_resource *resource)
173  : QtWaylandServer::org_kde_kwin_server_decoration(resource)
174  , surface(surface)
175  , q(_q)
176 {
177  s_all << this;
178 }
179 
180 ServerSideDecorationInterfacePrivate::~ServerSideDecorationInterfacePrivate()
181 {
182  s_all.removeAll(this);
183 }
184 
185 void ServerSideDecorationInterfacePrivate::setMode(ServerSideDecorationManagerInterface::Mode mode)
186 {
187  this->mode = mode;
188  send_mode(modeWayland(mode));
189 }
190 
191 ServerSideDecorationInterface::ServerSideDecorationInterface(SurfaceInterface *surface, wl_resource *resource)
192  : QObject()
193  , d(new ServerSideDecorationInterfacePrivate(this, surface, resource))
194 {
195 }
196 
197 ServerSideDecorationInterface::~ServerSideDecorationInterface() = default;
198 
200 {
201  d->setMode(mode);
202 }
203 
205 {
206  return d->mode;
207 }
208 
210 {
211  return d->surface;
212 }
213 
215 {
216  return ServerSideDecorationInterfacePrivate::get(surface);
217 }
218 
219 }
static ServerSideDecorationInterface * get(SurfaceInterface *surface)
void setMode(ServerSideDecorationManagerInterface::Mode mode)
Sets the mode on the SurfaceInterface.
Mode
Decoration mode used for SurfaceInterfaces.
Undecorated: neither client, nor server provide decoration.
void setDefaultMode(Mode mode)
Sets the default mode which is pushed to the Clients on creating a ServerSideDecorationInterface.
static SurfaceInterface * get(wl_resource *native)
Representing how a SurfaceInterface should be decorated.
QObject(QObject *parent)
Resource representing a wl_surface.
QObject * parent() const const
ServerSideDecorationManagerInterface::Mode mode() const
The surface gets embedded into a decoration frame provided by the Server.
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.