From f4b8697c40bd476ef6bf83418e144adce7c7d4a3 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 3 Feb 2015 13:05:20 +0100 Subject: Handle gracefully the removal and re-attachment of all QScreens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't crash; restore windows when all screens are removed and re-added. xcb: on configure notify, check for screen change: it may be that a window belonging to a screen which was removed has now gotten mapped to the new screen. On screen change, send a synthetic expose event, because the real expose events already happened. Task-number: QTBUG-38326 Task-number: QTBUG-32973 Task-number: QTBUG-42985 Change-Id: If334f55c248468ad3c95e7066bb14eca377d2050 Reviewed-by: Jørgen Lind --- src/plugins/platforms/xcb/qxcbconnection.cpp | 3 ++ src/plugins/platforms/xcb/qxcbnativeinterface.cpp | 7 ++++- src/plugins/platforms/xcb/qxcbscreen.cpp | 7 +++++ src/plugins/platforms/xcb/qxcbwindow.cpp | 36 +++++++++++++++-------- src/plugins/platforms/xcb/qxcbwindow.h | 1 + 5 files changed, 41 insertions(+), 13 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/platforms/xcb/qxcbconnection.cpp b/src/plugins/platforms/xcb/qxcbconnection.cpp index 93914c0cba..48cf366d28 100644 --- a/src/plugins/platforms/xcb/qxcbconnection.cpp +++ b/src/plugins/platforms/xcb/qxcbconnection.cpp @@ -332,6 +332,9 @@ QXcbConnection::QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGra initializeXRandr(); updateScreens(); + if (m_screens.isEmpty()) + qFatal("QXcbConnection: no screens available"); + initializeXFixes(); initializeXRender(); m_xi2Enabled = false; diff --git a/src/plugins/platforms/xcb/qxcbnativeinterface.cpp b/src/plugins/platforms/xcb/qxcbnativeinterface.cpp index 8974ab549f..915287d93e 100644 --- a/src/plugins/platforms/xcb/qxcbnativeinterface.cpp +++ b/src/plugins/platforms/xcb/qxcbnativeinterface.cpp @@ -249,8 +249,13 @@ void *QXcbNativeInterface::nativeResourceForContext(const QByteArray &resourceSt void *QXcbNativeInterface::nativeResourceForScreen(const QByteArray &resourceString, QScreen *screen) { + if (!screen) { + qWarning() << "nativeResourceForScreen: null screen"; + return Q_NULLPTR; + } + QByteArray lowerCaseResource = resourceString.toLower(); - void *result = handlerNativeResourceForScreen(lowerCaseResource ,screen); + void *result = handlerNativeResourceForScreen(lowerCaseResource, screen); if (result) return result; diff --git a/src/plugins/platforms/xcb/qxcbscreen.cpp b/src/plugins/platforms/xcb/qxcbscreen.cpp index 0f25d10aac..b5eed4f4fe 100644 --- a/src/plugins/platforms/xcb/qxcbscreen.cpp +++ b/src/plugins/platforms/xcb/qxcbscreen.cpp @@ -417,6 +417,13 @@ void QXcbScreen::handleScreenChange(xcb_randr_screen_change_notify_event_t *chan QDpi ldpi = logicalDpi(); QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(QPlatformScreen::screen(), ldpi.first, ldpi.second); + + // Windows which had null screens have already had expose events by now. + // They need to be told the screen is back, it's OK to render. + foreach (QWindow *window, QGuiApplication::topLevelWindows()) { + QXcbWindow *xcbWin = static_cast(window->handle()); + xcbWin->maybeSetScreen(this); + } } void QXcbScreen::updateGeometry(xcb_timestamp_t timestamp) diff --git a/src/plugins/platforms/xcb/qxcbwindow.cpp b/src/plugins/platforms/xcb/qxcbwindow.cpp index c407caae2f..0e28cf2698 100644 --- a/src/plugins/platforms/xcb/qxcbwindow.cpp +++ b/src/plugins/platforms/xcb/qxcbwindow.cpp @@ -556,6 +556,14 @@ void QXcbWindow::destroy() m_pendingSyncRequest->invalidate(); } +void QXcbWindow::maybeSetScreen(QXcbScreen *screen) +{ + if (!window()->screen() && screen->geometry().contains(geometry().topLeft() * int(devicePixelRatio()))) { + QWindowSystemInterface::handleWindowScreenChanged(window(), static_cast(screen)->screen()); + QWindowSystemInterface::handleExposeEvent(window(), QRegion(QRect(QPoint(0, 0), window()->size()))); + } +} + void QXcbWindow::setGeometry(const QRect &rect) { QPlatformWindow::setGeometry(rect); @@ -737,13 +745,15 @@ void QXcbWindow::hide() Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window)); // send synthetic UnmapNotify event according to icccm 4.1.4 - xcb_unmap_notify_event_t event; - event.response_type = XCB_UNMAP_NOTIFY; - event.event = xcbScreen()->root(); - event.window = m_window; - event.from_configure = false; - Q_XCB_CALL(xcb_send_event(xcb_connection(), false, xcbScreen()->root(), - XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event)); + if (xcbScreen()) { + xcb_unmap_notify_event_t event; + event.response_type = XCB_UNMAP_NOTIFY; + event.event = xcbScreen()->root(); + event.window = m_window; + event.from_configure = false; + Q_XCB_CALL(xcb_send_event(xcb_connection(), false, xcbScreen()->root(), + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event)); + } xcb_flush(xcb_connection()); @@ -1794,7 +1804,9 @@ void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *even QPlatformScreen *QXcbWindow::screenForNativeGeometry(const QRect &newGeometry) const { QXcbScreen *currentScreen = static_cast(screen()); - if (!parent() && !currentScreen->nativeGeometry().intersects(newGeometry)) { + if (!currentScreen && QGuiApplication::primaryScreen()) + currentScreen = static_cast(QGuiApplication::primaryScreen()->handle()); + if (currentScreen && !parent() && !currentScreen->nativeGeometry().intersects(newGeometry)) { Q_FOREACH (QPlatformScreen* screen, currentScreen->virtualSiblings()) { if (static_cast(screen)->nativeGeometry().intersects(newGeometry)) return screen; @@ -1807,7 +1819,7 @@ void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t * { bool fromSendEvent = (event->response_type & 0x80); QPoint pos(event->x, event->y); - if (!parent() && !fromSendEvent) { + if (!parent() && !fromSendEvent && xcbScreen()) { // Do not trust the position, query it instead. xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(), xcbScreen()->root(), 0, 0); @@ -1825,10 +1837,10 @@ void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t * QPlatformWindow::setGeometry(rect); QWindowSystemInterface::handleGeometryChange(window(), rect); - QPlatformScreen *newScreen = screenForNativeGeometry(nativeRect); if (newScreen != screen()) { - QWindowSystemInterface::handleWindowScreenChanged(window(), newScreen->screen()); + if (newScreen) + QWindowSystemInterface::handleWindowScreenChanged(window(), newScreen->screen()); int newDpr = devicePixelRatio(); if (newDpr != dpr) { QRect newRect = mapGeometryFromNative(nativeRect, newDpr); @@ -2116,7 +2128,7 @@ void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *ev m_windowState = newState; } return; - } else if (event->atom == atom(QXcbAtom::_NET_WORKAREA) && event->window == xcbScreen()->root()) { + } else if (event->atom == atom(QXcbAtom::_NET_WORKAREA) && xcbScreen() && event->window == xcbScreen()->root()) { xcbScreen()->updateGeometry(event->time); } } diff --git a/src/plugins/platforms/xcb/qxcbwindow.h b/src/plugins/platforms/xcb/qxcbwindow.h index 63fdb250da..981f4e7496 100644 --- a/src/plugins/platforms/xcb/qxcbwindow.h +++ b/src/plugins/platforms/xcb/qxcbwindow.h @@ -153,6 +153,7 @@ public: virtual void create(); virtual void destroy(); + void maybeSetScreen(QXcbScreen *screen); QPlatformScreen *screenForNativeGeometry(const QRect &newGeometry) const; public Q_SLOTS: -- cgit v1.2.3