Commit db4e961e authored by Chege Gitau's avatar Chege Gitau Committed by Commit Bot

[UI] [Win] Fix scaling constant in mouse move

To convert from screen coordinates to the normalized absolute
coordinates dimensions used by SendInput, the scaling factor should be
(65536.0 / screen_dimension).

Previously, we used (65535.0 / (screen_dimension - 1)). Although the
difference between these two factors is small, the discrepancy becomes
more prominent when it's multiplied out. This discrepancy may lead to
the computed absolute coordinates being off by a pixel or two depending
on the resolution of the screen in question.

Change-Id: Id305abc6ddf904ffbbb07dc740c655ac1be3cf83
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2278347Reviewed-by: default avatarDavid Bienvenu <davidbienvenu@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Commit-Queue: Chege Gitau <dagitau@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#786075}
parent 576b9aff
...@@ -18,18 +18,24 @@ bool SendMouseEvent(const gfx::Point& point, int flags) { ...@@ -18,18 +18,24 @@ bool SendMouseEvent(const gfx::Point& point, int flags) {
INPUT input = {INPUT_MOUSE}; INPUT input = {INPUT_MOUSE};
// Get the max screen coordinate for use in computing the normalized absolute // Get the max screen coordinate for use in computing the normalized absolute
// coordinates required by SendInput. // coordinates required by SendInput.
const int max_x = ::GetSystemMetrics(SM_CXSCREEN) - 1; const int screen_width = ::GetSystemMetrics(SM_CXSCREEN);
const int max_y = ::GetSystemMetrics(SM_CYSCREEN) - 1; const int screen_height = ::GetSystemMetrics(SM_CYSCREEN);
int screen_x = base::ClampToRange(point.x(), 0, max_x); int screen_x = base::ClampToRange(point.x(), 0, screen_width - 1);
int screen_y = base::ClampToRange(point.y(), 0, max_y); int screen_y = base::ClampToRange(point.y(), 0, screen_height - 1);
// In normalized absolute coordinates, (0, 0) maps onto the upper-left corner
// of the display surface, while (65535, 65535) maps onto the lower-right
// corner.
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event#remarks
static constexpr double kNormalizedScreenSize = 65536.0;
// Form the input data containing the normalized absolute coordinates. As of // Form the input data containing the normalized absolute coordinates. As of
// Windows 10 Fall Creators Update, moving to an absolute position of zero // Windows 10 Fall Creators Update, moving to an absolute position of zero
// does not work. It seems that moving to 1,1 does, though. // does not work. It seems that moving to 1,1 does, though.
input.mi.dx = input.mi.dx = static_cast<LONG>(std::max(
static_cast<LONG>(std::max(1.0, std::ceil(screen_x * (65535.0 / max_x)))); 1.0, std::ceil(screen_x * (kNormalizedScreenSize / screen_width))));
input.mi.dy = input.mi.dy = static_cast<LONG>(std::max(
static_cast<LONG>(std::max(1.0, std::ceil(screen_y * (65535.0 / max_y)))); 1.0, std::ceil(screen_y * (kNormalizedScreenSize / screen_height))));
input.mi.dwFlags = flags; input.mi.dwFlags = flags;
return ::SendInput(1, &input, sizeof(input)) == 1; return ::SendInput(1, &input, sizeof(input)) == 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment