Skip to content
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

fix(macos/input): incorrect mouse input for non-main display #2461

Merged
merged 25 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a161573
Complete output_name implementation in macos to select a different m…
TimmyOVO Apr 19, 2024
59c3978
Update locale for both en.json and en_GB.json
TimmyOVO Apr 19, 2024
81cf98f
Improved detected displays startup log format (now every display has …
TimmyOVO Apr 19, 2024
4d079a4
Update macOS output_name doc
TimmyOVO Apr 19, 2024
ecc86b8
Update macOS output_name example doc
TimmyOVO Apr 19, 2024
79326e1
Merge branch 'nightly' into nightly
TimmyOVO Apr 19, 2024
50f47d5
Reordered some entries & locale cleanup.
TimmyOVO Apr 20, 2024
1bc0b2c
Merge remote-tracking branch 'origin/nightly' into nightly
TimmyOVO Apr 20, 2024
6a377ec
Unify startup display detection log format in macOS and Linux
TimmyOVO Apr 20, 2024
7286502
Merge output_name doc Linux and macOS section into one.
TimmyOVO Apr 20, 2024
22762e7
Update config.html to match the macOS & Linux display detection code.
TimmyOVO Apr 20, 2024
a3c3f01
Merge macOS and linux output_name config ui into one.
TimmyOVO Apr 20, 2024
e9dd2aa
Merge branch 'nightly' into nightly
TimmyOVO Apr 20, 2024
70447d6
Separated output_name linux and macos in doc examples and config ui
TimmyOVO Apr 22, 2024
a23b491
Merge remote-tracking branch 'origin/nightly' into nightly
TimmyOVO Apr 22, 2024
ee4014b
Merge branch 'nightly' into nightly
TimmyOVO Apr 22, 2024
32f5c1c
SUNSHINE_EXTERNAL_LIBRARIES in macos reordered.
TimmyOVO Apr 22, 2024
8a6b0bb
Merge remote-tracking branch 'origin/nightly' into nightly
TimmyOVO Apr 22, 2024
a755c7e
doc spacing issue fixed.
TimmyOVO Apr 22, 2024
dcbaae6
Merge branch 'LizardByte:nightly' into nightly
TimmyOVO Apr 22, 2024
111e6de
fix: abs mouse input not working in macOS
TimmyOVO Apr 22, 2024
75f48f5
Merge remote-tracking branch 'origin/nightly' into nightly
TimmyOVO Apr 22, 2024
9f44d3e
reformat using clang-format
TimmyOVO Apr 22, 2024
98d0dea
Merge branch 'nightly' into nightly
TimmyOVO Apr 30, 2024
4d89d73
use std::clamp instead of if statement.
TimmyOVO Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/platform/macos/display.mm
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@

display->width = display->av_capture.frameWidth;
display->height = display->av_capture.frameHeight;
// We also need set env_width and env_height for absolute mouse coordinates
display->env_width = display->width;
display->env_height = display->height;

return display;
}
Expand Down
22 changes: 12 additions & 10 deletions src/platform/macos/input.cpp
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before we support change the display we need to capture in macOS, the location we get here according to Apple Document should be The current location of the specified mouse event in global display coordinates, we can not just simply assuming the display's bounds to [0,display_width] and [0,display_height]. So we need to get the correspond display bounds for display we are capturing using CGDisplayBounds which return The bounds of the display, expressed as a rectangle in the global display coordinate space (relative to the upper-left corner of the main display)..

Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,12 @@
auto display = macos_input->display;
auto event = macos_input->mouse_event;

if (location.x < 0)
location.x = 0;
if (location.x >= (double) CGDisplayPixelsWide(display))
location.x = (double) CGDisplayPixelsWide(display) - 1;
// get display bounds for current display
CGRect display_bounds = CGDisplayBounds(display);

Check warning on line 332 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L332

Added line #L332 was not covered by tests

if (location.y < 0)
location.y = 0;
if (location.y >= (double) CGDisplayPixelsHigh(display))
location.y = (double) CGDisplayPixelsHigh(display) - 1;
// limit mouse to current display bounds
location.x = std::clamp(location.x, display_bounds.origin.x, display_bounds.origin.x + display_bounds.size.width - 1);
location.y = std::clamp(location.y, display_bounds.origin.y, display_bounds.origin.y + display_bounds.size.height - 1);

Check warning on line 336 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L335-L336

Added lines #L335 - L336 were not covered by tests

CGEventSetType(event, type);
CGEventSetLocation(event, location);
Expand Down Expand Up @@ -379,10 +376,15 @@

void
abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) {
auto scaling = ((macos_input_t *) input.get())->displayScaling;
auto macos_input = static_cast<macos_input_t *>(input.get());
auto scaling = macos_input->displayScaling;
auto display = macos_input->display;

Check warning on line 381 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L379-L381

Added lines #L379 - L381 were not covered by tests

CGPoint location = CGPointMake(x * scaling, y * scaling);

CGRect display_bounds = CGDisplayBounds(display);

Check warning on line 384 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L384

Added line #L384 was not covered by tests
// in order to get the correct mouse location for capturing display , we need to add the display bounds to the location
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The location we received are relative to the display we are capturing ,so we get the display bounds using CGDisplayBounds then add them up to get the right input coordinates.

location.x += display_bounds.origin.x;
location.y += display_bounds.origin.y;

Check warning on line 387 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L386-L387

Added lines #L386 - L387 were not covered by tests
post_mouse(input, kCGMouseButtonLeft, event_type_mouse(input), location, 0);
}

Expand Down
Loading