KWaylandServer

shadow_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 "shadow_interface.h"
7 #include "clientbuffer.h"
8 #include "display.h"
9 #include "surface_interface_p.h"
10 
11 #include <qwayland-server-shadow.h>
12 
13 namespace KWaylandServer
14 {
15 static const quint32 s_version = 2;
16 
17 class ShadowManagerInterfacePrivate : public QtWaylandServer::org_kde_kwin_shadow_manager
18 {
19 public:
20  ShadowManagerInterfacePrivate(ShadowManagerInterface *_q, Display *display);
21 
22  ShadowManagerInterface *q;
23  Display *display;
24 
25 protected:
26  void org_kde_kwin_shadow_manager_create(Resource *resource, uint32_t id, wl_resource *surface) override;
27  void org_kde_kwin_shadow_manager_unset(Resource *resource, wl_resource *surface) override;
28  void org_kde_kwin_shadow_manager_destroy(Resource *resource) override;
29 };
30 
31 ShadowManagerInterfacePrivate::ShadowManagerInterfacePrivate(ShadowManagerInterface *_q, Display *display)
32  : QtWaylandServer::org_kde_kwin_shadow_manager(*display, s_version)
33  , q(_q)
34  , display(display)
35 {
36 }
37 
38 void ShadowManagerInterfacePrivate::org_kde_kwin_shadow_manager_destroy(Resource *resource)
39 {
40  wl_resource_destroy(resource->handle);
41 }
42 
43 void ShadowManagerInterfacePrivate::org_kde_kwin_shadow_manager_create(Resource *resource, uint32_t id, wl_resource *surface)
44 {
45  SurfaceInterface *s = SurfaceInterface::get(surface);
46  if (!s) {
47  wl_resource_post_error(resource->handle, 0, "Invalid surface");
48  return;
49  }
50 
51  wl_resource *shadow_resource = wl_resource_create(resource->client(), &org_kde_kwin_shadow_interface, resource->version(), id);
52  if (!shadow_resource) {
53  wl_client_post_no_memory(resource->client());
54  return;
55  }
56 
57  auto shadow = new ShadowInterface(q, shadow_resource);
58 
59  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(s);
60  surfacePrivate->setShadow(QPointer<ShadowInterface>(shadow));
61 }
62 
63 void ShadowManagerInterfacePrivate::org_kde_kwin_shadow_manager_unset(Resource *resource, wl_resource *surface)
64 {
65  Q_UNUSED(resource)
66  SurfaceInterface *s = SurfaceInterface::get(surface);
67  if (!s) {
68  wl_resource_post_error(resource->handle, 0, "Invalid surface");
69  return;
70  }
71  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(s);
72  surfacePrivate->setShadow(QPointer<ShadowInterface>());
73 }
74 
75 ShadowManagerInterface::ShadowManagerInterface(Display *display, QObject *parent)
76  : QObject(parent)
77  , d(new ShadowManagerInterfacePrivate(this, display))
78 {
79 }
80 
81 ShadowManagerInterface::~ShadowManagerInterface() = default;
82 
83 Display *ShadowManagerInterface::display() const
84 {
85  return d->display;
86 }
87 
88 class ShadowInterfacePrivate : public QtWaylandServer::org_kde_kwin_shadow
89 {
90 public:
91  ShadowInterfacePrivate(ShadowInterface *_q, wl_resource *resource);
92  ~ShadowInterfacePrivate();
93 
94  struct State {
95  enum Flags {
96  None = 0,
97  LeftBuffer = 1 << 0,
98  TopLeftBuffer = 1 << 1,
99  TopBuffer = 1 << 2,
100  TopRightBuffer = 1 << 3,
101  RightBuffer = 1 << 4,
102  BottomRightBuffer = 1 << 5,
103  BottomBuffer = 1 << 6,
104  BottomLeftBuffer = 1 << 7,
105  Offset = 1 << 8,
106  };
108  QPointer<ClientBuffer> topLeft;
110  QPointer<ClientBuffer> topRight;
112  QPointer<ClientBuffer> bottomRight;
113  QPointer<ClientBuffer> bottom;
114  QPointer<ClientBuffer> bottomLeft;
115  QMarginsF offset;
116  Flags flags = Flags::None;
117  };
118 
119  void commit();
120  void attach(State::Flags flag, wl_resource *buffer);
121 
122  ShadowManagerInterface *manager;
123  State current;
124  State pending;
125  ShadowInterface *q;
126 
127 protected:
128  void org_kde_kwin_shadow_destroy_resource(Resource *resource) override;
129  void org_kde_kwin_shadow_commit(Resource *resource) override;
130  void org_kde_kwin_shadow_attach_left(Resource *resource, wl_resource *buffer) override;
131  void org_kde_kwin_shadow_attach_top_left(Resource *resource, wl_resource *buffer) override;
132  void org_kde_kwin_shadow_attach_top(Resource *resource, wl_resource *buffer) override;
133  void org_kde_kwin_shadow_attach_top_right(Resource *resource, wl_resource *buffer) override;
134  void org_kde_kwin_shadow_attach_right(Resource *resource, wl_resource *buffer) override;
135  void org_kde_kwin_shadow_attach_bottom_right(Resource *resource, wl_resource *buffer) override;
136  void org_kde_kwin_shadow_attach_bottom(Resource *resource, wl_resource *buffer) override;
137  void org_kde_kwin_shadow_attach_bottom_left(Resource *resource, wl_resource *buffer) override;
138  void org_kde_kwin_shadow_set_left_offset(Resource *resource, wl_fixed_t offset) override;
139  void org_kde_kwin_shadow_set_top_offset(Resource *resource, wl_fixed_t offset) override;
140  void org_kde_kwin_shadow_set_right_offset(Resource *resource, wl_fixed_t offset) override;
141  void org_kde_kwin_shadow_set_bottom_offset(Resource *resource, wl_fixed_t offset) override;
142  void org_kde_kwin_shadow_destroy(Resource *resource) override;
143 };
144 
145 void ShadowInterfacePrivate::org_kde_kwin_shadow_commit(Resource *resource)
146 {
147  Q_UNUSED(resource)
148 #define BUFFER(__FLAG__, __PART__) \
149  if (pending.flags & State::Flags::__FLAG__##Buffer) { \
150  if (current.__PART__) { \
151  current.__PART__->unref(); \
152  } \
153  if (pending.__PART__) { \
154  pending.__PART__->ref(); \
155  } \
156  current.__PART__ = pending.__PART__; \
157  }
158  BUFFER(Left, left)
159  BUFFER(TopLeft, topLeft)
160  BUFFER(Top, top)
161  BUFFER(TopRight, topRight)
162  BUFFER(Right, right)
163  BUFFER(BottomRight, bottomRight)
164  BUFFER(Bottom, bottom)
165  BUFFER(BottomLeft, bottomLeft)
166 #undef BUFFER
167 
168  if (pending.flags & State::Offset) {
169  current.offset = pending.offset;
170  }
171  pending = State();
172 }
173 
174 void ShadowInterfacePrivate::attach(ShadowInterfacePrivate::State::Flags flag, wl_resource *buffer)
175 {
176  ClientBuffer *b = manager->display()->clientBufferForResource(buffer);
177  switch (flag) {
178  case State::LeftBuffer:
179  pending.left = b;
180  break;
181  case State::TopLeftBuffer:
182  pending.topLeft = b;
183  break;
184  case State::TopBuffer:
185  pending.top = b;
186  break;
187  case State::TopRightBuffer:
188  pending.topRight = b;
189  break;
190  case State::RightBuffer:
191  pending.right = b;
192  break;
193  case State::BottomRightBuffer:
194  pending.bottomRight = b;
195  break;
196  case State::BottomBuffer:
197  pending.bottom = b;
198  break;
199  case State::BottomLeftBuffer:
200  pending.bottomLeft = b;
201  break;
202  default:
203  Q_UNREACHABLE();
204  break;
205  }
206  pending.flags = State::Flags(pending.flags | flag);
207 }
208 
209 void ShadowInterfacePrivate::org_kde_kwin_shadow_destroy(Resource *resource)
210 {
211  wl_resource_destroy(resource->handle);
212 }
213 
214 void ShadowInterfacePrivate::org_kde_kwin_shadow_destroy_resource(Resource *resource)
215 {
216  Q_UNUSED(resource)
217  delete q;
218 }
219 
220 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_left(Resource *resource, wl_resource *buffer)
221 {
222  Q_UNUSED(resource)
223  attach(State::LeftBuffer, buffer);
224 }
225 
226 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_top_left(Resource *resource, wl_resource *buffer)
227 {
228  Q_UNUSED(resource)
229  attach(State::TopLeftBuffer, buffer);
230 }
231 
232 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_top(Resource *resource, wl_resource *buffer)
233 {
234  Q_UNUSED(resource)
235  attach(State::TopBuffer, buffer);
236 }
237 
238 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_top_right(Resource *resource, wl_resource *buffer)
239 {
240  Q_UNUSED(resource)
241  attach(State::TopRightBuffer, buffer);
242 }
243 
244 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_right(Resource *resource, wl_resource *buffer)
245 {
246  Q_UNUSED(resource)
247  attach(State::RightBuffer, buffer);
248 }
249 
250 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_bottom_right(Resource *resource, wl_resource *buffer)
251 {
252  Q_UNUSED(resource)
253  attach(State::BottomRightBuffer, buffer);
254 }
255 
256 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_bottom(Resource *resource, wl_resource *buffer)
257 {
258  Q_UNUSED(resource)
259  attach(State::BottomBuffer, buffer);
260 }
261 
262 void ShadowInterfacePrivate::org_kde_kwin_shadow_attach_bottom_left(Resource *resource, wl_resource *buffer)
263 {
264  Q_UNUSED(resource)
265  attach(State::BottomLeftBuffer, buffer);
266 }
267 
268 void ShadowInterfacePrivate::org_kde_kwin_shadow_set_left_offset(Resource *resource, wl_fixed_t offset)
269 {
270  Q_UNUSED(resource)
271  pending.flags = State::Flags(pending.flags | State::Offset);
272  pending.offset.setLeft(wl_fixed_to_double(offset));
273 }
274 
275 void ShadowInterfacePrivate::org_kde_kwin_shadow_set_top_offset(Resource *resource, wl_fixed_t offset)
276 {
277  Q_UNUSED(resource)
278  pending.flags = State::Flags(pending.flags | State::Offset);
279  pending.offset.setTop(wl_fixed_to_double(offset));
280 }
281 
282 void ShadowInterfacePrivate::org_kde_kwin_shadow_set_right_offset(Resource *resource, wl_fixed_t offset)
283 {
284  Q_UNUSED(resource)
285  pending.flags = State::Flags(pending.flags | State::Offset);
286  pending.offset.setRight(wl_fixed_to_double(offset));
287 }
288 
289 void ShadowInterfacePrivate::org_kde_kwin_shadow_set_bottom_offset(Resource *resource, wl_fixed_t offset)
290 {
291  Q_UNUSED(resource)
292  pending.flags = State::Flags(pending.flags | State::Offset);
293  pending.offset.setBottom(wl_fixed_to_double(offset));
294 }
295 
296 ShadowInterfacePrivate::ShadowInterfacePrivate(ShadowInterface *_q, wl_resource *resource)
297  : QtWaylandServer::org_kde_kwin_shadow(resource)
298  , q(_q)
299 {
300 }
301 
302 ShadowInterfacePrivate::~ShadowInterfacePrivate()
303 {
304 #define CURRENT(__PART__) \
305  if (current.__PART__) { \
306  current.__PART__->unref(); \
307  }
308  CURRENT(left)
309  CURRENT(topLeft)
310  CURRENT(top)
311  CURRENT(topRight)
312  CURRENT(right)
313  CURRENT(bottomRight)
314  CURRENT(bottom)
315  CURRENT(bottomLeft)
316 #undef CURRENT
317 }
318 
319 ShadowInterface::ShadowInterface(ShadowManagerInterface *manager, wl_resource *resource)
320  : QObject()
321  , d(new ShadowInterfacePrivate(this, resource))
322 {
323  d->manager = manager;
324 }
325 
326 ShadowInterface::~ShadowInterface() = default;
327 
328 QMarginsF ShadowInterface::offset() const
329 {
330  return d->current.offset;
331 }
332 
333 #define BUFFER(__PART__) \
334  ClientBuffer *ShadowInterface::__PART__() const \
335  { \
336  return d->current.__PART__; \
337  }
338 
339 BUFFER(left)
340 BUFFER(topLeft)
341 BUFFER(top)
342 BUFFER(topRight)
343 BUFFER(right)
344 BUFFER(bottomRight)
345 BUFFER(bottom)
346 BUFFER(bottomLeft)
347 
348 }
Left
QTextStream & right(QTextStream &stream)
QTextStream & left(QTextStream &stream)
static SurfaceInterface * get(wl_resource *native)
Right
Top
QObject * parent() const const
State
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.