forked from linuxdeepin/dde-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx11windowmonitor.cpp
More file actions
246 lines (208 loc) · 7.88 KB
/
x11windowmonitor.cpp
File metadata and controls
246 lines (208 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "x11utils.h"
#include "x11window.h"
#include "x11preview.h"
#include "abstractwindow.h"
#include "x11windowmonitor.h"
#include "abstractwindowmonitor.h"
#include <cstdint>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <DDBusSender>
#include <QPointer>
#include <QWindow>
#include <QGuiApplication>
#include <QLoggingCategory>
#define X11 X11Utils::instance()
Q_LOGGING_CATEGORY(x11Log, "dde.shell.dock.taskmanager.x11windowmonitor")
namespace dock {
static QPointer<X11WindowMonitor> monitor;
bool XcbEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *)
{
if (eventType != "xcb_generic_event_t" || monitor.isNull())
return false;
auto xcb_event = reinterpret_cast<xcb_generic_event_t*>(message);
switch (xcb_event->response_type) {
case XCB_PROPERTY_NOTIFY: {
auto pE = reinterpret_cast<xcb_property_notify_event_t*>(xcb_event);
Q_EMIT monitor->windowPropertyChanged(pE->window, pE->atom);
break;
}
}
return false;
};
X11WindowMonitor::X11WindowMonitor(QObject* parent)
: AbstractWindowMonitor(parent)
, m_opacity(0.2)
{
monitor = this;
connect(this, &X11WindowMonitor::windowMapped, this, &X11WindowMonitor::onWindowMapped);
connect(this, &X11WindowMonitor::windowDestroyed, this, &X11WindowMonitor::onWindowDestroyed);
connect(this, &X11WindowMonitor::windowPropertyChanged, this, &X11WindowMonitor::onWindowPropertyChanged);
}
void X11WindowMonitor::start()
{
const xcb_setup_t *setup = xcb_get_setup(X11->getXcbConnection());
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_screen_t *screen = iter.data;
m_rootWindow = screen->root;
uint32_t value_list[] = {
0 | XCB_EVENT_MASK_PROPERTY_CHANGE |
XCB_EVENT_MASK_VISIBILITY_CHANGE | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_FOCUS_CHANGE
};
xcb_change_window_attributes(X11->getXcbConnection(), m_rootWindow, XCB_CW_EVENT_MASK, value_list);
xcb_flush(X11->getXcbConnection());
m_xcbEventFilter.reset(new XcbEventFilter());
qApp->installNativeEventFilter(m_xcbEventFilter.get());
QMetaObject::invokeMethod(this, &X11WindowMonitor::handleRootWindowClientListChanged);
}
void X11WindowMonitor::stop()
{
qApp->removeNativeEventFilter(m_xcbEventFilter.get());
m_xcbEventFilter.reset(nullptr);
Q_EMIT AbstractWindowMonitor::WindowMonitorShutdown();
}
void X11WindowMonitor::clear()
{
m_windows.clear();
m_windowPreview.reset(nullptr);
}
QPointer<AbstractWindow> X11WindowMonitor::getWindowByWindowId(ulong windowId)
{
return m_windows.value(windowId).get();
}
void X11WindowMonitor::presentWindows(QList<uint32_t> windows)
{
DDBusSender().interface("com.deepin.wm")
.path("/com/deepin/wm")
.service("com.deepin.wm")
.method("PresentWindows")
.arg(windows)
.call().waitForFinished();
}
void X11WindowMonitor::hideItemPreview()
{
if (m_windowPreview.isNull()) return;
m_windowPreview->hidePreView();
}
void X11WindowMonitor::previewWindow(uint32_t winId)
{
DDBusSender().interface("com.deepin.wm")
.path("/com/deepin/wm")
.service("com.deepin.wm")
.method("PreviewWindow")
.arg(winId)
.call().waitForFinished();
}
void X11WindowMonitor::cancelPreviewWindow()
{
DDBusSender().interface("com.deepin.wm")
.path("/com/deepin/wm")
.service("com.deepin.wm")
.method("CancelPreviewWindow")
.call().waitForFinished();
}
void X11WindowMonitor::setPreviewOpacity(double opacity)
{
m_opacity = opacity;
if (!m_windowPreview.isNull()) {
m_windowPreview->setMaskAlpha(static_cast<int>(m_opacity * 255));
}
}
void X11WindowMonitor::requestPreview(QAbstractItemModel *sourceModel,
QWindow *relativePositionItem,
int32_t previewXoffset,
int32_t previewYoffset,
uint32_t direction)
{
if (m_windowPreview.isNull()) {
m_windowPreview.reset(new X11WindowPreviewContainer(this));
m_windowPreview->setMaskAlpha(static_cast<int>(m_opacity * 255));
m_windowPreview->windowHandle()->setTransientParent(relativePositionItem);
}
m_windowPreview->showPreviewWithModel(sourceModel, relativePositionItem, previewXoffset, previewYoffset, direction);
m_windowPreview->updatePosition();
}
void X11WindowMonitor::requestUpdateWindowIconGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate) const
{
xcb_window_t winId = index.data(TaskManager::WinIdRole).toUInt();
auto pos = qobject_cast<QWindow *>(delegate)->position() + geometry.topLeft();
X11->setWindowIconGemeotry(winId, QRect(pos.x(), pos.y(), geometry.width(), geometry.height()));
}
void X11WindowMonitor::clearPreviewState()
{
// 发出信号通知 TaskManager 清空预览过滤状态
emit previewShouldClear();
}
void X11WindowMonitor::onWindowMapped(xcb_window_t xcb_window)
{
auto window = m_windows.value(xcb_window, nullptr);
if (window) return;
window = QSharedPointer<X11Window>{new X11Window(xcb_window, this)};
m_windows.insert(xcb_window, window);
if (window->pid() == qApp->applicationPid()) return;
uint32_t value_list[] = { XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_VISIBILITY_CHANGE};
xcb_change_window_attributes(X11->getXcbConnection(), xcb_window, XCB_CW_EVENT_MASK, value_list);
trackWindow(window.get());
Q_EMIT AbstractWindowMonitor::windowAdded(static_cast<QPointer<AbstractWindow>>(window.get()));
}
void X11WindowMonitor::onWindowDestroyed(xcb_window_t xcb_window)
{
auto window = m_windows.value(xcb_window, nullptr);
if (window) {
destroyWindow(window.get());
m_windows.remove(xcb_window);
}
}
void X11WindowMonitor::onWindowPropertyChanged(xcb_window_t window, xcb_atom_t atom)
{
if (window == m_rootWindow) {
handleRootWindowPropertyNotifyEvent(atom);
return;
}
auto x11Window = m_windows.value(window);
if (!x11Window) {
return;
}
if (atom == X11->getAtomByName("_NET_WM_STATE")) {
x11Window->updateWindowState();
}else if (atom == X11->getAtomByName("_NET_WM_PID")) {
x11Window->updatePid();
} else if (atom == X11->getAtomByName("_NET_WM_NAME")) {
x11Window->updateTitle();
} else if (atom == X11->getAtomByName("_NET_WM_ICON")) {
x11Window->updateIcon();
} else if (atom == X11->getAtomByName("_NET_WM_ALLOWED_ACTIONS")) {
x11Window->updateWindowAllowedActions();
} else if (atom == X11->getAtomByName("_NET_WM_WINDOW_TYPE")) {
x11Window->updateWindowTypes();
} else if (atom == X11->getAtomByName("_MOTIF_WM_HINTS")) {
x11Window->updateMotifWmHints();
} else if (atom == X11->getAtomByName("WM_CLASS")) {
x11Window->updateIdentify();
}
}
void X11WindowMonitor::handleRootWindowPropertyNotifyEvent(xcb_atom_t atom)
{
if (atom == X11->getAtomByName("_NET_CLIENT_LIST")) {
handleRootWindowClientListChanged();
}
}
void X11WindowMonitor::handleRootWindowClientListChanged()
{
auto currentOpenedWindowList = X11->getWindowClientList(m_rootWindow);
for (auto openedWindows : currentOpenedWindowList) {
if (!m_windows.contains(openedWindows)) {
onWindowMapped(openedWindows);
}
}
for (auto alreadyOpenedWindow : m_windows.keys()) {
if (!currentOpenedWindowList.contains(alreadyOpenedWindow)) {
Q_EMIT windowDestroyed(alreadyOpenedWindow);
}
}
}
}