KWayland

server_decoration_palette_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2017 David Edmundson <[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_palette_interface.h"
7 #include "display.h"
8 #include "global_p.h"
9 #include "logging.h"
10 #include "resource_p.h"
11 #include "surface_interface.h"
12 
13 #include <QtGlobal>
14 
15 #include <wayland-server_decoration_palette-server-protocol.h>
16 
17 namespace KWayland
18 {
19 namespace Server
20 {
21 class ServerSideDecorationPaletteManagerInterface::Private : public Global::Private
22 {
23 public:
24  Private(ServerSideDecorationPaletteManagerInterface *q, Display *d);
25 
27 
28 private:
29  void bind(wl_client *client, uint32_t version, uint32_t id) override;
30 
31  static void unbind(wl_resource *resource);
32  static Private *cast(wl_resource *r)
33  {
34  return reinterpret_cast<Private *>(wl_resource_get_user_data(r));
35  }
36 
37  static void createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
38 
39  ServerSideDecorationPaletteManagerInterface *q;
40  static const struct org_kde_kwin_server_decoration_palette_manager_interface s_interface;
41  static const quint32 s_version;
42 };
43 
44 const quint32 ServerSideDecorationPaletteManagerInterface::Private::s_version = 1;
45 
46 #ifndef K_DOXYGEN
47 const struct org_kde_kwin_server_decoration_palette_manager_interface ServerSideDecorationPaletteManagerInterface::Private::s_interface = {createCallback};
48 #endif
49 
50 void ServerSideDecorationPaletteManagerInterface::Private::createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
51 {
52  auto p = reinterpret_cast<Private *>(wl_resource_get_user_data(resource));
53  Q_ASSERT(p);
54 
55  SurfaceInterface *s = SurfaceInterface::get(surface);
56  if (!s) {
57  // TODO: send error?
58  qCWarning(KWAYLAND_SERVER) << "ServerSideDecorationPaletteInterface requested for non existing SurfaceInterface";
59  return;
60  }
61  auto palette = new ServerSideDecorationPaletteInterface(p->q, s, resource);
62  palette->create(p->display->getConnection(client), wl_resource_get_version(resource), id);
63  if (!palette->resource()) {
64  wl_resource_post_no_memory(resource);
65  delete palette;
66  return;
67  }
68  p->palettes.append(palette);
69  QObject::connect(palette, &QObject::destroyed, p->q, [=]() {
70  p->palettes.removeOne(palette);
71  });
72  Q_EMIT p->q->paletteCreated(palette);
73 }
74 
75 ServerSideDecorationPaletteManagerInterface::Private::Private(ServerSideDecorationPaletteManagerInterface *q, Display *d)
76  : Global::Private(d, &org_kde_kwin_server_decoration_palette_manager_interface, s_version)
77  , q(q)
78 {
79 }
80 
81 void ServerSideDecorationPaletteManagerInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
82 {
83  auto c = display->getConnection(client);
84  wl_resource *resource = c->createResource(&org_kde_kwin_server_decoration_palette_manager_interface, qMin(version, s_version), id);
85  if (!resource) {
86  wl_client_post_no_memory(client);
87  return;
88  }
89  wl_resource_set_implementation(resource, &s_interface, this, unbind);
90 }
91 
92 void ServerSideDecorationPaletteManagerInterface::Private::unbind(wl_resource *resource)
93 {
94  Q_UNUSED(resource)
95 }
96 
97 class ServerSideDecorationPaletteInterface::Private : public Resource::Private
98 {
99 public:
100  Private(ServerSideDecorationPaletteInterface *q, ServerSideDecorationPaletteManagerInterface *c, SurfaceInterface *surface, wl_resource *parentResource);
101  ~Private() override;
102 
103  SurfaceInterface *surface;
104  QString palette;
105 
106 private:
107  static void setPaletteCallback(wl_client *client, wl_resource *resource, const char *palette);
108 
109  ServerSideDecorationPaletteInterface *q_func()
110  {
111  return reinterpret_cast<ServerSideDecorationPaletteInterface *>(q);
112  }
113  static ServerSideDecorationPaletteInterface *get(SurfaceInterface *s);
114  static const struct org_kde_kwin_server_decoration_palette_interface s_interface;
115 };
116 
117 #ifndef K_DOXYGEN
118 const struct org_kde_kwin_server_decoration_palette_interface ServerSideDecorationPaletteInterface::Private::s_interface = {setPaletteCallback,
119  resourceDestroyedCallback};
120 #endif
121 
122 void ServerSideDecorationPaletteInterface::Private::setPaletteCallback(wl_client *client, wl_resource *resource, const char *palette)
123 {
124  Q_UNUSED(client);
125  auto p = reinterpret_cast<Private *>(wl_resource_get_user_data(resource));
126  Q_ASSERT(p);
127 
128  if (p->palette == QLatin1String(palette)) {
129  return;
130  }
131  p->palette = QString::fromUtf8(palette);
132  Q_EMIT p->q_func()->paletteChanged(p->palette);
133 }
134 
135 ServerSideDecorationPaletteInterface::Private::Private(ServerSideDecorationPaletteInterface *q,
136  ServerSideDecorationPaletteManagerInterface *c,
137  SurfaceInterface *s,
138  wl_resource *parentResource)
139  : Resource::Private(q, c, parentResource, &org_kde_kwin_server_decoration_palette_interface, &s_interface)
140  , surface(s)
141 {
142 }
143 
144 ServerSideDecorationPaletteInterface::Private::~Private()
145 {
146  if (resource) {
147  wl_resource_destroy(resource);
148  resource = nullptr;
149  }
150 }
151 
152 ServerSideDecorationPaletteManagerInterface::ServerSideDecorationPaletteManagerInterface(Display *display, QObject *parent)
153  : Global(new Private(this, display), parent)
154 {
155 }
156 
157 ServerSideDecorationPaletteManagerInterface::~ServerSideDecorationPaletteManagerInterface()
158 {
159 }
160 
161 ServerSideDecorationPaletteManagerInterface::Private *ServerSideDecorationPaletteManagerInterface::d_func() const
162 {
163  return reinterpret_cast<ServerSideDecorationPaletteManagerInterface::Private *>(d.data());
164 }
165 
166 ServerSideDecorationPaletteInterface *ServerSideDecorationPaletteManagerInterface::paletteForSurface(SurfaceInterface *surface)
167 {
168  Q_D();
169  for (ServerSideDecorationPaletteInterface *menu : d->palettes) {
170  if (menu->surface() == surface) {
171  return menu;
172  }
173  }
174  return nullptr;
175 }
176 
177 ServerSideDecorationPaletteInterface::ServerSideDecorationPaletteInterface(ServerSideDecorationPaletteManagerInterface *parent,
178  SurfaceInterface *s,
179  wl_resource *parentResource)
180  : Resource(new Private(this, parent, s, parentResource))
181 {
182 }
183 
184 ServerSideDecorationPaletteInterface::Private *ServerSideDecorationPaletteInterface::d_func() const
185 {
186  return reinterpret_cast<ServerSideDecorationPaletteInterface::Private *>(d.data());
187 }
188 
189 ServerSideDecorationPaletteInterface::~ServerSideDecorationPaletteInterface()
190 {
191 }
192 
194 {
195  Q_D();
196  return d->palette;
197 }
198 
200 {
201  Q_D();
202  return d->surface;
203 }
204 
205 } // namespace
206 }
QString fromUtf8(const char *str, int size)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static SurfaceInterface * get(wl_resource *native)
void destroyed(QObject *obj)
T * data() const const
Allows a client to specify a preferred palette to use for server-side window decorations.
Represents a bound Resource.
Definition: resource.h:31
Provides the palette This interface is attached to a wl_surface and informs the server of a requested...
Resource representing a wl_surface.
Q_D(Todo)
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 03:56:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.