Skip to content

Commit 7b625b1

Browse files
committed
fix: fix wine systray can't interact with
dde-dock use XTest to send mouse button event, but wine do not support XTest extension, so use XEvent to deal with wine. Log: fix wine systray can't interact with Influence: tray Issue: linuxdeepin/developer-center#2262 Bug: https://pms.uniontech.com/bug-view-125181.html Change-Id: Ic3ebee7748be7ee458ef8ed1aa0575237bbbafd5
1 parent 9da22bc commit 7b625b1

2 files changed

Lines changed: 114 additions & 31 deletions

File tree

‎plugins/tray/xembedtraywidget.cpp‎

Lines changed: 106 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <QX11Info>
2929
#include <QDebug>
3030
#include <QMouseEvent>
31+
#include <QScopedPointer>
3132
#include <QProcess>
3233
#include <QThread>
3334
#include <QApplication>
@@ -47,7 +48,7 @@
4748
#define WINE_WINDOW_PROP_NAME "__wine_prefix"
4849
#define IS_WINE_WINDOW_BY_WM_CLASS "explorer.exe"
4950

50-
static const qreal iconSize = PLUGIN_ICON_MAX_SIZE;
51+
static const uint16_t iconDefaultSize = PLUGIN_ICON_MAX_SIZE;
5152

5253
// this static var hold all suffix of tray widget keys.
5354
// that is in order to fix can not show multiple trays provide by one application,
@@ -87,6 +88,7 @@ XEmbedTrayWidget::XEmbedTrayWidget(quint32 winId, xcb_connection_t *cnn, Display
8788
, m_valid(true)
8889
, m_xcbCnn(cnn)
8990
, m_display(disp)
91+
, m_injectMode(Direct)
9092
{
9193
wrapWindow();
9294
setOwnerPID(getWindowPID(winId));
@@ -187,15 +189,16 @@ void XEmbedTrayWidget::wrapWindow()
187189
}
188190

189191
auto cookie = xcb_get_geometry(c, m_windowId);
190-
xcb_get_geometry_reply_t *clientGeom(xcb_get_geometry_reply(c, cookie, Q_NULLPTR));
192+
QScopedPointer<xcb_get_geometry_reply_t, QScopedPointerPodDeleter> clientGeom(xcb_get_geometry_reply(c, cookie, nullptr));
191193
if (!clientGeom) {
192194
m_valid = false;
193195
return;
194196
}
195-
free(clientGeom);
196197

197198
//create a container window
199+
//创建托盘window,并使背景透明化
198200
const auto ratio = devicePixelRatioF();
201+
uint16_t iconSize = iconDefaultSize * ratio;
199202
auto screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
200203
m_containerWid = xcb_generate_id(c);
201204
uint32_t values[2];
@@ -207,7 +210,7 @@ void XEmbedTrayWidget::wrapWindow()
207210
m_containerWid, /* window Id */
208211
screen->root, /* parent window */
209212
0, 0, /* x, y */
210-
iconSize * ratio, iconSize * ratio, /* width, height */
213+
iconSize, iconSize, /* width, height */
211214
0, /* border_width */
212215
XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class */
213216
screen->root_visual, /* visual */
@@ -270,31 +273,45 @@ void XEmbedTrayWidget::wrapWindow()
270273
// xembed_message_send(m_windowId, XEMBED_EMBEDDED_NOTIFY, m_containerWid, 0, 0);
271274

272275
//move window we're embedding
273-
/*
274276
const uint32_t windowMoveConfigVals[2] = { 0, 0 };
275277
xcb_configure_window(c, m_windowId,
276-
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
277-
windowMoveCentially quitting the application. Returns onfigVals);
278-
*/
278+
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, windowMoveConfigVals);
279279

280-
//if the window is a clearly stupid size resize to be something sensible
281-
//this is needed as chormium and such when resized just fill the icon with transparent space and only draw in the middle
282-
//however spotify does need this as by default the window size is 900px wide.
283-
//use an artbitrary heuristic to make sure icons are always sensible
284-
// if (clientGeom->width > iconSize || clientGeom->height > iconSize )
285-
{
286-
const uint32_t windowMoveConfigVals[2] = { uint32_t(iconSize * ratio), uint32_t(iconSize * ratio) };
287-
xcb_configure_window(c, m_windowId,
288-
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
289-
windowMoveConfigVals);
280+
// 判断托盘的大小是否超出iconSize
281+
QSize clientWindowSize;
282+
if (clientGeom) {
283+
clientWindowSize = QSize(clientGeom->width, clientGeom->height);
284+
}
285+
286+
if (clientWindowSize.isEmpty() || clientWindowSize.width() > iconSize || clientWindowSize.height() > iconSize ) {
287+
288+
uint16_t widthNormalized = std::min(clientGeom->width, iconSize);
289+
uint16_t heighNormalized = std::min(clientGeom->height, iconSize);
290+
291+
const uint32_t windowSizeConfigVals[2] = {widthNormalized, heighNormalized};
292+
xcb_configure_window(c, m_windowId, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, windowSizeConfigVals);
293+
294+
xcb_flush(c);
295+
clientWindowSize = QSize(iconSize, iconSize);
290296
}
291297

292298
//show the embedded window otherwise nothing happens
293299
xcb_map_window(c, m_windowId);
294300

301+
xcb_clear_area(c, 0, m_windowId, 0, 0, clientWindowSize.width(), clientWindowSize.height());
302+
295303
// xcb_clear_area(c, 0, m_windowId, 0, 0, qMin(clientGeom->width, iconSize), qMin(clientGeom->height, iconSize));
296304

297305
xcb_flush(c);
306+
307+
// 通过xcb获取window属性,判断该window是否处理button press事件
308+
// 当window不关注button press等事件时,使用xtest extension
309+
auto windowAttributesCookie = xcb_get_window_attributes(c, m_windowId);
310+
QScopedPointer<xcb_get_window_attributes_reply_t, QScopedPointerPodDeleter> windowAttributes(xcb_get_window_attributes_reply(c, windowAttributesCookie, nullptr));
311+
if (windowAttributes && !(windowAttributes->all_event_masks & XCB_EVENT_MASK_BUTTON_PRESS)) {
312+
m_injectMode = XTest;
313+
}
314+
298315
// setWindowOnTop(false);
299316
setWindowOnTop(true);
300317
setX11PassMouseEvent(true);
@@ -306,15 +323,33 @@ void XEmbedTrayWidget::sendHoverEvent()
306323
return;
307324
}
308325

309-
// fake enter event
310326
const QPoint p(rawXPosition(QCursor::pos()));
311327
configContainerPosition();
312328
setX11PassMouseEvent(false);
313329
setWindowOnTop(true);
314330
Display *display = IS_WAYLAND_DISPLAY ? m_display : QX11Info::display();
315331
if (display) {
316-
XTestFakeMotionEvent(display, 0, p.x(), p.y(), CurrentTime);
317-
XFlush(display);
332+
if (m_injectMode == XTest) {
333+
// fake enter event
334+
XTestFakeMotionEvent(display, 0, p.x(), p.y(), CurrentTime);
335+
XFlush(display);
336+
} else {
337+
// 发送 montion notify event到client,实现hover事件
338+
auto c = QX11Info::connection();
339+
xcb_motion_notify_event_t* event = new xcb_motion_notify_event_t;
340+
memset(event, 0x00, sizeof(xcb_button_press_event_t));
341+
event->response_type = XCB_MOTION_NOTIFY;
342+
event->event = m_windowId;
343+
event->same_screen = 1;
344+
event->root = QX11Info::appRootWindow();
345+
event->time = 0;
346+
event->root_x = p.x();
347+
event->root_y = p.y();
348+
event->child = 0;
349+
event->state = 0;
350+
xcb_send_event(c, false, m_windowId, XCB_EVENT_MASK_POINTER_MOTION, (char*)event);
351+
delete event;
352+
}
318353
}
319354

320355
QTimer::singleShot(100, this, [=] { setX11PassMouseEvent(true); });
@@ -349,19 +384,59 @@ void XEmbedTrayWidget::sendClick(uint8_t mouseButton, int x, int y)
349384
return;
350385

351386
m_sendHoverEvent->stop();
352-
387+
auto c = QX11Info::connection();
388+
if (!c) {
389+
qWarning() << "QX11Info::connection() is " << c;
390+
return;
391+
}
353392
const QPoint p(rawXPosition(QPoint(x, y)));
354393
configContainerPosition();
355394
setX11PassMouseEvent(false);
356395
setWindowOnTop(true);
357396

