Skip to content

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Jan 23, 2026

  1. Implement color control protocol support for brightness adjustment
  2. Add scale factor consideration in screen dimension calculations
  3. Improve Wayland output management with proper event handling
  4. Add brightness synchronization between compositor and display settings

Log: Added brightness control support for Treeland compositor

Influence:

  1. Test brightness adjustment on monitors with Treeland compositor
  2. Verify screen resolution calculations account for scale factor
  3. Check monitor detection and removal with brightness controls
  4. Test multi-monitor setup with individual brightness settings
  5. Verify brightness changes persist across display configuration changes

feat: 为 Treeland 合成器添加亮度控制功能

  1. 实现颜色控制协议支持亮度调节
  2. 在屏幕尺寸计算中考虑缩放因子
  3. 改进 Wayland 输出管理,完善事件处理
  4. 添加合成器与显示设置之间的亮度同步

Log: 新增 Treeland 合成器的亮度控制支持

Influence:

  1. 在 Treeland 合成器环境下测试显示器亮度调节
  2. 验证屏幕分辨率计算是否考虑缩放因子
  3. 检查带亮度控制的显示器检测和移除功能
  4. 测试多显示器环境下的独立亮度设置
  5. 验证亮度变更在显示配置更改后是否持久化

PMS: BUG-344299

Summary by Sourcery

Introduce Treeland compositor color control protocol support and wire it into display handling for brightness-aware output management.

New Features:

  • Add Treeland output color control Wayland protocol, including per-output brightness and color temperature control with commit/result events.
  • Enable per-monitor brightness control integration with Treeland compositor via TreeLandOutputManager and DisplayWorker mappings between monitors and Wayland outputs.

Bug Fixes:

  • Ensure Treeland output manager initialization and monitor list updates are driven by registry interface registration events, improving robustness on wlroots-based compositors.

Enhancements:

  • Update Wayland registry and output handling to manage Treeland output manager v2, track output readiness, and react to dynamic output add/remove events.
  • Adjust DccScreen dimensions to account for monitor scale factors when reporting width and height.

@caixr23 caixr23 requested a review from mhduiy January 23, 2026 05:48
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 23, 2026

Reviewer's Guide

Adds Treeland-specific per-output brightness control based on a new color-control Wayland protocol, wires it into the display worker and registry/output abstractions, and fixes screen dimension reporting to account for output scale factors.

Sequence diagram for Treeland per-output brightness update and sync

sequenceDiagram
    actor User
    participant DisplayUI
    participant DisplayWorker
    participant Monitor
    participant TreeLandOutputManager
    participant ColorControl as treeland_output_color_control_v1
    participant TreelandCompositor

    User->>DisplayUI: Adjust monitor brightness slider
    DisplayUI->>DisplayWorker: setMonitorBrightness(monitor, newBrightness)
    DisplayWorker->>DisplayWorker: lookup ColorControl in m_control_monitors
    alt ColorControl found
        DisplayWorker->>TreeLandOutputManager: setBrightness(ColorControl, newBrightness * 100.0)
        TreeLandOutputManager->>ColorControl: treeland_output_color_control_v1_set_brightness()
        TreeLandOutputManager->>ColorControl: treeland_output_color_control_v1_commit()
        ColorControl->>TreelandCompositor: commit brightness request
        TreelandCompositor-->>ColorControl: brightness event (current brightness)
        ColorControl-->>TreeLandOutputManager: handleBrightness(data, obj, value)
        TreeLandOutputManager-->>DisplayWorker: brightnessChanged(ColorControl, value)
        DisplayWorker->>DisplayWorker: onBrightnessChanged(ColorControl, value)
        DisplayWorker->>Monitor: setBrightness(value / 100.0)
    else ColorControl not found
        DisplayWorker->>DisplayWorker: No-op (no color control for monitor)
    end
Loading

Updated class diagram for Treeland output brightness control integration

classDiagram
    class DisplayWorker {
        - WQt_Registry* m_reg
        - QMap~Monitor*, WQt_OutputHead*~ m_wl_monitors
        - QMap~Monitor*, treeland_output_color_control_v1*~ m_control_monitors
        + onInterfaceRegistered(interface: WQt_Registry_Interface) void
        + onWlMonitorListChanged() void
        + wlMonitorAdded(head: WQt_OutputHead*) void
        + wlMonitorRemoved(head: WQt_OutputHead*) void
        + wlOutputAdded(output: WQt_Output*) void
        + wlOutputRemoved(output: WQt_Output*) void
        + updateControl() void
        + setMonitorBrightness(mon: Monitor*, brightness: double) void
        + onBrightnessChanged(colorControl: treeland_output_color_control_v1*, brightness: double) void
    }

    class TreeLandOutputManager {
        - treeland_output_manager_v1* mObj
        + setPrimaryOutput(name: const char*) void
        + getColorControl(output: wl_output*) treeland_output_color_control_v1*
        + setBrightness(colorControl: treeland_output_color_control_v1*, brightness: double) void
        + destroyColorControl(colorControl: treeland_output_color_control_v1*) void
        + primaryOutputChanged(outputName: const char*) void
        + brightnessChanged(colorControl: treeland_output_color_control_v1*, brightness: double) void
        + colorTemperatureChanged(colorControl: treeland_output_color_control_v1*, temperature: uint) void
        - handlePrimaryOutput(data: void*, mgr: treeland_output_manager_v1*, output_name: const char*) void
        - handleResult(data: void*, colorControl: treeland_output_color_control_v1*, success: uint32_t) void
        - handleColorTemperature(data: void*, colorControl: treeland_output_color_control_v1*, temperature: uint32_t) void
        - handleBrightness(data: void*, colorControl: treeland_output_color_control_v1*, brightness: wl_fixed_t) void
        - static treeland_output_manager_v1_listener mListener
        - static treeland_output_color_control_v1_listener mColorControlListener
    }

    class Output {
        - bool mDone
        + scale() int32_t
        + isReady() bool
        + waitForReady() void
        + get() wl_output*
        + done() void
    }

    class Registry {
        + setup() void
        + outputManager() OutputManager*
        + treeLandOutputManager() TreeLandOutputManager*
        + waylandOutputs() QList~WQt_Output*~
        + interfaceRegistered(interface: WQt_Registry_Interface) void
    }

    class Monitor {
        + name() QString
        + w() int
        + h() int
        + scale() int
        + setBrightness(value: double) void
    }

    DisplayWorker --> Registry : uses
    DisplayWorker --> TreeLandOutputManager : uses
    DisplayWorker --> Monitor : maps via m_wl_monitors and m_control_monitors
    DisplayWorker --> Output : uses in updateControl()
    Registry --> Output : manages list of Wayland outputs
    Registry --> TreeLandOutputManager : creates and owns

    class treeland_output_manager_v1 {
    }
    class treeland_output_color_control_v1 {
    }
    treeland_output_manager_v1 <.. TreeLandOutputManager : wraps
    treeland_output_color_control_v1 <.. TreeLandOutputManager : creates and controls
    Monitor "1" --> "0..1" treeland_output_color_control_v1 : brightness control handle
Loading

Class diagram for updated Treeland Wayland protocol interfaces

classDiagram
    class treeland_output_manager_v1 {
        <<interface>>
        + set_primary_output(output: string) void
        + get_color_control(id: treeland_output_color_control_v1*, output: wl_output*) void
        + destroy() void
        + primary_output(output_name: string)
    }

    class treeland_output_color_control_v1 {
        <<interface>>
        + set_color_temperature(temperature: uint) void
        + set_brightness(brightness: wl_fixed_t) void
        + commit() void
        + destroy() void
        + result(success: uint)
        + color_temperature(temperature: uint)
        + brightness(brightness: wl_fixed_t)
    }

    class treeland_output_color_control_v1_error {
        <<enum>>
        invalid_color_temperature = 1
        invalid_brightness = 2
    }

    treeland_output_manager_v1 --> treeland_output_color_control_v1 : creates per wl_output
    treeland_output_color_control_v1 ..> treeland_output_color_control_v1_error : uses error codes
