diff --git a/src/plugins/platforms/xcb/qxcbnativeinterface.cpp b/src/plugins/platforms/xcb/qxcbnativeinterface.cpp index fa5f5f43d0..a44e7fb959 100644 --- a/src/plugins/platforms/xcb/qxcbnativeinterface.cpp +++ b/src/plugins/platforms/xcb/qxcbnativeinterface.cpp @@ -104,6 +104,25 @@ void *QXcbNativeInterface::nativeResourceForContext(const QByteArray &resourceSt return result; } +void *QXcbNativeInterface::nativeResourceForScreen(const QByteArray &resource, QScreen *screen) +{ + const QXcbResourceMap::const_iterator it = qXcbResourceMap()->constFind(resource.toLower()); + if (it == qXcbResourceMap()->constEnd() || !screen->handle()) + return 0; + const QXcbScreen *xcbScreen = static_cast(screen->handle()); + switch (it.value()) { + case Display: +#ifdef XCB_USE_XLIB + return xcbScreen->connection()->xlib_display(); +#else + break; +#endif + default: + break; + } + return 0; +} + void *QXcbNativeInterface::nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) { QByteArray lowerCaseResource = resourceString.toLower(); diff --git a/src/plugins/platforms/xcb/qxcbnativeinterface.h b/src/plugins/platforms/xcb/qxcbnativeinterface.h index c15d00255a..a7e0a207cb 100644 --- a/src/plugins/platforms/xcb/qxcbnativeinterface.h +++ b/src/plugins/platforms/xcb/qxcbnativeinterface.h @@ -65,6 +65,7 @@ public: QXcbNativeInterface(); void *nativeResourceForContext(const QByteArray &resourceString, QOpenGLContext *context); + void *nativeResourceForScreen(const QByteArray &resource, QScreen *screen); void *nativeResourceForWindow(const QByteArray &resourceString, QWindow *window); NativeResourceForContextFunction nativeResourceFunctionForContext(const QByteArray &resource); diff --git a/src/widgets/util/qsystemtrayicon_x11.cpp b/src/widgets/util/qsystemtrayicon_x11.cpp index 62dab8fa11..5fefa08f77 100644 --- a/src/widgets/util/qsystemtrayicon_x11.cpp +++ b/src/widgets/util/qsystemtrayicon_x11.cpp @@ -102,21 +102,14 @@ QX11SystemTrayContext::QX11SystemTrayContext() : m_display(0), m_screenNumber(0) qWarning("%s: No screen.", Q_FUNC_INFO); return; } - // Open display using screen name and retrieve screen number from "hostname:0.0" - const QByteArray name = screen->name().toLocal8Bit(); - const int dotPos = name.lastIndexOf('.'); - if (dotPos != -1) { - bool ok; - const int n = name.mid(dotPos + 1).toInt(&ok); - if (ok) - m_screenNumber = n; - } - m_display = XOpenDisplay(name.constData()); - if (!m_display) { - qWarning("%s: Cannot open display '%s'.", Q_FUNC_INFO, name.constData()); + void *displayV = QGuiApplication::platformNativeInterface()->nativeResourceForScreen(QByteArrayLiteral("display"), screen); + if (!displayV) { + qWarning("%s: Unable to obtain X11 display of primary screen.", Q_FUNC_INFO); return; } + m_display = static_cast(displayV); + const QByteArray netSysTray = "_NET_SYSTEM_TRAY_S" + QByteArray::number(m_screenNumber); m_systemTraySelection = XInternAtom(m_display, netSysTray.constData(), False); if (!m_systemTraySelection) { @@ -134,8 +127,6 @@ Window QX11SystemTrayContext::locateSystemTray() const QX11SystemTrayContext::~QX11SystemTrayContext() { - if (m_display) - XCloseDisplay(m_display); } Q_GLOBAL_STATIC(QX11SystemTrayContext, qX11SystemTrayContext)