| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dforeignwindow.h" |
| 6 |
#include <DObjectPrivate> |
| 7 |
|
| 8 |
#include <private/qguiapplication_p.h> |
| 9 |
#include <qpa/qplatformintegration.h> |
| 10 |
|
| 11 |
#include <QEvent> |
| 12 |
#include <QDynamicPropertyChangeEvent> |
| 13 |
#include <QDebug> |
| 14 |
|
| 15 |
DGUI_BEGIN_NAMESPACE |
| 16 |
|
| 17 |
#define DEFINE_CONST_CHAR(Name) const char _##Name[] = "_d_" #Name |
| 18 |
|
| 19 |
// propertys |
| 20 |
DEFINE_CONST_CHAR(WmClass); |
| 21 |
DEFINE_CONST_CHAR(ProcessId); |
| 22 |
|
| 23 |
class DForeignWindowPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate |
| 24 |
{ |
| 25 |
public: |
| 26 |
explicit DForeignWindowPrivate(DForeignWindow *qq) |
| 27 |
: DObjectPrivate(qq) {} |
| 28 |
}; |
| 29 |
|
| 30 |
/*! |
| 31 |
\class Dtk::Gui::DForeignWindow |
| 32 |
\inmodule dtkgui |
| 33 |
|
| 34 |
\brief 一个用于获取本地窗口信息的类. |
| 35 |
|
| 36 |
继承于 QWindow,支持 QWindow::geometry |
| 37 |
QWindow::x QWindow::y QWindow::width QWindow::height |
| 38 |
QWindow::title QWindow::flags QWindow::visibility QWindow::type |
| 39 |
QWindow::windowStates QWindow::windowState 等接口的使用,另外扩展 |
| 40 |
增加了一部分接口,方面更加详细的获取窗口信息。依赖于 dxcb 插件,在未加载 |
| 41 |
dxcb 插件的应用中使用时结果未知 |
| 42 |
|
| 43 |
\sa DWindowManagerHelper::currentWorkspaceWindows |
| 44 |
\sa Dtk::Widget::DApplication::loadDXcbPlugin |
| 45 |
\sa Dtk::Widget::DApplication::isDXcbPlatform |
| 46 |
*/ |
| 47 |
|
| 48 |
/*! |
| 49 |
\property DForeignWindow::wmClass |
| 50 |
\brief 窗口 WM_CLASS 的值 |
| 51 |
\note 只读 |
| 52 |
\ref {https://tronche.com/gui/x/icccm/sec-4.html#WM_CLASS}{WM_CLASS} |
| 53 |
*/ |
| 54 |
|
| 55 |
/*! |
| 56 |
\property DForeignWindow::pid |
| 57 |
\brief 窗口所属进程的 pid |
| 58 |
\note 只读 |
| 59 |
\ref {https://specifications.freedesktop.org/wm-spec/1.3/ar01s05.html}{_NET_WM_PID} |
| 60 |
*/ |
| 61 |
|
| 62 |
/*! |
| 63 |
\fn void DForeignWindow::wmClassChanged() |
| 64 |
\brief 信号会在 wmClass 属性改变时被发送. |
| 65 |
\sa DForeignWindow::wmClass |
| 66 |
*/ |
| 67 |
|
| 68 |
/*! |
| 69 |
\fn void DForeignWindow::pidChanged() |
| 70 |
\brief 信号会在 pid 属性的值改变时被发送. |
| 71 |
\sa DForeignWindow::pid |
| 72 |
*/ |
| 73 |
|
| 74 |
/*! |
| 75 |
\brief 直接构造一个 DForeignWindow 对象和使用 QWindow 对象没有区别. |
| 76 |
|
| 77 |
\a parent |
| 78 |
\sa DForeignWindow::fromWinId |
| 79 |
*/ |
| 80 |
DForeignWindow::DForeignWindow(QWindow *parent) |
| 81 |
: QWindow(parent) |
| 82 |
, DObject(*new DForeignWindowPrivate(this)) |
| 83 |
{ |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/*! |
| 88 |
\brief DForeignWindow::fromWinId. |
| 89 |
使用这个窗口id创建一个 DForeignWindow 对象,此对象不会被加到 QGuiApplication::allWindows |
| 90 |
中。一般应用在需要获取一个本地窗口信息的场景。示例: |
| 91 |
\code |
| 92 |
// a.cpp |
| 93 |
int main(int argc, char *argv[]) |
| 94 |
{ |
| 95 |
DApplication a(argc, argv); |
| 96 |
|
| 97 |
QWidget w; |
| 98 |
|
| 99 |
w.setWindowTitle("deepin"); |
| 100 |
w.show(); |
| 101 |
|
| 102 |
QFile app_win_id("/tmp/window_id.txt"); |
| 103 |
if (app_win_id.open(QFile::WriteOnly)) { |
| 104 |
app_win_id.write(QByteArray::number(w.winId())); |
| 105 |
app_win_id.close(); |
| 106 |
} |
| 107 |
|
| 108 |
return a.exec(); |
| 109 |
} |
| 110 |
\endcode |
| 111 |
|
| 112 |
\code |
| 113 |
// b.cpp |
| 114 |
int main(int argc, char *argv[]) |
| 115 |
{ |
| 116 |
DApplication::loadDXcbPlugin(); |
| 117 |
DApplication a(argc, argv); |
| 118 |
|
| 119 |
DForeignWindow *fw = nullptr; |
| 120 |
QFile app_win_id("/tmp/window_id.txt"); |
| 121 |
if (app_win_id.open(QFile::ReadOnly)) { |
| 122 |
fw = DForeignWindow::fromWinId(app_win_id.readAll().toInt()); |
| 123 |
} |
| 124 |
|
| 125 |
if (fw) { |
| 126 |
qDebug() << fw->title(); |
| 127 |
|
| 128 |
fw->connect(fw, &DForeignWindow::widthChanged, [&] { |
| 129 |
qDebug() << fw->width(); |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
return a.exec(); |
| 134 |
} |
| 135 |
\endcode |
| 136 |
|
| 137 |
先启动应用 a |
| 138 |
再启动应用 b |
| 139 |
|
| 140 |
在应用 b 启动后将看到如下输出: |
| 141 |
\code |
| 142 |
"deepin" |
| 143 |
\endcode |
| 144 |
当改变应用 a 中的窗口宽度时,在应用 b 中会看到宽度的输出 |
| 145 |
\a id |
| 146 |
\return |
| 147 |
\warning 不要尝试对由本应用创建的窗口调用此接口,可能会导致窗口行为发生不可逆转的变化 |
| 148 |
*/ |
| 149 |
DForeignWindow *DForeignWindow::fromWinId(WId id) |
| 150 |
{ |
| 151 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ForeignWindows)) { |
| 152 |
qWarning() << "DForeignWindow::fromWinId(): platform plugin does not support foreign windows."; |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
DForeignWindow *window = new DForeignWindow; |
| 157 |
window->setFlags(Qt::ForeignWindow); |
| 158 |
window->setProperty("_q_foreignWinId", QVariant::fromValue(id)); |
| 159 |
window->create(); |
| 160 |
return window; |
| 161 |
} |
| 162 |
|
| 163 |
QString DForeignWindow::wmClass() const |
| 164 |
{ |
| 165 |
return property(_WmClass).toString(); |
| 166 |
} |
| 167 |
|
| 168 |
quint32 DForeignWindow::pid() const |
| 169 |
{ |
| 170 |
return qvariant_cast<quint32>(property(_ProcessId)); |
| 171 |
} |
| 172 |
|
| 173 |
bool DForeignWindow::event(QEvent *e) |
| 174 |
{ |
| 175 |
if (e->type() == QEvent::DynamicPropertyChange) { |
| 176 |
QDynamicPropertyChangeEvent *event = static_cast<QDynamicPropertyChangeEvent*>(e); |
| 177 |
|
| 178 |
if (event->propertyName() == _WmClass) { |
| 179 |
Q_EMIT wmClassChanged(); |
| 180 |
|
| 181 |
return true; |
| 182 |
} else if (event->propertyName() == _ProcessId) { |
| 183 |
Q_EMIT pidChanged(); |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
DGUI_END_NAMESPACE |