-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathx11support.cpp
382 lines (329 loc) · 11.3 KB
/
x11support.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "x11support.h"
#include <QtGui/QApplication>
#include <QtGui/QX11Info>
#include <QtGui/QImage>
// Keep all the X11 stuff with scary defines below normal headers.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
static XErrorHandler oldX11ErrorHandler = NULL;
static int x11errorHandler(Display* display, XErrorEvent* error)
{
if(error->error_code == BadWindow)
return 0; // This usually happens when querying property on a window that's already gone. That's OK.
return (*oldX11ErrorHandler)(display, error);
}
X11Support* X11Support::m_instance = NULL;
X11Support::X11Support()
{
m_instance = this;
oldX11ErrorHandler = XSetErrorHandler(x11errorHandler);
int damageErrorBase;
XDamageQueryExtension(QX11Info::display(), &m_damageEventBase, &damageErrorBase);
}
X11Support::~X11Support()
{
m_instance = NULL;
}
void X11Support::onX11Event(XEvent* event)
{
if(event->type == m_damageEventBase + XDamageNotify)
{
// Repair damaged area.
XDamageNotifyEvent* damageEvent = reinterpret_cast<XDamageNotifyEvent*>(event);
XDamageSubtract(QX11Info::display(), damageEvent->damage, None, None);
emit windowDamaged(event->xany.window);
}
if(event->type == DestroyNotify)
emit windowClosed(event->xdestroywindow.window);
if(event->type == ConfigureNotify)
emit windowReconfigured(event->xconfigure.window, event->xconfigure.x, event->xconfigure.y, event->xconfigure.width, event->xconfigure.height);
if(event->type == PropertyNotify)
emit windowPropertyChanged(event->xproperty.window, event->xproperty.atom);
if(event->type == ClientMessage)
emit clientMessageReceived(event->xclient.window, event->xclient.message_type, event->xclient.data.b);
}
unsigned long X11Support::rootWindow()
{
return QX11Info::appRootWindow();
}
unsigned long X11Support::atom(const QString& name)
{
if(!m_instance->m_cachedAtoms.contains(name))
m_instance->m_cachedAtoms[name] = XInternAtom(QX11Info::display(), name.toLatin1().data(), False);
return m_instance->m_cachedAtoms[name];
}
void X11Support::removeWindowProperty(unsigned long window, const QString& name)
{
XDeleteProperty(QX11Info::display(), window, atom(name));
}
void X11Support::setWindowPropertyCardinalArray(unsigned long window, const QString& name, const QVector<unsigned long>& values)
{
XChangeProperty(QX11Info::display(), window, atom(name), XA_CARDINAL, 32, PropModeReplace, reinterpret_cast<const unsigned char*>(values.data()), values.size());
}
void X11Support::setWindowPropertyCardinal(unsigned long window, const QString& name, unsigned long value)
{
XChangeProperty(QX11Info::display(), window, atom(name), XA_CARDINAL, 32, PropModeReplace, reinterpret_cast<const unsigned char*>(&value), 1);
}
void X11Support::setWindowPropertyVisualId(unsigned long window, const QString& name, unsigned long value)
{
XChangeProperty(QX11Info::display(), window, atom(name), XA_VISUALID, 32, PropModeReplace, reinterpret_cast<const unsigned char*>(&value), 1);
}
template<class T>
static bool getWindowPropertyHelper(unsigned long window, unsigned long atom, unsigned long type, int& numItems, T*& data)
{
Atom retType;
int retFormat;
unsigned long numItemsTemp;
unsigned long bytesLeft;
if(XGetWindowProperty(QX11Info::display(), window, atom, 0, 0x7FFFFFFF, False, type, &retType, &retFormat, &numItemsTemp, &bytesLeft, reinterpret_cast<unsigned char**>(&data)) != Success)
return false;
numItems = numItemsTemp;
if(numItems == 0)
return false;
return true;
}
unsigned long X11Support::getWindowPropertyCardinal(unsigned long window, const QString& name)
{
int numItems;
unsigned long* data;
unsigned long value = 0;
if(!getWindowPropertyHelper(window, atom(name), XA_CARDINAL, numItems, data))
return value;
value = data[0];
XFree(data);
return value;
}
unsigned long X11Support::getWindowPropertyWindow(unsigned long window, const QString& name)
{
int numItems;
unsigned long* data;
unsigned long value = 0;
if(!getWindowPropertyHelper(window, atom(name), XA_WINDOW, numItems, data))
return value;
value = data[0];
XFree(data);
return value;
}
QVector<unsigned long> X11Support::getWindowPropertyWindowsArray(unsigned long window, const QString& name)
{
int numItems;
unsigned long* data;
QVector<unsigned long> values;
if(!getWindowPropertyHelper(window, atom(name), XA_WINDOW, numItems, data))
return values;
for(int i = 0; i < numItems; i++)
values.append(data[i]);
XFree(data);
return values;
}
QVector<unsigned long> X11Support::getWindowPropertyAtomsArray(unsigned long window, const QString& name)
{
int numItems;
unsigned long* data;
QVector<unsigned long> values;
if(!getWindowPropertyHelper(window, atom(name), XA_ATOM, numItems, data))
return values;
for(int i = 0; i < numItems; i++)
values.append(data[i]);
XFree(data);
return values;
}
QString X11Support::getWindowPropertyUTF8String(unsigned long window, const QString& name)
{
int numItems;
char* data;
QString value;
if(!getWindowPropertyHelper(window, atom(name), atom("UTF8_STRING"), numItems, data))
return value;
value = QString::fromUtf8(data);
XFree(data);
return value;
}
QString X11Support::getWindowPropertyLatin1String(unsigned long window, const QString& name)
{
int numItems;
char* data;
QString value;
if(!getWindowPropertyHelper(window, atom(name), XA_STRING, numItems, data))
return value;
value = QString::fromLatin1(data);
XFree(data);
return value;
}
QString X11Support::getWindowName(unsigned long window)
{
QString result = getWindowPropertyUTF8String(window, "_NET_WM_VISIBLE_NAME");
if(result.isEmpty())
result = getWindowPropertyUTF8String(window, "_NET_WM_NAME");
if(result.isEmpty())
result = getWindowPropertyLatin1String(window, "WM_NAME");
if(result.isEmpty())
result = "<Unknown>";
return result;
}
QIcon X11Support::getWindowIcon(unsigned long window)
{
int numItems;
unsigned long* rawData;
QIcon icon;
if(!getWindowPropertyHelper(window, atom("_NET_WM_ICON"), XA_CARDINAL, numItems, rawData))
return icon;
unsigned long* data = rawData;
while(numItems > 0)
{
int width = static_cast<int>(data[0]);
int height = static_cast<int>(data[1]);
data += 2;
numItems -= 2;
QImage image(width, height, QImage::Format_ARGB32);
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width; k++)
{
image.setPixel(k, i, static_cast<unsigned int>(data[i*width + k]));
}
}
data += width*height;
numItems -= width*height;
icon.addPixmap(QPixmap::fromImage(image));
}
XFree(rawData);
return icon;
}
bool X11Support::getWindowUrgency(unsigned long window)
{
XWMHints* hints = XGetWMHints(QX11Info::display(), window);
if(hints == NULL)
return false;
bool isUrgent = (hints->flags & 256) != 0; // UrgencyHint
XFree(hints);
return isUrgent;
}
void X11Support::registerForWindowPropertyChanges(unsigned long window)
{
XSelectInput(QX11Info::display(), window, PropertyChangeMask);
}
void X11Support::registerForTrayIconUpdates(unsigned long window)
{
XSelectInput(QX11Info::display(), window, StructureNotifyMask);
// Apparently, there is no need to destroy damage object, as it's gone automatically when window is destroyed.
XDamageCreate(QX11Info::display(), window, XDamageReportNonEmpty);
}
static void sendNETWMMessage(unsigned long window, const QString& atomName, unsigned long l0 = 0, unsigned long l1 = 0, unsigned long l2 = 0, unsigned long l3 = 0, unsigned long l4 = 0)
{
XClientMessageEvent event;
event.type = ClientMessage;
event.window = window;
event.message_type = X11Support::atom(atomName);
event.format = 32;
event.data.l[0] = l0;
event.data.l[1] = l1;
event.data.l[2] = l2;
event.data.l[3] = l3;
event.data.l[4] = l4;
XSendEvent(QX11Info::display(), X11Support::rootWindow(), False, SubstructureNotifyMask | SubstructureRedirectMask, reinterpret_cast<XEvent*>(&event));
}
void X11Support::activateWindow(unsigned long window)
{
XWindowChanges wc;
wc.stack_mode = Above;
XConfigureWindow(QX11Info::display(), window, CWStackMode, &wc);
// Apparently, KWin won't bring window to top with configure request,
// so we also need to ask it politely by sending a message.
sendNETWMMessage(window, "_NET_ACTIVE_WINDOW", 2, CurrentTime);
}
void X11Support::minimizeWindow(unsigned long window)
{
XIconifyWindow(QX11Info::display(), window, QX11Info::appScreen());
}
void X11Support::closeWindow(unsigned long window)
{
sendNETWMMessage(window, "_NET_CLOSE_WINDOW", CurrentTime, 2);
}
void X11Support::destroyWindow(unsigned long window)
{
XDestroyWindow(QX11Info::display(), window);
}
void X11Support::killClient(unsigned long window)
{
XKillClient(QX11Info::display(), window);
}
unsigned long X11Support::systemTrayAtom()
{
return atom(QString("_NET_SYSTEM_TRAY_S") + QString::number(QX11Info::appScreen()));
}
bool X11Support::makeSystemTray(unsigned long window)
{
if(XGetSelectionOwner(QX11Info::display(), systemTrayAtom()) != 0)
return false;
XSetSelectionOwner(QX11Info::display(), systemTrayAtom(), window, CurrentTime);
setWindowPropertyVisualId(window, "_NET_SYSTEM_TRAY_VISUAL", getARGBVisualId());
XSync(QX11Info::display(), False);
// Inform other clients.
XClientMessageEvent event;
event.type = ClientMessage;
event.window = rootWindow();
event.message_type = atom("MANAGER");
event.format = 32;
event.data.l[0] = CurrentTime;
event.data.l[1] = systemTrayAtom();
event.data.l[2] = window;
event.data.l[3] = 0;
event.data.l[4] = 0;
XSendEvent(QX11Info::display(), X11Support::rootWindow(), False, StructureNotifyMask, reinterpret_cast<XEvent*>(&event));
return true;
}
void X11Support::freeSystemTray()
{
XSetSelectionOwner(QX11Info::display(), systemTrayAtom(), None, CurrentTime);
}
unsigned long X11Support::getARGBVisualId()
{
XVisualInfo visualInfoTemplate;
visualInfoTemplate.screen = QX11Info::appScreen();
visualInfoTemplate.depth = 32;
visualInfoTemplate.red_mask = 0x00FF0000;
visualInfoTemplate.green_mask = 0x0000FF00;
visualInfoTemplate.blue_mask = 0x000000FF;
int numVisuals;
XVisualInfo* visualInfoList = XGetVisualInfo(QX11Info::display(), VisualScreenMask | VisualDepthMask | VisualRedMaskMask | VisualGreenMaskMask | VisualBlueMaskMask, &visualInfoTemplate, &numVisuals);
unsigned long id = visualInfoList[0].visualid;
XFree(visualInfoList);
return id;
}
void X11Support::redirectWindow(unsigned long window)
{
XCompositeRedirectWindow(QX11Info::display(), window, CompositeRedirectManual);
}
void X11Support::unredirectWindow(unsigned long window)
{
XCompositeUnredirectWindow(QX11Info::display(), window, CompositeRedirectManual);
}
QPixmap X11Support::getWindowPixmap(unsigned long window)
{
return QPixmap::fromX11Pixmap(XCompositeNameWindowPixmap(QX11Info::display(), window));
}
void X11Support::resizeWindow(unsigned long window, int width, int height)
{
XResizeWindow(QX11Info::display(), window, width, height);
}
void X11Support::moveWindow(unsigned long window, int x, int y)
{
XMoveWindow(QX11Info::display(), window, x, y);
}
void X11Support::mapWindow(unsigned long window)
{
XMapWindow(QX11Info::display(), window);
}
void X11Support::reparentWindow(unsigned long window, unsigned long parent)
{
XReparentWindow(QX11Info::display(), window, parent, 0, 0);
XSync(QX11Info::display(), False);
}
void X11Support::setWindowBackgroundBlack(unsigned long window)
{
XSetWindowBackground(QX11Info::display(), window, BlackPixel(QX11Info::display(), QX11Info::appScreen()));
}