GCC Code Coverage Report


Directory: ./
File: server/plasma_virtual_desktop.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 157 165 95.2%
Branches: 53 84 63.1%

Line Branch Exec Source
1 /****************************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 ****************************************************************************/
20 #include "plasma_virtual_desktop_p.h"
21
22 #include "utils.h"
23 #include "wayland/display.h"
24 #include "wayland/global.h"
25 #include "wayland/resource.h"
26
27 #include <wayland-server.h>
28
29 namespace Wrapland::Server
30 {
31
32 const struct org_kde_plasma_virtual_desktop_management_interface
33 PlasmaVirtualDesktopManager::Private::s_interface
34 = {
35 getVirtualDesktopCallback,
36 requestCreateVirtualDesktopCallback,
37 requestRemoveVirtualDesktopCallback,
38 };
39
40 208 PlasmaVirtualDesktopManager::Private::Private(Display* display, PlasmaVirtualDesktopManager* q_ptr)
41 208 : PlasmaVirtualDesktopManagerGlobal(q_ptr,
42 104 display,
43 &org_kde_plasma_virtual_desktop_management_interface,
44 &s_interface)
45 104 {
46
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 create();
47 104 }
48
49 252 auto find_desktop(std::vector<PlasmaVirtualDesktop*> const& desktops, std::string const& id)
50 104 {
51 356 return std::find_if(
52 445 desktops.cbegin(), desktops.cend(), [&id](auto desk) { return desk->id() == id; });
53 }
54
55 27 void PlasmaVirtualDesktopManager::Private::getVirtualDesktopCallback(
56 [[maybe_unused]] wl_client* wlClient,
57 wl_resource* wlResource,
58 uint32_t serial,
59 char const* id)
60 {
61 27 auto priv = get_handle(wlResource)->d_ptr.get();
62 27 auto bind = priv->getBind(wlResource);
63
64
3/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
27 if (auto it = find_desktop(priv->desktops, id); it != priv->desktops.cend()) {
65 27 (*it)->d_ptr->createResource(bind->client, bind->version, serial);
66 27 }
67 27 }
68
69 1 void PlasmaVirtualDesktopManager::Private::requestCreateVirtualDesktopCallback(
70 [[maybe_unused]] wl_client* wlClient,
71 wl_resource* wlResource,
72 char const* name,
73 uint32_t position)
74 {
75 1 auto manager = get_handle(wlResource);
76
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 Q_EMIT manager->desktopCreateRequested(
77
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 name, qBound<uint32_t>(0, position, static_cast<uint32_t>(manager->desktops().size())));
78 1 }
79
80 1 void PlasmaVirtualDesktopManager::Private::requestRemoveVirtualDesktopCallback(
81 [[maybe_unused]] wl_client* wlClient,
82 wl_resource* wlResource,
83 218 char const* id)
84 {
85 1 auto manager = get_handle(wlResource);
86
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 Q_EMIT manager->desktopRemoveRequested(id);
87 1 }
88
89 10 void PlasmaVirtualDesktopManager::Private::bindInit(PlasmaVirtualDesktopManagerBind* bind)
90 {
91 10 uint32_t position = 0;
92
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
13 for (auto& desktop : desktops) {
93 6 bind->send<org_kde_plasma_virtual_desktop_management_send_desktop_created>(
94 3 desktop->id().c_str(), position++);
95 }
96
97 20 bind->send<org_kde_plasma_virtual_desktop_management_send_rows,
98 10 ORG_KDE_PLASMA_VIRTUAL_DESKTOP_MANAGEMENT_ROWS_SINCE_VERSION>(rows);
99
100 10 bind->send<org_kde_plasma_virtual_desktop_management_send_done>();
101 10 }
102
103 104 PlasmaVirtualDesktopManager::PlasmaVirtualDesktopManager(Display* display)
104
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
104 : d_ptr(new Private(display, this))
105 104 {
106 104 }
107
108 218 void PlasmaVirtualDesktopManager::Private::send_removed(std::string const& id)
109 {
110 218 send<org_kde_plasma_virtual_desktop_management_send_desktop_removed>(id.c_str());
111 218 }
112
113 208 PlasmaVirtualDesktopManager::~PlasmaVirtualDesktopManager()
114 208 {
115
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 104 times.
319 for (auto desktop : d_ptr->desktops) {
116
2/4
✓ Branch 0 taken 215 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 215 times.
✗ Branch 3 not taken.
215 d_ptr->send_removed(desktop->id());
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 delete desktop;
118 }
119 208 }
120
121 1 void PlasmaVirtualDesktopManager::setRows(uint32_t rows)
122 {
123
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (rows == 0 || d_ptr->rows == rows) {
124 return;
125 }
126
127 1 d_ptr->rows = rows;
128 1 d_ptr->send<org_kde_plasma_virtual_desktop_management_send_rows>(rows);
129 1 }
130
131 7 PlasmaVirtualDesktop* PlasmaVirtualDesktopManager::desktop(std::string const& id)
132 {
133
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (auto it = find_desktop(d_ptr->desktops, id); it != d_ptr->desktops.cend()) {
134 6 return *it;
135 }
136 1 return nullptr;
137 7 }
138
139 218 PlasmaVirtualDesktop* PlasmaVirtualDesktopManager::createDesktop(std::string const& id,
140 uint32_t position)
141 {
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (auto it = find_desktop(d_ptr->desktops, id); it != d_ptr->desktops.cend()) {
143 return *it;
144 }
145
146 218 auto const actualPosition = std::min(position, static_cast<uint32_t>(d_ptr->desktops.size()));
147
148
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 auto desktop = new PlasmaVirtualDesktop(this);
149 218 desktop->d_ptr->id = id;
150
151 // Activate the first desktop.
152 // TODO(unknown author): to be done here?
153
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 104 times.
218 if (d_ptr->desktops.empty()) {
154 104 desktop->d_ptr->active = true;
155 104 }
156
157 218 d_ptr->desktops.insert(d_ptr->desktops.cbegin() + actualPosition, desktop);
158
159 218 d_ptr->send<org_kde_plasma_virtual_desktop_management_send_desktop_created>(id.c_str(),
160 actualPosition);
161
162 218 return desktop;
163 218 }
164
165 3 void PlasmaVirtualDesktopManager::removeDesktop(std::string const& id)
166 {
167 8 auto deskIt = std::find_if(d_ptr->desktops.begin(), d_ptr->desktops.end(), [&id](auto desk) {
168 5 return desk->id() == id;
169 });
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (deskIt == d_ptr->desktops.end()) {
171 return;
172 }
173
174
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 delete *deskIt;
175 3 d_ptr->desktops.erase(deskIt);
176
177 3 d_ptr->send_removed(id);
178 3 }
179
180 48 std::vector<PlasmaVirtualDesktop*> const& PlasmaVirtualDesktopManager::desktops() const
181 {
182 48 return d_ptr->desktops;
183 }
184
185 18 void PlasmaVirtualDesktopManager::sendDone()
186 {
187 18 d_ptr->send<org_kde_plasma_virtual_desktop_management_send_done>();
188 18 }
189
190 /////////////////////////// Plasma Virtual Desktop ///////////////////////////
191
192 218 PlasmaVirtualDesktop::Private::Private(PlasmaVirtualDesktop* q_ptr,
193 PlasmaVirtualDesktopManager* manager)
194 218 : manager(manager)
195 218 , q_ptr{q_ptr}
196 {
197 218 }
198
199 218 PlasmaVirtualDesktop::Private::~Private()
200 {
201
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 218 times.
221 for (auto resource : resources) {
202
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 resource->d_ptr->send<org_kde_plasma_virtual_desktop_send_removed>();
203 3 resource->d_ptr->virtualDesktop = nullptr;
204 }
205 218 }
206
207 27 void PlasmaVirtualDesktop::Private::createResource(Wayland::Client* client,
208 uint32_t version,
209 uint32_t serial)
210 {
211
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 auto resource = new PlasmaVirtualDesktopRes(client->handle, version, serial, q_ptr);
212 27 resources.push_back(resource);
213 51 connect(resource, &PlasmaVirtualDesktopRes::resourceDestroyed, q_ptr, [this, resource]() {
214 24 remove_one(resources, resource);
215 24 });
216
217 27 resource->d_ptr->send<org_kde_plasma_virtual_desktop_send_desktop_id>(id.c_str());
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!name.empty()) {
220 27 resource->d_ptr->send<org_kde_plasma_virtual_desktop_send_name>(name.c_str());
221 27 }
222
223
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 if (active) {
224 9 resource->d_ptr->send<org_kde_plasma_virtual_desktop_send_activated>();
225 9 }
226
227 27 resource->d_ptr->send<org_kde_plasma_virtual_desktop_send_done>();
228
229 27 client->flush();
230 27 }
231
232 218 PlasmaVirtualDesktop::PlasmaVirtualDesktop(PlasmaVirtualDesktopManager* parent)
233 218 : QObject(parent)
234
2/4
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 218 times.
218 , d_ptr(new Private(this, parent))
235 218 {
236 218 }
237
238 436 PlasmaVirtualDesktop::~PlasmaVirtualDesktop() = default;
239
240 455 std::string const& PlasmaVirtualDesktop::id() const
241 {
242 455 return d_ptr->id;
243 }
244
245 28 void PlasmaVirtualDesktop::setName(std::string const& name)
246 {
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (d_ptr->name == name) {
248 return;
249 }
250
251 28 d_ptr->name = name;
252
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 for (auto& res : d_ptr->resources) {
253 res->d_ptr->send<org_kde_plasma_virtual_desktop_send_name>(name.c_str());
254 }
255 28 }
256
257 std::string const& PlasmaVirtualDesktop::name() const
258 {
259 return d_ptr->name;
260 }
261
262 6 void PlasmaVirtualDesktop::setActive(bool active)
263 {
264
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (d_ptr->active == active) {
265 2 return;
266 }
267
268 4 d_ptr->active = active;
269
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (active) {
270
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (auto& res : d_ptr->resources) {
271 2 res->d_ptr->send<org_kde_plasma_virtual_desktop_send_activated>();
272 }
273 2 } else {
274
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (auto& res : d_ptr->resources) {
275 2 res->d_ptr->send<org_kde_plasma_virtual_desktop_send_deactivated>();
276 }
277 }
278 6 }
279
280 9 bool PlasmaVirtualDesktop::active() const
281 {
282 9 return d_ptr->active;
283 }
284
285 28 void PlasmaVirtualDesktop::sendDone()
286 {
287
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 28 times.
55 for (auto res : d_ptr->resources) {
288 27 res->d_ptr->send<org_kde_plasma_virtual_desktop_send_done>();
289 }
290 28 }
291
292 /////////////////////////// Plasma Virtual Desktop Resource ///////////////////////////
293
294 27 PlasmaVirtualDesktopRes::Private::Private(Client* client,
295 uint32_t version,
296 uint32_t id,
297 PlasmaVirtualDesktop* virtualDesktop,
298 PlasmaVirtualDesktopRes* q_ptr)
299 54 : Wayland::Resource<PlasmaVirtualDesktopRes>(client,
300 27 version,
301 27 id,
302 &org_kde_plasma_virtual_desktop_interface,
303 &s_interface,
304 27 q_ptr)
305 27 , virtualDesktop(virtualDesktop)
306 27 {
307 27 }
308
309 const struct org_kde_plasma_virtual_desktop_interface PlasmaVirtualDesktopRes::Private::s_interface
310 = {requestActivateCallback};
311
312 1 void PlasmaVirtualDesktopRes::Private::requestActivateCallback([[maybe_unused]] wl_client* wlClient,
313 wl_resource* wlResource)
314 {
315 1 auto priv = get_handle(wlResource)->d_ptr;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!priv->virtualDesktop) {
317 return;
318 }
319 1 Q_EMIT priv->virtualDesktop->activateRequested();
320 1 }
321
322 27 PlasmaVirtualDesktopRes::PlasmaVirtualDesktopRes(Client* client,
323 uint32_t version,
324 uint32_t id,
325 PlasmaVirtualDesktop* virtualDesktop)
326 27 : QObject(nullptr)
327
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
27 , d_ptr(new Private(client, version, id, virtualDesktop, this))
328 27 {
329 27 }
330
331 }
332