This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
compositor.cpp
359 lines (305 loc) · 10.7 KB
/
compositor.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "compositor.h"
#include <memory>
#include <QDebug>
#include <QCoreApplication>
#include <QWindow>
#include <QX11Info>
#include <xcb/composite.h>
#include <xcb/xfixes.h>
#include <xcb/xcb_event.h>
#include "clientwindow.h"
#include "windowpixmap.h"
template<typename T>
std::unique_ptr<T, decltype(&std::free)> xcbReply(T *ptr)
{
return std::unique_ptr<T, decltype(&std::free)>(ptr, std::free);
}
Compositor::Compositor()
: connection_(QX11Info::connection()),
root_(QX11Info::appRootWindow()),
damageExt_(xcb_get_extension_data(connection_, &xcb_damage_id)),
initFinished_(false)
{
qRegisterMetaType<ClientWindow *>();
Q_ASSERT(QCoreApplication::instance());
QCoreApplication::instance()->installNativeEventFilter(this);
auto ewmhCookie = xcb_ewmh_init_atoms(connection_, &ewmh_);
if (!xcb_ewmh_init_atoms_replies(&ewmh_, ewmhCookie, Q_NULLPTR)) {
qFatal("Cannot init EWMH");
}
auto wmCmCookie = xcb_ewmh_get_wm_cm_owner_unchecked(&ewmh_, QX11Info::appScreen());
xcb_window_t wmCmOwnerWin = XCB_NONE;
if (!xcb_ewmh_get_wm_cm_owner_reply(&ewmh_, wmCmCookie, &wmCmOwnerWin, Q_NULLPTR)) {
qFatal("Cannot check _NET_WM_CM_Sn");
}
if (wmCmOwnerWin) {
qFatal("Another compositing manager is already running");
}
auto attributesCookie = xcb_get_window_attributes_unchecked(connection_, root_);
auto damageQueryVersionCookie = xcb_damage_query_version_unchecked(connection_, 1, 1);
auto overlayWindowCookie = xcb_composite_get_overlay_window_unchecked(connection_, root_);
auto attributes =
xcbReply(xcb_get_window_attributes_reply(connection_, attributesCookie, Q_NULLPTR));
if (!attributes) {
qFatal("Cannot get root window attributes");
}
auto newEventMask = attributes->your_event_mask
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes(connection_, root_, XCB_CW_EVENT_MASK, &newEventMask);
auto treeCookie = xcb_query_tree_unchecked(connection_, root_);
auto rootGeometryCookie = xcb_get_geometry_unchecked(connection_, root_);
auto damageVersion =
xcbReply(xcb_damage_query_version_reply(connection_, damageQueryVersionCookie, Q_NULLPTR));
if (!damageVersion) {
qFatal("Cannot query version of Damage extension");
}
auto overlayWindow =
xcbReply(xcb_composite_get_overlay_window_reply(connection_, overlayWindowCookie, Q_NULLPTR));
if (!overlayWindow) {
qFatal("Cannot get overlay window");
}
overlayWindow_.reset(QWindow::fromWinId(overlayWindow->overlay_win));
auto region = xcb_generate_id(connection_);
xcb_xfixes_create_region(connection_, region, 0, Q_NULLPTR);
xcb_xfixes_set_window_shape_region(connection_, overlayWindow->overlay_win, XCB_SHAPE_SK_INPUT, 0, 0, region);
xcb_xfixes_destroy_region(connection_, region);
xcb_composite_redirect_subwindows(connection_, root_, XCB_COMPOSITE_REDIRECT_MANUAL);
auto rootGeometry =
xcbReply(xcb_get_geometry_reply(connection_, rootGeometryCookie, Q_NULLPTR));
if (!rootGeometry) {
qFatal("Cannot query root window geometry");
}
rootGeometry_ = QRect(rootGeometry->x, rootGeometry->y, rootGeometry->width, rootGeometry->height);
auto tree = xcbReply(xcb_query_tree_reply(connection_, treeCookie, Q_NULLPTR));
if (!tree) {
qFatal("Cannot query window tree");
}
auto children = xcb_query_tree_children(tree.get());
for (int i = 0; i < xcb_query_tree_children_length(tree.get()); i++) {
addChildWindow(children[i]);
}
updateActiveWindow();
initFinished_ = true;
}
Compositor::~Compositor()
{
xcb_ewmh_connection_wipe(&ewmh_);
}
void Compositor::registerCompositor(QWindow *w)
{
xcb_ewmh_set_wm_cm_owner(&ewmh_, QX11Info::appScreen(), w->winId(), QX11Info::getTimestamp(), 0, 0);
auto wmCmCookie = xcb_ewmh_get_wm_cm_owner_unchecked(&ewmh_, QX11Info::appScreen());
xcb_window_t wmCmOwnerWin = XCB_NONE;
if (!xcb_ewmh_get_wm_cm_owner_reply(&ewmh_, wmCmCookie, &wmCmOwnerWin, Q_NULLPTR)) {
qFatal("Cannot check _NET_WM_CM_Sn");
}
if (wmCmOwnerWin != w->winId()) {
qFatal("Another compositing manager is already running");
}
}
template<typename T>
bool Compositor::xcbDispatchEvent(const T *e, xcb_window_t window)
{
auto i = windows_.constFind(window);
if (i != windows_.constEnd()) {
(*i)->xcbEvent(e);
return true;
}
return false;
}
template<typename T>
bool Compositor::xcbDispatchEvent(const T *e)
{
return xcbDispatchEvent(e, e->window);
}
template<typename T>
bool Compositor::xcbEvent(const T *e)
{
if (e->event != root_) {
return false;
}
return xcbDispatchEvent(e);
}
template<>
bool Compositor::xcbEvent(const xcb_configure_notify_event_t *e)
{
if (e->window == root_) {
QRect newGeometry(0, 0, e->width, e->height);
if (rootGeometry_ != newGeometry) {
rootGeometry_ = newGeometry;
Q_EMIT rootGeometryChanged(rootGeometry_);
}
}
if (e->window != e->event) {
return false;
}
return xcbDispatchEvent(e);
}
template<>
bool Compositor::xcbEvent(const xcb_create_notify_event_t *e)
{
if (e->parent != root_) {
return false;
}
addChildWindow(e->window);
return true;
}
template<>
bool Compositor::xcbEvent(const xcb_destroy_notify_event_t *e)
{
if (e->event != root_) {
return false;
}
removeChildWindow(e->window);
return true;
}
template<>
bool Compositor::xcbEvent(const xcb_reparent_notify_event_t *e)
{
if (e->event != root_) {
return false;
}
if (e->parent == root_) {
addChildWindow(e->window);
} else {
removeChildWindow(e->window);
}
return xcbDispatchEvent(e);
}
template<>
bool Compositor::xcbEvent(const xcb_property_notify_event_t *e)
{
if (e->window == root_) {
if (e->atom == ewmh_._NET_ACTIVE_WINDOW) {
updateActiveWindow();
}
return false;
}
return xcbDispatchEvent(e);
}
bool Compositor::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
Q_ASSERT(eventType == QByteArrayLiteral("xcb_generic_event_t"));
auto responseType = XCB_EVENT_RESPONSE_TYPE(static_cast<xcb_generic_event_t *>(message));
if (responseType == damageExt_->first_event + XCB_DAMAGE_NOTIFY) {
auto e = static_cast<xcb_damage_notify_event_t *>(message);
auto i = pixmaps_.constFind(e->damage);
if (i == pixmaps_.constEnd()) {
return false;
}
(*i)->xcbEvent(e);
return true;
}
switch (responseType) {
case XCB_CREATE_NOTIFY:
return xcbEvent(static_cast<xcb_create_notify_event_t *>(message));
case XCB_DESTROY_NOTIFY:
return xcbEvent(static_cast<xcb_destroy_notify_event_t *>(message));
case XCB_REPARENT_NOTIFY:
return xcbEvent(static_cast<xcb_reparent_notify_event_t *>(message));
case XCB_CONFIGURE_NOTIFY:
return xcbEvent(static_cast<xcb_configure_notify_event_t *>(message));
case XCB_MAP_NOTIFY:
return xcbEvent(static_cast<xcb_map_notify_event_t *>(message));
case XCB_UNMAP_NOTIFY:
return xcbEvent(static_cast<xcb_unmap_notify_event_t *>(message));
case XCB_GRAVITY_NOTIFY:
return xcbEvent(static_cast<xcb_gravity_notify_event_t *>(message));
case XCB_CIRCULATE_NOTIFY:
return xcbEvent(static_cast<xcb_circulate_notify_event_t *>(message));
case XCB_PROPERTY_NOTIFY:
return xcbEvent(static_cast<xcb_property_notify_event_t *>(message));
default:
return false;
}
}
void Compositor::addChildWindow(xcb_window_t window)
{
Q_ASSERT(window != root_);
Q_ASSERT(!overlayWindow_ || window != overlayWindow_->winId());
if (windows_.contains(window)) {
return;
}
QSharedPointer<ClientWindow> w(new ClientWindow(&ewmh_, window)); // TODO: replace with ::create
if (w->isValid() && w->windowClass() != XCB_WINDOW_CLASS_INPUT_ONLY) {
windows_.insert(window, w);
connect(w.data(), SIGNAL(pixmapChanged(WindowPixmap*)), SLOT(registerPixmap(WindowPixmap*)));
connect(w.data(), SIGNAL(stackingOrderChanged()), SLOT(restack()));
restack();
if (initFinished_) {
Q_EMIT windowCreated(w.data());
} else {
QMetaObject::invokeMethod(this, "windowCreated", Qt::QueuedConnection, Q_ARG(ClientWindow*, w.data()));
}
updateActiveWindow();
}
}
void Compositor::removeChildWindow(xcb_window_t window)
{
auto i = windows_.find(window);
if (i == windows_.end()) {
return;
}
(*i)->invalidate();
(*i)->disconnect(this);
windows_.erase(i);
}
void Compositor::registerPixmap(WindowPixmap *pixmap)
{
if (pixmap->isValid()) {
connect(pixmap, SIGNAL(destroyed(WindowPixmap*)),
SLOT(unregisterPixmap(WindowPixmap*)), Qt::DirectConnection);
pixmaps_.insert(pixmap->damage(), pixmap);
}
}
void Compositor::unregisterPixmap(WindowPixmap *pixmap)
{
pixmaps_.remove(pixmap->damage());
}
void Compositor::restack() // TODO: maintain stacking order somehow
{
auto treeCookie = xcb_query_tree_unchecked(connection_, root_);
auto tree = xcbReply(xcb_query_tree_reply(connection_, treeCookie, Q_NULLPTR));
auto children = xcb_query_tree_children(tree.get());
for (int i = 0; i < xcb_query_tree_children_length(tree.get()); i++) {
auto w = windows_.constFind(children[i]);
if (w != windows_.constEnd()) {
(*w)->setZIndex(i);
if (i) {
(*w)->setAbove(children[i - 1]);
} else {
(*w)->setAbove(XCB_NONE);
}
}
}
}
QSharedPointer<ClientWindow> Compositor::findTopLevel(xcb_window_t subWindow)
{
while (subWindow && subWindow != root_) {
auto found = windows_.constFind(subWindow);
if (found != windows_.constEnd()) {
return *found;
}
auto cookie = xcb_query_tree(connection_, subWindow);
auto tree = xcb_query_tree_reply(connection_, cookie, Q_NULLPTR);
subWindow = tree->parent;
std::free(tree);
}
return QSharedPointer<ClientWindow>();
}
void Compositor::updateActiveWindow()
{
auto cookie = xcb_ewmh_get_active_window_unchecked(&ewmh_, QX11Info::appScreen());
xcb_window_t activeWindow = XCB_NONE;
if (!xcb_ewmh_get_active_window_reply(&ewmh_, cookie, &activeWindow, Q_NULLPTR)) {
return;
}
auto newActiveWindow = findTopLevel(activeWindow);
if (activeWindow_ == newActiveWindow) {
return;
}
activeWindow_ = newActiveWindow;
Q_EMIT activeWindowChanged();
}