forked from linuxdeepin/dde-tray-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
423 lines (355 loc) · 13.1 KB
/
Copy pathutil.cpp
File metadata and controls
423 lines (355 loc) · 13.1 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QLoggingCategory>
#include "util.h"
#include <QSize>
#include <QPixmap>
#include <QBitmap>
#include <QFileInfo>
#include <QtGlobal>
#include <QSocketNotifier>
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <X11/Xlib.h>
#include <mutex>
#include <xcb/res.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xtest.h>
#include <xcb/xproto.h>
#include <xcb/composite.h>
Q_LOGGING_CATEGORY(TRAYUTIL, "org.deepin.dde.trayloader.util")
namespace tray {
void clean_xcb_image(void *data)
{
xcb_image_destroy(static_cast<xcb_image_t *>(data));
}
Util* Util::instance()
{
static Util* _instance = nullptr;
if (_instance == nullptr)
_instance = new Util();
return _instance;
}
void Util::dispatchEvents(DispatchEventsMode mode)
{
xcb_connection_t *connection = m_x11connection;
if (!connection) {
qCWarning(TRAYUTIL, "Attempting to dispatch X11 events with no connection");
return;
}
auto pollEventFunc = mode == DispatchEventsMode::Poll ? xcb_poll_for_event : xcb_poll_for_queued_event;
while (xcb_generic_event_t *event = pollEventFunc(connection)) {
qintptr result = 0;
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
dispatcher->filterNativeEvent(QByteArrayLiteral("xcb_generic_event_t"), event, &result);
free(event);
}
xcb_flush(connection);
}
Util::Util()
: QObject()
{
m_x11connection = xcb_connect(nullptr, nullptr);
m_display = XOpenDisplay("");
if (!m_x11connection || !isXAvaliable()) {
return;
}
const xcb_setup_t *setup = xcb_get_setup(m_x11connection);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_screen_t* screen = iter.data;
m_rootWindow = screen->root;
xcb_ewmh_init_atoms_replies(&m_ewmh, xcb_ewmh_init_atoms(m_x11connection, &m_ewmh), nullptr);
const int fd = xcb_get_file_descriptor(m_x11connection);
QSocketNotifier * qfd = new QSocketNotifier(fd, QSocketNotifier::Read, this);
connect(qfd, &QSocketNotifier::activated, this, [this](){
dispatchEvents(DispatchEventsMode::Poll);
});
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, [this]() {
dispatchEvents(DispatchEventsMode::EventQueue);
});
connect(dispatcher, &QAbstractEventDispatcher::awake, this, [this]() {
dispatchEvents(DispatchEventsMode::EventQueue);
});
}
Util::~Util()
{
}
bool Util::isXAvaliable()
{
static std::once_flag flag;
static bool avaliable = false;
std::call_once(flag, [this](){
if (!(m_x11connection && m_display)) return;
// xtest support
const xcb_query_extension_reply_t *xtest_ext_reply;
xtest_ext_reply = xcb_get_extension_data(m_x11connection, &xcb_test_id);
// xshape support
const xcb_query_extension_reply_t *xshape_ext_reply;
xshape_ext_reply = xcb_get_extension_data(m_x11connection, &xcb_shape_id);
// xewmh support
xcb_ewmh_connection_t ewmh;
xcb_intern_atom_cookie_t *ewmh_cookie = xcb_ewmh_init_atoms(m_x11connection, &ewmh);
if (!ewmh_cookie) return;
xcb_ewmh_init_atoms_replies(&ewmh, ewmh_cookie, NULL);
avaliable = m_x11connection && m_display &&
(xtest_ext_reply && xtest_ext_reply->present) &&
(xshape_ext_reply && xshape_ext_reply->present) &&
(ewmh._NET_WM_STATE && ewmh._NET_WM_WINDOW_TYPE);
});
return avaliable;
}
xcb_connection_t* Util::getX11Connection()
{
return m_x11connection;
}
xcb_window_t Util::getRootWindow()
{
return m_rootWindow;
}
_XDisplay* Util::getDisplay()
{
return m_display;
}
xcb_atom_t Util::getAtomByName(const QString &name)
{
xcb_atom_t ret = m_atoms.value(name, 0);
if (!ret) {
xcb_intern_atom_cookie_t cookie = xcb_intern_atom(m_x11connection, false, name.size(), name.toStdString().c_str());
QSharedPointer<xcb_intern_atom_reply_t> reply(xcb_intern_atom_reply(m_x11connection, cookie, nullptr), [=](xcb_intern_atom_reply_t* reply){
free(reply);}
);
if (reply) {
m_atoms.insert(name, xcb_atom_t(reply->atom));
ret = reply->atom;
}
}
return ret;
}
QString Util::getNameByAtom(const xcb_atom_t& atom)
{
auto name = m_atoms.key(atom);
if (name.isEmpty()) {
xcb_get_atom_name_cookie_t cookie = xcb_get_atom_name(m_x11connection, atom);
QSharedPointer<xcb_get_atom_name_reply_t> reply(
xcb_get_atom_name_reply(m_x11connection, cookie, nullptr),
[=](xcb_get_atom_name_reply_t* reply) {free(reply);});
if (!reply) {
return name;
}
std::string tmp;
tmp.assign(xcb_get_atom_name_name(reply.get()), xcb_get_atom_name_name_length(reply.get()));
name = tmp.c_str();
if (!name.isEmpty()) {
m_atoms.insert(name, atom);
}
}
return name;
}
xcb_atom_t Util::getAtomFromDisplay(const char * name)
{
return getAtomByName(xcb_atom_name_by_screen(name, DefaultScreen(getDisplay())));
}
void Util::moveX11Window(const xcb_window_t& window, const uint32_t& x, const uint32_t& y)
{
const uint32_t windowMoveConfigVals[2] = {x, y};
xcb_configure_window(m_x11connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, windowMoveConfigVals);
xcb_flush(m_x11connection);
}
void Util::setX11WindowSize(const xcb_window_t& window, const QSize& size)
{
const int windowSizeConfigVals[2] = {size.width(), size.height()};
xcb_configure_window(m_x11connection, window, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, windowSizeConfigVals);
xcb_flush(m_x11connection);
}
QRect Util::getX11WindowGeometry(const xcb_window_t& window) const
{
auto cookie = xcb_get_geometry(m_x11connection, window);
QSharedPointer<xcb_get_geometry_reply_t> clientGeom(xcb_get_geometry_reply(m_x11connection, cookie, nullptr));
return clientGeom ? QRect(clientGeom->x, clientGeom->y, clientGeom->width, clientGeom->height) : QRect();
}
QString Util::getX11WindowName(const xcb_window_t& window)
{
std::string ret;
xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name(&m_ewmh, window);
xcb_ewmh_get_utf8_strings_reply_t reply;
if (xcb_ewmh_get_wm_name_reply(&m_ewmh, cookie, &reply, nullptr)) {
ret.assign(reply.strings, reply.strings_len);
xcb_ewmh_get_utf8_strings_reply_wipe(&reply);
}
return ret.c_str();
}
void Util::setX11WindowInputShape(const xcb_window_t& window, const QSize& size)
{
xcb_rectangle_t rectangle;
rectangle.x = 0;
rectangle.y = 0;
rectangle.width = size.width();
rectangle.height = size.height();
xcb_shape_rectangles(m_x11connection, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_INPUT, XCB_CLIP_ORDERING_UNSORTED, window, 0, 0, 1, &rectangle);
xcb_shape_mask(m_x11connection, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_INPUT, window, 0, 0, XCB_PIXMAP_NONE);
const uint32_t stackData[] = {size.width() > 0 && size.height() > 0 ? XCB_STACK_MODE_ABOVE : XCB_STACK_MODE_BELOW};
xcb_configure_window(m_x11connection, window, XCB_CONFIG_WINDOW_STACK_MODE, stackData);
xcb_flush(m_x11connection);
}
QImage Util::getX11WindowImageNonComposite(const xcb_window_t& window)
{
QSize size = getX11WindowGeometry(window).size();
xcb_image_t *image = xcb_image_get(m_x11connection, window, 0, 0, size.width(), size.height(), 0xFFFFFFFF, XCB_IMAGE_FORMAT_Z_PIXMAP);
QImage naiveConversion;
if (image) {
naiveConversion = QImage(image->data, image->width, image->height, QImage::Format_ARGB32);
} else {
return QImage();
}
if (isTransparentImage(naiveConversion)) {
QImage elaborateConversion = QImage(convertFromNative(image));
if (isTransparentImage(elaborateConversion)) {
return QImage();
} else
return elaborateConversion;
} else {
return QImage(image->data, image->width, image->height, image->stride, QImage::Format_ARGB32, clean_xcb_image, image);
}
}
void Util::setX11WindowOpacity(const xcb_window_t& window, const double& opacity)
{
xcb_atom_t opacityAtom = getAtomByName("_NET_WM_WINDOW_OPACITY");
quint32 value = qRound64(qBound(qreal(0), opacity, qreal(1)) * 0xffffffff);
xcb_change_property(m_x11connection,
XCB_PROP_MODE_REPLACE,
window,
opacityAtom,
XCB_ATOM_CARDINAL,
32,
1,
(uchar *)&value);
xcb_flush(m_x11connection);
}
pid_t Util::getWindowPid(const xcb_window_t& window)
{
xcb_res_client_id_spec_t spec = { window, XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID };
xcb_res_query_client_ids_cookie_t cookie = xcb_res_query_client_ids_unchecked(m_x11connection, 1, &spec);
QSharedPointer<xcb_res_query_client_ids_reply_t> reply(xcb_res_query_client_ids_reply(m_x11connection, cookie, NULL),[](xcb_res_query_client_ids_reply_t* reply){
free(reply);
});
if (reply) {
xcb_res_client_id_value_iterator_t iter = xcb_res_query_client_ids_ids_iterator(reply.get());
for (; iter.rem; xcb_res_client_id_value_next(&iter)) {
if (iter.data->spec.mask == XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID && xcb_res_client_id_value_value_length(iter.data) == 1) {
return xcb_res_client_id_value_value(iter.data)[0];
}
}
}
// qWarning() << "failed to get pid for window: " << window;
return 0;
}
QString Util::getProcExe(const pid_t& pid)
{
return QFileInfo(QString("/proc/").append(QString::number(pid).append("/exe"))).canonicalFilePath().split("/").last();
}
void Util::sendXembedMessage(const xcb_window_t& window, const long& message, const long& data1, const long& data2, const long& data3)
{
xcb_client_message_event_t ev;
ev.response_type = XCB_CLIENT_MESSAGE;
ev.window = window;
ev.format = 32;
ev.data.data32[0] = XCB_CURRENT_TIME;
ev.data.data32[1] = message;
ev.data.data32[2] = data1;
ev.data.data32[3] = data2;
ev.data.data32[4] = data3;
ev.type = getAtomByName(QStringLiteral("_XEMBED"));
xcb_send_event(m_x11connection, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)&ev);
}
QString Util::generateUniqueId(const QString &id)
{
QMutexLocker locker(&m_idMutex);
for (int i = 0; i < 100; i++) {
QString newId = id + "-" + QString::number(i);
if (!m_currentIds.contains(newId)) {
m_currentIds.insert(newId);
return newId;
}
}
qWarning() << "failed to generate unique id:" << id;
return id;
}
void Util::removeUniqueId(const QString &id) {
QMutexLocker locker(&m_idMutex);
m_currentIds.remove(id);
}
bool Util::isTransparentImage(const QImage &image)
{
int w = image.width();
int h = image.height();
if (!(qAlpha(image.pixel(w >> 1, h >> 1)) + qAlpha(image.pixel(w >> 2, h >> 2)) == 0))
return false;
for (int x = 0; x < w; ++x) {
for (int y = 0; y < h; ++y) {
if (qAlpha(image.pixel(x, y))) {
return false;
}
}
}
return true;
}
QImage Util::convertFromNative(xcb_image_t *xcbImage)
{
QImage::Format format = QImage::Format_Invalid;
switch (xcbImage->depth) {
case 1:
format = QImage::Format_MonoLSB;
break;
case 16:
format = QImage::Format_RGB16;
break;
case 24:
format = QImage::Format_RGB32;
break;
case 30: {
quint32 *pixels = reinterpret_cast<quint32 *>(xcbImage->data);
for (uint i = 0; i < (xcbImage->size / 4); i++) {
int r = (pixels[i] >> 22) & 0xff;
int g = (pixels[i] >> 12) & 0xff;
int b = (pixels[i] >> 2) & 0xff;
pixels[i] = qRgba(r, g, b, 0xff);
}
Q_FALLTHROUGH();
}
case 32:
format = QImage::Format_ARGB32_Premultiplied;
break;
default:
return QImage();
}
QImage image(xcbImage->data, xcbImage->width, xcbImage->height, xcbImage->stride, format, clean_xcb_image, xcbImage);
if (image.isNull()) {
return QImage();
}
if (format == QImage::Format_RGB32 && xcbImage->bpp == 32) {
QImage m = image.createHeuristicMask();
QPixmap p = QPixmap::fromImage(std::move(image));
p.setMask(QBitmap::fromImage(std::move(m)));
image = p.toImage();
}
if (image.format() == QImage::Format_MonoLSB) {
image.setColorCount(2);
image.setColor(0, QColor(Qt::white).rgb());
image.setColor(1, QColor(Qt::black).rgb());
}
return image;
}
QPoint Util::getMousePos() const
{
QPoint pos;
xcb_query_pointer_cookie_t cookie = xcb_query_pointer(m_x11connection, m_rootWindow);
QScopedPointer<xcb_query_pointer_reply_t> reply(xcb_query_pointer_reply(m_x11connection, cookie, NULL));
if (reply) {
pos = QPoint(reply->root_x, reply->root_y);
}
return pos;
}
}