Skip to content

refactor: MaaFW style refactoring#1241

Merged
MistEO merged 2 commits intomainfrom
refactor/input
Apr 2, 2026
Merged

refactor: MaaFW style refactoring#1241
MistEO merged 2 commits intomainfrom
refactor/input

Conversation

@MistEO
Copy link
Copy Markdown
Member

@MistEO MistEO commented Apr 2, 2026

@ZeroAd-06 康康

Summary by Sourcery

优化 Win32 基于消息的输入控制器中的 DPI 感知初始化、鼠标锁定跟随行为以及目标进程处理。

Bug 修复:

  • 在无法打开目标进程句柄时立即提前返回,避免使用无效句柄。
  • mouse_lock_follow_active_ 改为原子类型,并移除过时的活动实例重置逻辑,以更准确反映实际的跟踪状态。

增强功能:

  • 用直接、显式地加载库并解析函数的方式替换通用 DLL 辅助工具,针对 User32、Shcore 和 Ntdll。
  • 将窗口居中逻辑提取为可复用的辅助函数,并用于鼠标锁定跟随激活流程。
  • 通过检测未稳定的移动并记录警告日志,提高 SetWindowPos 处理的健壮性。
  • 在光标限制中使用 std::clamp,并清理鼠标锁定跟随处理中的冗余注释和日志格式。
  • 简化鼠标锁定跟随启用/禁用流程,并确保在打开目标进程时已加载 Ntdll。
Original summary in English

Summary by Sourcery

Refine DPI awareness initialization, mouse lock follow behavior, and target process handling for the Win32 message-based input controller.

Bug Fixes:

  • Ensure early return when failing to open the target process handle to avoid using an invalid handle.
  • Make mouse_lock_follow_active_ atomic and remove stale active-instance reset logic to better reflect the actual tracking state.

Enhancements:

  • Replace generic DLL helper utilities with direct, explicit library loading and function resolution for User32, Shcore, and Ntdll.
  • Factor out window-centering logic into a reusable helper used by mouse lock follow activation.
  • Improve robustness of SetWindowPos handling by detecting non-settling moves and logging a warning.
  • Use std::clamp for cursor clamping and clean up redundant comments and logging formats in mouse lock follow handling.
  • Simplify mouse lock follow enable/disable flow and ensure Ntdll is loaded when opening the target process.

Copilot AI review requested due to automatic review settings April 2, 2026 02:46
Copy link
Copy Markdown
Contributor

@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 - 我在这里给出了一些整体性的反馈:

  • 在析构函数中,你移除了 s_active_instance_.compare_exchange_strong(this, nullptr) 调用,但 activate_mouse_lock_follow 仍然会设置 s_active_instance_ = this;建议在销毁或停用时恢复相应的清理逻辑,以避免将悬空指针暴露给其他线程。
  • 你将 mouse_lock_follow_active_ 改成了 std::atomic<bool>,但跟踪线程使用的其他状态(例如 lock_anchor_cursor_lock_offset_x_lock_offset_y_lock_anchor_window_)仍然是非原子的;请再次检查读/写模式,以确保在 mouse_lock_follow_active_ 现在可能更明显地允许并发线程访问的情况下,不会产生数据竞争。
  • ensure_process_dpi_awareness_once 中,之前的实现会在 SetProcessDpiAwareness 返回 E_ACCESSDENIED 后停止,而新版本忽略了 HRESULT 并始终返回 0;请确认这是有意删除对 E_ACCESSDENIED 的特殊处理,并在该情况下不再提前返回。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- In the destructor you removed the `s_active_instance_.compare_exchange_strong(this, nullptr)` call, but `activate_mouse_lock_follow` still sets `s_active_instance_ = this`; consider restoring a corresponding clear on destruction or deactivation to avoid leaving a dangling pointer visible to other threads.
- You changed `mouse_lock_follow_active_` to `std::atomic<bool>` but other state used by the tracking thread (e.g. `lock_anchor_cursor_`, `lock_offset_x_`, `lock_offset_y_`, `lock_anchor_window_`) remains non-atomic; double-check the read/write pattern to ensure there is no data race now that `mouse_lock_follow_active_` may allow concurrent thread access more visibly.
- In `ensure_process_dpi_awareness_once`, the previous implementation stopped after `SetProcessDpiAwareness` returned `E_ACCESSDENIED`, whereas the new version ignores the HRESULT and always returns 0; confirm that it is intentional to drop the special handling of `E_ACCESSDENIED` and not early-out in that case.

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've left some high level feedback:

  • In the destructor you removed the s_active_instance_.compare_exchange_strong(this, nullptr) call, but activate_mouse_lock_follow still sets s_active_instance_ = this; consider restoring a corresponding clear on destruction or deactivation to avoid leaving a dangling pointer visible to other threads.
  • You changed mouse_lock_follow_active_ to std::atomic<bool> but other state used by the tracking thread (e.g. lock_anchor_cursor_, lock_offset_x_, lock_offset_y_, lock_anchor_window_) remains non-atomic; double-check the read/write pattern to ensure there is no data race now that mouse_lock_follow_active_ may allow concurrent thread access more visibly.
  • In ensure_process_dpi_awareness_once, the previous implementation stopped after SetProcessDpiAwareness returned E_ACCESSDENIED, whereas the new version ignores the HRESULT and always returns 0; confirm that it is intentional to drop the special handling of E_ACCESSDENIED and not early-out in that case.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the destructor you removed the `s_active_instance_.compare_exchange_strong(this, nullptr)` call, but `activate_mouse_lock_follow` still sets `s_active_instance_ = this`; consider restoring a corresponding clear on destruction or deactivation to avoid leaving a dangling pointer visible to other threads.
- You changed `mouse_lock_follow_active_` to `std::atomic<bool>` but other state used by the tracking thread (e.g. `lock_anchor_cursor_`, `lock_offset_x_`, `lock_offset_y_`, `lock_anchor_window_`) remains non-atomic; double-check the read/write pattern to ensure there is no data race now that `mouse_lock_follow_active_` may allow concurrent thread access more visibly.
- In `ensure_process_dpi_awareness_once`, the previous implementation stopped after `SetProcessDpiAwareness` returned `E_ACCESSDENIED`, whereas the new version ignores the HRESULT and always returns 0; confirm that it is intentional to drop the special handling of `E_ACCESSDENIED` and not early-out in that case.

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors parts of MaaFramework’s Win32 message-based input implementation (notably mouse-lock-follow) and tidies the C++ module export surface to better match the public API.

Changes:

  • Refactors Win32 MessageInput mouse-lock-follow internals (extracts window-centering helper, improves state thread-safety via atomics, minor flow cleanups).
  • Simplifies DLL/function resolution logic for DPI-awareness initialization and ntdll suspend/resume calls.
  • Removes MaaControllerPostMouseLockFollow from the module export list.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
source/modules/MaaFramework.cppm Removes an exported symbol (MaaControllerPostMouseLockFollow) from the module surface.
source/MaaWin32ControlUnit/Input/MessageInput.h Adjusts mouse-lock-follow state to std::atomic<bool> and adds a helper declaration.
source/MaaWin32ControlUnit/Input/MessageInput.cpp Refactors DPI-awareness one-time init, ntdll loading, and mouse-lock-follow window positioning; uses std::clamp.

@MistEO MistEO merged commit 8520de9 into main Apr 2, 2026
19 checks passed
@MistEO MistEO deleted the refactor/input branch April 2, 2026 03:32
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.

2 participants