Loading

File-Level Changes

Change Details Files
Extend Treeland Wayland protocol to support per-output color/brightness control and bump manager version.
  • Bump treeland_output_manager_v1 protocol to version 2 and add get_color_control request.
  • Introduce treeland_output_color_control_v1 interface with requests for color temperature, brightness, commit, and associated events and error enum.
src/plugin-display/wayland/libwayqt/protocols/treeland-output-management.xml
Integrate Treeland color control into DisplayWorker for brightness synchronization and output lifecycle handling.
  • Change Treeland registry setup to use interfaceRegistered callback instead of immediate outputManager access, and hook OutputManager::done to onWlMonitorListChanged.
  • Track wl_output objects and bind them to treeland_output_color_control_v1 controls per Monitor via updateControl, reacting to WQt::Output::done and registry outputAdded/outputRemoved events.
  • Route brightness change events from TreeLandOutputManager to monitors via onBrightnessChanged, and send brightness updates back using setMonitorBrightness and TreeLandOutputManager::setBrightness.
  • Clean up color control objects on wlMonitorRemoved and maintain a Monitor→color_control map.
src/plugin-display/operation/private/displayworker.cpp
src/plugin-display/operation/private/displayworker.h
Implement Treeland color control client wrapper on top of the generated protocol.
  • Add TreeLandOutputManager helpers to create/destroy treeland_output_color_control_v1 objects and to set/commit brightness.
  • Register and implement treeland_output_color_control_v1_listener callbacks, emitting Qt signals for brightness and color temperature changes.
  • Keep treeland_output_manager_v1 listener as before and add a static listener for color control objects.
src/plugin-display/wayland/libwayqt/TreeLandOutputManager.cpp
src/plugin-display/wayland/libwayqt/TreeLandOutputManager.hpp
Expose WQt::Output readiness as a signal and helper used for color control setup.
  • Add isReady() accessor exposing mDone state and emit a done() Qt signal when the wl_output done event fires.
src/plugin-display/wayland/libwayqt/Output.cpp
src/plugin-display/wayland/libwayqt/Output.hpp
Make DccScreen logical size respect per-monitor scale factor.
  • Change DccScreen::width()/height() to divide monitor width/height by monitor scale, so reported dimensions are in logical coordinates.
src/plugin-display/operation/dccscreen.cpp
Bind Treeland output manager with protocol version 2 to match the new color-control requests.
  • Update wl_registry_bind for treeland_output_manager_v1 to request version 2 instead of 1.
src/plugin-display/wayland/libwayqt/Registry.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • onWlMonitorListChanged() wires outputAdded/outputRemoved signals every time it runs, which can lead to duplicate connections and repeated callbacks; consider moving these connects to a one-time initialization path instead.
  • wlOutputRemoved() is currently empty while you create per-output color controls in updateControl(); you likely need to destroy the associated color_control object and remove its entry from m_control_monitors when an output is removed to avoid leaks and stale mappings.
  • TreeLandOutputManager::handleResult() is a no-op even though the protocol exposes success/failure for commits; consider emitting a signal or handling non-success values so callers can react to failed brightness/color changes instead of assuming they always succeed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- onWlMonitorListChanged() wires outputAdded/outputRemoved signals every time it runs, which can lead to duplicate connections and repeated callbacks; consider moving these connects to a one-time initialization path instead.
