KWayland

plasmavirtualdesktop.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Marco Martin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "plasmavirtualdesktop.h"
7 #include "event_queue.h"
8 #include "wayland_pointer_p.h"
9 
10 #include <QMap>
11 #include <QDebug>
12 
13 #include <wayland-plasma-virtual-desktop-client-protocol.h>
14 
15 namespace KWayland
16 {
17 namespace Client
18 {
19 
20 class Q_DECL_HIDDEN PlasmaVirtualDesktopManagement::Private
21 {
22 public:
23  Private(PlasmaVirtualDesktopManagement *q);
24 
25  void setup(org_kde_plasma_virtual_desktop_management *arg);
26 
27  WaylandPointer<org_kde_plasma_virtual_desktop_management, org_kde_plasma_virtual_desktop_management_destroy> plasmavirtualdesktopmanagement;
28  EventQueue *queue = nullptr;
29 
30  quint32 rows = 1;
32 
33  inline QList<PlasmaVirtualDesktop*>::const_iterator constFindDesktop(const QString &id);
34  inline QList<PlasmaVirtualDesktop*>::iterator findDesktop(const QString &id);
35 
36 private:
37  static void createdCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, const char *id, uint32_t position);
38  static void removedCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, const char *id);
39  static void rowsCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, uint32_t rows);
40  static void doneCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management);
41 
42  PlasmaVirtualDesktopManagement *q;
43 
44  static const org_kde_plasma_virtual_desktop_management_listener s_listener;
45 };
46 
47 class Q_DECL_HIDDEN PlasmaVirtualDesktop::Private
48 {
49 public:
50  Private(PlasmaVirtualDesktop *q);
51 
52  void setup(org_kde_plasma_virtual_desktop *arg);
53 
54  WaylandPointer<org_kde_plasma_virtual_desktop, org_kde_plasma_virtual_desktop_destroy> plasmavirtualdesktop;
55 
56  QString id;
57  QString name;
58  bool active = false;
59 
60 private:
61  PlasmaVirtualDesktop *q;
62 
63 private:
64  static void idCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop, const char * id);
65  static void nameCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop, const char * name);
66 
67  static void activatedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop);
68  static void deactivatedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop);
69  static void doneCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop);
70  static void removedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop);
71 
72  static const org_kde_plasma_virtual_desktop_listener s_listener;
73 };
74 
75 
76 
77 inline QList<PlasmaVirtualDesktop*>::const_iterator PlasmaVirtualDesktopManagement::Private::constFindDesktop(const QString &id)
78 {
79  return std::find_if( desktops.constBegin(),
80  desktops.constEnd(),
81  [id]( const PlasmaVirtualDesktop *desk ){ return desk->id() == id; } );
82 }
83 
84 inline QList<PlasmaVirtualDesktop*>::iterator PlasmaVirtualDesktopManagement::Private::findDesktop(const QString &id)
85 {
86  return std::find_if( desktops.begin(),
87  desktops.end(),
88  [id]( const PlasmaVirtualDesktop *desk ){ return desk->id() == id; } );
89 }
90 
91 const org_kde_plasma_virtual_desktop_management_listener PlasmaVirtualDesktopManagement::Private::s_listener = {
92  createdCallback,
93  removedCallback,
94  doneCallback,
95  rowsCallback
96 };
97 
98 void PlasmaVirtualDesktopManagement::Private::createdCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, const char *id, uint32_t position)
99 {
100  auto p = reinterpret_cast<PlasmaVirtualDesktopManagement::Private*>(data);
101  Q_ASSERT(p->plasmavirtualdesktopmanagement == org_kde_plasma_virtual_desktop_management);
102  const QString stringId = QString::fromUtf8(id);
103  PlasmaVirtualDesktop *vd = p->q->getVirtualDesktop(stringId);
104  Q_ASSERT(vd);
105 
106  p->desktops.insert(position, vd);
107  //TODO: emit a lot of desktopMoved?
108 
109  emit p->q->desktopCreated(stringId, position);
110 }
111 
112 void PlasmaVirtualDesktopManagement::Private::removedCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, const char *id)
113 {
114  auto p = reinterpret_cast<PlasmaVirtualDesktopManagement::Private*>(data);
115  Q_ASSERT(p->plasmavirtualdesktopmanagement == org_kde_plasma_virtual_desktop_management);
116  const QString stringId = QString::fromUtf8(id);
117  PlasmaVirtualDesktop *vd = p->q->getVirtualDesktop(stringId);
118  //TODO: emit a lot of desktopMoved?
119  Q_ASSERT(vd);
120  auto i = p->findDesktop(stringId);
121  p->desktops.erase(i);
122  vd->release();
123  vd->destroy();
124  vd->deleteLater();
125  emit p->q->desktopRemoved(stringId);
126 }
127 
128 void PlasmaVirtualDesktopManagement::Private::rowsCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management, uint32_t rows)
129 {
130  auto p = reinterpret_cast<PlasmaVirtualDesktopManagement::Private*>(data);
131  Q_ASSERT(p->plasmavirtualdesktopmanagement == org_kde_plasma_virtual_desktop_management);
132  if (rows == 0) {
133  return;
134  }
135  p->rows = rows;
136  emit p->q->rowsChanged(rows);
137 }
138 
139 void PlasmaVirtualDesktopManagement::Private::doneCallback(void *data, org_kde_plasma_virtual_desktop_management *org_kde_plasma_virtual_desktop_management)
140 {
141  auto p = reinterpret_cast<PlasmaVirtualDesktopManagement::Private*>(data);
142  Q_ASSERT(p->plasmavirtualdesktopmanagement == org_kde_plasma_virtual_desktop_management);
143  emit p->q->done();
144 }
145 
147  : QObject(parent)
148  , d(new Private(this))
149 {
150 }
151 
152 PlasmaVirtualDesktopManagement::Private::Private(PlasmaVirtualDesktopManagement *q)
153  : q(q)
154 {}
155 
156 void PlasmaVirtualDesktopManagement::Private::setup(org_kde_plasma_virtual_desktop_management *arg)
157 {
158  Q_ASSERT(arg);
159  Q_ASSERT(!plasmavirtualdesktopmanagement);
160  plasmavirtualdesktopmanagement.setup(arg);
161  org_kde_plasma_virtual_desktop_management_add_listener(plasmavirtualdesktopmanagement, &s_listener, this);
162 }
163 
164 PlasmaVirtualDesktopManagement::~PlasmaVirtualDesktopManagement()
165 {
166  release();
167 }
168 
169 void PlasmaVirtualDesktopManagement::setup(org_kde_plasma_virtual_desktop_management *plasmavirtualdesktopmanagement)
170 {
171  d->setup(plasmavirtualdesktopmanagement);
172 }
173 
175 {
176  d->plasmavirtualdesktopmanagement.release();
177 }
178 
180 {
181  d->plasmavirtualdesktopmanagement.destroy();
182 }
183 
184 PlasmaVirtualDesktopManagement::operator org_kde_plasma_virtual_desktop_management*() {
185  return d->plasmavirtualdesktopmanagement;
186 }
187 
188 PlasmaVirtualDesktopManagement::operator org_kde_plasma_virtual_desktop_management*() const {
189  return d->plasmavirtualdesktopmanagement;
190 }
191 
193 {
194  return d->plasmavirtualdesktopmanagement.isValid();
195 }
196 
198 {
199  d->queue = queue;
200 }
201 
203 {
204  return d->queue;
205 }
206 
208 {
209  Q_ASSERT(isValid());
210 
211  if (id.isEmpty()) {
212  return nullptr;
213  }
214 
215  auto i = d->constFindDesktop(id);
216  if (i != d->desktops.constEnd()) {
217  return *i;
218  }
219 
220  auto w = org_kde_plasma_virtual_desktop_management_get_virtual_desktop(d->plasmavirtualdesktopmanagement, id.toUtf8());
221 
222  if (!w) {
223  return nullptr;
224  }
225 
226  if (d->queue) {
227  d->queue->addProxy(w);
228  }
229 
230  auto desktop = new PlasmaVirtualDesktop(this);
231  desktop->setup(w);
232  desktop->d->id = id;
233 
234  return desktop;
235 }
236 
238 {
239  Q_ASSERT(isValid());
240 
241  org_kde_plasma_virtual_desktop_management_request_remove_virtual_desktop(d->plasmavirtualdesktopmanagement, id.toUtf8());
242 }
243 
245 {
246  Q_ASSERT(isValid());
247 
248  org_kde_plasma_virtual_desktop_management_request_create_virtual_desktop(d->plasmavirtualdesktopmanagement, name.toUtf8(), position);
249 }
250 
252 {
253  return d->desktops;
254 }
255 
257 {
258  return d->rows;
259 }
260 
261 const org_kde_plasma_virtual_desktop_listener PlasmaVirtualDesktop::Private::s_listener = {
262  idCallback,
263  nameCallback,
264  activatedCallback,
265  deactivatedCallback,
266  doneCallback,
267  removedCallback
268 };
269 
270 void PlasmaVirtualDesktop::Private::idCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop, const char * id)
271 {
272  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
273  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
274  p->id = QString::fromUtf8(id);
275 }
276 
277 void PlasmaVirtualDesktop::Private::nameCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop, const char * name)
278 {
279  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
280  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
281  p->name = QString::fromUtf8(name);
282 }
283 
284 void PlasmaVirtualDesktop::Private::activatedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop)
285 {
286  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
287  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
288  p->active = true;
289  emit p->q->activated();
290 }
291 
292 void PlasmaVirtualDesktop::Private::deactivatedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop)
293 {
294  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
295  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
296  p->active = false;
297  emit p->q->deactivated();
298 }
299 
300 void PlasmaVirtualDesktop::Private::doneCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop)
301 {
302  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
303  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
304  emit p->q->done();
305 }
306 
307 void PlasmaVirtualDesktop::Private::removedCallback(void *data, org_kde_plasma_virtual_desktop *org_kde_plasma_virtual_desktop)
308 {
309  auto p = reinterpret_cast<PlasmaVirtualDesktop::Private*>(data);
310  Q_ASSERT(p->plasmavirtualdesktop == org_kde_plasma_virtual_desktop);
311  emit p->q->removed();
312 }
313 
314 PlasmaVirtualDesktop::Private::Private(PlasmaVirtualDesktop *q)
315  : q(q)
316 {
317 }
318 
319 PlasmaVirtualDesktop::PlasmaVirtualDesktop(QObject *parent)
320  : QObject(parent)
321  , d(new Private(this))
322 {
323 }
324 
325 void PlasmaVirtualDesktop::Private::setup(org_kde_plasma_virtual_desktop *arg)
326 {
327  Q_ASSERT(arg);
328  Q_ASSERT(!plasmavirtualdesktop);
329  plasmavirtualdesktop.setup(arg);
330  org_kde_plasma_virtual_desktop_add_listener(plasmavirtualdesktop, &s_listener, this);
331 }
332 
333 PlasmaVirtualDesktop::~PlasmaVirtualDesktop()
334 {
335  release();
336 }
337 
338 void PlasmaVirtualDesktop::setup(org_kde_plasma_virtual_desktop *plasmavirtualdesktop)
339 {
340  d->setup(plasmavirtualdesktop);
341 }
342 
343 void PlasmaVirtualDesktop::release()
344 {
345  d->plasmavirtualdesktop.release();
346 }
347 
348 void PlasmaVirtualDesktop::destroy()
349 {
350  d->plasmavirtualdesktop.destroy();
351 }
352 
353 PlasmaVirtualDesktop::operator org_kde_plasma_virtual_desktop*() {
354  return d->plasmavirtualdesktop;
355 }
356 
357 PlasmaVirtualDesktop::operator org_kde_plasma_virtual_desktop*() const {
358  return d->plasmavirtualdesktop;
359 }
360 
361 bool PlasmaVirtualDesktop::isValid() const
362 {
363  return d->plasmavirtualdesktop.isValid();
364 }
365 
366 void PlasmaVirtualDesktop::requestActivate()
367 {
368  Q_ASSERT(isValid());
369  org_kde_plasma_virtual_desktop_request_activate(d->plasmavirtualdesktop);
370 }
371 
372 QString PlasmaVirtualDesktop::id() const
373 {
374  return d->id;
375 }
376 
377 QString PlasmaVirtualDesktop::name() const
378 {
379  return d->name;
380 }
381 
382 bool PlasmaVirtualDesktop::isActive() const
383 {
384  return d->active;
385 }
386 
387 }
388 }
389 
void setEventQueue(EventQueue *queue)
Sets the queue to use for creating objects with this PlasmaVirtualDesktopManagement.
Wrapper class for wl_event_queue interface.
Definition: event_queue.h:55
void requestCreateVirtualDesktop(const QString &name, quint32 position=std::numeric_limits< uint32_t >::max())
Ask the server to create a new virtual desktop, and position it at a specified position.
Wrapper for the org_kde_plasma_virtual_desktop_management interface.
QString fromUtf8(const char *str, int size)
PlasmaVirtualDesktopManagement(QObject *parent=nullptr)
Creates a new PlasmaVirtualDesktopManagement.
QList< PlasmaVirtualDesktop * > desktops() const
void destroy()
Destroys the data held by this PlasmaVirtualDesktopManagement.
void release()
Releases the org_kde_plasma_virtual_desktop_management interface.
QObject(QObject *parent)
PlasmaVirtualDesktop * getVirtualDesktop(const QString &id)
void setup(org_kde_plasma_virtual_desktop_management *plasmavirtualdesktopmanagement)
Setup this PlasmaVirtualDesktopManagement to manage the plasmavirtualdesktopmanagement.
void requestRemoveVirtualDesktop(const QString &id)
Requests for the desktop identified by id to be removed.
QObject * parent() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:48:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.