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 "surface_interface.h"
9 #include "global_p.h"
10 #include "resource_p.h"
11 #include "logging.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 private:
28  void bind(wl_client *client, uint32_t version, uint32_t id) override;
29 
30  static void unbind(wl_resource *resource);
31  static Private *cast(wl_resource *r) {
32  return reinterpret_cast<Private*>(wl_resource_get_user_data(r));
33  }
34 
35  static void createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource * surface);
36 
37  ServerSideDecorationPaletteManagerInterface *q;
38  static const struct org_kde_kwin_server_decoration_palette_manager_interface s_interface;
39  static const quint32 s_version;
40 };
41 
42 const quint32 ServerSideDecorationPaletteManagerInterface::Private::s_version = 1;
43 
44 #ifndef K_DOXYGEN
45 const struct org_kde_kwin_server_decoration_palette_manager_interface ServerSideDecorationPaletteManagerInterface::Private::s_interface = {
46  createCallback
47 };
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  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();
102 
103 
104  SurfaceInterface *surface;
105  QString palette;
106 private:
107  static void setPaletteCallback(wl_client *client, wl_resource *resource, const char * palette);
108 
109  ServerSideDecorationPaletteInterface *q_func() {
110  return reinterpret_cast<ServerSideDecorationPaletteInterface *>(q);
111  }
112  static ServerSideDecorationPaletteInterface *get(SurfaceInterface *s);
113  static const struct org_kde_kwin_server_decoration_palette_interface s_interface;
114 };
115 
116 #ifndef K_DOXYGEN
117 const struct org_kde_kwin_server_decoration_palette_interface ServerSideDecorationPaletteInterface::Private::s_interface = {
118  setPaletteCallback,
119  resourceDestroyedCallback
120 };
121 #endif
122 
123 void ServerSideDecorationPaletteInterface::Private::setPaletteCallback(wl_client *client, wl_resource *resource, const char * palette)
124 {
125  Q_UNUSED(client);
126  auto p = reinterpret_cast<Private*>(wl_resource_get_user_data(resource));
127  Q_ASSERT(p);
128 
129  if (p->palette == QLatin1String(palette)) {
130  return;
131  }
132  p->palette = QString::fromUtf8(palette);
133  emit p->q_func()->paletteChanged(p->palette);
134 }
135 
136 ServerSideDecorationPaletteInterface::Private::Private(ServerSideDecorationPaletteInterface *q, ServerSideDecorationPaletteManagerInterface *c, SurfaceInterface *s, wl_resource *parentResource)
137  : Resource::Private(q, c, parentResource, &org_kde_kwin_server_decoration_palette_interface, &s_interface),
138  surface(s)
139 {
140 }
141 
142 ServerSideDecorationPaletteInterface::Private::~Private()
143 {
144  if (resource) {
145  wl_resource_destroy(resource);
146  resource = nullptr;
147  }
148 }
149 
150 ServerSideDecorationPaletteManagerInterface::ServerSideDecorationPaletteManagerInterface(Display *display, QObject *parent)
151  : Global(new Private(this, display), parent)
152 {
153 }
154 
155 ServerSideDecorationPaletteManagerInterface::~ServerSideDecorationPaletteManagerInterface()
156 {
157 }
158 
159 ServerSideDecorationPaletteManagerInterface::Private *ServerSideDecorationPaletteManagerInterface::d_func() const
160 {
161  return reinterpret_cast<ServerSideDecorationPaletteManagerInterface::Private*>(d.data());
162 }
163 
164 ServerSideDecorationPaletteInterface* ServerSideDecorationPaletteManagerInterface::paletteForSurface(SurfaceInterface *surface)
165 {
166  Q_D();
167  for (ServerSideDecorationPaletteInterface* menu: d->palettes) {
168  if (menu->surface() == surface) {
169  return menu;
170  }
171  }
172  return nullptr;
173 }
174 
175 ServerSideDecorationPaletteInterface::ServerSideDecorationPaletteInterface(ServerSideDecorationPaletteManagerInterface *parent, SurfaceInterface *s, wl_resource *parentResource):
176  Resource(new Private(this, parent, s, parentResource))
177 {
178 }
179 
180 ServerSideDecorationPaletteInterface::Private *ServerSideDecorationPaletteInterface::d_func() const
181 {
182  return reinterpret_cast<ServerSideDecorationPaletteInterface::Private*>(d.data());
183 }
184 
185 ServerSideDecorationPaletteInterface::~ServerSideDecorationPaletteInterface()
186 {}
187 
188 QString ServerSideDecorationPaletteInterface::palette() const
189 {
190  Q_D();
191  return d->palette;
192 }
193 
194 SurfaceInterface* ServerSideDecorationPaletteInterface::surface() const
195 {
196  Q_D();
197  return d->surface;
198 }
199 
200 }//namespace
201 }
202 
QString fromUtf8(const char *str, int size)
static SurfaceInterface * get(wl_resource *native)
Resource representing a wl_surface.
T * data() const const
Represents a bound Resource.
Definition: resource.h:32
Allows a client to specify a preferred palette to use for server-side window decorations.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Provides the palette This interface is attached to a wl_surface and informs the server of a requested...
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:53:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.