-
Notifications
You must be signed in to change notification settings - Fork 6
/
sniproxy.cpp
404 lines (329 loc) · 14.1 KB
/
sniproxy.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Holds one embedded window, registers as DBus entry
* Copyright (C) 2015 <[email protected]> David Edmundson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "sniproxy.h"
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_image.h>
#include "xcbutils.h"
#include "debug.h"
#include <QX11Info>
#include <QScreen>
#include <QGuiApplication>
#include <QTimer>
#include <QPainter>
#include <KWindowSystem>
#include <netwm.h>
#include "statusnotifieritemadaptor.h"
#include "statusnotifierwatcher_interface.h"
#define SNI_WATCHER_SERVICE_NAME "org.kde.StatusNotifierWatcher"
#define SNI_WATCHER_PATH "/StatusNotifierWatcher"
static uint16_t s_embedSize = 32; //max size of window to embed. We no longer resize the embedded window as Chromium acts stupidly.
int SNIProxy::s_serviceCount = 0;
void
xembed_message_send(xcb_window_t towin,
long message, long d1, long d2, long d3)
{
xcb_client_message_event_t ev;
ev.response_type = XCB_CLIENT_MESSAGE;
ev.window = towin;
ev.format = 32;
ev.data.data32[0] = XCB_CURRENT_TIME;
ev.data.data32[1] = message;
ev.data.data32[2] = d1;
ev.data.data32[3] = d2;
ev.data.data32[4] = d3;
ev.type = Xcb::atoms->xembedAtom;
xcb_send_event(QX11Info::connection(), false, towin, XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
}
SNIProxy::SNIProxy(xcb_window_t wid, QObject* parent):
QObject(parent),
//Work round a bug in our SNIWatcher with multiple SNIs per connection.
//there is an undocumented feature that you can register an SNI by path, however it doesn't detect an object on a service being removed, only the entire service closing
//instead lets use one DBus connection per SNI
m_dbus(QDBusConnection::connectToBus(QDBusConnection::SessionBus, QStringLiteral("XembedSniProxy%1").arg(s_serviceCount++))),
m_windowId(wid)
{
//create new SNI
new StatusNotifierItemAdaptor(this);
m_dbus.registerObject(QStringLiteral("/StatusNotifierItem"), this);
auto statusNotifierWatcher = new org::kde::StatusNotifierWatcher(QStringLiteral(SNI_WATCHER_SERVICE_NAME), QStringLiteral(SNI_WATCHER_PATH), QDBusConnection::sessionBus(), this);
auto reply = statusNotifierWatcher->RegisterStatusNotifierItem(m_dbus.baseService());
reply.waitForFinished();
if (reply.isError()) {
qCWarning(SNIPROXY) << "could not register SNI:" << reply.error().message();
}
auto c = QX11Info::connection();
auto cookie = xcb_get_geometry(c, m_windowId);
QScopedPointer<xcb_get_geometry_reply_t> clientGeom(xcb_get_geometry_reply(c, cookie, Q_NULLPTR));
//create a container window
auto screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
m_containerWid = xcb_generate_id(c);
uint32_t values[2];
auto mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT;
values[0] = screen->black_pixel; //draw a solid background so the embeded icon doesn't get garbage in it
values[1] = true; //bypass wM
xcb_create_window (c, /* connection */
XCB_COPY_FROM_PARENT, /* depth */
m_containerWid, /* window Id */
screen->root, /* parent window */
0, 0, /* x, y */
s_embedSize, s_embedSize, /* width, height */
0, /* border_width */
XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class */
screen->root_visual, /* visual */
mask, values); /* masks */
/*
We need the window to exist and be mapped otherwise the child won't render it's contents
We also need it to exist in the right place to get the clicks working as GTK will check sendEvent locations to see if our window is in the right place. So even though our contents are drawn via compositing we still put this window in the right place
We can't composite it away anything parented owned by the root window (apparently)
Stack Under works in the non composited case, but it doesn't seem to work in kwin's composited case (probably need set relevant NETWM hint)
As a last resort set opacity to 0 just to make sure this container never appears
*/
#ifndef VISUAL_DEBUG
const uint32_t stackBelowData[] = {XCB_STACK_MODE_BELOW};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackBelowData);
NETWinInfo wm(c, m_containerWid, screen->root, 0, 0);
wm.setOpacity(0);
#endif
xcb_flush(c);
xcb_map_window(c, m_containerWid);
xcb_reparent_window(c, wid,
m_containerWid,
0, 0);
/*
* Render the embedded window offscreen
*/
xcb_composite_redirect_window(c, wid, XCB_COMPOSITE_REDIRECT_MANUAL);
/* we grab the window, but also make sure it's automatically reparented back
* to the root window if we should die.
*/
xcb_change_save_set(c, XCB_SET_MODE_INSERT, wid);
//tell client we're embedding it
xembed_message_send(wid, XEMBED_EMBEDDED_NOTIFY, m_containerWid, 0, 0);
//move window we're embedding
const uint32_t windowMoveConfigVals[2] = { 0, 0 };
xcb_configure_window(c, wid,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
windowMoveConfigVals);
//if the window is a clearly stupid size resize to be something sensible
//this is needed as chormium and such when resized just fill the icon with transparent space and only draw in the middle
//however spotify does need this as by default the window size is 900px wide.
//use an artbitrary heuristic to make sure icons are always sensible
if (clientGeom->width > s_embedSize || clientGeom->height > s_embedSize )
{
const uint32_t windowMoveConfigVals[2] = { s_embedSize, s_embedSize };
xcb_configure_window(c, wid,
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
windowMoveConfigVals);
qCDebug(SNIPROXY) << "Resizing window" << wid << Title() << "from w*h" << clientGeom->width << clientGeom->height;
}
//show the embedded window otherwise nothing happens
xcb_map_window(c, wid);
xcb_clear_area(c, 0, wid, 0, 0, qMin(clientGeom->width, s_embedSize), qMin(clientGeom->height, s_embedSize));
xcb_flush(c);
//there's no damage event for the first paint, and sometimes it's not drawn immediately
//not ideal, but it works better than nothing
//test with xchat before changing
QTimer::singleShot(500, this, &SNIProxy::update);
}
SNIProxy::~SNIProxy()
{
QDBusConnection::disconnectFromBus(m_dbus.name());
}
void SNIProxy::update()
{
const QImage image = getImageNonComposite();
int w = image.width();
int h = image.height();
// check for the center and sub-center pixels first and avoid full image scan
bool isTransparentImage = qAlpha(image.pixel(w >> 1, h >> 1)) + qAlpha(image.pixel(w >> 2, h >> 2)) == 0;
// skip scan altogether if sub-center pixel found to be opaque
// and break out from the outer loop too on full scan
for (int x = 0; x < w && isTransparentImage; ++x) {
for (int y = 0; y < h; ++y) {
if (qAlpha(image.pixel(x, y))) {
// Found an opaque pixel.
isTransparentImage = false;
break;
}
}
}
// Update icon only if it is at least partially opaque.
// This is just a workaround for X11 bug: xembed icon may suddenly
// become transparent for a one or few frames. Reproducible at least
// with WINE applications.
if (!isTransparentImage) {
m_pixmap = QPixmap::fromImage(image);
if (w != s_embedSize || h != s_embedSize) {
qCDebug(SNIPROXY) << "Scaling pixmap of window" << m_windowId << Title() << "from w*h" << w << h;
m_pixmap = m_pixmap.scaled(s_embedSize, s_embedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
emit NewIcon();
emit NewToolTip();
}
else {
qCDebug(SNIPROXY) << "Skip transparent xembed icon for" << m_windowId << Title();
}
}
void sni_cleanup_xcb_image(void *data) {
xcb_image_destroy(static_cast<xcb_image_t*>(data));
}
QImage SNIProxy::getImageNonComposite()
{
auto c = QX11Info::connection();
auto cookie = xcb_get_geometry(c, m_windowId);
QScopedPointer<xcb_get_geometry_reply_t> geom(xcb_get_geometry_reply(c, cookie, Q_NULLPTR));
xcb_image_t *image = xcb_image_get(c, m_windowId, 0, 0, geom->width, geom->height, 0xFFFFFF, XCB_IMAGE_FORMAT_Z_PIXMAP);
QImage qimage(image->data, image->width, image->height, image->stride, QImage::Format_ARGB32, sni_cleanup_xcb_image, image);
return qimage;
}
//____________properties__________
QString SNIProxy::Category() const
{
return QStringLiteral("ApplicationStatus");
}
QString SNIProxy::Id() const
{
return QString::number(m_windowId);
}
KDbusImageVector SNIProxy::IconPixmap() const
{
KDbusImageStruct dbusImage(m_pixmap.toImage());
return KDbusImageVector() << dbusImage;
}
bool SNIProxy::ItemIsMenu() const
{
return false;
}
QString SNIProxy::Status() const
{
return QStringLiteral("Active");
}
QString SNIProxy::Title() const
{
KWindowInfo window (m_windowId, NET::WMName);
return window.name();
}
int SNIProxy::WindowId() const
{
return m_windowId;
}
//____________actions_____________
void SNIProxy::Activate(int x, int y)
{
sendClick(XCB_BUTTON_INDEX_1, x, y);
}
void SNIProxy::SecondaryActivate(int x, int y)
{
sendClick(XCB_BUTTON_INDEX_2, x, y);
}
void SNIProxy::ContextMenu(int x, int y)
{
sendClick(XCB_BUTTON_INDEX_3, x, y);
}
void SNIProxy::Scroll(int delta, const QString& orientation)
{
if (orientation == QLatin1String("vertical")) {
sendClick(delta > 0 ? XCB_BUTTON_INDEX_4: XCB_BUTTON_INDEX_5, 0, 0);
} else {
sendClick(delta > 0 ? 6: 7, 0, 0);
}
}
void SNIProxy::sendClick(uint8_t mouseButton, int x, int y)
{
//it's best not to look at this code
//GTK doesn't like send_events and double checks the mouse position matches where the window is and is top level
//in order to solve this we move the embed container over to where the mouse is then replay the event using send_event
//if patching, test with xchat + xchat context menus
//note x,y are not actually where the mouse is, but the plasmoid
//ideally we should make this match the plasmoid hit area
qCDebug(SNIPROXY) << "Received click" << mouseButton << "with passed x*y" << x << y;
auto c = QX11Info::connection();
auto cookieSize = xcb_get_geometry(c, m_windowId);
QScopedPointer<xcb_get_geometry_reply_t> clientGeom(xcb_get_geometry_reply(c, cookieSize, Q_NULLPTR));
auto cookie = xcb_query_pointer(c, m_windowId);
QScopedPointer<xcb_query_pointer_reply_t> pointer(xcb_query_pointer_reply(c, cookie, Q_NULLPTR));
/*qCDebug(SNIPROXY) << "samescreen" << pointer->same_screen << endl
<< "root x*y" << pointer->root_x << pointer->root_y << endl
<< "win x*y" << pointer->win_x << pointer->win_y;*/
//move our window so the mouse is within its geometry
uint32_t configVals[2] = {0, 0};
if (mouseButton >= XCB_BUTTON_INDEX_4) {
//scroll event, take pointer position
configVals[0] = pointer->root_x;
configVals[1] = pointer->root_y;
} else {
if (pointer->root_x > x + clientGeom->width)
configVals[0] = pointer->root_x - clientGeom->width + 1;
else
configVals[0] = static_cast<uint32_t>(x);
if (pointer->root_y > y + clientGeom->height)
configVals[1] = pointer->root_y - clientGeom->height + 1;
else
configVals[1] = static_cast<uint32_t>(y);
}
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, configVals);
//pull window up
const uint32_t stackAboveData[] = {XCB_STACK_MODE_ABOVE};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData);
//mouse down
{
xcb_button_press_event_t* event = new xcb_button_press_event_t;
memset(event, 0x00, sizeof(xcb_button_press_event_t));
event->response_type = XCB_BUTTON_PRESS;
event->event = m_windowId;
event->time = QX11Info::getTimestamp();
event->same_screen = 1;
event->root = QX11Info::appRootWindow();
event->root_x = x;
event->root_y = y;
event->event_x = 0;
event->event_y = 0;
event->child = 0;
event->state = 0;
event->detail = mouseButton;
xcb_send_event(c, false, m_windowId, XCB_EVENT_MASK_BUTTON_PRESS, (char *) event);
free(event);
}
//mouse up
{
xcb_button_release_event_t* event = new xcb_button_release_event_t;
memset(event, 0x00, sizeof(xcb_button_release_event_t));
event->response_type = XCB_BUTTON_RELEASE;
event->event = m_windowId;
event->time = QX11Info::getTimestamp();
event->same_screen = 1;
event->root = QX11Info::appRootWindow();
event->root_x = x;
event->root_y = y;
event->event_x = 0;
event->event_y = 0;
event->child = 0;
event->state = 0;
event->detail = mouseButton;
xcb_send_event(c, false, m_windowId, XCB_EVENT_MASK_BUTTON_RELEASE, (char *) event);
free(event);
}
#ifndef VISUAL_DEBUG
const uint32_t stackBelowData[] = {XCB_STACK_MODE_BELOW};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackBelowData);
#endif
}