Debian Codegraph @

panel/backends/wayland/wayfire/lxqtwmbackend_wf.cpp

raw
1 #include "wayfire-common.h"
2 #include "lxqtwmbackend_wf.h"
3
4 #include <QIcon>
5 #include <QTime>
6 #include <QScreen>
7 #include <QStandardPaths>
8 #include <algorithm>
9
10 // Shortforms
11 #define QSL QStringLiteral
12 #define U8Str QString::fromUtf8
13
14 static inline QJsonObject updateJsonObject(QJsonObject source, QJsonObject other)
15 {
16 /** Overwrite all (key,value) pairs of @osurce from @other */
17 for ( QString key : other.keys())
18 {
19 source[key] = other[key];
20 }
21
22 return source;
23 }
24
25 static inline bool isValidToplevel(QJsonObject view)
26 {
27 if (view.isEmpty())
28 {
29 return false;
30 }
31
32 /** Ghost view: these are unmapped xwayland views */
33 if (view[QSL("pid")].toInt() <= 1)
34 {
35 return false;
36 }
37
38 /** We want only the "toplevel" views */
39 if (view[QSL("role")].toString() != QSL("toplevel"))
40 {
41 return false;
42 }
43
44 /** We want only the mapped views */
45 if (view[QSL("mapped")].toBool() == false)
46 {
47 return false;
48 }
49
50 return true;
51 }
52
53 static inline QString getPixmapIcon(QString name)
54 {
55 QStringList paths{
56 QSL("/usr/local/share/pixmaps/"),
57 QSL("/usr/share/pixmaps/"),
58 };
59
60 QStringList sfxs{
61 QSL(".svg"), QSL(".png"), QSL(".xpm")
62 };
63
64 for (QString path : paths)
65 {
66 for (QString sfx : sfxs)
67 {
68 if (QFile::exists(path + name + sfx))
69 {
70 return path + name + sfx;
71 }
72 }
73 }
74
75 return QString();
76 }
77
78 QIcon getIconForAppId(QString mAppId)
79 {
80 if (mAppId.isEmpty() or (mAppId == QSL("Unknown")))
81 {
82 return QIcon();
83 }
84
85 /** Wine apps */
86 if (mAppId.endsWith(QSL(".exe")))
87 {
88 return QIcon::fromTheme(QSL("wine"));
89 }
90
91 /** Check if a theme icon exists called @mAppId */
92 if (QIcon::hasThemeIcon(mAppId))
93 {
94 return QIcon::fromTheme(mAppId);
95 }
96 /** Check if the theme icon is @mAppId, but all lower-case letters */
97 else if (QIcon::hasThemeIcon(mAppId.toLower()))
98 {
99 return QIcon::fromTheme(mAppId.toLower());
100 }
101
102 const QStringList appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
103
104 /**
105 * Assume mAppId == desktop-file-name (ideal situation) or mAppId.toLower() == desktop-file-name (cheap
106 * fallback)
107 */
108 QString iconName;
109
110 for (const QString &path : appDirs)
111 {
112 /** Get the icon name from desktop (mAppId: as it is) */
113 if (QFile::exists(path + mAppId + QSL(".desktop")))
114 {
115 QSettings desktop(path + mAppId + QSL(".desktop"), QSettings::IniFormat);
116 iconName = desktop.value(QSL("Desktop Entry/Icon")).toString();
117 }
118 /** Get the icon name from desktop (mAppId: all lower-case letters) */
119 else if (QFile::exists(path + mAppId.toLower() + QSL(".desktop")))
120 {
121 QSettings desktop(path + mAppId.toLower() + QSL(".desktop"), QSettings::IniFormat);
122 iconName = desktop.value(QSL("Desktop Entry/Icon")).toString();
123 }
124
125 /** No icon specified: try else-where */
126 if (iconName.isEmpty())
127 {
128 continue;
129 }
130
131 /** We got an iconName, and it's in the current theme */
132 if (QIcon::hasThemeIcon(iconName))
133 {
134 return QIcon::fromTheme(iconName);
135 }
136 /** Not a theme icon, but an absolute path */
137 else if (QFile::exists(iconName))
138 {
139 return QIcon(iconName);
140 }
141 /** Not theme icon or absolute path. So check /usr/share/pixmaps/ */
142 else
143 {
144 iconName = getPixmapIcon(iconName);
145
146 if (not iconName.isEmpty())
147 {
148 return QIcon(iconName);
149 }
150 }
151 }
152
153 /* Check all desktop files for @mAppId */
154 for (const QString &path : appDirs)
155 {
156 QStringList desktops = QDir(path).entryList({QSL("*.desktop")});
157 for (QString dskf : desktops)
158 {
159 QSettings desktop(path + dskf, QSettings::IniFormat);
160
161 QString exec = desktop.value(QSL("Desktop Entry/Exec"), QSL("abcd1234/-")).toString();
162 QString name = desktop.value(QSL("Desktop Entry/Name"), QSL("abcd1234/-")).toString();
163 QString cls = desktop.value(QSL("Desktop Entry/StartupWMClass"),
164 QSL("abcd1234/-")).toString();
165
166 QString execPath = U8Str(std::filesystem::path(exec.toStdString()).filename().c_str());
167
168 if (mAppId.compare(execPath, Qt::CaseInsensitive) == 0)
169 {
170 iconName = desktop.value(QSL("Desktop Entry/Icon")).toString();
171 }
172 else if (mAppId.compare(name, Qt::CaseInsensitive) == 0)
173 {
174 iconName = desktop.value(QSL("Desktop Entry/Icon")).toString();
175 }
176 else if (mAppId.compare(cls, Qt::CaseInsensitive) == 0)
177 {
178 iconName = desktop.value(QSL("Desktop Entry/Icon")).toString();
179 }
180
181 if (!iconName.isEmpty())
182 {
183 if (QIcon::hasThemeIcon(iconName))
184 {
185 return QIcon::fromTheme(iconName);
186 }
187 else if (QFile::exists(iconName))
188 {
189 return QIcon(iconName);
190 }
191 else
192 {
193 iconName = getPixmapIcon(iconName);
194
195 if (!iconName.isEmpty())
196 {
197 return QIcon(iconName);
198 }
199 }
200 }
201 }
202 }
203
204 iconName = getPixmapIcon(iconName);
205
206 if (not iconName.isEmpty())
207 {
208 return QIcon(iconName);
209 }
210
211 return QIcon();
212 }
213
214 LXQtTaskbarWayfireBackend::LXQtTaskbarWayfireBackend(QObject *parent) :
215 ILXQtAbstractWMInterface(parent)
216 {
217 mWayfire.reset(new LXQt::Panel::Wayfire);
218
219 connect(mWayfire.get(), &LXQt::Panel::Wayfire::workspaceSetChanged, [] ( QJsonDocument )
220 {
221 // no-op
222 });
223
224 connect(mWayfire.get(), &LXQt::Panel::Wayfire::workspaceChanged, [this] ( QJsonDocument respJson )
225 {
226 QJsonObject response = respJson.object();
227 QJsonObject output = response[QSL("output-data")].toObject();
228 QString outputName = output[QSL("name")].toString();
229 QJsonObject outputInfo = mWayfire->getOutputInfo(mWayfire->getActiveOutput());
230 QJsonObject outputWS = outputInfo[QSL("workspace")].toObject();
231
232 int nCols = outputWS[QSL("grid_width")].toInt(); // Total columns in workspace grid
233 int curRow = outputWS[QSL("y")].toInt(); // Current workspace row (0-based)
234 int curCol = outputWS[QSL("x")].toInt(); // Current workspace column (0-based)
235
236 emit currentWorkspaceChanged(curRow * nCols + curCol + 1, outputName);
237 });
238
239 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewMapped, [this] ( QJsonDocument respJson )
240 {
241 QJsonObject response = respJson.object();
242 QJsonObject view = response[QSL("view")].toObject();
243 WaylandId viewId(view[QSL("id")].toInt());
244
245 if (mViews.contains(viewId))
246 {
247 emit windowRemoved(viewId);
248 }
249
250 /** Filter non-toplevel views */
251 if (!isValidToplevel(view))
252 {
253 mViews.remove(viewId);
254 return;
255 }
256
257 mViews[viewId] = view;
258
259 emit windowAdded(viewId);
260 });
261
262 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewTitleChanged, [this] ( QJsonDocument respJson )
263 {
264 QJsonObject response = respJson.object();
265 QJsonObject view = response[QSL("view")].toObject();
266 WaylandId viewId(view[QSL("id")].toInt());
267
268 /** Filter non-toplevel views */
269 if (!isValidToplevel(view))
270 {
271 if (mViews.contains(viewId))
272 {
273 mViews.remove(viewId);
274 emit windowRemoved(viewId);
275 }
276 return;
277 }
278
279 if (!mViews.contains(viewId))
280 {
281 mViews[viewId] = view;
282 emit windowAdded(viewId);
283 }
284
285 mViews[viewId] = updateJsonObject(mViews[viewId], view);
286
287 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::Title);
288 });
289
290 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewAppIdChanged, [this] ( QJsonDocument respJson )
291 {
292 QJsonObject response = respJson.object();
293 QJsonObject view = response[QSL("view")].toObject();
294 WaylandId viewId(view[QSL("id")].toInt());
295
296 /** Filter non-toplevel views */
297 if (!isValidToplevel(view))
298 {
299 if (mViews.contains(viewId))
300 {
301 mViews.remove(viewId);
302 emit windowRemoved(viewId);
303 }
304 return;
305 }
306
307 if (!mViews.contains(viewId))
308 {
309 mViews[viewId] = view;
310 emit windowAdded(viewId);
311 }
312
313 mViews[viewId] = updateJsonObject(mViews[viewId], view);
314
315 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::WindowClass);
316 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::Icon);
317 });
318
319 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewFocused, [this] ( QJsonDocument respJson )
320 {
321 QJsonObject response = respJson.object();
322 QJsonObject view = response[QSL("view")].toObject();
323 WaylandId viewId(view[QSL("id")].toInt());
324
325 /** Filter non-toplevel views */
326 if (!isValidToplevel(view))
327 {
328 if (mViews.contains(viewId))
329 {
330 mViews.remove(viewId);
331 emit windowRemoved(viewId);
332 }
333 return;
334 }
335
336 if (!mViews.contains(viewId))
337 {
338 mViews[viewId] = view;
339 emit windowAdded(viewId);
340 }
341
342 mViews[viewId] = updateJsonObject(mViews[viewId], view);
343
344 emit activeWindowChanged(viewId);
345 });
346
347 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewMinimized, [this] ( QJsonDocument respJson )
348 {
349 QJsonObject response = respJson.object();
350 QJsonObject view = response[QSL("view")].toObject();
351 WaylandId viewId(view[QSL("id")].toInt());
352
353 /** Filter non-toplevel views */
354 if (!isValidToplevel(view))
355 {
356 if (mViews.contains(viewId))
357 {
358 mViews.remove(viewId);
359 emit windowRemoved(viewId);
360 }
361 return;
362 }
363
364 if (!mViews.contains(viewId))
365 {
366 mViews[viewId] = view;
367 emit windowAdded(viewId);
368 }
369
370 mViews[viewId] = updateJsonObject(mViews[viewId], view);
371
372 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::State);
373 });
374
375 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewTiled, [this] ( QJsonDocument respJson )
376 {
377 QJsonObject response = respJson.object();
378 QJsonObject view = response[QSL("view")].toObject();
379 WaylandId viewId(view[QSL("id")].toInt());
380
381 /** Filter non-toplevel views */
382 if (!isValidToplevel(view))
383 {
384 if (mViews.contains(viewId))
385 {
386 mViews.remove(viewId);
387 emit windowRemoved(viewId);
388 }
389 return;
390 }
391
392 if (!mViews.contains(viewId))
393 {
394 mViews[viewId] = view;
395 emit windowAdded(viewId);
396 }
397
398 mViews[viewId] = updateJsonObject(mViews[viewId], view);
399
400 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::State);
401 });
402
403 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewGeometryChanged, [this] ( QJsonDocument respJson )
404 {
405 QJsonObject response = respJson.object();
406 QJsonObject view = response[QSL("view")].toObject();
407 WaylandId viewId(view[QSL("id")].toInt());
408
409 /** Filter non-toplevel views */
410 if (!isValidToplevel(view))
411 {
412 if (mViews.contains(viewId))
413 {
414 mViews.remove(viewId);
415 emit windowRemoved(viewId);
416 }
417 return;
418 }
419
420 if (!mViews.contains(viewId))
421 {
422 mViews[viewId] = view;
423 emit windowAdded(viewId);
424 }
425
426 mViews[viewId] = updateJsonObject(mViews[viewId], view);
427
428 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::Geometry);
429 });
430
431 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewOutputChanged, [this] ( QJsonDocument respJson )
432 {
433 QJsonObject response = respJson.object();
434 QJsonObject view = response[QSL("view")].toObject();
435 WaylandId viewId(view[QSL("id")].toInt());
436
437 /** Filter non-toplevel views */
438 if (!isValidToplevel(view))
439 {
440 if (mViews.contains(viewId))
441 {
442 mViews.remove(viewId);
443 emit windowRemoved(viewId);
444 }
445 return;
446 }
447
448 //QString oldOp;
449
450 if (!mViews.contains(viewId))
451 {
452 mViews[viewId] = view;
453 emit windowAdded(viewId);
454 }/* else {
455 oldOp = mViews[viewId][QSL("output-name")].toString();
456 }
457
458 QString newOp = view[QSL("output-name")].toString();*/
459
460 mViews[viewId] = updateJsonObject(mViews[viewId], view);
461
462 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::Geometry);
463 });
464
465 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewWorkspaceChanged, [this] ( QJsonDocument respJson )
466 {
467 QJsonObject response = respJson.object();
468 QJsonObject view = response[QSL("view")].toObject();
469 WaylandId viewId(view[QSL("id")].toInt());
470
471 /** Filter non-toplevel views */
472 if (!isValidToplevel(view))
473 {
474 if (mViews.contains(viewId))
475 {
476 mViews.remove(viewId);
477 emit windowRemoved(viewId);
478 }
479 return;
480 }
481
482 if (!mViews.contains(viewId))
483 {
484 mViews[viewId] = view;
485 emit windowAdded(viewId);
486 }
487
488 mViews[viewId] = updateJsonObject(mViews[viewId], view);
489 mViews[viewId][QSL("workspace")] = QJsonObject({
490 {QSL("x"), response[QSL("to")][QSL("x")]},
491 {QSL("y"), response[QSL("to")][QSL("y")]}
492 });
493
494 emit windowPropertyChanged(viewId, (int)LXQtTaskBarWindowProperty::Workspace);
495 });
496
497 connect(mWayfire.get(), &LXQt::Panel::Wayfire::viewUnmapped, [this] ( QJsonDocument respJson )
498 {
499 QJsonObject response = respJson.object();
500 QJsonObject view = response[QSL("view")].toObject();
501
502 WaylandId viewId(view[QSL("id")].toInt());
503
504 if (mViews.contains(viewId))
505 {
506 mViews.remove(viewId);
507 emit windowRemoved(viewId);
508 }
509 });
510
511 mWayfire->connectToServer();
512 }
513
514 bool LXQtTaskbarWayfireBackend::supportsAction(WId, LXQtTaskBarBackendAction action) const
515 {
516 switch (action)
517 {
518 case LXQtTaskBarBackendAction::Move:
519 return false;
520
521 case LXQtTaskBarBackendAction::Resize:
522 return false;
523
524 case LXQtTaskBarBackendAction::Maximize:
525 return true;
526
527 /** To be implemented in wayfire ipc */
528 case LXQtTaskBarBackendAction::MaximizeVertically:
529 return false;
530
531 /** To be implemented in wayfire ipc */
532 case LXQtTaskBarBackendAction::MaximizeHorizontally:
533 return false;
534
535 case LXQtTaskBarBackendAction::Minimize:
536 return true;
537
538 /** Not implemented */
539 case LXQtTaskBarBackendAction::RollUp:
540 return false;
541
542 case LXQtTaskBarBackendAction::FullScreen:
543 return true;
544
545 case LXQtTaskBarBackendAction::DesktopSwitch:
546 return true;
547
548 /** Available via wsets plugin */
549 case LXQtTaskBarBackendAction::MoveToDesktop:
550 return true;
551
552 /** Not implemented yet */
553 case LXQtTaskBarBackendAction::MoveToLayer:
554 return false;
555
556 /** Available via wsets plugin */
557 case LXQtTaskBarBackendAction::MoveToOutput:
558 return true;
559
560 default:
561 return false;
562 }
563
564 return false;
565 }
566
567 bool LXQtTaskbarWayfireBackend::reloadWindows()
568 {
569 // Force removal and re-adding
570 for (WaylandId viewId : mViews.keys())
571 {
572 mViews.remove(viewId);
573 emit windowRemoved(viewId);
574 }
575
576 QJsonArray views = mWayfire->listViews();
577 while (views.count())
578 {
579 QJsonObject view = views.takeAt(0).toObject();
580 WaylandId id(view[QSL("id")].toInt());
581
582 mViews[id] = view;
583 emit windowAdded(id);
584 }
585
586 return true;
587 }
588
589 QVector<WId> LXQtTaskbarWayfireBackend::getCurrentWindows() const
590 {
591 QVector<WId> ids;
592 for ( WaylandId viewId : mViews.keys())
593 {
594 ids << viewId;
595 }
596
597 return ids;
598 }
599
600 QString LXQtTaskbarWayfireBackend::getWindowTitle(WId windowId) const
601 {
602 WaylandId viewId(windowId);
603 if (!mViews.contains(viewId))
604 {
605 return QString();
606 }
607
608 return mViews[viewId][QSL("title")].toString();
609 }
610
611 bool LXQtTaskbarWayfireBackend::applicationDemandsAttention(WId) const
612 {
613 return false;
614 }
615
616 QIcon LXQtTaskbarWayfireBackend::getApplicationIcon(WId windowId, int devicePixels) const
617 {
618 Q_UNUSED(devicePixels)
619
620 WaylandId viewId(windowId);
621 if (!mViews.contains(viewId))
622 {
623 return QIcon();
624 }
625
626 return getIconForAppId(mViews[viewId][QSL("app-id")].toString());
627 }
628
629 QString LXQtTaskbarWayfireBackend::getWindowClass(WId windowId) const
630 {
631 WaylandId viewId(windowId);
632 if (!mViews.contains(viewId))
633 {
634 return QString();
635 }
636
637 return mViews[viewId][QSL("app-id")].toString();
638 }
639
640 LXQtTaskBarWindowLayer LXQtTaskbarWayfireBackend::getWindowLayer(WId) const
641 {
642 return LXQtTaskBarWindowLayer::Normal;
643 }
644
645 bool LXQtTaskbarWayfireBackend::setWindowLayer(WId, LXQtTaskBarWindowLayer)
646 {
647 return false;
648 }
649
650 LXQtTaskBarWindowState LXQtTaskbarWayfireBackend::getWindowState(WId windowId) const
651 {
652 WaylandId viewId(windowId);
653 if (!mViews.contains(viewId))
654 {
655 return LXQtTaskBarWindowState::Hidden;
656 }
657
658 if (!mViews[viewId][QSL("mapped")].toBool())
659 {
660 return LXQtTaskBarWindowState::Hidden;
661 }
662
663 if (mViews[viewId][QSL("minimized")].toBool())
664 {
665 return LXQtTaskBarWindowState::Minimized;
666 }
667
668 if (mViews[viewId][QSL("fullscreen")].toBool())
669 {
670 return LXQtTaskBarWindowState::FullScreen;
671 }
672
673 // WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT | WLR_EDGE_RIGHT == 1 | 2 | 4 | 8 == 15
674 if (mViews[viewId][QSL("tiled-edges")].toInt() > 0)
675 {
676 return LXQtTaskBarWindowState::Maximized;
677 }
678
679 // // WLR_EDGE_TOP | WLR_EDGE_BOTTOM == 1 | 2 == 3
680 // if (mViews[viewId][QSL("tiled-edges")].toInt() == 3)
681 // {
682 // return LXQtTaskBarWindowState::MaximizedVertically;
683 // }
684
685 // // WLR_EDGE_LEFT | WLR_EDGE_RIGHT == 4 | 8 == 12
686 // if (mViews[viewId][QSL("tiled-edges")].toInt() == 12)
687 // {
688 // return LXQtTaskBarWindowState::MaximizedHorizontally;
689 // }
690
691 return LXQtTaskBarWindowState::Normal;
692 }
693
694 bool LXQtTaskbarWayfireBackend::setWindowState(WId windowId, LXQtTaskBarWindowState state, bool set)
695 {
696 WaylandId viewId(windowId);
697 if (!mViews.contains(viewId))
698 {
699 return false;
700 }
701
702 switch (state)
703 {
704 case LXQtTaskBarWindowState::Minimized:
705 {
706 mWayfire->minimizeView(viewId, set);
707 break;
708 }
709
710 case LXQtTaskBarWindowState::Maximized:
711 {
712 mWayfire->maximizeView(viewId, (set ? 15 : 0));
713 break;
714 }
715
716 case LXQtTaskBarWindowState::MaximizedVertically:
717 {
718 mWayfire->maximizeView(viewId, (set ? 3 : 0));
719 break;
720 }
721
722 case LXQtTaskBarWindowState::MaximizedHorizontally:
723 {
724 mWayfire->maximizeView(viewId, (set ? 12 : 0));
725 break;
726 }
727
728 case LXQtTaskBarWindowState::Normal:
729 {
730 mWayfire->restoreView(viewId);
731 break;
732 }
733
734 case LXQtTaskBarWindowState::FullScreen:
735 {
736 mWayfire->fullscreenView(viewId, set);
737 break;
738 }
739
740 default:
741 return false;
742 }
743
744 return true;
745 }
746
747 bool LXQtTaskbarWayfireBackend::isWindowActive(WId windowId) const
748 {
749 WaylandId viewId(windowId);
750 return (mWayfire->getActiveView() == viewId);
751 }
752
753 bool LXQtTaskbarWayfireBackend::raiseWindow(WId windowId, bool onCurrentWorkSpace)
754 {
755 WaylandId viewId(windowId);
756 if (!mViews.contains(viewId))
757 {
758 return false;
759 }
760
761 if (getWindowState(windowId)==LXQtTaskBarWindowState::Minimized)
762 {
763 mWayfire->minimizeView(WaylandId(windowId), false);
764 // Wayfire::focusView() does not switch the workspace if the window was minimized,
765 // although it reports that the window is focused. This is a workaround:
766 if (!onCurrentWorkSpace)
767 setCurrentWorkspace(getWindowWorkspace(windowId));
768 }
769
770 bool raised = mWayfire->focusView(viewId);
771 if (onCurrentWorkSpace)
772 mWayfire->sendViewToWorkspace(viewId, getCurrentWorkspace());
773 return raised;
774 }
775
776 bool LXQtTaskbarWayfireBackend::closeWindow(WId windowId)
777 {
778 WaylandId viewId(windowId);
779 if (!mViews.contains(viewId))
780 {
781 return false;
782 }
783
784 return mWayfire->closeView(viewId);
785 }
786
787 WId LXQtTaskbarWayfireBackend::getActiveWindow() const
788 {
789 return mWayfire->getActiveView();
790 }
791
792 int LXQtTaskbarWayfireBackend::getWorkspacesCount(QScreen*) const
793 {
794 QJsonObject wsetsInfo = mWayfire->getWorkspaceSetsInfo().at(0).toObject();
795 QJsonObject workspace = wsetsInfo[QSL("workspace")].toObject();
796 int64_t nRows = workspace[QSL("grid_height")].toInt();
797 int64_t nCols = workspace[QSL("grid_width")].toInt();
798
799 return (nRows * nCols);
800 }
801
802 QString LXQtTaskbarWayfireBackend::getWorkspaceName(int x, QString outputName) const
803 {
804 return mWayfire->getWorkspaceName(x, outputName);
805 }
806
807 int LXQtTaskbarWayfireBackend::getCurrentWorkspace(QScreen*) const
808 {
809 QJsonObject outputInfo = mWayfire->getOutputInfo(mWayfire->getActiveOutput());
810 QJsonObject outputWS = outputInfo[QSL("workspace")].toObject();
811
812 int nCols = outputWS[QSL("grid_width")].toInt(); // Total columns in workspace grid
813 int curRow = outputWS[QSL("y")].toInt(); // Current workspace row (0-based)
814 int curCol = outputWS[QSL("x")].toInt(); // Current workspace column (0-based)
815
816 return curRow * nCols + curCol + 1;
817 }
818
819 bool LXQtTaskbarWayfireBackend::setCurrentWorkspace(int x, QScreen*)
820 {
821 return mWayfire->switchToWorkspace(mWayfire->getActiveOutput(), x);
822 }
823
824 int LXQtTaskbarWayfireBackend::getWindowWorkspace(WId windowId) const
825 {
826 WaylandId viewId(windowId);
827 QJsonObject viewInfo = mWayfire->getViewInfo(viewId);
828
829 QJsonObject outputInfo = mWayfire->getOutputInfo(WaylandId(viewInfo[QSL("output-id")].toInt()));
830 QJsonObject outputWS = outputInfo[QSL("workspace")].toObject();
831
832 int nRows = outputWS[QSL("grid_height")].toInt(); // Total rows in workspace grid
833 int nCols = outputWS[QSL("grid_width")].toInt(); // Total columns in workspace grid
834
835 if (viewInfo.contains(QSL("workspace")))
836 {
837 int currentRow = viewInfo[QSL("workspace")][QSL("y")].toInt(); // Current workspace row (0-based)
838 int currentCol = viewInfo[QSL("workspace")][QSL("x")].toInt(); // Current workspace column (0-based)
839
840 return currentRow * nCols + currentCol + 1;
841 }
842
843 QJsonObject viewGeom = viewInfo[QSL("geometry")].toObject();
844
845 QJsonObject outputGeom = outputInfo[QSL("geometry")].toObject();
846
847 // Calculate the center of the window
848 QPoint viewCenter(
849 outputGeom[QSL("x")].toInt() + viewGeom[QSL("x")].toInt() + viewGeom[QSL("width")].toInt() / 2,
850 outputGeom[QSL("y")].toInt() + viewGeom[QSL("y")].toInt() + viewGeom[QSL("height")].toInt() / 2
851 );
852
853 QRect opGeom(
854 outputGeom[QSL("x")].toInt(),
855 outputGeom[QSL("y")].toInt(),
856 outputGeom[QSL("width")].toInt(),
857 outputGeom[QSL("height")].toInt()
858 );
859
860 int currentRow = outputWS[QSL("y")].toInt(); // Current workspace row (0-based)
861 int currentCol = outputWS[QSL("x")].toInt(); // Current workspace column (0-based)
862
863 // Calculate the geometries of all workspaces relative to the current workspace
864 QHash<int, QRect> wsGeomHash;
865 for (int row = 0; row < nRows; ++row)
866 {
867 for (int col = 0; col < nCols; ++col)
868 {
869 // Workspace index (0-based)
870 int wsIndex = row * nCols + col;
871
872 // Workspace geometry (relative to the current workspace)
873 QRect wsGeom(
874 opGeom.x() + (col - currentCol) * opGeom.width(),
875 opGeom.y() + (row - currentRow) * opGeom.height(),
876 opGeom.width(),
877 opGeom.height()
878 );
879
880 wsGeomHash[wsIndex] = wsGeom;
881 }
882 }
883
884 // Find which workspace contains the view's center
885 for (auto it = wsGeomHash.constBegin(); it != wsGeomHash.constEnd(); ++it)
886 {
887 if (it.value().contains(viewCenter))
888 {
889 return it.key() + 1;
890 }
891 }
892
893 // Fallback: If not found, assume current workspace
894 return currentRow * nCols + currentCol + 1;
895 }
896
897 bool LXQtTaskbarWayfireBackend::setWindowOnWorkspace(WId windowId, int idx)
898 {
899 WaylandId viewId(windowId);
900 return mWayfire->sendViewToWorkspace(viewId, idx);
901 }
902
903 void LXQtTaskbarWayfireBackend::moveApplicationToPrevNextMonitor(WId viewId, bool nextOp, bool raiseWindow)
904 {
905 // 1. Get the current output id and its active wset-id
906 // Get view info to find which output it's currently on
907 QJsonObject viewInfo = mWayfire->getViewInfo(WaylandId(viewId));
908 if (viewInfo.isEmpty())
909 {
910 qWarning() << "Failed to get view info for view" << viewId;
911 return;
912 }
913
914 WaylandId currentOutputId(viewInfo[QSL("output-id")].toInt());
915
916 // Get all outputs
917 QJsonArray outputs = mWayfire->listOutputs();
918 if (outputs.isEmpty())
919 {
920 qWarning() << "No outputs available";
921 return;
922 }
923
924 // 2. Find the previous/next output
925 int currentIndex = -1;
926 for (int i = 0; i < outputs.size(); i++)
927 {
928 QJsonObject output = outputs[i].toObject();
929 if (output[QSL("id")].toInt() == (int)currentOutputId.id)
930 {
931 currentIndex = i;
932 break;
933 }
934 }
935
936 if (currentIndex == -1)
937 {
938 qWarning() << "Current output not found in outputs list";
939 return;
940 }
941
942 // Calculate target output index with wrap-around
943 int targetIndex;
944 if (nextOp)
945 {
946 targetIndex = (currentIndex + 1) % outputs.size();
947 } else
948 {
949 targetIndex = (currentIndex - 1 + outputs.size()) % outputs.size();
950 }
951
952 QJsonObject targetOutput = outputs[targetIndex].toObject();
953 WaylandId targetOutputId(targetOutput[QSL("id")].toInt());
954
955 // 3. Move the view to target output's workspace set
956 // Get workspace sets info
957 QJsonArray wsets = mWayfire->getWorkspaceSetsInfo();
958 if (wsets.isEmpty())
959 {
960 qWarning() << "No workspace sets available";
961 return;
962 }
963
964 // Find the target output's active workspace set
965 WaylandId targetWsetId(0);
966 for (const QJsonValue & wsVal : wsets)
967 {
968 QJsonObject ws = wsVal.toObject();
969 if (ws[QSL("output-id")].toInt() == (int)targetOutputId.id)
970 {
971 targetWsetId = WaylandId(ws[QSL("index")].toInt());
972 break;
973 }
974 }
975
976 if (targetWsetId == 0)
977 {
978 qWarning() << "Failed to find workspace set for target output";
979 return;
980 }
981
982 // Move the view to target workspace set
983 QJsonObject moveRequest;
984 moveRequest[QSL("method")] = QSL("wsets/send-view-to-wset");
985 moveRequest[QSL("data")] = QJsonObject{
986 {QSL("view-id"), QJsonValue::fromVariant((quint64)viewId)},
987 {QSL("wset-index"), QJsonValue::fromVariant((quint64)targetWsetId.id)}
988 };
989
990 QJsonDocument reply = mWayfire->genericRequest(QJsonDocument(moveRequest));
991 if (reply[QSL("result")].toString() != QSL("ok"))
992 {
993 qWarning() << "Failed to move view to target workspace set:" << reply.toJson();
994 return;
995 }
996
997 // 4. Focus the window if requested
998 if (raiseWindow)
999 {
1000 mWayfire->focusView(WaylandId(viewId));
1001 }
1002 }
1003
1004 bool LXQtTaskbarWayfireBackend::isWindowOnScreen(QScreen *scrn, WId windowId) const
1005 {
1006 WaylandId viewId(windowId);
1007 if (!mViews.contains(viewId))
1008 {
1009 return false;
1010 }
1011
1012 return mViews[viewId][QSL("output-name")] == scrn->name();
1013 }
1014
1015 bool LXQtTaskbarWayfireBackend::setDesktopLayout(Qt::Orientation, int, int, bool)
1016 {
1017 // Wayfire does not support dynamic setting of desktops.
1018 return false;
1019 }
1020
1021 void LXQtTaskbarWayfireBackend::moveApplication(WId)
1022 {
1023 // no-op
1024 }
1025
1026 void LXQtTaskbarWayfireBackend::resizeApplication(WId)
1027 {
1028 // no-op
1029 }
1030
1031 void LXQtTaskbarWayfireBackend::refreshIconGeometry(WId, const QRect &)
1032 {
1033 // no-op
1034 }
1035
1036 bool LXQtTaskbarWayfireBackend::isAreaOverlapped(const QRect &area) const
1037 {
1038 int d;
1039 const auto keys = mViews.keys();
1040 for (const WaylandId viewId : keys)
1041 {
1042 auto id = WaylandId(viewId);
1043 if (((d = getWindowWorkspace(id) == getCurrentWorkspace()) || d == onAllWorkspacesEnum())
1044 && getWindowState(id) != LXQtTaskBarWindowState::Minimized)
1045 {
1046 QJsonObject viewInfo = mWayfire->getViewInfo(viewId);
1047 QJsonObject viewGeom = viewInfo[QSL("geometry")].toObject();
1048 QJsonObject outputInfo = mWayfire->getOutputInfo(WaylandId(viewInfo[QSL("output-id")].toInt()));
1049 QJsonObject outputGeom = outputInfo[QSL("geometry")].toObject();
1050 QRect viewRect(outputGeom[QSL("x")].toInt() + viewGeom[QSL("x")].toInt(),
1051 outputGeom[QSL("y")].toInt() + viewGeom[QSL("y")].toInt(),
1052 viewGeom[QSL("width")].toInt(),
1053 viewGeom[QSL("height")].toInt());
1054 if (viewRect.intersects(area))
1055 return true;
1056 }
1057 }
1058 return false;
1059 }
1060
1061 bool LXQtTaskbarWayfireBackend::isShowingDesktop() const
1062 {
1063 return mIsDesktopShowing;
1064 }
1065
1066 bool LXQtTaskbarWayfireBackend::showDesktop(bool yes)
1067 {
1068 if (mIsDesktopShowing == yes)
1069 {
1070 return true;
1071 }
1072
1073 mIsDesktopShowing = yes;
1074
1075 return mWayfire->showDesktop(mWayfire->getActiveOutput());
1076 }
1077
1078 int LXQtWMBackendWayfireLibrary::getBackendScore(const QString& key) const
1079 {
1080 // Only wayfire is supported
1081 if (key.compare(QSL("wayfire"), Qt::CaseInsensitive) == 0)
1082 {
1083 return 100;
1084 }
1085
1086 // Unsupported
1087 return 0;
1088 }
1089
1090 ILXQtAbstractWMInterface *LXQtWMBackendWayfireLibrary::instance() const
1091 {
1092 return new LXQtTaskbarWayfireBackend(nullptr);
1093 }