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
/
clientwindow.cpp
260 lines (221 loc) · 6.89 KB
/
clientwindow.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
#include "clientwindow.h"
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include "windowpixmap.h"
class XcbServerGrab
{
public:
explicit XcbServerGrab(xcb_connection_t *connection)
: connection(connection)
{
xcb_grab_server(connection);
}
~XcbServerGrab()
{
xcb_ungrab_server(connection);
xcb_flush(connection);
}
private:
Q_DISABLE_COPY(XcbServerGrab)
xcb_connection_t *connection;
};
ClientWindow::ClientWindow(xcb_ewmh_connection_t *ewmh, xcb_window_t window, QObject *parent)
: QObject(parent),
connection_(ewmh->connection),
ewmh_(ewmh),
window_(window),
windowClass_(XCB_WINDOW_CLASS_COPY_FROM_PARENT),
valid_(false),
mapped_(false),
pixmapRealloc_(true),
above_(XCB_NONE),
overrideRedirect_(false),
transientFor_(XCB_NONE),
wmType_(XCB_NONE)
{
XcbServerGrab grab(connection_);
auto attributesCookie = xcb_get_window_attributes(connection_, window_);
auto attributes = xcb_get_window_attributes_reply(connection_, attributesCookie, Q_NULLPTR);
if (!attributes) {
return;
}
attributes->your_event_mask = attributes->your_event_mask
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes(connection_, window_, XCB_CW_EVENT_MASK, &attributes->your_event_mask);
auto geometryCookie = xcb_get_geometry_unchecked(connection_, window_);
auto transientCookie = xcb_icccm_get_wm_transient_for_unchecked(connection_, window_);
auto wmTypeCookie = xcb_ewmh_get_wm_window_type_unchecked(ewmh_, window_);
auto geometry = xcb_get_geometry_reply(connection_, geometryCookie, Q_NULLPTR);
xcb_icccm_get_wm_transient_for_reply(connection_, transientCookie, &transientFor_, Q_NULLPTR);
xcb_ewmh_get_atoms_reply_t wmType = {0};
xcb_ewmh_get_wm_window_type_reply(ewmh_, wmTypeCookie, &wmType, Q_NULLPTR);
if (wmType.atoms_len > 0) {
wmType_ = wmType.atoms[0];
}
xcb_ewmh_get_atoms_reply_wipe(&wmType);
if (!geometry) {
std::free(attributes);
std::free(geometry);
return;
}
valid_ = true;
windowClass_ = static_cast<xcb_window_class_t>(attributes->_class);
geometry_ = QRect(geometry->x, geometry->y, geometry->width, geometry->height);
mapped_ = (attributes->map_state == XCB_MAP_STATE_VIEWABLE);
overrideRedirect_ = attributes->override_redirect;
std::free(attributes);
std::free(geometry);
}
ClientWindow::~ClientWindow()
{
}
const QSharedPointer<WindowPixmap> &ClientWindow::pixmap()
{
if (!pixmapRealloc_ || !mapped_) {
return pixmap_;
}
pixmapRealloc_ = false;
XcbServerGrab grab(connection_);
QSharedPointer<WindowPixmap> newPixmap(new WindowPixmap(connection_, window_)); // TODO: replace with ::create
if (newPixmap->isValid()) {
if (newPixmap->thread() != thread()) { // This method is called from render thread
newPixmap->moveToThread(thread());
}
pixmap_ = newPixmap;
Q_EMIT pixmapChanged(pixmap_.data());
}
return pixmap_;
}
ClientWindow::WmType ClientWindow::wmType() const
{
if (wmType_ == XCB_NONE) {
return NONE;
}
#define DETECT_WM_TYPE(x) \
if (wmType_ == ewmh_->_NET_WM_WINDOW_TYPE_##x) { \
return x; \
}
DETECT_WM_TYPE(DESKTOP);
DETECT_WM_TYPE(DOCK);
DETECT_WM_TYPE(TOOLBAR);
DETECT_WM_TYPE(MENU);
DETECT_WM_TYPE(UTILITY);
DETECT_WM_TYPE(SPLASH);
DETECT_WM_TYPE(DIALOG);
DETECT_WM_TYPE(DROPDOWN_MENU);
DETECT_WM_TYPE(POPUP_MENU);
DETECT_WM_TYPE(TOOLTIP);
DETECT_WM_TYPE(NOTIFICATION);
DETECT_WM_TYPE(COMBO);
DETECT_WM_TYPE(DND);
DETECT_WM_TYPE(NORMAL);
return UNKNOWN;
}
void ClientWindow::invalidate()
{
if (valid_) {
valid_ = false;
Q_EMIT invalidated();
}
}
void ClientWindow::setGeometry(const QRect &geometry)
{
if (geometry_ != geometry) {
if (geometry.size() != geometry_.size()) {
pixmapRealloc_ = true;
}
geometry_ = geometry;
Q_EMIT geometryChanged(geometry);
}
}
void ClientWindow::setMapped(bool mapped)
{
if (mapped_ != mapped) {
mapped_ = mapped;
Q_EMIT mapStateChanged(mapped);
}
}
void ClientWindow::setOverrideRedirect(bool overrideRedirect)
{
if (overrideRedirect_ != overrideRedirect) {
overrideRedirect_ = overrideRedirect;
Q_EMIT overrideRedirectChanged(overrideRedirect);
}
}
void ClientWindow::xcbEvent(const xcb_configure_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
setGeometry(QRect(e->x, e->y, e->width, e->height));
setOverrideRedirect(e->override_redirect);
if (e->above_sibling != above_) {
Q_EMIT stackingOrderChanged();
}
}
void ClientWindow::xcbEvent(const xcb_map_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
pixmapRealloc_ = true;
setOverrideRedirect(e->override_redirect);
setMapped(true);
}
void ClientWindow::xcbEvent(const xcb_unmap_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
setMapped(false);
}
void ClientWindow::xcbEvent(const xcb_reparent_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
setGeometry(QRect(e->x, e->y, geometry_.width(), geometry_.height()));
setOverrideRedirect(e->override_redirect);
}
void ClientWindow::xcbEvent(const xcb_gravity_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
setGeometry(QRect(e->x, e->y, geometry_.width(), geometry_.height()));
}
void ClientWindow::xcbEvent(const xcb_circulate_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
Q_EMIT stackingOrderChanged();
}
void ClientWindow::updateTransientFor()
{
auto oldIsTransient = isTransient();
auto oldTransientFor = transientFor_;
auto cookie = xcb_icccm_get_wm_transient_for(connection_, window_);
xcb_icccm_get_wm_transient_for_reply(connection_, cookie, &transientFor_, Q_NULLPTR);
if (oldTransientFor != transientFor_) {
Q_EMIT transientForChanged();
}
if (oldIsTransient != isTransient()) {
Q_EMIT transientChanged(isTransient());
}
}
void ClientWindow::updateWmType()
{
auto wmTypeCookie = xcb_ewmh_get_wm_window_type(ewmh_, window_);
xcb_ewmh_get_atoms_reply_t wmTypeReply = {0};
if (!xcb_ewmh_get_wm_window_type_reply(ewmh_, wmTypeCookie, &wmTypeReply, Q_NULLPTR)) {
return;
}
xcb_atom_t newWmType = wmType_;
if (wmTypeReply.atoms_len > 0) {
newWmType = wmTypeReply.atoms[0];
}
xcb_ewmh_get_atoms_reply_wipe(&wmTypeReply);
if (newWmType != wmType_) {
wmType_ = newWmType;
Q_EMIT wmTypeChanged(wmType());
}
}
void ClientWindow::xcbEvent(const xcb_property_notify_event_t *e)
{
Q_ASSERT(e->window == window_);
if (e->atom == XCB_ATOM_WM_TRANSIENT_FOR) {
updateTransientFor();
} else if (e->atom == ewmh_->_NET_WM_WINDOW_TYPE) {
updateWmType();
}
}