From a21fd469af24ebb99b9f9909d7a64cfc595474ee Mon Sep 17 00:00:00 2001 From: luhongxu Date: Tue, 3 Nov 2026 14:07:55 +0800 Subject: [PATCH] fix: upgrade treeland output protocol from v1 to v2 with UUID support [cherry-pick] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 更新 CMakeLists 的 find_package 要求 TreelandProtocols 0.9 2. 迁移 treelandoutputwatcher 至 treeland_output_manager_v2 3. 迁移 treelandbrightness 从 color_control_v1 到 picture_control_v2 4. primary_output 事件回调从 wl_output* 改为 const QString &uuid 5. get_picture_control 请求从 wl_output* 改为 const QString &uuid 6. 新增 TreelandOutputUuidManager 和 TreelandOutputUuid 建立 wl_output→uuid 映射 7. 主屏匹配方式从指针比较改为 UUID 映射表查找 8. 补充 result 事件处理,含 commit_result 枚举(invalid_output=3) 9. 补充 primary_output_failed 事件处理 10. 缓存 pending primary uuid 处理时序问题 11. 处理屏幕移除并修复 TreelandOutputUuid 生命周期 12. 修复重复的 SPDX-FileCopyrightText 行 13. 更新 debian/control 构建依赖为 treeland-protocols (>= 0.9.0) 14. 提取 TreelandOutputUuidManager/TreelandOutputUuid 到共享头文件 shell/treelandoutputuuid.h,消除 shell 与 osd/brightness 模块间的重复定义 Log: 迁移 treeland output 协议消费者从 v1 到 v2 并适配 UUID 标识,提取共享 UUID 类消除重复代码 Influence: 多屏环境下主屏切换、OSD 亮度显示、屏幕热插拔 Ref: DDE-541 (cherry picked from commit f6363aac9632712446803808ce4396850dbcd420) --- .../osd/brightness/CMakeLists.txt | 12 +- .../osd/brightness/treelandbrightness.cpp | 111 ++++++++++++----- .../osd/brightness/treelandbrightness.h | 38 +++--- shell/CMakeLists.txt | 7 +- shell/treelandoutputuuid.cpp | 36 ++++++ shell/treelandoutputuuid.h | 41 ++++++ shell/treelandoutputwatcher.cpp | 117 +++++++++++++++--- shell/treelandoutputwatcher.h | 22 +++- 8 files changed, 311 insertions(+), 73 deletions(-) create mode 100644 shell/treelandoutputuuid.cpp create mode 100644 shell/treelandoutputuuid.h diff --git a/panels/notification/osd/brightness/CMakeLists.txt b/panels/notification/osd/brightness/CMakeLists.txt index 9a1e2b0b8..d1bd9d81a 100644 --- a/panels/notification/osd/brightness/CMakeLists.txt +++ b/panels/notification/osd/brightness/CMakeLists.txt @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -find_package(TreelandProtocols REQUIRED) +find_package(TreelandProtocols 0.9 REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(WaylandClient REQUIRED IMPORTED_TARGET wayland-client) @@ -11,12 +11,18 @@ add_library(osd-brightness SHARED brightnessapplet.h treelandbrightness.cpp treelandbrightness.h + ${CMAKE_SOURCE_DIR}/shell/treelandoutputuuid.h + ${CMAKE_SOURCE_DIR}/shell/treelandoutputuuid.cpp ) qt_generate_wayland_protocol_client_sources(osd-brightness - NO_INCLUDE_CORE_ONLY FILES - ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-manager-v1.xml + ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-manager-unstable-v2.xml + ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-uuid-unstable-v1.xml +) + +target_include_directories(osd-brightness PRIVATE + ${CMAKE_SOURCE_DIR}/shell ) target_link_libraries(osd-brightness PRIVATE diff --git a/panels/notification/osd/brightness/treelandbrightness.cpp b/panels/notification/osd/brightness/treelandbrightness.cpp index dd6b99983..63b24aa0a 100644 --- a/panels/notification/osd/brightness/treelandbrightness.cpp +++ b/panels/notification/osd/brightness/treelandbrightness.cpp @@ -4,7 +4,7 @@ #include "treelandbrightness.h" -#include "wayland-treeland-output-manager-v1-client-protocol.h" +#include "wayland-treeland-output-manager-unstable-v2-client-protocol.h" #include #include @@ -13,42 +13,53 @@ namespace osd { -TreelandColorControl::TreelandColorControl(struct ::treeland_output_color_control_v1 *object, QObject *parent) +TreelandPictureControl::TreelandPictureControl(struct ::treeland_output_picture_control_v2 *object, QObject *parent) : QObject(parent) - , QtWayland::treeland_output_color_control_v1(object) + , QtWayland::treeland_output_picture_control_v2(object) { } -TreelandColorControl::~TreelandColorControl() +TreelandPictureControl::~TreelandPictureControl() { if (isInitialized()) { destroy(); } } -double TreelandColorControl::brightness() const +double TreelandPictureControl::brightness() const { return m_brightness; } -void TreelandColorControl::treeland_output_color_control_v1_brightness(wl_fixed_t brightness) +void TreelandPictureControl::treeland_output_picture_control_v2_brightness(wl_fixed_t brightness) { m_brightness = wl_fixed_to_double(brightness); Q_EMIT brightnessChanged(m_brightness); } +void TreelandPictureControl::treeland_output_picture_control_v2_result(uint32_t result) +{ + Q_EMIT resultReceived(result); +} + TreelandBrightness::TreelandBrightness(QObject *parent) - : QWaylandClientExtensionTemplate(treeland_output_manager_v1_interface.version) + : QWaylandClientExtensionTemplate(treeland_output_manager_v2_interface.version) { setParent(parent); + + m_uuidManager = new ::TreelandOutputUuidManager(this); + connect(m_uuidManager, &::TreelandOutputUuidManager::activeChanged, this, &TreelandBrightness::refresh); connect(this, &TreelandBrightness::activeChanged, this, &TreelandBrightness::refresh); + connect(qApp, &QGuiApplication::primaryScreenChanged, this, &TreelandBrightness::refresh); } TreelandBrightness::~TreelandBrightness() { delete m_control; m_control = nullptr; - m_output = nullptr; + m_primaryScreen = nullptr; + delete m_uuidRequest; + m_uuidRequest = nullptr; if (isInitialized()) { destroy(); } @@ -56,34 +67,83 @@ TreelandBrightness::~TreelandBrightness() void TreelandBrightness::refresh() { - if (!isActive()) { + if (!isActive() || !m_uuidManager || !m_uuidManager->isActive()) { delete m_control; m_control = nullptr; - m_output = nullptr; + m_primaryScreen = nullptr; + delete m_uuidRequest; + m_uuidRequest = nullptr; + m_currentUuid.clear(); return; } - struct wl_output *output = primaryWlOutput(); - if (m_control && m_output == output && output) { + QScreen *screen = qApp->primaryScreen(); + if (m_primaryScreen == screen && m_control && !m_currentUuid.isEmpty()) { return; } delete m_control; m_control = nullptr; - m_output = output; + m_primaryScreen = screen; + m_currentUuid.clear(); - if (!output) { + if (!screen) { return; } - auto *raw = get_color_control(output); - if (!raw) { + requestPrimaryUuid(); +} + +void TreelandBrightness::requestPrimaryUuid() +{ + if (!m_uuidManager || !m_uuidManager->isActive()) return; - } - m_control = new TreelandColorControl(raw, this); - connect(m_control, &TreelandColorControl::brightnessChanged, - this, &TreelandBrightness::brightnessChanged); + if (!m_primaryScreen) + return; + + auto *waylandScreen = m_primaryScreen->nativeInterface(); + if (!waylandScreen || !waylandScreen->output()) + return; + + struct wl_output *output = waylandScreen->output(); + + delete m_uuidRequest; + m_uuidRequest = nullptr; + + auto *raw = m_uuidManager->get_uuid(output); + if (!raw) + return; + + m_uuidRequest = new ::TreelandOutputUuid(raw, this); + + connect(m_uuidRequest, &::TreelandOutputUuid::uuidReceived, this, [this](const QString &uuid) { + if (sender() != m_uuidRequest) + return; + + if (uuid.isEmpty()) + return; + + m_currentUuid = uuid; + + delete m_uuidRequest; + m_uuidRequest = nullptr; + + auto *raw = get_picture_control(uuid); + if (!raw) + return; + + m_control = new TreelandPictureControl(raw, this); + connect(m_control, &TreelandPictureControl::brightnessChanged, + this, &TreelandBrightness::brightnessChanged); + connect(m_control, &TreelandPictureControl::resultReceived, this, [this](uint32_t result) { + if (result == TreelandPictureControl::commit_result_invalid_output) { + delete m_control; + m_control = nullptr; + m_currentUuid.clear(); + } + }); + }); } double TreelandBrightness::brightness() const @@ -91,15 +151,4 @@ double TreelandBrightness::brightness() const return m_control ? m_control->brightness() : 0.0; } -struct wl_output *TreelandBrightness::primaryWlOutput() const -{ - auto *screen = qApp->primaryScreen(); - if (!screen) { - return nullptr; - } - - auto *waylandScreen = screen->nativeInterface(); - return waylandScreen ? waylandScreen->output() : nullptr; -} - } // namespace osd diff --git a/panels/notification/osd/brightness/treelandbrightness.h b/panels/notification/osd/brightness/treelandbrightness.h index ea87d8a92..7256a84f7 100644 --- a/panels/notification/osd/brightness/treelandbrightness.h +++ b/panels/notification/osd/brightness/treelandbrightness.h @@ -4,43 +4,49 @@ #pragma once -#include "qwayland-treeland-output-manager-v1.h" +#include "treelandoutputuuid.h" +#include "qwayland-treeland-output-manager-unstable-v2.h" #include #include #include struct wl_output; -struct treeland_output_color_control_v1; +struct treeland_output_picture_control_v2; +struct treeland_output_uuid_v1; + +class QScreen; namespace osd { -class TreelandColorControl : public QObject, public QtWayland::treeland_output_color_control_v1 +class TreelandPictureControl : public QObject, public QtWayland::treeland_output_picture_control_v2 { Q_OBJECT public: - explicit TreelandColorControl(struct ::treeland_output_color_control_v1 *object, QObject *parent = nullptr); - ~TreelandColorControl() override; + explicit TreelandPictureControl(struct ::treeland_output_picture_control_v2 *object, QObject *parent = nullptr); + ~TreelandPictureControl() override; double brightness() const; Q_SIGNALS: void brightnessChanged(double brightness); + void resultReceived(uint32_t result); protected: - void treeland_output_color_control_v1_brightness(wl_fixed_t brightness) override; + void treeland_output_picture_control_v2_brightness(wl_fixed_t brightness) override; + void treeland_output_picture_control_v2_result(uint32_t result) override; private: double m_brightness = 0.0; }; // Read-only Treeland brightness provider for the OSD. It binds a single -// treeland_output_color_control_v1 to the primary wl_output and caches the -// brightness reported by the compositor. It never commits brightness changes; -// dde-shortcut-tool is responsible for adjusting brightness, and this provider -// only reflects the resulting value. -class TreelandBrightness : public QWaylandClientExtensionTemplate, public QtWayland::treeland_output_manager_v1 +// treeland_output_picture_control_v2 to the primary output (identified by uuid) +// and caches the brightness reported by the compositor. It never commits +// brightness changes; dde-shortcut-tool is responsible for adjusting brightness, +// and this provider only reflects the resulting value. +class TreelandBrightness : public QWaylandClientExtensionTemplate, public QtWayland::treeland_output_manager_v2 { Q_OBJECT @@ -56,10 +62,14 @@ class TreelandBrightness : public QWaylandClientExtensionTemplate m_control; + QPointer m_primaryScreen; + QString m_currentUuid; - QPointer m_control; - struct wl_output *m_output = nullptr; + ::TreelandOutputUuidManager *m_uuidManager = nullptr; + QPointer<::TreelandOutputUuid> m_uuidRequest; }; } // namespace osd diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt index 8f6adb1c9..d5932f612 100644 --- a/shell/CMakeLists.txt +++ b/shell/CMakeLists.txt @@ -4,7 +4,7 @@ find_package(Dtk${DTK_VERSION_MAJOR} COMPONENTS Widget REQUIRED) find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Widgets Gui WaylandClient) -find_package(TreelandProtocols REQUIRED) +find_package(TreelandProtocols 0.9 REQUIRED) pkg_check_modules(WaylandClient REQUIRED IMPORTED_TARGET wayland-client) if (NOT DEFINED SYSTEMD_USER_UNIT_DIR) @@ -23,6 +23,8 @@ add_executable(dde-shell shell.cpp treelandoutputwatcher.h treelandoutputwatcher.cpp + treelandoutputuuid.h + treelandoutputuuid.cpp dde-shell.qrc override_dtkdeclarative_qml.qrc dde-shell-icons.qrc @@ -30,7 +32,8 @@ add_executable(dde-shell qt_generate_wayland_protocol_client_sources(dde-shell FILES - ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-manager-v1.xml + ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-manager-unstable-v2.xml + ${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-uuid-unstable-v1.xml ) target_compile_definitions(dde-shell diff --git a/shell/treelandoutputuuid.cpp b/shell/treelandoutputuuid.cpp new file mode 100644 index 000000000..f077eeb7e --- /dev/null +++ b/shell/treelandoutputuuid.cpp @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "treelandoutputuuid.h" +#include "wayland-treeland-output-uuid-unstable-v1-client-protocol.h" + +TreelandOutputUuidManager::TreelandOutputUuidManager(QObject *parent) + : QWaylandClientExtensionTemplate(treeland_output_uuid_manager_v1_interface.version) +{ + setParent(parent); +} + +TreelandOutputUuidManager::~TreelandOutputUuidManager() +{ + if (isInitialized()) + destroy(); +} + +TreelandOutputUuid::TreelandOutputUuid(struct ::treeland_output_uuid_v1 *object, QObject *parent) + : QObject(parent) + , QtWayland::treeland_output_uuid_v1(object) +{ +} + +TreelandOutputUuid::~TreelandOutputUuid() +{ + if (isInitialized()) + destroy(); +} + +void TreelandOutputUuid::treeland_output_uuid_v1_uuid(const QString &uuid) +{ + m_uuid = uuid; + Q_EMIT uuidReceived(uuid); +} diff --git a/shell/treelandoutputuuid.h b/shell/treelandoutputuuid.h new file mode 100644 index 000000000..8a9c87692 --- /dev/null +++ b/shell/treelandoutputuuid.h @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include "qwayland-treeland-output-uuid-unstable-v1.h" +#include +#include +#include + +class TreelandOutputUuidManager + : public QWaylandClientExtensionTemplate + , public QtWayland::treeland_output_uuid_manager_v1 +{ + Q_OBJECT +public: + TreelandOutputUuidManager(QObject *parent = nullptr); + ~TreelandOutputUuidManager(); +}; + +class TreelandOutputUuid + : public QObject + , public QtWayland::treeland_output_uuid_v1 +{ + Q_OBJECT +public: + TreelandOutputUuid(struct ::treeland_output_uuid_v1 *object, QObject *parent = nullptr); + ~TreelandOutputUuid() override; + + QString uuid() const { return m_uuid; } + +Q_SIGNALS: + void uuidReceived(const QString &uuid); + +protected: + void treeland_output_uuid_v1_uuid(const QString &uuid) override; + +private: + QString m_uuid; +}; diff --git a/shell/treelandoutputwatcher.cpp b/shell/treelandoutputwatcher.cpp index c71da0e2f..3621b6df4 100644 --- a/shell/treelandoutputwatcher.cpp +++ b/shell/treelandoutputwatcher.cpp @@ -3,52 +3,135 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "treelandoutputwatcher.h" -#include "wayland-treeland-output-manager-v1-client-protocol.h" +#include "wayland-treeland-output-manager-unstable-v2-client-protocol.h" #include #include +#include +#include #include +Q_LOGGING_CATEGORY(ddeShellOutputWatcher, "dde-shell.output-watcher") + TreelandOutputWatcher::TreelandOutputWatcher(QObject *parent) - : QWaylandClientExtensionTemplate(treeland_output_manager_v1_interface.version) + : QWaylandClientExtensionTemplate(treeland_output_manager_v2_interface.version) { setParent(parent); connect(qApp, &QGuiApplication::screenAdded, this, &TreelandOutputWatcher::onScreenAdded); + connect(qApp, &QGuiApplication::screenRemoved, this, &TreelandOutputWatcher::onScreenRemoved); + + m_uuidManager = new TreelandOutputUuidManager(this); + connect(m_uuidManager, &TreelandOutputUuidManager::activeChanged, this, &TreelandOutputWatcher::onUuidManagerActiveChanged); + onUuidManagerActiveChanged(); } TreelandOutputWatcher::~TreelandOutputWatcher() { if (isInitialized()) destroy(); + + for (const auto &obj : m_uuidRequests) { + if (obj) + delete obj.data(); + } + m_uuidRequests.clear(); + m_uuidToScreen.clear(); } -void TreelandOutputWatcher::onScreenAdded(QScreen *) +void TreelandOutputWatcher::onUuidManagerActiveChanged() { - if (!m_pendingPrimaryName.isEmpty()) - applyPrimary(m_pendingPrimaryName); + if (m_uuidManager && m_uuidManager->isActive()) { + for (auto *screen : qApp->screens()) + requestUuid(screen); + } } -void TreelandOutputWatcher::treeland_output_manager_v1_primary_output(const QString &output_name) +void TreelandOutputWatcher::requestUuid(QScreen *screen) { - applyPrimary(output_name); + if (!screen || !m_uuidManager || !m_uuidManager->isActive()) + return; + + if (m_uuidRequests.contains(screen)) + return; + + auto *waylandScreen = screen->nativeInterface(); + if (!waylandScreen || !waylandScreen->output()) + return; + + auto *raw = m_uuidManager->get_uuid(waylandScreen->output()); + if (!raw) + return; + + auto *uuidObj = new TreelandOutputUuid(raw, this); + m_uuidRequests.insert(screen, uuidObj); + + connect(uuidObj, &TreelandOutputUuid::uuidReceived, this, [this, screen](const QString &uuid) { + if (auto obj = m_uuidRequests.take(screen)) + obj->deleteLater(); + if (!uuid.isEmpty()) + m_uuidToScreen.insert(uuid, screen); + + if (!m_pendingPrimaryUuid.isEmpty()) + applyPrimary(m_pendingPrimaryUuid); + }); } -void TreelandOutputWatcher::applyPrimary(const QString &output_name) +void TreelandOutputWatcher::onScreenAdded(QScreen *screen) { - if (qApp->primaryScreen() && qApp->primaryScreen()->name() == output_name) { - m_pendingPrimaryName.clear(); + requestUuid(screen); + + if (!m_pendingPrimaryUuid.isEmpty()) + applyPrimary(m_pendingPrimaryUuid); +} + +void TreelandOutputWatcher::onScreenRemoved(QScreen *screen) +{ + for (auto it = m_uuidToScreen.begin(); it != m_uuidToScreen.end();) { + if (it.value() == screen) + it = m_uuidToScreen.erase(it); + else + ++it; + } + if (auto obj = m_uuidRequests.take(screen)) + delete obj.data(); +} + +void TreelandOutputWatcher::treeland_output_manager_v2_primary_output(const QString &uuid) +{ + applyPrimary(uuid); +} + +void TreelandOutputWatcher::treeland_output_manager_v2_primary_output_failed(uint32_t reason) +{ + qCWarning(ddeShellOutputWatcher) << "primary_output_failed, reason:" << reason; + m_pendingPrimaryUuid.clear(); +} + +void TreelandOutputWatcher::applyPrimary(const QString &uuid) +{ + if (uuid.isEmpty()) { + m_pendingPrimaryUuid.clear(); + return; + } + + auto it = m_uuidToScreen.find(uuid); + if (it == m_uuidToScreen.end()) { + m_pendingPrimaryUuid = uuid; return; } - for (auto screen : qApp->screens()) { - if (screen->name() == output_name) { - m_pendingPrimaryName.clear(); - QWindowSystemInterface::handlePrimaryScreenChanged(screen->handle()); - return; - } + QPointer screen = it.value(); + if (!screen) { + m_pendingPrimaryUuid = uuid; + return; + } + if (qApp->primaryScreen() == screen) { + m_pendingPrimaryUuid.clear(); + return; } - m_pendingPrimaryName = output_name; + m_pendingPrimaryUuid.clear(); + QWindowSystemInterface::handlePrimaryScreenChanged(screen->handle()); } diff --git a/shell/treelandoutputwatcher.h b/shell/treelandoutputwatcher.h index 33413a0c7..f5c3cc03e 100644 --- a/shell/treelandoutputwatcher.h +++ b/shell/treelandoutputwatcher.h @@ -4,13 +4,16 @@ #pragma once -#include "qwayland-treeland-output-manager-v1.h" +#include "treelandoutputuuid.h" +#include "qwayland-treeland-output-manager-unstable-v2.h" #include #include +#include +#include class QScreen; -class TreelandOutputWatcher : public QWaylandClientExtensionTemplate, public QtWayland::treeland_output_manager_v1 +class TreelandOutputWatcher : public QWaylandClientExtensionTemplate, public QtWayland::treeland_output_manager_v2 { Q_OBJECT public: @@ -18,11 +21,18 @@ class TreelandOutputWatcher : public QWaylandClientExtensionTemplate> m_uuidRequests; + QHash> m_uuidToScreen; + QString m_pendingPrimaryUuid; };