diff --git a/panels/dock/x11dockhelper.cpp b/panels/dock/x11dockhelper.cpp index 378e0c2bc..897f00513 100644 --- a/panels/dock/x11dockhelper.cpp +++ b/panels/dock/x11dockhelper.cpp @@ -307,10 +307,40 @@ bool XcbEventFilter::shouldSkip(const xcb_window_t &window) return false; } -void XcbEventFilter::monitorWindowChange(const xcb_window_t &window) -{ - uint32_t value_list[] = { XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_VISIBILITY_CHANGE }; - xcb_change_window_attributes(m_connection, window, XCB_CW_EVENT_MASK, value_list); +void XcbEventFilter::monitorWindowChange(const QList &windows) +{ + const uint32_t monitorMask = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_VISIBILITY_CHANGE; + + // Queue the attribute requests before reading replies to avoid serial round trips. + QList cookies; + cookies.reserve(windows.size()); + for (auto window : windows) + cookies.append(xcb_get_window_attributes(m_connection, window)); + + for (int i = 0; i < windows.size(); ++i) { + xcb_generic_error_t *error = nullptr; + xcb_get_window_attributes_reply_t *attributes = + xcb_get_window_attributes_reply(m_connection, cookies.at(i), &error); + if (!attributes) { + if (error) { + qCWarning(dockX11Log) << "failed to query event mask of window" << windows.at(i) << "error" << error->error_code; + free(error); + } + continue; + } + + const uint32_t currentMask = attributes->your_event_mask; + free(attributes); + + const uint32_t mergedMask = currentMask | monitorMask; + if (mergedMask == currentMask) + continue; + + // This runs on the connection owned by Qt. Merging keeps Qt's own subscriptions + // (focus, keyboard, buttons, exposure) instead of replacing them. + uint32_t value_list[] = { mergedMask }; + xcb_change_window_attributes(m_connection, windows.at(i), XCB_CW_EVENT_MASK, value_list); + } }; void XcbEventFilter::setWindowState(const xcb_window_t& window, uint32_t list_len, xcb_atom_t *state) @@ -389,12 +419,17 @@ void X11DockHelper::onHideModeChanged(HideMode mode) void X11DockHelper::onWindowClientListChanged() { QList windows = m_xcbHelper->getWindowClientList(); + QList addedWindows; for (auto &&window : windows) { if (!m_windows.contains(window) && !m_xcbHelper->shouldSkip(window)) { m_windows.insert(window, new WindowData()); - onWindowAdded(window); + addedWindows.append(window); } } + m_xcbHelper->monitorWindowChange(addedWindows); + for (auto &&window : addedWindows) { + onWindowAdded(window); + } bool mightNeedRecheckDockOverlap = false; for (auto it = m_windows.cbegin(); it != m_windows.cend();) { if (!windows.contains(it.key())) { @@ -412,7 +447,6 @@ void X11DockHelper::onWindowClientListChanged() void X11DockHelper::onWindowAdded(xcb_window_t window) { - m_xcbHelper->monitorWindowChange(window); onWindowPropertyChanged(window, m_xcbHelper->getAtomByName("WM_STATE")); onWindowGeometryChanged(window); onWindowWorkspaceChanged(window); diff --git a/panels/dock/x11dockhelper.h b/panels/dock/x11dockhelper.h index 1e694b685..ec519ab8b 100644 --- a/panels/dock/x11dockhelper.h +++ b/panels/dock/x11dockhelper.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later #pragma once @@ -32,7 +32,7 @@ class XcbEventFilter: public QObject, public QAbstractNativeEventFilter uint32_t getCurrentWorkspace(); void checkCurrentWorkspace(); bool shouldSkip(const xcb_window_t& window); - void monitorWindowChange(const xcb_window_t& window); + void monitorWindowChange(const QList &windows); void setWindowState(const xcb_window_t& window, uint32_t list_len, xcb_atom_t *state); Q_SIGNALS: