Debian Codegraph @

wayland/wayland-shell/dwaylandshellmanager.cpp

raw
1 // SPDX-FileCopyrightText: 2022 Uniontech Software Technology Co.,Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dwaylandshellmanager.h"
6 #include "dkeyboard.h"
7 #include "global.h"
8 #include "../xcb/utility.h"
9
10 #define protected public
11 #include <qwindow.h>
12 #undef protected
13
14 #include <QtWaylandClientVersion>
15 #include <QLoggingCategory>
16 #include <QPainterPath>
17
18 #ifndef QT_DEBUG
19 Q_LOGGING_CATEGORY(dwlp, "dtk.wayland.plugin" , QtInfoMsg);
20 #else
21 Q_LOGGING_CATEGORY(dwlp, "dtk.wayland.plugin");
22 #endif
23
24 DPP_USE_NAMESPACE
25
26 #define _DWAYALND_ "_d_dwayland_"
27 #define CHECK_PREFIX(key) (key.startsWith(_DWAYALND_) || key.startsWith("_d_"))
28 #define wlDisplay reinterpret_cast<wl_display *>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("display", nullptr))
29
30 Q_DECLARE_METATYPE(QPainterPath)
31
32 namespace QtWaylandClient {
33
34 namespace {
35 // kwayland中PlasmaShell的全局对象,用于使用kwayland中的扩展协议
36 PlasmaShell *kwayland_shell = nullptr;
37 // kwin合成器提供的窗口边框管理器
38 ServerSideDecorationManager *kwayland_ssd = nullptr;
39 // 创建ddeshell
40 DDEShell *ddeShell = nullptr;
41 // kwayland
42 Strut *kwayland_strut = nullptr;
43 DDESeat *kwayland_dde_seat = nullptr;
44 DDETouch *kwayland_dde_touch = nullptr;
45 DDEPointer *kwayland_dde_pointer = nullptr;
46 FakeInput *kwayland_dde_fake_input = nullptr;
47 DDEKeyboard *kwayland_dde_keyboard = nullptr;
48 BlurManager *kwayland_blur_manager = nullptr;
49 Surface *kwayland_surface = nullptr;
50 Compositor *kwayland_compositor = nullptr;
51 PlasmaWindowManagement *kwayland_manage = nullptr;
52 };
53
54 QList<QPointer<QWaylandWindow>> DWaylandShellManager::send_property_window_list;
55
56 inline static wl_surface *getWindowWLSurface(QWaylandWindow *window)
57 {
58 #if QTWAYLANDCLIENT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
59 return window->wlSurface();
60 #else
61 return window->object();
62 #endif
63 }
64
65 static PlasmaShellSurface* createKWayland(QWaylandWindow *window)
66 {
67 if (!window || !kwayland_shell)
68 return nullptr;
69
70 auto surface = window->shellSurface();
71 return kwayland_shell->createSurface(getWindowWLSurface(window), surface);
72 }
73
74 static PlasmaShellSurface *ensureKWaylandSurface(QWaylandShellSurface *self)
75 {
76 if (auto *ksurface = self->findChild<PlasmaShellSurface*>(QString(), Qt::FindDirectChildrenOnly)) {
77 return ksurface;
78 }
79
80 return createKWayland(self->window());
81 }
82
83 static DDEShellSurface* createDDESurface(QWaylandShellSurface *shellSurface)
84 {
85 if (!shellSurface || !ddeShell)
86 return nullptr;
87
88 return ddeShell->createShellSurface(getWindowWLSurface(shellSurface->window()), shellSurface);
89 }
90
91 static DDEShellSurface *ensureDDEShellSurface(QWaylandShellSurface *shellSurface)
92 {
93 if (!shellSurface)
94 return nullptr;
95
96 if (auto *shell_surface = shellSurface->findChild<DDEShellSurface*>(QString(), Qt::FindDirectChildrenOnly)) {
97 return shell_surface;
98 }
99
100 return createDDESurface(shellSurface);
101 }
102
103 static Surface *ensureSurface(QWaylandWindow *wlWindow)
104 {
105 if (!kwayland_surface) {
106 qCWarning(dwlp) << "invalid wayland surface";
107 return nullptr;
108 }
109 if (!wlWindow->window()) {
110 qCWarning(dwlp) << "invalid wlWindow";
111 return nullptr;
112 }
113 return kwayland_surface->fromWindow(wlWindow->window());
114 }
115
116 static Blur *ensureBlur(Surface *surface, QObject *parent = nullptr)
117 {
118 if (parent) {
119 if (auto *blur = parent->findChild<Blur *>(QString(), Qt::FindDirectChildrenOnly))
120 return blur;
121 }
122 if (!kwayland_blur_manager) {
123 qCWarning(dwlp) << "invalid blur manager";
124 return nullptr;
125 }
126 return kwayland_blur_manager->createBlur(surface, parent);
127 }
128
129 static Region *ensureRegion(QObject *parent = nullptr)
130 {
131 if (parent) {
132 if (auto *region = parent->findChild<Region *>(QString(), Qt::FindDirectChildrenOnly)) {
133 return region;
134 }
135 }
136 if (!kwayland_compositor) {
137 qCWarning(dwlp) << "invalid wayland compositor";
138 return nullptr;
139 }
140 return kwayland_compositor->createRegion(parent);
141 }
142
143 static void checkIsDWayland(const QString &function)
144 {
145 #ifndef D_DEEPIN_IS_DWAYLAND
146 qCWarning(dwlp) << "This package is not compiled as dwayland, and [" << function << "] not support in this version.";
147 #endif
148 }
149
150 DWaylandShellManager::DWaylandShellManager()
151 : m_registry (new Registry())
152 {
153
154 }
155
156 DWaylandShellManager::~DWaylandShellManager()
157 {
158
159 }
160
161 void DWaylandShellManager::sendProperty(QWaylandShellSurface *self, const QString &name, const QVariant &value)
162 {
163 // 某些应用程序(比如日历,启动器)调用此方法时 self为空,导致插件崩溃
164 if (Q_UNLIKELY(!self)) {
165 return;
166 }
167
168 if (Q_UNLIKELY(!CHECK_PREFIX(name))) {
169 HookCall(self, &QWaylandShellSurface::sendProperty, name, value);
170 return;
171 }
172
173 QWaylandWindow *wlWindow = self->window();
174 if (Q_UNLIKELY(!wlWindow)) {
175 qCWarning(dwlp) << "Error, wlWindow is nullptr";
176 return;
177 }
178
179 // 如果创建失败则说明kwaylnd_shell对象还未初始化,应当终止设置
180 // 记录下本次的设置行为,kwayland_shell创建后会重新设置这些属性
181 auto *ksurface = ensureKWaylandSurface(self);
182 if (Q_UNLIKELY(!ksurface)) {
183 send_property_window_list << wlWindow;
184 return;
185 }
186
187 if (auto *dde_shell_surface = ensureDDEShellSurface(self)) {
188 if (!name.compare(noTitlebar)) {
189 qCDebug(dwlp()) << "Request NoTitleBar, value: " << value;
190 dde_shell_surface->requestNoTitleBarProperty(value.toBool());
191 }
192 if (!name.compare(windowRadius)) {
193 bool ok = false;
194 qreal radius = value.toInt(&ok);
195 if (wlWindow->screen())
196 radius *= wlWindow->screen()->devicePixelRatio();
197 qCDebug(dwlp()) << "Rquest window radius, value: " << radius << value;
198 if (ok)
199 dde_shell_surface->requestWindowRadiusProperty({radius, radius});
200 else
201 qCWarning(dwlp) << "invalid property" << name << value;
202 }
203 if (!name.compare(splitWindowOnScreen)) {
204 using KWayland::Client::DDEShellSurface;
205 const auto tmp = value.toList();
206 if (tmp.size() >= 2) {
207 const auto &position = tmp[0].toInt();
208 const auto &type = tmp[1].toInt();
209 checkIsDWayland("requestSplitWindow()");
210 #ifdef D_DEEPIN_IS_DWAYLAND
211 dde_shell_surface->requestSplitWindow(DDEShellSurface::SplitType(position), DDEShellSurface::SplitMode(type));
212 qCDebug(dwlp) << "requestSplitWindow splitType: " << position << " mode: " << type;
213 #endif
214 } else {
215 qCWarning(dwlp) << "invalid property: " << name << value;
216 }
217 wlWindow->window()->setProperty(splitWindowOnScreen, 0);
218 }
219 if (!name.compare(supportForSplittingWindow)) {
220 checkIsDWayland("getSplitable()");
221 #ifdef D_DEEPIN_IS_DWAYLAND
222 wlWindow->window()->setProperty(supportForSplittingWindow, dde_shell_surface->getSplitable());
223 #endif
224 return;
225 }
226 if (!name.compare(windowInWorkSpace)) {
227 dde_shell_surface->requestOnAllDesktops(value.toBool());
228 qCDebug(dwlp()) << "### requestOnAllDesktops" << name << value;
229 }
230 if (!name.compare(enableBlurWindow)) {
231 qCDebug(dwlp) << "### enableBlurWindow" << name << value;
232 if (!value.isValid()) {
233 qCWarning(dwlp) << "invalid enableBlurWindow";
234 return;
235 }
236 setEnableBlurWidow(wlWindow, value);
237 }
238 if (!name.compare(windowBlurAreas) || !name.compare(windowBlurPaths)) {
239 qCDebug(dwlp) << "### requestWindowBlur" << name << value;
240 updateWindowBlurAreasForWM(wlWindow, name, value);
241 }
242 }
243
244 // 将popup的窗口设置为tooltop层级, 包括qmenu,combobox弹出窗口
245 if (wlWindow->window()->type() == Qt::Popup)
246 ksurface->setRole(PlasmaShellSurface::Role::ToolTip);
247
248 #ifdef D_DEEPIN_KWIN
249 // 禁止窗口移动接口适配。
250 typedef PlasmaShellSurface::Role KRole;
251 if (!name.compare(enableSystemMove)) {
252 ksurface->setRole(value.toBool() ? KRole::Normal : KRole::StandAlone);
253 return;
254 }
255
256 if (QStringLiteral(_DWAYALND_ "global_keyevent") == name && value.toBool()) {
257 if (wlWindow->findChild<DKeyboard*>(QString(), Qt::FindDirectChildrenOnly)) {
258 return;
259 }
260
261 DKeyboard *keyboard = new DKeyboard(wlWindow);
262 // 只有关心全局键盘事件才连接, 并且随窗口销毁而断开
263 QObject::connect(kwayland_dde_keyboard, &DDEKeyboard::keyChanged,
264 keyboard, &DKeyboard::handleKeyEvent);
265 QObject::connect(kwayland_dde_keyboard, &DDEKeyboard::modifiersChanged,
266 keyboard, &DKeyboard::handleModifiersChanged);
267 }
268 #endif
269
270 if (QStringLiteral(_DWAYALND_ "dockstrut") == name) {
271 setDockStrut(self, value);
272 }
273 if (QStringLiteral(_DWAYALND_ "window-position") == name) {
274 ksurface->setPosition(value.toPoint());
275 }
276
277 if (QStringLiteral(_DWAYALND_ "dock-appitem-geometry") == name) {
278 setDockAppItemMinimizedGeometry(self, value);
279 }
280
281 static const QMap<PlasmaShellSurface::Role, QStringList> role2type = {
282 {PlasmaShellSurface::Role::Normal, {"normal"}},
283 {PlasmaShellSurface::Role::Desktop, {"desktop"}},
284 {PlasmaShellSurface::Role::Panel, {"dock", "panel"}},
285 {PlasmaShellSurface::Role::OnScreenDisplay, {"wallpaper", "onScreenDisplay"}},
286 {PlasmaShellSurface::Role::Notification, {"notification"}},
287 {PlasmaShellSurface::Role::ToolTip, {"tooltip"}},
288 #ifdef D_DEEPIN_KWIN
289 {PlasmaShellSurface::Role::StandAlone, {"launcher", "standAlone"}},
290 {PlasmaShellSurface::Role::Override, {"session-shell", "menu", "wallpaper-set", "override"}},
291 #endif
292 };
293
294 if (QStringLiteral(_DWAYALND_ "window-type") == name) {
295 // 根据 type 设置对应的 role
296 const QByteArray &type = value.toByteArray();
297 for (int i = 0; i <= (int)PlasmaShellSurface::Role::ActiveFullScreen; ++i) {
298 PlasmaShellSurface::Role role = PlasmaShellSurface::Role(i);
299 if (role2type.value(role).contains(type)) {
300 ksurface->setRole(role);
301 if (type == "dock" || type == "panel") {
302 ksurface->setPanelBehavior(PlasmaShellSurface::PanelBehavior::AlwaysVisible);
303 }
304 break;
305 }
306 }
307 }
308 if (QStringLiteral(_DWAYALND_ "staysontop") == name) {
309 setWindowStaysOnTop(self, value.toBool());
310 }
311 }
312
313 void DWaylandShellManager::setGeometry(QPlatformWindow *self, const QRect &rect)
314 {
315 HookCall(self, &QPlatformWindow::setGeometry, rect);
316 if (self->QPlatformWindow::parent()) {
317 return;
318 }
319 if (auto wl_window = static_cast<QWaylandWindow*>(self)) {
320 wl_window->sendProperty(QStringLiteral(_DWAYALND_ "window-position"), rect.topLeft());
321 }
322 }
323
324 void DWaylandShellManager::pointerEvent(const QPointF &pointF, QEvent::Type type)
325 {
326 if (Q_UNLIKELY(!(type == QEvent::MouseButtonPress
327 || type == QEvent::MouseButtonRelease
328 || type == QEvent::Move))) {
329 return;
330 }
331 // cursor()->pointerEvent 中只用到 event.globalPos(), 即 pointF 这个参数
332 for (QScreen *screen : qApp->screens()) {
333 if (screen && screen->handle() && screen->handle()->cursor()) {
334 const QMouseEvent event(type, QPointF(), QPointF(), pointF, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
335 screen->handle()->cursor()->pointerEvent(event);
336 }
337 }
338 }
339
340 QWaylandShellSurface *DWaylandShellManager::createShellSurface(QWaylandShellIntegration *self, QWaylandWindow *window)
341 {
342 auto surface = HookCall(self, &QWaylandShellIntegration::createShellSurface, window);
343
344 HookOverride(surface, &QWaylandShellSurface::sendProperty, DWaylandShellManager::sendProperty);
345 HookOverride(surface, &QWaylandShellSurface::wantsDecorations, DWaylandShellManager::disableClientDecorations);
346 HookOverride(window, &QPlatformWindow::setGeometry, DWaylandShellManager::setGeometry);
347 HookOverride(window, &QPlatformWindow::requestActivateWindow, DWaylandShellManager::requestActivateWindow);
348 HookOverride(window, &QPlatformWindow::frameMargins, DWaylandShellManager::frameMargins);
349 HookOverride(window, &QPlatformWindow::setWindowFlags, DWaylandShellManager::setWindowFlags);
350
351 onShellSurfaceCreated(surface);
352
353 // 设置窗口位置, 默认都需要设置,同时判断如果窗口并没有移动过,则不需要再设置位置,而是由窗管默认平铺显示
354 bool bSetPosition = true;
355 QWidgetWindow *widgetWin = static_cast<QWidgetWindow*>(window->window());
356 if (widgetWin->inherits("QWidgetWindow") && widgetWin->widget()) {
357 if (!widgetWin->widget()->testAttribute(Qt::WA_Moved)) {
358 bSetPosition = false;
359 }
360
361 // 1. dabstractdialog 的 showevent 中会主动move到屏幕居中的位置, 即 setAttribute(Qt::WA_Moved)。
362 // 2. 有 parent(ddialog dlg(this)) 的 window 窗管会主动调整位置,没有设置parent的才需要插件调整位置 如 ddialog dlg;
363 if (window->transientParent() && !widgetWin->widget()->inherits("QMenu")) {
364 bSetPosition = false;
365 }
366 }
367
368 if (bSetPosition) {
369 //QWaylandWindow对应surface的geometry,如果使用QWindow会导致缩放后surface坐标错误。
370 window->sendProperty(_DWAYALND_ "window-position", window->geometry().topLeft());
371 }
372
373 for (const QByteArray &pname : widgetWin->dynamicPropertyNames()) {
374 if (Q_LIKELY(!CHECK_PREFIX(pname)))
375 continue;
376 // 将窗口自定义属性记录到wayland window property中
377 window->sendProperty(pname, widgetWin->property(pname.constData()));
378 }
379
380 //将拖拽图标窗口置顶,QShapedPixmapWindow是Qt中拖拽图标窗口专用类
381 if (widgetWin->inherits("QShapedPixmapWindow")) {
382 window->sendProperty(QStringLiteral(_DWAYALND_ "staysontop"), true);
383 }
384
385 return surface;
386 }
387
388 void DWaylandShellManager::createKWaylandShell(quint32 name, quint32 version)
389 {
390 kwayland_shell = registry()->createPlasmaShell(name, version, registry()->parent());
391
392 Q_ASSERT_X(kwayland_shell, "PlasmaShell", "Registry create PlasmaShell failed.");
393
394 for (QPointer<QWaylandWindow> lw_window : send_property_window_list) {
395 if (!lw_window) {
396 continue;
397 }
398 const QVariantMap &properites = lw_window->properties();
399 // 当kwayland_shell被创建后,找到以_d_dwayland_开头的扩展属性将其设置一遍
400 for (auto p = properites.cbegin(); p != properites.cend(); ++p) {
401 if (CHECK_PREFIX(p.key()))
402 sendProperty(lw_window->shellSurface(), p.key(), p.value());
403 }
404 }
405
406 send_property_window_list.clear();
407 }
408
409 void DWaylandShellManager::createKWaylandSSD(quint32 name, quint32 version)
410 {
411 kwayland_ssd = registry()->createServerSideDecorationManager(name, version, registry()->parent());
412 Q_ASSERT_X(kwayland_ssd, "ServerSideDecorationManager", "KWayland Registry ServerSideDecorationManager failed.");
413 }
414
415 void DWaylandShellManager::createDDEShell(quint32 name, quint32 version)
416 {
417 ddeShell = registry()->createDDEShell(name, version, registry()->parent());
418 Q_ASSERT_X(ddeShell, "DDEShell", "Registry create DDEShell failed.");
419 }
420
421 void DWaylandShellManager::createDDESeat(quint32 name, quint32 version)
422 {
423 kwayland_dde_seat = registry()->createDDESeat(name, version, registry()->parent());
424 Q_ASSERT_X(kwayland_dde_seat, "DDESeat", "Registry create DDESeat failed.");
425 }
426
427 /*
428 * @brief createStrut 创建dock区域,仅dock使用
429 * @param registry
430 * @param name
431 * @param version
432 */
433 void DWaylandShellManager::createStrut(quint32 name, quint32 version)
434 {
435 kwayland_strut = registry()->createStrut(name, version, registry()->parent());
436 Q_ASSERT_X(kwayland_strut, "strut", "Registry create strut failed.");
437 }
438
439 void DWaylandShellManager::createDDEKeyboard()
440 {
441 //create dde keyboard
442 Q_ASSERT(kwayland_dde_seat);
443
444 kwayland_dde_keyboard = kwayland_dde_seat->createDDEKeyboard(registry()->parent());
445 Q_ASSERT(kwayland_dde_keyboard);
446
447 //刷新时间队列,等待kwin反馈消息
448 if (wlDisplay){
449 wl_display_roundtrip(wlDisplay);
450 }
451 }
452
453 void DWaylandShellManager::createDDEFakeInput()
454 {
455 kwayland_dde_fake_input = registry()->createFakeInput(registry()->interface(Registry::Interface::FakeInput).name,
456 registry()->interface(Registry::Interface::FakeInput).version);
457 if (!kwayland_dde_fake_input || !kwayland_dde_fake_input->isValid()) {
458 qCWarning(dwlp) << "fake input create failed.";
459 return;
460 }
461 // 打开设置光标位置的开关
462 kwayland_dde_fake_input->authenticate("dtk", QString("set cursor pos"));
463 }
464
465 void DWaylandShellManager::createDDEPointer()
466 {
467 //create dde pointer
468 Q_ASSERT(kwayland_dde_seat);
469
470 kwayland_dde_pointer = kwayland_dde_seat->createDDePointer();
471 Q_ASSERT(kwayland_dde_pointer);
472
473 //向kwin发送获取全局坐标请求
474 kwayland_dde_pointer->getMotion();
475
476 //刷新时间队列,等待kwin反馈消息
477 if (wlDisplay) {
478 wl_display_roundtrip(wlDisplay);
479 }
480
481 //更新一次全局坐标
482 pointerEvent(kwayland_dde_pointer->getGlobalPointerPos(), QEvent::Move);
483
484 // mouse move
485 static bool isTouchMotion = false;
486 QObject::connect(kwayland_dde_pointer, &DDEPointer::motion,
487 [] (const QPointF &posF) {
488 if (isTouchMotion)
489 return;
490 pointerEvent(posF, QEvent::Move);
491 });
492
493 // 适配触屏
494 // 1.25倍的缩放还是需要单独处理
495 static QPointF releasePos;
496 kwayland_dde_touch = kwayland_dde_seat->createDDETouch();
497 QObject::connect(kwayland_dde_touch, &DDETouch::touchDown, [=] (int32_t kwaylandId, const QPointF &pos) {
498 if (kwaylandId != 0) {
499 return;
500 }
501 releasePos = pos;
502
503 setCursorPoint(pos);
504 pointerEvent(pos, QEvent::MouseButtonPress);
505 });
506 QObject::connect(kwayland_dde_touch, &DDETouch::touchMotion, [=] (int32_t kwaylandId, const QPointF &pos) {
507 if (kwaylandId != 0) {
508 return;
509 }
510 isTouchMotion = true;
511 pointerEvent(pos, QEvent::Move);
512 setCursorPoint(pos);
513 releasePos = pos;
514 });
515 QObject::connect(kwayland_dde_touch, &DDETouch::touchUp, [=] (int32_t kwaylandId) {
516 if (kwaylandId != 0) {
517 return;
518 }
519
520 // 和 motion 的最后一个位置相等, 无需再更新
521 if (isTouchMotion) {
522 isTouchMotion = false;
523 return;
524 }
525
526 setCursorPoint(releasePos);
527 pointerEvent(releasePos, QEvent::MouseButtonRelease);
528 });
529 }
530
531 void DWaylandShellManager::createBlurManager(quint32 name, quint32 version)
532 {
533 kwayland_blur_manager = registry()->createBlurManager(name, version);
534 if (!kwayland_blur_manager) {
535 qCWarning(dwlp) << "kwayland_blur_manager create failed.";
536 return;
537 }
538 }
539
540 void DWaylandShellManager::createCompositor(quint32 name, quint32 version)
541 {
542 kwayland_compositor = registry()->createCompositor(name, version);
543 if (!kwayland_compositor) {
544 qCWarning(dwlp) << "kwayland_compositor create failed.";
545 return;
546 }
547 }
548
549 void DWaylandShellManager::createSurface()
550 {
551 if (!kwayland_compositor) {
552 qCWarning(dwlp) << "kwayland_compositor is invalid.";
553 return;
554 }
555 kwayland_surface = kwayland_compositor->createSurface();
556 if (!kwayland_surface) {
557 qCWarning(dwlp) << "kwayland_surface create failed.";
558 return;
559 }
560 }
561
562 void DWaylandShellManager::createPlasmaWindowManagement(KWayland::Client::Registry *registry, quint32 name, quint32 version)
563 {
564 kwayland_manage = registry->createPlasmaWindowManagement(name, version, registry->parent());
565 }
566
567 void DWaylandShellManager::requestActivateWindow(QPlatformWindow *self)
568 {
569 HookCall(self, &QPlatformWindow::requestActivateWindow);
570
571 auto qwlWindow = static_cast<QWaylandWindow *>(self);
572 if (self->QPlatformWindow::parent() || !ddeShell || !qwlWindow) {
573 return;
574 }
575
576 if (auto *dde_shell_surface = ensureDDEShellSurface(qwlWindow->shellSurface())) {
577 dde_shell_surface->requestActive();
578 }
579 }
580
581 /*插件把qt自身的标题栏去掉了,使用的是窗管的标题栏,但是qtwaylandwindow每次传递坐标给kwin的时候,
582 * 都计算了(3, 30)的偏移,导致每次设置窗口属性的时候,窗口会下移,这个(3, 30)的偏移其实是qt自身
583 * 标题栏计算的偏移量,我们uos桌面不能带入这个偏移量
584 */
585 QMargins DWaylandShellManager::frameMargins(QPlatformWindow *self)
586 {
587 Q_UNUSED(self)
588
589 return QMargins(0, 0, 0, 0);
590 }
591
592 void DWaylandShellManager::setWindowFlags(QPlatformWindow *self, Qt::WindowFlags flags)
593 {
594 HookCall(self, &QPlatformWindow::setWindowFlags, flags);
595 auto qwlWindow = static_cast<QWaylandWindow *>(self);
596 if (!qwlWindow) {
597 return;
598 }
599 qCDebug(dwlp) << "Qt::WindowStaysOnTopHint: " << flags.testFlag(Qt::WindowStaysOnTopHint);
600 setWindowStaysOnTop(qwlWindow->shellSurface(), flags.testFlag(Qt::WindowStaysOnTopHint));
601 }
602
603 void DWaylandShellManager::handleGeometryChange(QWaylandShellSurface *shellSurface)
604 {
605 auto ddeShellSurface = ensureDDEShellSurface(shellSurface);
606 if (!ddeShellSurface || !shellSurface->window()) {
607 return;
608 }
609 QObject::connect(ddeShellSurface, &DDEShellSurface::geometryChanged, shellSurface->window(),
610 [=] (const QRect &geom) {
611 QRect newRect(geom.topLeft(), shellSurface->window()->geometry().size());
612 QWindowSystemInterface::handleGeometryChange(shellSurface->window()->window(), newRect);
613 });
614 }
615
616 typedef DDEShellSurface KCDFace;
617 Qt::WindowStates getwindowStates(KCDFace *surface)
618 {
619 // handleWindowStateChanged 不能传 WindowActive 状态,单独处理
620 Qt::WindowStates state = Qt::WindowNoState;
621 if (surface->isMinimized())
622 state = Qt::WindowMinimized;
623 else if (surface->isFullscreen())
624 state = Qt::WindowFullScreen;
625 else if (surface->isMaximized())
626 state = Qt::WindowMaximized;
627
628 return state;
629 }
630
631 void DWaylandShellManager::handleWindowStateChanged(QWaylandShellSurface *shellSurface)
632 {
633 if (!shellSurface) {
634 qCWarning(dwlp) << "shellSurface is null";
635 return;
636 }
637 QWaylandWindow *window = shellSurface->window();
638 auto ddeShellSurface = ensureDDEShellSurface(shellSurface);
639 if (!ddeShellSurface)
640 return;
641
642 #define d_oldState QStringLiteral("_d_oldState")
643
644 #define STATE_CHANGED(sig) \
645 QObject::connect(ddeShellSurface, &KCDFace::sig, window, [window, ddeShellSurface](){ \
646 qCDebug(dwlp) << "==== "#sig ; \
647 const Qt::WindowStates &newState = getwindowStates(ddeShellSurface); \
648 const int &oldState = window->property(d_oldState).toInt(); \
649 QWindowSystemInterface::handleWindowStateChanged(window->window(), newState, oldState); \
650 window->setProperty(d_oldState, static_cast<int>(newState)); \
651 })
652
653 window->setProperty(d_oldState, (int)getwindowStates(ddeShellSurface));
654
655 STATE_CHANGED(minimizedChanged);
656 STATE_CHANGED(maximizedChanged);
657 STATE_CHANGED(fullscreenChanged);
658
659 STATE_CHANGED(activeChanged);
660 QObject::connect(ddeShellSurface, &KCDFace::activeChanged, window, [window, ddeShellSurface](){
661 if (QWindow *w = ddeShellSurface->isActive() ? window->window() : nullptr)
662 #if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
663 QWindowSystemInterface::handleWindowActivated(w, Qt::FocusReason::ActiveWindowFocusReason);
664 #else
665 QWindowSystemInterface::handleFocusWindowChanged(w, Qt::FocusReason::ActiveWindowFocusReason);
666 #endif
667 });
668
669 #define SYNC_FLAG(sig, enableFunc, flag) \
670 QObject::connect(ddeShellSurface, &KCDFace::sig, window, [window, ddeShellSurface](){ \
671 qCDebug(dwlp) << "==== "#sig << (enableFunc); \
672 window->window()->setFlag(flag, enableFunc); \
673 })
674
675 // SYNC_FLAG(keepAboveChanged, ddeShellSurface->isKeepAbove(), Qt::WindowStaysOnTopHint);
676 QObject::connect(ddeShellSurface, &KCDFace::keepAboveChanged, window, [window, ddeShellSurface](){
677 bool isKeepAbove = ddeShellSurface->isKeepAbove();
678 qCDebug(dwlp) << "==== keepAboveChanged" << isKeepAbove;
679 window->window()->setProperty(_DWAYALND_ "staysontop", isKeepAbove);
680 });
681 SYNC_FLAG(keepBelowChanged, ddeShellSurface->isKeepBelow(), Qt::WindowStaysOnBottomHint);
682 SYNC_FLAG(minimizeableChanged, ddeShellSurface->isMinimizeable(), Qt::WindowMinimizeButtonHint);
683 SYNC_FLAG(maximizeableChanged, ddeShellSurface->isMinimizeable(), Qt::WindowMaximizeButtonHint);
684 SYNC_FLAG(closeableChanged, ddeShellSurface->isCloseable(), Qt::WindowCloseButtonHint);
685 SYNC_FLAG(fullscreenableChanged, ddeShellSurface->isFullscreenable(), Qt::WindowFullscreenButtonHint);
686
687 // TODO: not support yet
688 //SYNC_FLAG(movableChanged, ddeShellSurface->isMovable(), Qt::??);
689 //SYNC_FLAG(resizableChanged, ddeShellSurface->isResizable(), Qt::??);
690 //SYNC_FLAG(acceptFocusChanged, ddeShellSurface->isAcceptFocus(), Qt::??);
691 //SYNC_FLAG(modalityChanged, ddeShellSurface->isModal(), Qt::??);
692 }
693
694 /*
695 * @brief setWindowStaysOnTop 设置窗口置顶
696 * @param surface
697 * @param state true:设置窗口置顶,false:取消置顶
698 */
699 void DWaylandShellManager::setWindowStaysOnTop(QWaylandShellSurface *surface, const bool state)
700 {
701 if (auto *dde_shell_surface = ensureDDEShellSurface(surface)) {
702 dde_shell_surface->requestKeepAbove(state);
703 }
704 }
705
706 /*
707 * @brief setDockStrut 设置窗口有效区域
708 * @param surface
709 * @param var[0]:dock位置 var[1]:dock高度或者宽度 var[2]:start值 var[3]:end值
710 */
711 void DWaylandShellManager::setDockStrut(QWaylandShellSurface *surface, const QVariant var)
712 {
713 deepinKwinStrut dockStrut;
714 switch (var.toList()[0].toInt()) {
715 case 0:
716 dockStrut.left = var.toList()[1].toInt();
717 dockStrut.left_start_y = var.toList()[2].toInt();
718 dockStrut.left_end_y = var.toList()[3].toInt();
719 break;
720 case 1:
721 dockStrut.top = var.toList()[1].toInt();
722 dockStrut.top_start_x = var.toList()[2].toInt();
723 dockStrut.top_end_x = var.toList()[3].toInt();
724 break;
725 case 2:
726 dockStrut.right = var.toList()[1].toInt();
727 dockStrut.right_start_y = var.toList()[2].toInt();
728 dockStrut.right_end_y = var.toList()[3].toInt();
729 break;
730 case 3:
731 dockStrut.bottom = var.toList()[1].toInt();
732 dockStrut.bottom_start_x = var.toList()[2].toInt();
733 dockStrut.bottom_end_x = var.toList()[3].toInt();
734 break;
735 default:
736 break;
737 }
738
739 kwayland_strut->setStrutPartial(getWindowWLSurface(surface->window()), dockStrut);
740 }
741
742 /*
743 * @brief setDockAppItemMinimizedGeometry 设置驻留任务栏应用窗口最小化的位置信息
744 * @param surface
745 * @param var[0]:应用 窗口Id
746 * var[1]:app window x
747 * var[2]:app window y
748 * var[3]:app window width
749 * var[4]:app window height
750 */
751 void DWaylandShellManager::setDockAppItemMinimizedGeometry(QWaylandShellSurface *surface, const QVariant var)
752 {
753 if (!surface) {
754 return;
755 }
756
757 for (auto w : kwayland_manage->windows()){
758 if (w->windowId() == var.toList()[0].toUInt()) {
759 auto x = var.toList()[1].toInt();
760 auto y = var.toList()[2].toInt();
761 auto width = var.toList()[3].toInt();
762 auto height = var.toList()[4].toInt();
763
764 QRect r(QPoint(x, y), QSize(width, height));
765
766 if (w) {
767 auto s = ensureSurface(surface->window());
768 if (!s) {
769 qCWarning(dwlp) << "invalid surface";
770 return;
771 }
772
773 w->setMinimizedGeometry(s, r);
774 }
775
776 return;
777 }
778 }
779 }
780
781 void DWaylandShellManager::onShellSurfaceCreated(QWaylandShellSurface *shellSurface)
782 {
783 handleGeometryChange(shellSurface);
784 handleWindowStateChanged(shellSurface);
785 createServerDecoration(shellSurface);
786 }
787
788 /*
789 * @brief setCursorPoint 设置光标在屏幕中的绝对位置
790 * @param pos
791 */
792 void DWaylandShellManager::setCursorPoint(QPointF pos) {
793 if (!kwayland_dde_fake_input) {
794 qCCritical(dwlp) << "kwayland_dde_fake_input is nullptr";
795 return;
796 }
797 if (!kwayland_dde_fake_input->isValid()) {
798 qCCritical(dwlp) << "kwayland_dde_fake_input is invalid";
799 return;
800 }
801 kwayland_dde_fake_input->requestPointerMoveAbsolute(pos);
802 }
803
804 void DWaylandShellManager::setEnableBlurWidow(QWaylandWindow *wlWindow, const QVariant &value)
805 {
806 auto surface = ensureSurface(wlWindow);
807 if (value.toBool()) {
808 auto blur = ensureBlur(surface, surface);
809 if (!blur) {
810 qCWarning(dwlp) << "invalid blur";
811 return;
812 }
813 auto region = ensureRegion(surface);
814 if (!region) {
815 qCWarning(dwlp) << "invalid region";
816 return;
817 }
818 blur->setRegion(region);
819 blur->commit();
820 if (!kwayland_surface) {
821 qCWarning(dwlp) << "invalid kwayland_surface";
822 return;
823 }
824 kwayland_surface->commit(Surface::CommitFlag::None);
825 } else {
826 if (!kwayland_blur_manager) {
827 qCWarning(dwlp) << "invalid kwayland_blur_manager";
828 return;
829 }
830 kwayland_blur_manager->removeBlur(surface);
831 if (!kwayland_surface) {
832 qCWarning(dwlp) << "invalid kwayland_surface";
833 return;
834 }
835 kwayland_surface->commit(Surface::CommitFlag::None);
836 // 取消模糊效果的更新需要主动调用应用侧的窗口
837 if (QWidgetWindow *widgetWin = static_cast<QWidgetWindow*>(wlWindow->window())) {
838 if (auto widget = widgetWin->widget()) {
839 widget->update();
840 }
841 }
842 }
843 }
844
845 void DWaylandShellManager::updateWindowBlurAreasForWM(QWaylandWindow *wlWindow, const QString &name, const QVariant &value)
846 {
847 if (!wlWindow->waylandScreen()) {
848 return;
849 }
850 auto screen = wlWindow->waylandScreen()->screen();
851 if (!screen) {
852 return;
853 }
854 auto devicePixelRatio = screen->devicePixelRatio();
855
856 auto surface = ensureSurface(wlWindow);
857 if (!surface) {
858 qCWarning(dwlp) << "invalid surface";
859 return;
860 }
861 auto blur = ensureBlur(surface, surface);
862 if (!blur) {
863 qCWarning(dwlp) << "invalid blur";
864 return;
865 }
866 auto region = kwayland_compositor->createRegion(surface);
867
868 if (!name.compare(windowBlurAreas)) {
869 const QVector<quint32> &tmpV = qvariant_cast<QVector<quint32>>(value);
870 const QVector<Utility::BlurArea> &blurAreas = *(reinterpret_cast<const QVector<Utility::BlurArea>*>(&tmpV));
871 if (blurAreas.isEmpty()) {
872 qCWarning(dwlp) << "invalid BlurAreas";
873 return;
874 }
875 for (auto ba : blurAreas) {
876 ba *= devicePixelRatio;
877 QPainterPath path;
878 path.addRoundedRect(ba.x, ba.y, ba.width, ba.height, ba.xRadius, ba.yRaduis);
879 region->add(path.toFillPolygon().toPolygon());
880 }
881 } else {
882 const QList<QPainterPath> paths = qvariant_cast<QList<QPainterPath>>(value);
883 if (paths.isEmpty()) {
884 qCWarning(dwlp) << "invalid BlurPaths";
885 return;
886 }
887 for (auto path : paths) {
888 path *= devicePixelRatio;
889 QPolygon polygon(path.toFillPolygon().toPolygon());
890 region->add(polygon);
891 }
892 }
893 blur->setRegion(region);
894 blur->commit();
895 kwayland_surface->commit(Surface::CommitFlag::None);
896 }
897
898 bool DWaylandShellManager::disableClientDecorations(QWaylandShellSurface *surface)
899 {
900 Q_UNUSED(surface)
901
902 // 禁用qtwayland自带的bradient边框(太丑了)
903 return false;
904 }
905
906 void DWaylandShellManager::createServerDecoration(QWaylandShellSurface *shellSurface)
907 {
908 if (!shellSurface) {
909 qCWarning(dwlp) << "shellSurface is null";
910 return;
911 }
912 QWaylandWindow *window = shellSurface->window();
913 // 如果kwayland的server窗口装饰已转变完成,则为窗口创建边框
914 if (!kwayland_ssd) {
915 qDebug()<<"====kwayland_ssd creat failed";
916 return;
917 }
918
919 // 通过窗口属性控制是否显示最小化和最大化按钮
920 if (shellSurface) {
921 auto *dde_shell_surface = ensureDDEShellSurface(shellSurface);
922 if (dde_shell_surface) {
923 if (!(window->window()->flags() & Qt::WindowMinimizeButtonHint)) {
924 dde_shell_surface->requestMinizeable(false);
925 }
926 if (!(window->window()->flags() & Qt::WindowMaximizeButtonHint)) {
927 dde_shell_surface->requestMaximizeable(false);
928 }
929 if ((window->window()->flags() & Qt::WindowStaysOnTopHint)) {
930 dde_shell_surface->requestKeepAbove(true);
931 }
932 if ((window->window()->flags() & Qt::WindowDoesNotAcceptFocus)) {
933 dde_shell_surface->requestAcceptFocus(false);
934 }
935 if (window->window()->modality() != Qt::NonModal) {
936 dde_shell_surface->requestModal(true);
937 }
938 }
939 }
940
941 bool decoration = false;
942 switch (window->window()->type()) {
943 case Qt::Window:
944 case Qt::Widget:
945 case Qt::Dialog:
946 case Qt::Tool:
947 case Qt::Drawer:
948 decoration = true;
949 break;
950 default:
951 break;
952 }
953 if (window->window()->flags() & Qt::FramelessWindowHint)
954 decoration = false;
955 if (window->window()->flags() & Qt::BypassWindowManagerHint)
956 decoration = false;
957
958 qCDebug(dwlp) << "create decoration ?" << decoration;
959 if (!decoration)
960 return;
961
962 auto *surface = getWindowWLSurface(window);
963
964 if (!surface)
965 return;
966
967 // 创建由kwin server渲染的窗口边框对象
968 if (auto ssd = kwayland_ssd->create(surface, shellSurface)) {
969 ssd->requestMode(ServerSideDecoration::Mode::Server);
970 }
971 }
972
973 }