KWaylandServer

plasmavirtualdesktop_interface.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_interface.h"
7 #include "display.h"
8 
9 #include <QDebug>
10 #include <QTimer>
11 
12 #include <qwayland-server-org-kde-plasma-virtual-desktop.h>
13 #include <wayland-server.h>
14 
15 namespace KWaylandServer
16 {
17 static const quint32 s_version = 2;
18 
19 class PlasmaVirtualDesktopInterfacePrivate : public QtWaylandServer::org_kde_plasma_virtual_desktop
20 {
21 public:
22  PlasmaVirtualDesktopInterfacePrivate(PlasmaVirtualDesktopInterface *q, PlasmaVirtualDesktopManagementInterface *c);
23  ~PlasmaVirtualDesktopInterfacePrivate();
24 
25  PlasmaVirtualDesktopInterface *q;
26  PlasmaVirtualDesktopManagementInterface *vdm;
27 
28  QString id;
29  QString name;
30  bool active = false;
31 
32 protected:
33  void org_kde_plasma_virtual_desktop_bind_resource(Resource *resource) override;
34  void org_kde_plasma_virtual_desktop_request_activate(Resource *resource) override;
35 };
36 
37 class PlasmaVirtualDesktopManagementInterfacePrivate : public QtWaylandServer::org_kde_plasma_virtual_desktop_management
38 {
39 public:
40  PlasmaVirtualDesktopManagementInterfacePrivate(PlasmaVirtualDesktopManagementInterface *_q, Display *display);
41 
43  quint32 rows = 0;
44  quint32 columns = 0;
45  PlasmaVirtualDesktopManagementInterface *q;
46 
47  inline QList<PlasmaVirtualDesktopInterface *>::const_iterator constFindDesktop(const QString &id);
48  inline QList<PlasmaVirtualDesktopInterface *>::iterator findDesktop(const QString &id);
49 
50 protected:
51  void org_kde_plasma_virtual_desktop_management_get_virtual_desktop(Resource *resource, uint32_t id, const QString &desktop_id) override;
52  void org_kde_plasma_virtual_desktop_management_request_create_virtual_desktop(Resource *resource, const QString &name, uint32_t position) override;
53  void org_kde_plasma_virtual_desktop_management_request_remove_virtual_desktop(Resource *resource, const QString &desktop_id) override;
54  void org_kde_plasma_virtual_desktop_management_bind_resource(Resource *resource) override;
55 };
56 
57 inline QList<PlasmaVirtualDesktopInterface *>::const_iterator PlasmaVirtualDesktopManagementInterfacePrivate::constFindDesktop(const QString &id)
58 {
59  return std::find_if(desktops.constBegin(), desktops.constEnd(), [id](const PlasmaVirtualDesktopInterface *desk) {
60  return desk->id() == id;
61  });
62 }
63 
64 inline QList<PlasmaVirtualDesktopInterface *>::iterator PlasmaVirtualDesktopManagementInterfacePrivate::findDesktop(const QString &id)
65 {
66  return std::find_if(desktops.begin(), desktops.end(), [id](const PlasmaVirtualDesktopInterface *desk) {
67  return desk->id() == id;
68  });
69 }
70 
71 void PlasmaVirtualDesktopManagementInterfacePrivate::org_kde_plasma_virtual_desktop_management_get_virtual_desktop(Resource *resource,
72  uint32_t id,
73  const QString &desktop_id)
74 {
75  auto i = constFindDesktop(desktop_id);
76  if (i == desktops.constEnd()) {
77  return;
78  }
79 
80  (*i)->d->add(resource->client(), id, resource->version());
81 }
82 
83 void PlasmaVirtualDesktopManagementInterfacePrivate::org_kde_plasma_virtual_desktop_management_request_create_virtual_desktop(Resource *resource,
84  const QString &name,
85  uint32_t position)
86 {
87  Q_UNUSED(resource)
88  Q_EMIT q->desktopCreateRequested(name, qBound<quint32>(0, position, (quint32)desktops.count()));
89 }
90 
91 void PlasmaVirtualDesktopManagementInterfacePrivate::org_kde_plasma_virtual_desktop_management_request_remove_virtual_desktop(Resource *resource,
92  const QString &desktop_id)
93 {
94  Q_UNUSED(resource)
95  Q_EMIT q->desktopRemoveRequested(desktop_id);
96 }
97 
98 PlasmaVirtualDesktopManagementInterfacePrivate::PlasmaVirtualDesktopManagementInterfacePrivate(PlasmaVirtualDesktopManagementInterface *_q, Display *display)
99  : QtWaylandServer::org_kde_plasma_virtual_desktop_management(*display, s_version)
100  , q(_q)
101 {
102 }
103 
104 void PlasmaVirtualDesktopManagementInterfacePrivate::org_kde_plasma_virtual_desktop_management_bind_resource(Resource *resource)
105 {
106  quint32 i = 0;
107  for (auto it = desktops.constBegin(); it != desktops.constEnd(); ++it) {
108  send_desktop_created(resource->handle, (*it)->id(), i++);
109  }
110 
111  if (resource->version() >= ORG_KDE_PLASMA_VIRTUAL_DESKTOP_MANAGEMENT_ROWS_SINCE_VERSION) {
112  send_rows(resource->handle, rows);
113  }
114 
115  send_done(resource->handle);
116 }
117 
118 PlasmaVirtualDesktopManagementInterface::PlasmaVirtualDesktopManagementInterface(Display *display, QObject *parent)
119  : QObject(parent)
120  , d(new PlasmaVirtualDesktopManagementInterfacePrivate(this, display))
121 {
122 }
123 
124 PlasmaVirtualDesktopManagementInterface::~PlasmaVirtualDesktopManagementInterface()
125 {
126  qDeleteAll(d->desktops);
127 }
128 
130 {
131  if (rows == 0 || d->rows == rows) {
132  return;
133  }
134 
135  d->rows = rows;
136 
137  const auto clientResources = d->resourceMap();
138  for (auto resource : clientResources) {
139  if (resource->version() < ORG_KDE_PLASMA_VIRTUAL_DESKTOP_MANAGEMENT_ROWS_SINCE_VERSION) {
140  continue;
141  }
142  d->send_rows(resource->handle, rows);
143  }
144 }
145 
146 PlasmaVirtualDesktopInterface *PlasmaVirtualDesktopManagementInterface::desktop(const QString &id)
147 {
148  auto i = d->constFindDesktop(id);
149  if (i != d->desktops.constEnd()) {
150  return *i;
151  }
152  return nullptr;
153 }
154 
155 PlasmaVirtualDesktopInterface *PlasmaVirtualDesktopManagementInterface::createDesktop(const QString &id, quint32 position)
156 {
157  auto i = d->constFindDesktop(id);
158  if (i != d->desktops.constEnd()) {
159  return *i;
160  }
161 
162  const quint32 actualPosition = qMin(position, (quint32)d->desktops.count());
163 
164  auto desktop = new PlasmaVirtualDesktopInterface(this);
165  desktop->d->id = id;
166 
167  const auto desktopClientResources = desktop->d->resourceMap();
168  for (auto resource : desktopClientResources) {
169  desktop->d->send_desktop_id(resource->handle, id);
170  }
171 
172  // activate the first desktop TODO: to be done here?
173  if (d->desktops.isEmpty()) {
174  desktop->d->active = true;
175  }
176 
177  d->desktops.insert(actualPosition, desktop);
178 
179  const auto clientResources = d->resourceMap();
180  for (auto resource : clientResources) {
181  d->send_desktop_created(resource->handle, id, actualPosition);
182  }
183 
184  return desktop;
185 }
186 
188 {
189  auto deskIt = d->findDesktop(id);
190  if (deskIt == d->desktops.end()) {
191  return;
192  }
193 
194  const auto desktopClientResources = (*deskIt)->d->resourceMap();
195  for (auto resource : desktopClientResources) {
196  (*deskIt)->d->send_removed(resource->handle);
197  }
198 
199  const auto clientResources = d->resourceMap();
200  for (auto resource : clientResources) {
201  d->send_desktop_removed(resource->handle, id);
202  }
203 
204  (*deskIt)->deleteLater();
205  d->desktops.erase(deskIt);
206 }
207 
209 {
210  return d->desktops;
211 }
212 
214 {
215  const auto clientResources = d->resourceMap();
216  for (auto resource : clientResources) {
217  d->send_done(resource->handle);
218  }
219 }
220 
221 //// PlasmaVirtualDesktopInterface
222 
223 void PlasmaVirtualDesktopInterfacePrivate::org_kde_plasma_virtual_desktop_request_activate(Resource *resource)
224 {
225  Q_UNUSED(resource)
226  Q_EMIT q->activateRequested();
227 }
228 
229 PlasmaVirtualDesktopInterfacePrivate::PlasmaVirtualDesktopInterfacePrivate(PlasmaVirtualDesktopInterface *q, PlasmaVirtualDesktopManagementInterface *c)
230  : QtWaylandServer::org_kde_plasma_virtual_desktop()
231  , q(q)
232  , vdm(c)
233 {
234 }
235 
236 PlasmaVirtualDesktopInterfacePrivate::~PlasmaVirtualDesktopInterfacePrivate()
237 {
238  const auto clientResources = resourceMap();
239  for (Resource *resource : clientResources) {
240  send_removed(resource->handle);
241  wl_resource_destroy(resource->handle);
242  }
243 }
244 
245 void PlasmaVirtualDesktopInterfacePrivate::org_kde_plasma_virtual_desktop_bind_resource(Resource *resource)
246 {
247  send_desktop_id(resource->handle, id);
248 
249  if (!name.isEmpty()) {
250  send_name(resource->handle, name);
251  }
252 
253  if (active) {
254  send_activated(resource->handle);
255  }
256 }
257 
258 PlasmaVirtualDesktopInterface::PlasmaVirtualDesktopInterface(PlasmaVirtualDesktopManagementInterface *parent)
259  : QObject()
260  , d(new PlasmaVirtualDesktopInterfacePrivate(this, parent))
261 {
262 }
263 
264 PlasmaVirtualDesktopInterface::~PlasmaVirtualDesktopInterface()
265 {
266  d->vdm->removeDesktop(id());
267 }
268 
269 QString PlasmaVirtualDesktopInterface::id() const
270 {
271  return d->id;
272 }
273 
274 void PlasmaVirtualDesktopInterface::setName(const QString &name)
275 {
276  if (d->name == name) {
277  return;
278  }
279 
280  d->name = name;
281 
282  const auto clientResources = d->resourceMap();
283  for (auto resource : clientResources) {
284  d->send_name(resource->handle, name);
285  }
286 }
287 
288 QString PlasmaVirtualDesktopInterface::name() const
289 {
290  return d->name;
291 }
292 
293 void PlasmaVirtualDesktopInterface::setActive(bool active)
294 {
295  if (d->active == active) {
296  return;
297  }
298 
299  d->active = active;
300  const auto clientResources = d->resourceMap();
301 
302  if (active) {
303  for (auto resource : clientResources) {
304  d->send_activated(resource->handle);
305  }
306  } else {
307  for (auto resource : clientResources) {
308  d->send_deactivated(resource->handle);
309  }
310  }
311 }
312 
313 bool PlasmaVirtualDesktopInterface::isActive() const
314 {
315  return d->active;
316 }
317 
318 void PlasmaVirtualDesktopInterface::sendDone()
319 {
320  const auto clientResources = d->resourceMap();
321  for (auto resource : clientResources) {
322  d->send_done(resource->handle);
323  }
324 }
325 
326 }
void sendDone()
Inform the clients that all the properties have been sent, and their client-side representation is co...
QString name(const QVariant &location)
Wrapper for the org_kde_plasma_virtual_desktop_management interface.
void setRows(quint32 rows)
Sets how many rows the virtual desktops should be laid into.
PlasmaVirtualDesktopInterface * desktop(const QString &id)
void removeDesktop(const QString &id)
Removed and destroys the desktop identified by id, if present.
QList< PlasmaVirtualDesktopInterface * > desktops() const
PlasmaVirtualDesktopInterface * createDesktop(const QString &id, quint32 position=std::numeric_limits< uint32_t >::max())
QObject(QObject *parent)
QObject * parent() const const
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:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.