- wlOutputRemoved() is currently empty while you create per-output color controls in updateControl(); you likely need to destroy the associated color_control object and remove its entry from m_control_monitors when an output is removed to avoid leaks and stale mappings.
- TreeLandOutputManager::handleResult() is a no-op even though the protocol exposes success/failure for commits; consider emitting a signal or handling non-success values so callers can react to failed brightness/color changes instead of assuming they always succeed.

## Individual Comments

### Comment 1
<location> `src/plugin-display/operation/private/displayworker.cpp:231-235` </location>
<code_context>
         if (isNew)
             wlMonitorAdded(head);
     }
+    for(auto output:m_reg->waylandOutputs()){
+        wlOutputAdded(output);
+    }
+    connect(m_reg,&WQt::Registry::outputAdded,this,&DisplayWorker::wlOutputAdded);
+    connect(m_reg,&WQt::Registry::outputRemoved,this,&DisplayWorker::wlOutputRemoved);
 }

</code_context>

<issue_to_address>
**issue (bug_risk):** Avoid reconnecting registry signals and reprocessing outputs every time onWlMonitorListChanged runs.

Because onWlMonitorListChanged iterates over all outputs and then connects to outputAdded/outputRemoved on m_reg, if this method is called more than once it will re-run wlOutputAdded on existing outputs and create duplicate signal connections. That will cause each outputAdded/outputRemoved event to trigger handlers multiple times. Please move the signal connections to a one-time initialization point (e.g., the Treeland constructor branch or onInterfaceRegistered) and either perform the initial output scan only once or guard both the scan and connections with a flag.
</issue_to_address>

### Comment 2
<location> `src/plugin-display/wayland/libwayqt/TreeLandOutputManager.cpp:49-52` </location>
<code_context>
+    }
+}
+
+void WQt::TreeLandOutputManager::destroyColorControl(treeland_output_color_control_v1 *treeland_output_color_control_v1)
+{
+    treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
+}
+
</code_context>

<issue_to_address>
**suggestion:** Guard destroyColorControl against null pointers for safety and consistency.

`setBrightness` guards `treeland_output_color_control_v1` against null, but this function does not. Even if current callers never pass null, adding the same guard here would make the API safer and consistent:

```cpp
if (!treeland_output_color_control_v1)
    return;

treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
```

```suggestion
void WQt::TreeLandOutputManager::destroyColorControl(treeland_output_color_control_v1 *treeland_output_color_control_v1)
{
    if (!treeland_output_color_control_v1) {
        return;
    }

    treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
}
```
</issue_to_address>

### Comment 3
<location> `src/plugin-display/wayland/libwayqt/protocols/treeland-output-management.xml:74-75` </location>
<code_context>
+                Color temperature is defined as the corresponding temperature (in Kelvin) of the current white point
+                of the display on a Planckian locus.
+                With the current implementation, the neutral temperature is 6600K.
+                This event is sent once after the treeland_output_color_control_v1 object is created,
+                or right after when a color temperature change for the output is successfully commited.
+            </description>
+            <arg name="temperature" type="uint" summary="current color temperature in Kelvin"/>
</code_context>

<issue_to_address>
**nitpick (typo):** Fix minor wording and spelling issues in the protocol documentation.

Two small issues here: "commited" → "committed", and "or right after when" reads awkwardly. Consider something like:

```xml
This event is sent once after the treeland_output_color_control_v1 object is created,
or immediately after a color temperature change for the output is successfully committed.
```

Suggested implementation:

```
                With the current implementation, the neutral temperature is 6600K.
                This event is sent once after the treeland_output_color_control_v1 object is created,
                or immediately after a color temperature change for the output is successfully committed.

```

```
                Brightness is valued in the range [0.0, 100.0].
                This event is sent once after the treeland_output_color_control_v1 object is created,
                or immediately after a brightness change for the output is successfully committed.

```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 231 to 235
for(auto output:m_reg->waylandOutputs()){
wlOutputAdded(output);
}
connect(m_reg,&WQt::Registry::outputAdded,this,&DisplayWorker::wlOutputAdded);
connect(m_reg,&WQt::Registry::outputRemoved,this,&DisplayWorker::wlOutputRemoved);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Avoid reconnecting registry signals and reprocessing outputs every time onWlMonitorListChanged runs.