358397
Display *display = IS_WAYLAND_DISPLAY ? m_display : QX11Info::display();
359-
XTestFakeMotionEvent(display, 0, p.x(), p.y(), CurrentTime);
360-
XFlush(display);
361-
XTestFakeButtonEvent(display, mouseButton, true, CurrentTime);
362-
XFlush(display);
363-
XTestFakeButtonEvent(display, mouseButton, false, CurrentTime);
364-
XFlush(display);
398+
399+
if (m_injectMode == XTest) {
400+
XTestFakeMotionEvent(display, 0, p.x(), p.y(), CurrentTime);
401+
XFlush(display);
402+
XTestFakeButtonEvent(display, mouseButton, true, CurrentTime);
403+
XFlush(display);
404+
XTestFakeButtonEvent(display, mouseButton, false, CurrentTime);
405+
XFlush(display);
406+
} else {
407+
// press event
408+
xcb_button_press_event_t *pressEvent = new xcb_button_press_event_t;
409+
memset(pressEvent, 0x00, sizeof(xcb_button_press_event_t));
410+
pressEvent->response_type = XCB_BUTTON_PRESS;
411+
pressEvent->event = m_windowId;
412+
pressEvent->same_screen = 1;
413+
pressEvent->root = QX11Info::appRootWindow();
414+
pressEvent->time = 0;
415+
pressEvent->root_x = x;
416+
pressEvent->root_y = y;
417+
pressEvent->child = 0;
418+
pressEvent->state = 0;
419+
pressEvent->detail = mouseButton;
420+
xcb_send_event(c, false, m_windowId, XCB_EVENT_MASK_BUTTON_PRESS, (char*)pressEvent);
421+
delete pressEvent;
422+
423+
// release event
424+
xcb_button_release_event_t *releaseEvent = new xcb_button_release_event_t;
425+
memset(releaseEvent, 0x00, sizeof(xcb_button_release_event_t));
426+
releaseEvent->response_type = XCB_BUTTON_RELEASE;
427+
releaseEvent->event = m_windowId;
428+
releaseEvent->same_screen = 1;
429+
releaseEvent->root = QX11Info::appRootWindow();
430+
releaseEvent->time = QX11Info::getTimestamp();
431+
releaseEvent->root_x = x;
432+
releaseEvent->root_y = y;
433+
releaseEvent->child = 0;
434+
releaseEvent->state = 0;
435+
releaseEvent->detail = mouseButton;
436+
xcb_send_event(c, false, m_windowId, XCB_EVENT_MASK_BUTTON_RELEASE, (char*)releaseEvent);
437+
delete releaseEvent;
438+
}
439+
365440
QTimer::singleShot(100, this, [=] { setX11PassMouseEvent(true); });
366441
}
367442

@@ -434,8 +509,8 @@ void XEmbedTrayWidget::refershIconImage()
434509
expose.window = m_containerWid;
435510
expose.x = 0;
436511
expose.y = 0;
437-
expose.width = iconSize * ratio;
438-
expose.height = iconSize * ratio;
512+
expose.width = iconDefaultSize * ratio;
513+
expose.height = iconDefaultSize * ratio;
439514
xcb_send_event_checked(c, false, m_containerWid, XCB_EVENT_MASK_VISIBILITY_CHANGE, reinterpret_cast<char *>(&expose));
440515
xcb_flush(c);
441516

@@ -451,7 +526,7 @@ void XEmbedTrayWidget::refershIconImage()
451526
return;
452527
}
453528

454-
m_image = qimage.scaled(iconSize * ratio, iconSize * ratio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
529+
m_image = qimage.scaled(iconDefaultSize * ratio, iconDefaultSize * ratio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
455530
m_image.setDevicePixelRatio(ratio);
456531

457532
update();

‎plugins/tray/xembedtraywidget.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ private slots:
6767
bool isBadWindow();
6868

6969
private:
70+
// Direct client关注xevent,使用xevent来处理button事件等
71+
// XTest client不关注xevent,使用xtest extension处理
72+
enum InjectMode {
73+
Direct,
74+
XTest,
75+
};
76+
7077
bool m_active = false;
7178
WId m_windowId;
7279
WId m_containerWid;
@@ -78,6 +85,7 @@ private slots:
7885
bool m_valid;
7986
xcb_connection_t *m_xcbCnn;
8087
Display* m_display;
88+
InjectMode m_injectMode;
8189
};
8290

8391
#endif // XEMBEDTRAYWIDGET_H

0 commit comments

Comments
 (0)