-
-
Notifications
You must be signed in to change notification settings - Fork 817
Dedicated mouse hook #20436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dedicated mouse hook #20436
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c37f68b
first draft
Boumtchack e01319f
tests
Boumtchack 8130cf8
revert changes
Boumtchack 9d6f4f6
refactoring
Boumtchack f40ae23
minor changes
Boumtchack a561914
copilot review
Boumtchack 346a962
logic changes
Boumtchack adb78b5
review modification
Boumtchack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # Copyright (C) 2026 NV Access Limited, Antoine Haffreingue | ||
| # This file may be used under the terms of the GNU General Public License, version 2 or later, as modified by the NVDA license. | ||
| # For full terms and any additional permissions, see the NVDA license file: https://github.com/nvaccess/nvda/blob/master/copying.txt | ||
|
|
||
| """Dedicated low-level mouse hook for the magnifier. | ||
|
|
||
| Runs in its own thread so it stays responsive even when the wx main thread is | ||
| busy (e.g. at high Windows display scale factors), which is the root cause of | ||
| the DPI-related lag compared to the built-in Windows Magnifier. | ||
| """ | ||
|
|
||
| import ctypes | ||
| import threading | ||
| from ctypes import byref | ||
| from ctypes.wintypes import MSG | ||
| from logHandler import log | ||
| from winBindings import user32 | ||
| from winInputHook import MSLLHOOKSTRUCT, HC_ACTION, WH_MOUSE_LL | ||
|
|
||
| WM_MOUSEMOVE: int = 0x0200 | ||
| WM_QUIT: int = 0x0012 | ||
| PM_NOREMOVE: int = 0x0000 | ||
|
|
||
|
|
||
| class MagnifierMouseHook: | ||
| """Installs a WH_MOUSE_LL hook in a dedicated thread. Calls onMouseMove(x, y) on every mouse move. | ||
|
|
||
| WH_MOUSE_LL is a global hook: it runs for every mouse move on the system, and | ||
| delivery of the real WM_MOUSEMOVE to the window under the cursor waits for the | ||
| whole hook chain to return. onMouseMove must therefore return immediately (no | ||
| Magnification API calls, no blocking work) — defer any real work to another | ||
| thread, e.g. via wx.CallAfter. | ||
| """ | ||
|
|
||
| def __init__(self, onMouseMove): | ||
|
Boumtchack marked this conversation as resolved.
Outdated
|
||
| self._onMouseMove = onMouseMove | ||
| self._thread: threading.Thread | None = None | ||
| self._cCallback = None # kept alive to prevent GC of the ctypes callback | ||
| self._hookReady = threading.Event() | ||
| self._hookInstalled: bool = False | ||
|
|
||
| def start(self) -> None: | ||
| self._thread = threading.Thread(target=self._run, name="magnifierMouseHook", daemon=True) | ||
| self._thread.start() | ||
| self._hookReady.wait(timeout=1.0) | ||
|
|
||
| def stop(self) -> None: | ||
| thread = self._thread | ||
| if not thread: | ||
| self._cCallback = None | ||
| return | ||
| if thread.ident is None or not user32.PostThreadMessage(thread.ident, WM_QUIT, 0, 0): | ||
| log.error( | ||
| f"Failed to post WM_QUIT to magnifier mouse hook thread (error {ctypes.GetLastError()})", | ||
| ) | ||
| thread.join() | ||
| self._thread = None | ||
| self._cCallback = None | ||
|
Boumtchack marked this conversation as resolved.
|
||
|
|
||
| def _run(self) -> None: | ||
| windowsMessage = MSG() | ||
| # Ensure the thread message queue exists so PostThreadMessage(WM_QUIT) succeeds. | ||
| user32.PeekMessage(byref(windowsMessage), None, 0, 0, PM_NOREMOVE) | ||
|
|
||
| def _onRawMouseEvent(code, eventType, mouseDataPointer): | ||
|
Boumtchack marked this conversation as resolved.
Outdated
|
||
| if code == HC_ACTION and eventType == WM_MOUSEMOVE: | ||
| mouseData = MSLLHOOKSTRUCT.from_address(mouseDataPointer) | ||
| try: | ||
| self._onMouseMove(mouseData.pt.x, mouseData.pt.y) | ||
| except Exception: | ||
| log.exception("Error in magnifier mouse hook callback") | ||
| return user32.CallNextHookEx(0, code, eventType, mouseDataPointer) | ||
|
|
||
| self._cCallback = user32.HOOKPROC(_onRawMouseEvent) | ||
| hookHandle = user32.SetWindowsHookEx(WH_MOUSE_LL, self._cCallback, None, 0) | ||
| self._hookInstalled = bool(hookHandle) | ||
| self._hookReady.set() | ||
| if not hookHandle: | ||
| log.error(f"Failed to install magnifier mouse hook (error {ctypes.GetLastError()})") | ||
| return | ||
|
|
||
| try: | ||
| while True: | ||
| result = user32.GetMessage(byref(windowsMessage), None, 0, 0) | ||
| if result == 0: | ||
| break | ||
| if result == -1: | ||
| log.error( | ||
| f"GetMessage failed in magnifier mouse hook thread (error {ctypes.GetLastError()})", | ||
| ) | ||
| break | ||
| finally: | ||
| user32.UnhookWindowsHookEx(hookHandle) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # Copyright (C) 2025-2026 NV Access Limited, Antoine Haffreingue | ||
| # This file may be used under the terms of the GNU General Public License, version 2 or later, as modified by the NVDA license. | ||
| # For full terms and any additional permissions, see the NVDA license file: https://github.com/nvaccess/nvda/blob/master/copying.txt | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
| from _magnifier.fullscreenMagnifier import FullScreenMagnifier | ||
| from _magnifier.utils.types import Coordinates | ||
| from tests.unit.test_magnifier.test_magnifier import _TestMagnifier | ||
|
|
||
|
|
||
| class TestMouseHookLifecycle(_TestMagnifier): | ||
| """Tests for the WH_MOUSE_LL hook lifecycle managed by the base Magnifier class.""" | ||
|
|
||
| def testHookStartedWithMagnifier(self): | ||
| """Mouse hook is started when the magnifier starts.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
|
|
||
| self.MockMouseHook.assert_called_once_with(magnifier._onMouseMove) | ||
| self.mock_hook_instance.start.assert_called_once() | ||
|
|
||
| magnifier._stopMagnifier() | ||
|
|
||
| def testHookStoppedWithMagnifier(self): | ||
| """Mouse hook is stopped and cleared when the magnifier stops.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
| magnifier._stopMagnifier() | ||
|
|
||
| self.mock_hook_instance.stop.assert_called_once() | ||
| self.assertIsNone(magnifier._mouseHook) | ||
|
|
||
|
|
||
| class TestOnMouseMove(_TestMagnifier): | ||
| """Tests for FullScreenMagnifier._onMouseMove — the hook callback. | ||
|
|
||
| _onMouseMove runs synchronously inside a global WH_MOUSE_LL hook chain (see | ||
| utils/mouseHook.py), so it must never call into the Magnification API directly: | ||
| doing so would delay delivery of the real WM_MOUSEMOVE to whatever window is | ||
| under the cursor, for every mouse move on the system. It should only record | ||
| the latest coordinates and defer the actual update to the main thread via | ||
| wx.CallAfter (_applyPendingMousePosition). | ||
| """ | ||
|
|
||
| def testDoesNotUpdateMagnifierSynchronously(self): | ||
| """_onMouseMove must not touch the Magnification API from the hook thread.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
| magnifier._fullscreenMagnifier = MagicMock() | ||
|
|
||
| with ( | ||
| patch("_magnifier.magnifier.getFollowState", return_value=True), | ||
| patch("_magnifier.magnifier.wx.CallAfter"), | ||
| ): | ||
| magnifier._onMouseMove(500, 400) | ||
|
|
||
| magnifier._fullscreenMagnifier.assert_not_called() | ||
| self.assertEqual(magnifier._pendingMouseCoordinates, Coordinates(500, 400)) | ||
| magnifier._stopMagnifier() | ||
|
|
||
| def testSchedulesMainThreadUpdate(self): | ||
| """_onMouseMove schedules exactly one wx.CallAfter, even for bursts of moves.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
|
|
||
| with ( | ||
| patch("_magnifier.magnifier.getFollowState", return_value=True), | ||
| patch("_magnifier.magnifier.wx.CallAfter") as mockCallAfter, | ||
| ): | ||
| magnifier._onMouseMove(500, 400) | ||
| magnifier._onMouseMove(510, 410) | ||
| magnifier._onMouseMove(520, 420) | ||
|
|
||
| mockCallAfter.assert_called_once_with(magnifier._applyPendingMousePosition) | ||
| self.assertEqual(magnifier._pendingMouseCoordinates, Coordinates(520, 420)) | ||
| magnifier._stopMagnifier() | ||
|
|
||
| def testApplyPendingMousePositionUpdatesMagnifier(self): | ||
| """_applyPendingMousePosition (run on the main thread) performs the real update.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
| magnifier._fullscreenMagnifier = MagicMock() | ||
|
|
||
| with ( | ||
| patch("_magnifier.magnifier.getFollowState", return_value=True), | ||
| patch("_magnifier.magnifier.wx.CallAfter", side_effect=lambda func, *a, **kw: func(*a, **kw)), | ||
| ): | ||
| magnifier._onMouseMove(500, 400) | ||
|
|
||
| magnifier._fullscreenMagnifier.assert_called_once() | ||
| self.assertEqual(magnifier.currentCoordinates, Coordinates(500, 400)) | ||
| self.assertFalse(magnifier._mouseUpdatePending) | ||
| magnifier._stopMagnifier() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.