Because onWlMonitorListChanged iterates over all outputs and then connects to outputAdded/outputRemoved on m_reg, if this method is called more than once it will re-run wlOutputAdded on existing outputs and create duplicate signal connections. That will cause each outputAdded/outputRemoved event to trigger handlers multiple times. Please move the signal connections to a one-time initialization point (e.g., the Treeland constructor branch or onInterfaceRegistered) and either perform the initial output scan only once or guard both the scan and connections with a flag.

Comment on lines +49 to +52
void WQt::TreeLandOutputManager::destroyColorControl(treeland_output_color_control_v1 *treeland_output_color_control_v1)
{
treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Guard destroyColorControl against null pointers for safety and consistency.

setBrightness guards treeland_output_color_control_v1 against null, but this function does not. Even if current callers never pass null, adding the same guard here would make the API safer and consistent:

if (!treeland_output_color_control_v1)
    return;

treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
Suggested change
void WQt::TreeLandOutputManager::destroyColorControl(treeland_output_color_control_v1 *treeland_output_color_control_v1)
{
treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
}
void WQt::TreeLandOutputManager::destroyColorControl(treeland_output_color_control_v1 *treeland_output_color_control_v1)
{
if (!treeland_output_color_control_v1) {
return;
}
treeland_output_color_control_v1_destroy(treeland_output_color_control_v1);
}

Comment on lines 74 to 75
This event is sent once after the treeland_output_color_control_v1 object is created,
or right after when a color temperature change for the output is successfully commited.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Fix minor wording and spelling issues in the protocol documentation.

Two small issues here: "commited" → "committed", and "or right after when" reads awkwardly. Consider something like:

This event is sent once after the treeland_output_color_control_v1 object is created,
or immediately after a color temperature change for the output is successfully committed.

Suggested implementation:

                With the current implementation, the neutral temperature is 6600K.
                This event is sent once after the treeland_output_color_control_v1 object is created,
                or immediately after a color temperature change for the output is successfully committed.

                Brightness is valued in the range [0.0, 100.0].
                This event is sent once after the treeland_output_color_control_v1 object is created,
                or immediately after a brightness change for the output is successfully committed.

@caixr23 caixr23 force-pushed the bug-display branch 2 times, most recently from d0174e4 to 89093be Compare January 26, 2026 05:18
@caixr23 caixr23 force-pushed the bug-display branch 5 times, most recently from 2539921 to c247aa5 Compare January 28, 2026 07:47

ws_generate_local(client
${CMAKE_CURRENT_SOURCE_DIR}/wayland/libwayqt/protocols/treeland-output-management.xml
${TREELAND_PROTOCOLS_DATA_DIR}/treeland-output-manager-v1.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable_TreelandSupport 没有包裹这里的胶水代码生成,和cpp的treelandoutputmanager,就没啥意义,要么删掉Enable_TreelandSupport,要么把这些地方都包裹上

@deepin-bot
Copy link

deepin-bot bot commented Jan 28, 2026

TAG Bot

New tag: 6.1.70
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #2964

@caixr23 caixr23 force-pushed the bug-display branch 2 times, most recently from e1c7ce7 to 3eb30bf Compare January 28, 2026 09:01
1. Implement color control protocol support for brightness adjustment
2. Add scale factor consideration in screen dimension calculations
3. Improve Wayland output management with proper event handling
4. Add brightness synchronization between compositor and display
settings

Log: Added brightness control support for Treeland compositor

Influence:
1. Test brightness adjustment on monitors with Treeland compositor
2. Verify screen resolution calculations account for scale factor
3. Check monitor detection and removal with brightness controls
4. Test multi-monitor setup with individual brightness settings
5. Verify brightness changes persist across display configuration
changes

feat: 为 Treeland 合成器添加亮度控制功能

1. 实现颜色控制协议支持亮度调节
2. 在屏幕尺寸计算中考虑缩放因子
3. 改进 Wayland 输出管理,完善事件处理
4. 添加合成器与显示设置之间的亮度同步

Log: 新增 Treeland 合成器的亮度控制支持

Influence:
1. 在 Treeland 合成器环境下测试显示器亮度调节
2. 验证屏幕分辨率计算是否考虑缩放因子
3. 检查带亮度控制的显示器检测和移除功能
4. 测试多显示器环境下的独立亮度设置
5. 验证亮度变更在显示配置更改后是否持久化

PMS: BUG-344299
@deepin-ci-robot
Copy link

deepin pr auto review

Git Diff 代码审查报告

1. 总体评估

这次提交主要涉及 Wayland 显示管理相关的代码变更,包括:

  • 依赖项更新(添加 wlr-protocols)
  • 头文件扩展名统一(从.hpp改为.h)
  • 代码版权声明更新
  • 显示缩放处理逻辑修改
  • 亮度控制功能增强
  • Wayland 协议处理改进

整体代码质量较好,但存在一些可以改进的地方。

2. 语法与逻辑问题

2.1 代码风格不一致

// src/plugin-display/wayland/libwayqt/TreeLandOutputManager.h
void setBrightness(treeland_output_color_control_v1 * treeland_output_color_control_v1,const double brightness);

参数名与类型名相同,且参数前有多余空格,建议修改为:

void setBrightness(treeland_output_color_control_v1 *control, const double brightness);

2.2 条件判断优化

// src/plugin-display/operation/dccscreen.cpp
return d_ptrDccScreen->monitor()->scale() > 0 ? (d_ptrDccScreen->monitor()->w() / d_ptrDccScreen->monitor()->scale()) : d_ptrDccScreen->monitor()->w();

这段代码在 width() 和 height() 方法中重复出现,建议提取为私有方法:

int DccScreen::scaledValue(int value) const
{
    int scale = d_ptrDccScreen->monitor()->scale();
    return scale > 0 ? value / scale : value;
}

2.3 逻辑顺序问题

// src/plugin-display/operation/private/displayworker.cpp
void DisplayWorker::onInterfaceRegistered(WQt::Registry::Interface interface)
{
    switch (interface) {
    case WQt::Registry::OutputManagerInterface: {
        auto *opMgr = m_reg->outputManager();
        if (!opMgr) {
            qCFatal(DdcDisplayWorker) << "Unable to start the output manager";
        }
        connect(opMgr, &WQt::OutputManager::done, this, &DisplayWorker::onWlOutputManagerDone);
    } break;

在检查 opMgr 是否为空之前,应该先确保 m_reg 不为空,否则可能导致空指针解引用。

3. 代码质量问题

3.1 魔法数字

// src/plugin-display/operation/private/displayworker.cpp
brightness = qBound(0.0, brightness / 100.0, 1.0);

硬编码的 100.0 应该定义为常量:

static constexpr double BRIGHTNESS_CONVERSION_FACTOR = 100.0;

3.2 错误处理不完整

// src/plugin-display/wayland/libwayqt/TreeLandOutputManager.cpp
void WQt::TreeLandOutputManager::handleResult(void *data, treeland_output_color_control_v1 *treeland_output_color_control_v1, uint32_t success)
{
    // TODO: handleResult
}

TODO 注释表明错误处理未实现,应该添加适当的错误处理逻辑。

3.3 资源管理

// src/plugin-display/wayland/libwayqt/TreeLandOutputManager.cpp
treeland_output_color_control_v1 *WQt::TreeLandOutputManager::getColorControl(struct wl_output *output)
{
    auto colorControl = treeland_output_manager_v1_get_color_control(mObj, output);
    if (colorControl) {
        treeland_output_color_control_v1_add_listener(colorControl, &mColorControlListener, this);
    }
    return colorControl;
}

返回的 colorControl 对象的生命周期管理不明确,应该明确说明由谁负责释放。

4. 代码性能问题

4.1 不必要的循环等待

// src/plugin-display/wayland/libwayqt/Output.cpp
void WQt::Output::waitForReady()
{
    if (mDone) {
        return;
    }

    do {
        QThread::usleep(100);
        qApp->processEvents();
    } while (not mDone);
}

虽然这段代码在 diff 中被删除了,但原来的实现存在性能问题。新实现的 isReady() 方法更好,但应该考虑添加异步通知机制。

4.2 重复查找

// src/plugin-display/operation/private/displayworker.cpp
void DisplayWorker::updateControl()
{
    for (auto output : m_reg->waylandOutputs()) {
        if (output->isReady()) {
            for (auto it(m_wl_monitors.cbegin()); it != m_wl_monitors.cend(); ++it) {
                if (it.key()->name() == output->name()) {
                    if (!m_control_monitors.contains(it.key())) {
                        auto control = m_reg->treeLandOutputManager()->getColorControl(output->get());
                        m_control_monitors.insert(it.key(), control);
                    }
                    break;
                }
            }
        }
    }
}

嵌套循环可能导致性能问题,特别是当显示器数量较多时。可以考虑使用哈希表来优化查找。

5. 代码安全问题

5.1 空指针检查

// src/plugin-display/operation/private/displayworker.cpp
void DisplayWorker::setMonitorBrightness(Monitor *mon, const double brightness)
{
    if (WQt::Utils::isTreeland()) {
#ifndef DCC_DISABLE_ROTATE
        auto *gammaConfig = m_wl_gammaConfig->value(mon);
        gammaConfig->brightness = brightness;
        gammaEffect->setConfiguration(*gammaConfig);
#else
        for (auto it(m_control_monitors.cbegin()); it != m_control_monitors.cend(); ++it) {
            if (it.key() == mon) {
                m_reg->treeLandOutputManager()->setBrightness(it.value(), brightness * 100.0);
                break;
            }
        }
#endif
    }

在访问 m_reg->treeLandOutputManager() 之前,应该检查 m_reg 是否为空。

5.2 边界检查

// src/plugin-display/operation/private/displayworker.cpp
void DisplayWorker::onBrightnessChanged(const treeland_output_color_control_v1 *colorControl, double brightness)
{
    brightness = qBound(0.0, brightness / 100.0, 1.0);
    for (auto it(m_control_monitors.cbegin()); it != m_control_monitors.cend(); ++it) {
        if (it.value() == colorControl) {
            it.key()->setBrightness(brightness);
            return;
        }
    }
}

虽然使用了 qBound 限制亮度值,但没有检查 colorControl 是否为空指针。

6. 改进建议

  1. 统一代码风格:确保整个代码库使用一致的命名约定、缩进和格式。

  2. 增强错误处理:添加更多的错误检查和适当的错误处理逻辑,特别是对于 Wayland 协议交互。

  3. 资源管理:明确资源所有权,确保所有分配的资源都被正确释放。

  4. 性能优化:考虑使用更高效的数据结构和算法,特别是在处理显示器列表时。

  5. 文档完善:添加更多的注释和文档,解释复杂的逻辑和设计决策。

  6. 测试覆盖:添加单元测试和集成测试,特别是对于新增的亮度控制功能。

  7. 日志记录:添加更多的日志记录,特别是在关键路径和错误情况下,以便于调试。

  8. 异步处理:考虑使用异步处理替代同步等待,以提高响应性和性能。

7. 总结

这次提交主要改进了 Wayland 显示管理功能,特别是亮度控制方面。代码整体质量较好,但在错误处理、资源管理和性能优化方面还有改进空间。建议在合并前解决上述问题,以提高代码的健壮性和可维护性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants