diff --git a/src/plugins/platforms/xcb/qxcbconnection.cpp b/src/plugins/platforms/xcb/qxcbconnection.cpp index 4e24c970b4..09b920a09e 100644 --- a/src/plugins/platforms/xcb/qxcbconnection.cpp +++ b/src/plugins/platforms/xcb/qxcbconnection.cpp @@ -525,59 +525,31 @@ void QXcbConnection::initializeScreens() } } -QXcbConnection *QXcbConnection::create(QXcbNativeInterface *nativeInterface, bool canGrabServer, - xcb_visualid_t defaultVisualId, - const char *displayNameIn) +QXcbConnection::QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, xcb_visualid_t defaultVisualId, const char *displayName) + : m_canGrabServer(canGrabServer) + , m_defaultVisualId(defaultVisualId) + , m_displayName(displayName ? QByteArray(displayName) : qgetenv("DISPLAY")) + , m_nativeInterface(nativeInterface) { - const QByteArray displayName = displayNameIn ? QByteArray(displayNameIn) : qgetenv("DISPLAY"); - int primaryScreenNumber = 0; - void *xlibDisplay = nullptr; - xcb_connection_t *connection = nullptr; #if QT_CONFIG(xcb_xlib) - Display *dpy = XOpenDisplay(displayName.constData()); + Display *dpy = XOpenDisplay(m_displayName.constData()); if (dpy) { - primaryScreenNumber = DefaultScreen(dpy); - connection = XGetXCBConnection(dpy); + m_primaryScreenNumber = DefaultScreen(dpy); + m_connection = XGetXCBConnection(dpy); XSetEventQueueOwner(dpy, XCBOwnsEventQueue); XSetErrorHandler(nullErrorHandler); XSetIOErrorHandler(ioErrorHandler); - xlibDisplay = dpy; + m_xlib_display = dpy; } #else - connection = xcb_connect(displayName.constData(), &primaryScreenNumber); + m_connection = xcb_connect(m_displayName.constData(), &m_primaryScreenNumber); #endif // QT_CONFIG(xcb_xlib) - if (Q_UNLIKELY(connection == nullptr)) { - qWarning("QXcbConnection: Could not connect to display \"%s\"", displayName.constData()); - return nullptr; - } - if (Q_UNLIKELY(xcb_connection_has_error(connection))) { -#if QT_CONFIG(xcb_xlib) - XCloseDisplay(static_cast(xlibDisplay)); -#else - xcb_disconnect(connection); -#endif - qWarning("QXcbConnection: Errors occurred connecting to display \"%s\"", displayName.constData()); - return nullptr; - } - return new QXcbConnection(connection, primaryScreenNumber, nativeInterface, - canGrabServer, defaultVisualId, displayName, xlibDisplay); -} + if (Q_UNLIKELY(!m_connection || xcb_connection_has_error(m_connection))) { + qCWarning(lcQpaScreen, "QXcbConnection: Could not connect to display %s", m_displayName.constData()); + return; + } -QXcbConnection::QXcbConnection(xcb_connection_t *c, int primaryScreenNumber, - QXcbNativeInterface *nativeInterface, bool canGrabServer, - xcb_visualid_t defaultVisualId, const QByteArray &displayName, - void *xlibDisplay) - : m_connection(c) - , m_canGrabServer(canGrabServer) - , m_defaultVisualId(defaultVisualId) - , m_primaryScreenNumber(primaryScreenNumber) - , m_displayName(displayName) - , m_nativeInterface(nativeInterface) -#if QT_CONFIG(xcb_xlib) - , m_xlib_display(xlibDisplay) -#endif -{ m_reader = new QXcbEventReader(this); m_reader->start(); diff --git a/src/plugins/platforms/xcb/qxcbconnection.h b/src/plugins/platforms/xcb/qxcbconnection.h index db45031cf4..a97a1f1002 100644 --- a/src/plugins/platforms/xcb/qxcbconnection.h +++ b/src/plugins/platforms/xcb/qxcbconnection.h @@ -380,16 +380,9 @@ class Q_XCB_EXPORT QXcbConnection : public QObject { Q_OBJECT public: - explicit QXcbConnection(xcb_connection_t *c, int primaryScreenNumber, - QXcbNativeInterface *nativeInterface, bool canGrabServer, - xcb_visualid_t defaultVisualId, const QByteArray &displayName, - void *xlibDisplay = nullptr); - + QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, xcb_visualid_t defaultVisualId, const char *displayName = 0); ~QXcbConnection(); - static QXcbConnection *create(QXcbNativeInterface *nativeInterface, bool canGrabServer, - xcb_visualid_t defaultVisualId, const char *displayName = nullptr); - QXcbConnection *connection() const { return const_cast(this); } bool isConnected() const; @@ -648,21 +641,21 @@ private: static bool xi2GetValuatorValueIfSet(const void *event, int valuatorNum, double *value); #endif - xcb_connection_t *const m_connection; + xcb_connection_t *m_connection = nullptr; const xcb_setup_t *m_setup = nullptr; const bool m_canGrabServer; const xcb_visualid_t m_defaultVisualId; QList m_virtualDesktops; QList m_screens; - const int m_primaryScreenNumber; + int m_primaryScreenNumber = 0; xcb_atom_t m_allAtoms[QXcbAtom::NAtoms]; xcb_timestamp_t m_time = XCB_CURRENT_TIME; xcb_timestamp_t m_netWmUserTime = XCB_CURRENT_TIME; - const QByteArray m_displayName; + QByteArray m_displayName; QXcbKeyboard *m_keyboard = nullptr; #ifndef QT_NO_CLIPBOARD @@ -675,7 +668,7 @@ private: QXcbNativeInterface *m_nativeInterface = nullptr; #if QT_CONFIG(xcb_xlib) - void *const m_xlib_display; + void *m_xlib_display = nullptr; #endif QXcbEventReader *m_reader = nullptr; diff --git a/src/plugins/platforms/xcb/qxcbintegration.cpp b/src/plugins/platforms/xcb/qxcbintegration.cpp index db8dc09025..db8f816a86 100644 --- a/src/plugins/platforms/xcb/qxcbintegration.cpp +++ b/src/plugins/platforms/xcb/qxcbintegration.cpp @@ -192,15 +192,25 @@ QXcbIntegration::QXcbIntegration(const QStringList ¶meters, int &argc, char const int numParameters = parameters.size(); m_connections.reserve(1 + numParameters / 2); + auto conn = new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, displayName); + if (conn->isConnected()) + m_connections << conn; + else + delete conn; - if (QXcbConnection *defaultConnection = QXcbConnection::create(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, displayName)) { - m_connections.append(defaultConnection); - for (int i = 0; i < numParameters - 1; i += 2) { - qCDebug(lcQpaScreen) << "connecting to additional display: " << parameters.at(i) << parameters.at(i+1); - QString display = parameters.at(i) + QLatin1Char(':') + parameters.at(i+1); - if (QXcbConnection *connection = QXcbConnection::create(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, display.toLatin1().constData())) - m_connections.append(connection); - } + for (int i = 0; i < numParameters - 1; i += 2) { + qCDebug(lcQpaScreen) << "connecting to additional display: " << parameters.at(i) << parameters.at(i+1); + QString display = parameters.at(i) + QLatin1Char(':') + parameters.at(i+1); + conn = new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, display.toLatin1().constData()); + if (conn->isConnected()) + m_connections << conn; + else + delete conn; + } + + if (m_connections.isEmpty()) { + qCritical("Could not connect to any X display."); + exit(1); } m_fontDatabase.reset(new QGenericUnixFontDatabase()); diff --git a/src/plugins/platforms/xcb/qxcbintegration.h b/src/plugins/platforms/xcb/qxcbintegration.h index a2de22d53d..69e49cb7f6 100644 --- a/src/plugins/platforms/xcb/qxcbintegration.h +++ b/src/plugins/platforms/xcb/qxcbintegration.h @@ -103,7 +103,6 @@ public: QPlatformTheme *createPlatformTheme(const QString &name) const override; QVariant styleHint(StyleHint hint) const override; - bool hasDefaultConnection() const { return !m_connections.isEmpty(); } QXcbConnection *defaultConnection() const { return m_connections.first(); } QByteArray wmClass() const; diff --git a/src/plugins/platforms/xcb/qxcbmain.cpp b/src/plugins/platforms/xcb/qxcbmain.cpp index 539d033ca9..f8cb9a9269 100644 --- a/src/plugins/platforms/xcb/qxcbmain.cpp +++ b/src/plugins/platforms/xcb/qxcbmain.cpp @@ -52,16 +52,10 @@ public: QPlatformIntegration* QXcbIntegrationPlugin::create(const QString& system, const QStringList& parameters, int &argc, char **argv) { - if (!system.compare(QLatin1String("xcb"), Qt::CaseInsensitive)) { - QXcbIntegration *xcbIntegration = new QXcbIntegration(parameters, argc, argv); - if (!xcbIntegration->hasDefaultConnection()) { - delete xcbIntegration; - return nullptr; - } - return xcbIntegration; - } + if (!system.compare(QLatin1String("xcb"), Qt::CaseInsensitive)) + return new QXcbIntegration(parameters, argc, argv); - return nullptr; + return 0; } QT_END_NAMESPACE