Debian Codegraph @

src/kernel/dwindowmanagerhelper.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dwindowmanagerhelper.h"
6 #include "dforeignwindow.h"
7
8 #include <DObjectPrivate>
9 #include <DGuiApplicationHelper>
10 #include <QGuiApplication>
11
12
13 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
14 #include <qpa/qplatformwindow_p.h>
15 #define D_XCB_WINDOW_TYPE QNativeInterface::Private::QXcbWindow::WindowType
16 #else
17 #include <QtPlatformHeaders/QXcbWindowFunctions>
18 #define D_XCB_WINDOW_TYPE QXcbWindowFunctions::WmWindowType
19 #endif
20
21
22 #include <qpa/qplatformwindow.h>
23 #include <qpa/qplatformnativeinterface.h>
24
25 #include <functional>
26
27 #ifndef DTK_DISABLE_TREELAND
28 #include "plugins/platform/treeland/dtreelandwindowmanagerhelper.h"
29 #endif
30
31 DGUI_BEGIN_NAMESPACE
32
33 #define DEFINE_CONST_CHAR(Name) const char _##Name[] = "_d_" #Name
34 #define MWM_FUNC_ALL (1L << 0)
35 #define MWM_DECOR_ALL (1L << 0)
36
37 // functions
38 DEFINE_CONST_CHAR(hasBlurWindow);
39 DEFINE_CONST_CHAR(hasComposite);
40 DEFINE_CONST_CHAR(hasNoTitlebar);
41 DEFINE_CONST_CHAR(hasWallpaperEffect);
42 DEFINE_CONST_CHAR(windowManagerName);
43 DEFINE_CONST_CHAR(connectWindowManagerChangedSignal);
44 DEFINE_CONST_CHAR(connectHasBlurWindowChanged);
45 DEFINE_CONST_CHAR(connectHasCompositeChanged);
46 DEFINE_CONST_CHAR(connectHasNoTitlebarChanged);
47 DEFINE_CONST_CHAR(connectHasWallpaperEffectChanged);
48 DEFINE_CONST_CHAR(getCurrentWorkspaceWindows);
49 DEFINE_CONST_CHAR(getWindows);
50 DEFINE_CONST_CHAR(windowFromPoint);
51 DEFINE_CONST_CHAR(connectWindowListChanged);
52 DEFINE_CONST_CHAR(setMWMFunctions);
53 DEFINE_CONST_CHAR(getMWMFunctions);
54 DEFINE_CONST_CHAR(setMWMDecorations);
55 DEFINE_CONST_CHAR(getMWMDecorations);
56 DEFINE_CONST_CHAR(connectWindowMotifWMHintsChanged);
57 DEFINE_CONST_CHAR(popupSystemWindowMenu);
58 DEFINE_CONST_CHAR(setWMClassName);
59
60 template<typename ReturnT, typename FunctionT, typename... Args>
61 static inline ReturnT callPlatformFunction(const QByteArray &funcName, Args... args)
62 {
63 QFunctionPointer func = Q_NULLPTR;
64
65 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
66 func = qApp->platformFunction(funcName);
67 #endif
68
69 return func ? reinterpret_cast<FunctionT>(func)(args...) : ReturnT();
70 }
71
72 typedef bool(*func_t)(QObject *object, std::function<void ()>);
73 static bool connectWindowManagerChangedSignal(QObject *object, std::function<void ()> slot)
74 {
75 return callPlatformFunction<bool, func_t>(_connectWindowManagerChangedSignal, object, slot);
76 }
77
78 static bool connectHasBlurWindowChanged(QObject *object, std::function<void ()> slot)
79 {
80 return callPlatformFunction<bool, func_t>(_connectHasBlurWindowChanged, object, slot);
81 }
82
83 static bool connectHasCompositeChanged(QObject *object, std::function<void ()> slot)
84 {
85 return callPlatformFunction<bool, func_t>(_connectHasCompositeChanged, object, slot);
86 }
87
88 static bool connectHasNoTitlebarChanged(QObject *object, std::function<void ()> slot)
89 {
90 return callPlatformFunction<bool, func_t>(_connectHasNoTitlebarChanged, object, slot);
91 }
92
93 static bool connectHasWallpaperEffectChanged(QObject *object, std::function<void ()> slot)
94 {
95 return callPlatformFunction<bool, func_t>(_connectHasWallpaperEffectChanged, object, slot);
96 }
97
98 static bool connectWindowListChanged(QObject *object, std::function<void ()> slot)
99 {
100 return callPlatformFunction<bool, func_t>(_connectWindowListChanged, object, slot);
101 }
102
103 static bool connectWindowMotifWMHintsChanged(QObject *object, std::function<void (quint32)> slot)
104 {
105 typedef bool(*quint32_func_t)(QObject *object, std::function<void (quint32)>);
106 return callPlatformFunction<bool, quint32_func_t>(_connectWindowMotifWMHintsChanged, object, slot);
107 }
108
109 class DWindowManagerHelperPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
110 {
111 public:
112 explicit DWindowManagerHelperPrivate(DWindowManagerHelper *qq)
113 : DObjectPrivate(qq) {}
114
115 mutable QList<DForeignWindow *> windowList;
116 };
117
118 // TODO abstract an interface to adapt to various WM.
119 #ifndef DTK_DISABLE_TREELAND
120 Q_GLOBAL_STATIC(TreelandWindowManagerHelper, treelandWMHGlobal)
121 #endif
122
123 class DWindowManagerHelper_ : public DWindowManagerHelper {};
124 Q_GLOBAL_STATIC(DWindowManagerHelper_, wmhGlobal)
125
126 /*!
127 \class Dtk::Gui::DWindowManagerHelper
128 \inmodule dtkgui
129 \brief 提供与窗口管理器交互的接口,同 \a DPlatformWindowHandle 依赖 dxcb 插件.
130
131 dxcb 插件抽象出所有需要和X11平台交互的接口以供上层调用,DTK 使用插件中提供的接口再
132 次封装提供给应用程序使用,从设计角度讲,DTK库中不应该直接使用任何跟平台相关的接口
133 (如:X11、Wayland、Windows),在这样的结构支撑下,在一个新的平台上,只需要提供和
134 dxcb 同样的接口,DTK应用即可无缝迁移。
135 \sa {https://github.com/linuxdeepin/qt5dxcb-plugin/}{dxcb插件}
136 \sa Dtk::Widget::DApplication::loadDXcbPlugin
137 \sa Dtk::Widget::DApplication::isDXcbPlatform
138 \sa Dtk::Widget::DPlatformWindowHandle
139 */
140
141 /*!
142 \property DWindowManagerHelper::hasBlurWindow
143 \brief 窗口管理器是否支持窗口背景模糊特效
144 \note 在 dxcb 插件中目前只支持 deepin-wm 和 kwin 这两种窗管的模糊特效
145 \note 只读
146 */
147
148 /*!
149 \property DWindowManagerHelper::hasComposite
150 \brief 窗口管理器是否支持混成效果。如果不支持混成,则表示所有窗口的背景都不能透明,
151 随之而来也不会有窗口阴影等效果,不规则窗口的边缘也会存在锯齿。
152 \note 只读
153 */
154
155 /*!
156 \property DWindowManagerHelper::hasNoTitlebar
157 \brief 窗口管理器是否支持隐藏窗口标题栏。如果支持,则 DPlatformWindowHandle::enableDXcbForWindow
158 会优先使用此方法支持自定义窗口标题栏。
159 \note 只读
160 \sa Dtk::Gui::DPlatformHandle::setEnabledNoTitlebarForWindow
161 */
162
163 /*!
164 \property DWindowManagerHelper::hasWallpaperEffect
165 \brief 窗口管理器是否支持窗口背景特效绘制。如果支持,则 绘制背景到透明窗口
166 会使用此方法开启特效窗口壁纸背景绘制。
167 \note 只读
168 \sa hasWallpaperEffectChanged()
169 */
170
171 /*!
172 \enum Dtk::Gui::DWindowManagerHelper::MotifFunction
173 MotifFunction::MotifFunction 窗口管理器对窗口所能控制的行为
174
175 \value FUNC_RESIZE
176 控制窗口大小。如果存在此标志,则窗口管理器可以改变窗口大小(如使用鼠标拖拽窗口边缘),
177 否则无法通过外部行为调整窗口大小。
178 \code
179 DMainWindow w;
180
181 w.resize(400, 200);
182 w.show();
183 DWindowManagerHelper::setMotifFunctions(w.windowHandle(), DWindowManagerHelper::FUNC_RESIZE, false);
184 \endcode
185 \image disable_resize_function.gif
186 \note 普通窗口默认存在此标志,对于 Qt::Popup 和 Qt::BypassWindowManagerHint
187 类型的窗口,不受此标志位影响
188 \note 设置此标志后也会影响窗口标题栏对应功能入口的状态
189 \note 对于使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
190 此标志无效。
191 \sa Dtk::Gui::DPlatformHandle::enableDXcbForWindow
192 \sa Dtk::Gui::DPlatformHandle::isEnabledDXcb
193
194 \value FUNC_MOVE
195 控制窗口位置。如果存在此标志,则窗口管理器可以移动窗口(如使用鼠标拖动标题栏),否则
196 无法通过外部行为移动窗口位置。
197 \code
198 DWindowManagerHelper::setMotifFunctions(w.windowHandle(), DWindowManagerHelper::FUNC_MOVE, false);
199 \endcode
200 \image disable_move_function.gif
201
202 \value FUNC_MINIMIZE
203 最小化窗口。如果存在此标志,则窗口可以被最小化(如点击标题栏的最小化按钮),否则无法
204 通过外部行为最小化窗口。
205 \code
206 DWindowManagerHelper::setMotifFunctions(w.windowHandle(), DWindowManagerHelper::FUNC_MINIMIZE, false);
207 \endcode
208 \note 设置此标志后也会影响窗口标题栏对应功能入口的状态
209
210 \value FUNC_MAXIMIZE
211 最大化窗口。如果存在此标志,则窗口可以被最大化(如点击标题栏的最大化按钮),否则无法
212 通过外部行为最大化窗口。
213 \code
214 DWindowManagerHelper::setMotifFunctions(w.windowHandle(), DWindowManagerHelper::FUNC_MAXIMIZE, false);
215 \endcode
216 \note 设置此标志后也会影响窗口标题栏对应功能入口的状态
217
218 \value FUNC_CLOSE
219 关闭窗口。如果存在此标志,则窗口可以被关闭(如点击标题栏的关闭按钮或使用Alt+F4快捷键),
220 否则无法通过外部行为关闭窗口。
221 \code
222 DWindowManagerHelper::setMotifFunctions(w.windowHandle(), DWindowManagerHelper::FUNC_CLOSE, false);
223 \endcode
224 \note 设置此标志后也会影响窗口标题栏对应功能入口的状态
225
226 \value FUNC_ALL
227 所有功能性行为
228 */
229
230 /*!
231 \enum Dtk::Gui::DWindowManagerHelper::MotifDecoration
232 MotifFunction::MotifDecoration 窗口管理器对窗口添加的修饰。只影响窗口上对应功能
233 的入口,不影响实际的功能,比如:禁用掉 FUNC_MAXIMIZE 后,还可以使用快捷键最大化窗口
234
235 \value DECOR_BORDER
236 窗口描边。如果存在此标志,则窗口管理器会为窗口绘制描边,否则窗口没有描边。
237 否则无法通过外部行为调整窗口大小。
238 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
239 此标志无效。
240
241 \value DECOR_RESIZEH
242 改变窗口大小。如果存在此标志,则窗口管理器会在窗口的修饰上显示一个更改窗口大小的控件,
243 否则无此控件。
244 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
245 此标志无效。
246
247 \value DECOR_TITLE
248 窗口标题。如果存在此标志,则窗口管理器会在窗口的修饰上显示窗口标题,否则不显示。
249 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
250 此标志无效。
251
252 \value DECOR_MENU
253 窗口菜单。如果存在此标志,则窗口管理器会在窗口的修饰上显示一个窗口菜单控件,否则不显示。
254 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
255 此标志无效。
256
257 \value DECOR_MINIMIZE
258 窗口最小化。如果存在此标志,则窗口管理器会在窗口的修饰上显示一个最小化窗口控件,否则不显示。
259 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
260 此标志无效。
261 \sa Qt::WindowMinimizeButtonHint
262
263 \value DECOR_MAXIMIZE
264 窗口最大化。如果存在此标志,则窗口管理器会在窗口的修饰上显示一个最大化窗口控件,否则不显示。
265 \note 只支持使用系统标题栏的窗口,此功能和具体窗口管理器实现相关,deepin-wm 中设置
266 此标志无效。
267 \sa Qt::WindowMaximizeButtonHint
268
269 \value DECOR_ALL
270 所有窗口修饰。
271 */
272
273 /*!
274 \enum Dtk::Gui::DWindowManagerHelper::WMName
275 DWindowManagerHelper::WMName 窗口管理器类型
276 \value DeepinWM
277 深度系统桌面环境窗口管理器
278
279 \value KWinWM
280 KDE系统桌面环境窗口管理器
281
282 \value OtherWM
283 其它窗口管理器
284 */
285
286 /*!
287 \fn void DWindowManagerHelper::windowManagerChanged()
288 \brief 信号会在当前环境窗口管理器变化时被发送.
289 */
290 /*!
291 \fn void DWindowManagerHelper::hasBlurWindowChanged()
292 \brief 信号会在 hasBlurWindow 属性的值改变时被发送.
293 */
294 /*!
295 \fn void DWindowManagerHelper::hasCompositeChanged()
296 \brief 信号会在 hasComposite 属性的值改变时被发送.
297 */
298 /*!
299 \fn void DWindowManagerHelper::hasNoTitlebarChanged()
300 \brief 信号会在 hasNoTitlebar 属性的值改变时被发送.
301 */
302 /*!
303 \fn void DWindowManagerHelper::hasWallpaperEffectChanged()
304 \brief 信号会在 hasWallpaperEffect 属性的值改变时被发送.
305 */
306 /*!
307 \fn void DWindowManagerHelper::windowListChanged()
308 \brief 信号会在当前环境本地窗口列表变化时被发送。包含打开新窗口、关闭窗口、改变窗口的
309 层叠顺序.
310 */
311 /*!
312 \fn void DWindowManagerHelper::windowMotifWMHintsChanged(quint32 winId)
313 \brief 信号会在窗口功能或修饰标志改变时被发送.
314
315 \a winId 窗口id
316 \note 只对当前应用程序中的窗口有效
317 */
318
319 DWindowManagerHelper::~DWindowManagerHelper()
320 {
321 D_DC(DWindowManagerHelper);
322
323 for (QWindow *w : d->windowList) {
324 w->deleteLater();
325 }
326 }
327
328 /*!
329 \brief DWindowManagerHelper::instance
330 DWindowManagerHelper 的单例对象,使用 Q_GLOBAL_STATIC 定义,在第一次调用时实例化。
331 \return
332 */
333 DWindowManagerHelper *DWindowManagerHelper::instance()
334 {
335 #ifndef DTK_DISABLE_TREELAND
336 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsWaylandPlatform)) {
337 return treelandWMHGlobal;
338 }
339 #endif
340 return wmhGlobal;
341 }
342
343 /*!
344 \brief DWindowManagerHelper::setMotifFunctions
345 设置窗口的功能性标志,会覆盖之前的设置
346 \a window
347 \a hints
348 */
349 void DWindowManagerHelper::setMotifFunctions(const QWindow *window, MotifFunctions hints)
350 {
351 QFunctionPointer setMWMFunctions = Q_NULLPTR;
352
353 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
354 setMWMFunctions = qApp->platformFunction(_setMWMFunctions);
355 #endif
356
357 if (setMWMFunctions && window->handle()) {
358 /*
359 fix bug: 18775, 3391
360 当所有function标志都设置时,用MWM_FUNC_ALL代替会导致窗管无法正确处理 dock栏发送的关闭消息.所以取消此设置
361
362 if (hints == FUNC_ALL)
363 hints = (MotifFunction)MWM_FUNC_ALL;
364 */
365
366 reinterpret_cast<void(*)(quint32, quint32)>(setMWMFunctions)(window->handle()->winId(), (quint32)hints);
367 }
368 }
369
370 /*!
371 \brief DWindowManagerHelper::setMotifFunctions
372 设置窗口某些标志位的开启状态,不影响其它标志位
373 \a window
374 \a hints 要设置的标志位
375 \a on 如果值为 true 则开启标志,否则关闭
376 \return 返回设置后的窗口标志
377 */
378 DWindowManagerHelper::MotifFunctions DWindowManagerHelper::setMotifFunctions(const QWindow *window, MotifFunctions hints, bool on)
379 {
380 MotifFunctions old_hints = getMotifFunctions(window);
381
382 if (on)
383 hints |= old_hints;
384 else
385 hints = old_hints & ~hints;
386
387 setMotifFunctions(window, hints);
388
389 return hints;
390 }
391
392 /*!
393 \brief DWindowManagerHelper::getMotifFunctions
394 \a window
395 \return 返回窗口当前的功能标志
396 */
397 DWindowManagerHelper::MotifFunctions DWindowManagerHelper::getMotifFunctions(const QWindow *window)
398 {
399 QFunctionPointer getMWMFunctions = Q_NULLPTR;
400
401 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
402 getMWMFunctions = qApp->platformFunction(_getMWMFunctions);
403 #endif
404
405 if (getMWMFunctions && window->handle()) {
406 quint32 hints = reinterpret_cast<quint32(*)(quint32)>(getMWMFunctions)(window->handle()->winId());
407
408 if (!(hints & MWM_FUNC_ALL))
409 return (MotifFunctions)hints;
410 }
411
412 return FUNC_ALL;
413 }
414
415 /*!
416 \brief DWindowManagerHelper::setMotifDecorations
417 设置窗口的修饰性标志,会覆盖之前的设置
418 \a window
419 \a hints
420 */
421 void DWindowManagerHelper::setMotifDecorations(const QWindow *window, MotifDecorations hints)
422 {
423 QFunctionPointer setMWMDecorations = Q_NULLPTR;
424
425 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
426 setMWMDecorations = qApp->platformFunction(_setMWMDecorations);
427 #endif
428
429 if (setMWMDecorations && window->handle()) {
430 if (hints == DECOR_ALL)
431 hints = (MotifDecoration)MWM_DECOR_ALL;
432
433 reinterpret_cast<void(*)(quint32, quint32)>(setMWMDecorations)(window->handle()->winId(), (quint32)hints);
434 }
435 }
436
437 /*!
438 \brief DWindowManagerHelper::setMotifFunctions
439 设置窗口某些标志位的开启状态,不影响其它标志位
440 \a window
441 \a hints 要设置的标志位
442 \a on 如果值为 true 则开启标志,否则关闭
443 \return 返回设置后的窗口标志
444 */
445 DWindowManagerHelper::MotifDecorations DWindowManagerHelper::setMotifDecorations(const QWindow *window, MotifDecorations hints, bool on)
446 {
447 MotifDecorations old_hints = getMotifDecorations(window);
448
449 if (on)
450 hints |= old_hints;
451 else
452 hints = old_hints & ~hints;
453
454 setMotifDecorations(window, hints);
455
456 return hints;
457 }
458
459 /*!
460 \brief DWindowManagerHelper::getMotifFunctions
461 \a window
462 \return 返回窗口当前的修饰标志
463 */
464 DWindowManagerHelper::MotifDecorations DWindowManagerHelper::getMotifDecorations(const QWindow *window)
465 {
466 QFunctionPointer getMWMDecorations = Q_NULLPTR;
467
468 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
469 getMWMDecorations = qApp->platformFunction(_getMWMDecorations);
470 #endif
471
472 if (getMWMDecorations && window->handle()) {
473 quint32 hints = reinterpret_cast<quint32(*)(quint32)>(getMWMDecorations)(window->handle()->winId());
474
475 if (!(hints & MWM_DECOR_ALL))
476 return (MotifDecorations)hints;
477 }
478
479 return DECOR_ALL;
480 }
481
482 /*!
483 \brief DWindowManagerHelper::setWmWindowTypes
484 直接设置窗口管理器层级提供的窗口类型,如DesktopType和DockType类型也被
485 桌面环境需要,但是Qt自身并没有提供对应的设置接口
486 \a window
487 \a types
488 */
489 void DWindowManagerHelper::setWmWindowTypes(QWindow *window, WmWindowTypes types)
490 {
491 const int _types = static_cast<int>(types);
492
493 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
494 typedef QNativeInterface::Private::QXcbWindow QXcbWindow_P;
495 if (auto w = dynamic_cast<QXcbWindow_P *>(window->handle())) {
496 w->setWindowType(static_cast<D_XCB_WINDOW_TYPE>(_types));
497 } else {
498 qWarning() << "cast" << window << "to platform window failed";
499 }
500 #else
501 QXcbWindowFunctions::setWmWindowType(window, static_cast<D_XCB_WINDOW_TYPE>(_types));
502 #endif
503 }
504
505 /*!
506 \brief DWindowManagerHelper::setWmClassName
507 设置x11环境上默认使用的wm class name,主要是在窗口创建时用于设置WM_CLASS窗口属性
508 \a name
509 \note 如果值为空,Qt将在下次使用此值时根据程序名称再次初始化此值
510 \sa QCoreApplication::applicationName
511 */
512 void DWindowManagerHelper::setWmClassName(const QByteArray &name)
513 {
514 typedef void (*SetWmNameType)(const QByteArray&);
515 return callPlatformFunction<void, SetWmNameType>(_setWMClassName, name);
516 }
517
518 /*!
519 \brief DWindowManagerHelper::popupSystemWindowMenu
520 显示窗口管理器对窗口的菜单,和有边框的窗口在标题栏上点击鼠标右键弹出的菜单内容一致。
521 在 DMainWindow 的标题栏上点击鼠标右键会调用此函数打开系统菜单:
522 \image window_system_menu.gif
523 \a window
524 */
525 void DWindowManagerHelper::popupSystemWindowMenu(const QWindow *window)
526 {
527 return callPlatformFunction<void, void(*)(quint32)>(_popupSystemWindowMenu, quint32(window->handle()->winId()));
528 }
529
530 /*!
531 \brief DWindowManagerHelper::hasBlurWindow
532 \return 如果当前窗口管理器支持窗口背景模糊特效则返回 true,否则返回 false
533 */
534 bool DWindowManagerHelper::hasBlurWindow() const
535 {
536 #ifndef DTK_DISABLE_TREELAND
537 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsWaylandPlatform)) {
538 return treelandWMHGlobal->hasBlurWindow();
539 }
540 #endif
541 return callPlatformFunction<bool, bool(*)()>(_hasBlurWindow);
542 }
543
544 /*!
545 \brief DWindowManagerHelper::hasComposite
546 \return 如果当前窗口管理器支持混成则返回 true,否则返回 false
547 */
548 bool DWindowManagerHelper::hasComposite() const
549 {
550 #ifndef DTK_DISABLE_TREELAND
551 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsWaylandPlatform)) {
552 return treelandWMHGlobal->hasComposite();
553 }
554 #endif
555
556 QFunctionPointer hasComposite = Q_NULLPTR;
557
558 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
559 hasComposite = qApp->platformFunction(_hasComposite);
560 #endif
561
562 if (!hasComposite) {
563 #ifdef Q_OS_LINUX
564 if (DGuiApplicationHelper::isXWindowPlatform()) {
565 QPlatformNativeInterface *native = qApp->platformNativeInterface();
566 if (Q_LIKELY(native)) {
567 QScreen *scr = QGuiApplication::primaryScreen();
568 return native->nativeResourceForScreen(QByteArray("compositingEnabled"), scr);
569 }
570 }
571 #endif
572 // 在其它平台上默认认为混成是开启的
573 return true;
574 }
575
576 return callPlatformFunction<bool, bool(*)()>(_hasComposite);
577 }
578
579 /*!
580 \brief DWindowManagerHelper::hasNoTitlebar
581 \return 如果窗口管理器当前支持设置隐藏窗口标题栏则返回 true,否则返回 false
582 */
583 bool DWindowManagerHelper::hasNoTitlebar() const
584 {
585 #ifndef DTK_DISABLE_TREELAND
586 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::IsWaylandPlatform)) {
587 return treelandWMHGlobal->hasNoTitlebar();
588 }
589 #endif
590 return callPlatformFunction<bool, bool(*)()>(_hasNoTitlebar);
591 }
592
593 /*!
594 \brief DWindowManagerHelper::hasWallpaperEffect
595 \return 如果窗口管理器当前支持背景图片特效绘制返回 true,否则返回 false
596 */
597 bool DWindowManagerHelper::hasWallpaperEffect() const
598 {
599 return callPlatformFunction<bool, bool(*)()>(_hasWallpaperEffect);
600 }
601
602 /*!
603 \brief DWindowManagerHelper::windowManagerNameString
604 \return 返回窗口管理器名称。在X11平台上,此值为窗口管理器对应窗口的 _NET_WM_NAME
605 的值
606 \ref {https://specifications.freedesktop.org/wm-spec/1.3/ar01s03.html}{_NET_SUPPORTING_WM_CHECK}
607 \ref {https://specifications.freedesktop.org/wm-spec/1.3/ar01s05.html}{_NET_WM_NAME}
608 */
609 QString DWindowManagerHelper::windowManagerNameString() const
610 {
611 return callPlatformFunction<QString, QString(*)()>(_windowManagerName);
612 }
613
614 /*!
615 \brief DWindowManagerHelper::windowManagerName
616 \return 返回当前窗口管理器类型
617 \sa DWindowManagerHelper::windowManagerNameString
618 */
619 DWindowManagerHelper::WMName DWindowManagerHelper::windowManagerName() const
620 {
621 const QString &wmName = windowManagerNameString();
622
623 if (wmName == QStringLiteral("Mutter(DeepinGala)")) {
624 return DeepinWM;
625 }
626
627 if (wmName == QStringLiteral("KWin")) {
628 return KWinWM;
629 }
630
631 return OtherWM;
632 }
633
634 /*!
635 \brief DWindowManagerHelper::allWindowIdList
636 \return 返回当前环境所有本地窗口的窗口id列表
637 \note 顺序和窗口层叠顺序相关,显示越靠下层的窗口在列表中顺序越靠前
638 \sa DWindowManagerHelper::currentWorkspaceWindowIdList
639 */
640 QVector<quint32> DWindowManagerHelper::allWindowIdList() const
641 {
642 return callPlatformFunction<QVector<quint32>, QVector<quint32>(*)()>(_getWindows);
643 }
644
645 /*!
646 \brief DWindowManagerHelper::currentWorkspaceWindowIdList
647 \return 返回当前工作区所有本地窗口的窗口id列表
648 \note 顺序和窗口层叠顺序相关,显示越靠下层的窗口在列表中顺序越靠前
649 \sa DWindowManagerHelper::allWindowIdList
650 */
651 QVector<quint32> DWindowManagerHelper::currentWorkspaceWindowIdList() const
652 {
653 return callPlatformFunction<QVector<quint32>, QVector<quint32>(*)()>(_getCurrentWorkspaceWindows);
654 }
655
656 /*!
657 \brief DWindowManagerHelper::currentWorkspaceWindowIdList
658 \return 返回当前工作区所有本地窗口对象列表。和 currentWorkspaceWindowIdList
659 类似,只不过自动通过窗口id创建了 DForeignWindow 对象
660 \note 顺序和窗口层叠顺序相关,显示越靠下层的窗口在列表中顺序越靠前
661 \note 列表中对象的生命周期由 DForeignWindow 负责
662 \warning 此列表中不包含由当前应用创建的窗口
663 \sa DWindowManagerHelper::currentWorkspaceWindowIdList
664 \sa DForeignWindow::fromWinId
665 */
666 QList<DForeignWindow *> DWindowManagerHelper::currentWorkspaceWindows() const
667 {
668 D_DC(DWindowManagerHelper);
669
670 for (QWindow *w : d->windowList) {
671 w->deleteLater();
672 }
673
674 d->windowList.clear();
675
676 QList<WId> currentApplicationWindowList;
677 const QWindowList &list = qApp->allWindows();
678
679 currentApplicationWindowList.reserve(list.size());
680
681 for (auto window : list) {
682 if (window->property("_q_foreignWinId").isValid()) continue;
683
684 currentApplicationWindowList.append(window->winId());
685 }
686
687 QVector<quint32> wmClientList = currentWorkspaceWindowIdList();
688
689 for (WId wid : wmClientList) {
690 if (currentApplicationWindowList.contains(wid))
691 continue;
692
693 if (DForeignWindow *w = DForeignWindow::fromWinId(wid)) {
694 d->windowList << w;
695 }
696 }
697
698 return d->windowList;
699 }
700
701 /*!
702 \brief DWindowManagerHelper::windowFromPoint
703 \return 返回 \a p 位置的窗口 Id,如果出错返回 0
704 \note 可以通过 DForeignWindow::fromWinId 创建窗口对象
705 */
706 quint32 DWindowManagerHelper::windowFromPoint(const QPoint &p)
707 {
708 return callPlatformFunction<quint32, quint32 (*)(const QPoint &)>(_windowFromPoint, p);
709 }
710
711 /*!
712 \brief DWindowManagerHelper::DWindowManagerHelper
713 不允许直接实例化此对象
714 \a parent
715 \sa DWindowManagerHelper::instance
716 */
717 DWindowManagerHelper::DWindowManagerHelper(QObject *parent)
718 : QObject(parent)
719 , DObject(*new DWindowManagerHelperPrivate(this))
720 {
721 connectWindowManagerChangedSignal(this, [this] {
722 Q_EMIT windowManagerChanged();
723 });
724 connectHasBlurWindowChanged(this, [this] {
725 Q_EMIT hasBlurWindowChanged();
726 });
727 connectHasCompositeChanged(this, [this] {
728 Q_EMIT hasCompositeChanged();
729 });
730 connectHasNoTitlebarChanged(this, [this] {
731 Q_EMIT hasNoTitlebarChanged();
732 });
733 connectHasWallpaperEffectChanged(this, [this] {
734 Q_EMIT hasWallpaperEffectChanged();
735 });
736 connectWindowListChanged(this, [this] {
737 Q_EMIT windowListChanged();
738 });
739 connectWindowMotifWMHintsChanged(this, [this] (quint32 winId) {
740 Q_EMIT windowMotifWMHintsChanged(winId);
741 });
742 }
743
744
745 DGUI_END_NAMESPACE