Debian Codegraph @

panel/backends/wayland/wlroots/workspace.cpp

raw
1 #include "workspace.hpp"
2
3 #include <QGuiApplication>
4 #include <QMap>
5
6 #include <private/qwaylandscreen_p.h>
7
8 /** ext_workspace_handle_v1 <-> WorkspaceHandleV1 map */
9 QMap<struct ::ext_workspace_handle_v1*, QPointer<LXQt::Taskbar::WorkspaceHandleV1>> workspaceMap;
10 QList<LXQt::Taskbar::WorkspaceGroupHandleV1*> workspaceGroups;
11
12 /**
13 * Implementation of the LXQt::TaskBar::WorkspaceManagerV1 class
14 */
15
16 LXQt::Taskbar::WorkspaceManagerV1::WorkspaceManagerV1() : QWaylandClientExtensionTemplate(
17 version)
18 {
19 /** Automatically destroy thie object */
20 connect(
21 this, &QWaylandClientExtension::activeChanged, this, [this]
22 {
23 if (!isActive())
24 {
25 ext_workspace_manager_v1_destroy(object());
26 }
27 });
28 }
29
30 LXQt::Taskbar::WorkspaceManagerV1::~WorkspaceManagerV1()
31 {
32 if (!isActive())
33 {
34 ext_workspace_manager_v1_destroy(object());
35 }
36 }
37
38 int LXQt::Taskbar::WorkspaceManagerV1::workspaceCount(QScreen *screen)
39 {
40 if (screen)
41 {
42 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(screen->handle()))
43 {
44 wl_output *output = waylandScreen->output();
45 for (auto *wg : std::as_const(workspaceGroups))
46 {
47 if (wg->outputs().contains(output))
48 return wg->workspaces().count();
49 }
50 }
51 }
52
53 return workspaceMap.count();
54 }
55
56 int LXQt::Taskbar::WorkspaceManagerV1::currentWorkspaceIndex(QScreen *screen)
57 {
58 QMap<QList<int>, WorkspaceHandleV1*> map; // sorted by key
59 if (screen)
60 {
61 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(screen->handle()))
62 {
63 wl_output *output = waylandScreen->output();
64 for (auto *wg : std::as_const(workspaceGroups))
65 {
66 if (wg->outputs().contains(output))
67 {
68 const auto workspaces = wg->workspaces();
69 for (const auto& ws : workspaces)
70 {
71 if (ws)
72 map[ws->coordinates()] = ws;
73 }
74 break;
75 }
76 }
77 }
78 }
79 if (!map.isEmpty())
80 {
81 int index = 0;
82 for (WorkspaceHandleV1 *ws : std::as_const(map))
83 {
84 ++index;
85 if (ws->state() & WorkspaceHandleV1::state_active)
86 {
87 return index;
88 }
89 }
90 }
91
92 return -1;
93 }
94
95 QString LXQt::Taskbar::WorkspaceManagerV1::workspaceName(int idx, const QString &sceenName)
96 {
97 QMap<QList<int>, WorkspaceHandleV1*> map;
98 const auto screens = QGuiApplication::screens();
99 for (const auto& scr : screens)
100 {
101 if (scr->name() == sceenName)
102 {
103 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(scr->handle()))
104 {
105 wl_output *output = waylandScreen->output();
106 for (auto *wg : std::as_const(workspaceGroups))
107 {
108 if (wg->outputs().contains(output))
109 {
110 const auto workspaces = wg->workspaces();
111 for (const auto& ws : workspaces)
112 {
113 if (ws)
114 map[ws->coordinates()] = ws;
115 }
116 break;
117 }
118 }
119 }
120 break;
121 }
122 }
123 if (idx <= map.size())
124 {
125 int index = 0;
126 for (WorkspaceHandleV1 *ws : std::as_const(map))
127 {
128 ++index;
129 if (index == idx)
130 {
131 return ws->name();
132 }
133 }
134 }
135
136 return QString();
137 }
138
139 void LXQt::Taskbar::WorkspaceManagerV1::setCurrentWorkspaceIndex(int idx, QScreen *screen)
140 {
141 if (idx <= 0) return;
142 QMap<QList<int>, WorkspaceHandleV1*> map; // sorted by key
143 if (screen)
144 {
145 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(screen->handle()))
146 {
147 wl_output *output = waylandScreen->output();
148 for (auto *wg : std::as_const(workspaceGroups))
149 {
150 if (wg->outputs().contains(output))
151 {
152 const auto workspaces = wg->workspaces();
153 for (const auto& ws : workspaces)
154 {
155 if (ws)
156 map[ws->coordinates()] = ws;
157 }
158 break;
159 }
160 }
161 }
162 }
163 if (idx <= map.size())
164 {
165 int index = 0;
166 for (WorkspaceHandleV1 *ws : std::as_const(map))
167 {
168 ++index;
169 if (index == idx)
170 {
171 //qDebug() << ws->name() << "Activating";
172 ws->activate();
173 return;
174 }
175 }
176 }
177 }
178
179 void LXQt::Taskbar::WorkspaceManagerV1::ext_workspace_manager_v1_workspace_group(
180 struct ::ext_workspace_group_handle_v1 *workspace_group)
181 {
182 auto wg = new WorkspaceGroupHandleV1(workspace_group);
183 if (!workspaceGroups.contains(wg))
184 {
185 workspaceGroups << wg;
186 emit workspaceGroupAdded(wg);
187 connect(wg, &WorkspaceGroupHandleV1::workspaceAdded, this, &WorkspaceManagerV1::workspaceAdded);
188 connect(wg, &WorkspaceGroupHandleV1::workspaceRemoved, this, &WorkspaceManagerV1::workspaceRemoved);
189
190 connect(this, &WorkspaceManagerV1::activation, wg, [this, wg]() {
191 QList<QScreen*> scrns;
192 const auto screens = QGuiApplication::screens();
193 for (const auto& scr : screens)
194 {
195 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(scr->handle()))
196 {
197 if (wg->outputs().contains(waylandScreen->output()))
198 {
199 scrns << scr;
200 }
201 }
202 }
203 emit currentWorkspaceChanged(scrns);
204 });
205
206 /* NOTE: This is a workaround for a Qt/QtWayland issue, because of which, the
207 QScreen corresponding to a new output may be created with a delay and
208 mess with the desktop switcher on the new screen. The issue usually
209 happened on re-plugging an external monitor or resuming from suspension. */
210 connect(qGuiApp, &QGuiApplication::screenAdded, wg, [this, wg]() {
211 QList<QScreen*> scrns;
212 const auto screens = QGuiApplication::screens();
213 for (const auto& scr : screens)
214 {
215 if (auto waylandScreen = dynamic_cast<QtWaylandClient::QWaylandScreen*>(scr->handle()))
216 {
217 if (wg->outputs().contains(waylandScreen->output()))
218 {
219 scrns << scr;
220 }
221 }
222 }
223
224 emit workspaceAdded(nullptr);
225 emit currentWorkspaceChanged(scrns);
226 emit nameChanged();
227 });
228 }
229 }
230
231 void LXQt::Taskbar::WorkspaceManagerV1::ext_workspace_manager_v1_workspace(
232 struct ::ext_workspace_handle_v1 *workspace_)
233 {
234 workspaceMap[workspace_] = new WorkspaceHandleV1(workspace_, workspaceMap.count() + 1);
235
236 connect(workspaceMap[workspace_], &WorkspaceHandleV1::activated, this,
237 &WorkspaceManagerV1::activation, Qt::QueuedConnection);
238 connect(workspaceMap[workspace_], &WorkspaceHandleV1::deactivated, this,
239 &WorkspaceManagerV1::activation, Qt::QueuedConnection);
240 connect(workspaceMap[workspace_], &WorkspaceHandleV1::nameChanged, this,
241 &WorkspaceManagerV1::nameChanged, Qt::QueuedConnection);
242
243 // handle coordinates change as renaming plus activation
244 connect(workspaceMap[workspace_], &WorkspaceHandleV1::coordinatesChanged, this,
245 &WorkspaceManagerV1::nameChanged, Qt::QueuedConnection);
246 connect(workspaceMap[workspace_], &WorkspaceHandleV1::coordinatesChanged, this,
247 &WorkspaceManagerV1::activation, Qt::QueuedConnection);
248 }
249
250 void LXQt::Taskbar::WorkspaceManagerV1::ext_workspace_manager_v1_done()
251 {
252 emit done();
253 }
254
255 void LXQt::Taskbar::WorkspaceManagerV1::ext_workspace_manager_v1_finished()
256 {
257 emit finished();
258 }
259
260 /**
261 * Implementation of the LXQt::TaskBar::WorkspaceGroupHandleV1 class
262 */
263
264 LXQt::Taskbar::WorkspaceGroupHandleV1::WorkspaceGroupHandleV1(
265 struct ::ext_workspace_group_handle_v1 *object) : QObject(),
266 QtWayland::ext_workspace_group_handle_v1(object)
267 {}
268
269 LXQt::Taskbar::WorkspaceGroupHandleV1::~WorkspaceGroupHandleV1()
270 {
271 destroy();
272 }
273
274 bool LXQt::Taskbar::WorkspaceGroupHandleV1::canCreateWorkspace() const
275 {
276 return (m_supported_capabilities & group_capabilities_create_workspace);
277 }
278
279 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_capabilities(uint32_t caps)
280 {
281 m_supported_capabilities = caps;
282 emit capabilities(caps);
283 }
284
285 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_output_enter(
286 struct ::wl_output *output)
287 {
288 if (!m_outputs.contains(output))
289 m_outputs << output;
290 emit outputEnter(output);
291 }
292
293 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_output_leave(
294 struct ::wl_output *output)
295 {
296 m_outputs.removeAll(output);
297 emit outputLeave(output);
298 }
299
300 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_workspace_enter(
301 struct ::ext_workspace_handle_v1 *workspace)
302 {
303 if (!workspaceGroups.contains(this))
304 {
305 workspaceGroups << this;
306 }
307 if (workspaceMap.contains(workspace) && !m_workspaces.contains(workspaceMap[workspace]))
308 {
309 m_workspaces << workspaceMap[workspace];
310
311 emit workspaceAdded(workspaceMap[workspace]);
312 //qDebug() << "Workspace added";
313 }
314 }
315
316 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_workspace_leave(
317 struct ::ext_workspace_handle_v1 *workspace)
318 {
319 if (workspaceMap.contains(workspace) && m_workspaces.contains(workspaceMap[workspace]))
320 {
321 m_workspaces.removeAll(workspaceMap[workspace]);
322 emit workspaceRemoved(workspaceMap[workspace]);
323 }
324 }
325
326 void LXQt::Taskbar::WorkspaceGroupHandleV1::ext_workspace_group_handle_v1_removed()
327 {
328 workspaceGroups.removeAll(this);
329 deleteLater();
330 emit removed();
331 }
332
333 /**
334 * Implementation of the LXQt::TaskBar::WorkspaceHandleV1 class
335 */
336
337
338 LXQt::Taskbar::WorkspaceHandleV1::WorkspaceHandleV1(struct ::ext_workspace_handle_v1 *object,
339 int index) : QObject(),
340 QtWayland::ext_workspace_handle_v1(object)
341 {
342 m_coordinates << index;
343 }
344
345 LXQt::Taskbar::WorkspaceHandleV1::~WorkspaceHandleV1()
346 {
347 destroy();
348 }
349
350 QString LXQt::Taskbar::WorkspaceHandleV1::id() const
351 {
352 return m_id;
353 }
354
355 QString LXQt::Taskbar::WorkspaceHandleV1::name() const
356 {
357 return m_name;
358 }
359
360 QList<int> LXQt::Taskbar::WorkspaceHandleV1::coordinates() const
361 {
362 return m_coordinates;
363 }
364
365 uint32_t LXQt::Taskbar::WorkspaceHandleV1::state() const
366 {
367 return m_state;
368 }
369
370 uint32_t LXQt::Taskbar::WorkspaceHandleV1::capabilities() const
371 {
372 return m_capabilities;
373 }
374
375 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_id(const QString& id_)
376 {
377 m_id = id_;
378 //qDebug() << "ID:" << m_id;
379
380 emit idChanged(m_id);
381 }
382
383 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_name(const QString& name_)
384 {
385 m_name = name_;
386 //qDebug() << "Name:" << m_name;
387
388 emit nameChanged(m_name);
389 }
390
391 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_coordinates(wl_array *coordinates_)
392 {
393 m_coordinates.clear();
394
395 int32_t *data = static_cast<int32_t*>(coordinates_->data);
396 size_t count = coordinates_->size / sizeof(int32_t);
397
398 for (size_t i = 0; i < count; ++i)
399 {
400 m_coordinates.append(data[i]);
401 }
402
403 //qDebug() << "Coords:" << m_name << m_coordinates;
404
405 emit coordinatesChanged(m_coordinates);
406 }
407
408 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_state(uint32_t state_)
409 {
410 auto old_state = m_state;
411 m_state = state_;
412 /** Check if this workspace was activated */
413 if (!(old_state & state_active) && (state_ & state_active))
414 {
415 emit activated();
416 }
417 /** Check if this workspace was deactivated */
418 else if ((old_state & state_active) && !(state_ & state_active))
419 {
420 emit deactivated();
421 }
422
423 //qDebug() << "State:" << m_name << m_state;
424
425 emit stateChanged(m_state);
426 }
427
428 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_capabilities(uint32_t capabilities_)
429 {
430 m_capabilities = capabilities_;
431 //qDebug() << "Caps:" << m_name << m_capabilities;
432
433 emit capabilitiesChanged(m_capabilities);
434 }
435
436 void LXQt::Taskbar::WorkspaceHandleV1::ext_workspace_handle_v1_removed()
437 {
438 if (auto key = workspaceMap.key(this))
439 {
440 workspaceMap.remove(key);
441 }
442 deleteLater();
443 emit removed();
444 }