mirror of
https://github.com/LizardByte/libvirtualhid.git
synced 2026-12-03 13:45:45 +09:00
fix(macos): recreate mouse event source when the session lock state changes (#239)
This commit is contained in:
1 parent
7c2b82e5d7
commit
afb56a94bb
4 files changed
+204
-7
No files matched your search
@@ -495,12 +495,9 @@ namespace lvh::detail {
|
||||
MacosInputState():
|
||||
source {CGEventSourceCreate(kCGEventSourceStateHIDSystemState)},
|
||||
keyboard_source {CGEventSourceCreate(kCGEventSourceStatePrivate)},
|
||||
scroll_lines_per_detent_value {read_scroll_lines_per_detent(scrollwheel_scaling)} {
|
||||
// Each move warps the cursor. The default 250ms suppression then drops the
|
||||
// following click and mouse-moved events. The cursor image still moves.
|
||||
if (source) {
|
||||
CGEventSourceSetLocalEventsSuppressionInterval(source, 0);
|
||||
}
|
||||
scroll_lines_per_detent_value {read_scroll_lines_per_detent(scrollwheel_scaling)},
|
||||
last_screen_locked {screen_locked()} {
|
||||
configure_source();
|
||||
}
|
||||
|
||||
MacosInputState(const MacosInputState &) = delete;
|
||||
@@ -517,12 +514,73 @@ namespace lvh::detail {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Recreate the mouse source when the session lock state changed.
|
||||
*
|
||||
* A HID-system-state source created before the session locks (or unlocks)
|
||||
* stops moving the cursor afterwards, clicks and keyboard still work.
|
||||
* The caller must hold `source_mutex`.
|
||||
*/
|
||||
void refresh_source_on_lock_change() {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - last_lock_check < std::chrono::milliseconds {500}) {
|
||||
return;
|
||||
}
|
||||
last_lock_check = now;
|
||||
|
||||
const bool locked = screen_locked();
|
||||
if (locked == last_screen_locked) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the current source and retry on the next poll when creation fails.
|
||||
const auto refreshed_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
|
||||
if (!refreshed_source) {
|
||||
return;
|
||||
}
|
||||
if (source) {
|
||||
CFRelease(source);
|
||||
}
|
||||
source = refreshed_source;
|
||||
configure_source();
|
||||
last_screen_locked = locked;
|
||||
}
|
||||
|
||||
std::mutex source_mutex; ///< Guards `source` replacement across mouse devices.
|
||||
CGEventSourceRef source {}; ///< Mouse/scroll source with local-event suppression disabled.
|
||||
CGEventSourceRef keyboard_source {}; ///< CoreGraphics event source for keyboard events.
|
||||
double scrollwheel_scaling = default_scrollwheel_scaling; ///< Raw macOS scroll-wheel scaling preference.
|
||||
int scroll_lines_per_detent_value = default_scroll_lines_per_detent; ///< Logical lines per wheel detent.
|
||||
CGEventFlags keyboard_flags {}; ///< Active modifier flags applied to mouse events.
|
||||
std::mutex keyboard_mutex; ///< Guards shared keyboard modifier state.
|
||||
std::chrono::steady_clock::time_point last_lock_check {}; ///< Last lock-state poll.
|
||||
bool last_screen_locked = false; ///< Lock state the current `source` was created in.
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Read the session lock state published by loginwindow.
|
||||
* @return `true` when the screen is locked.
|
||||
*/
|
||||
static bool screen_locked() {
|
||||
bool locked = false;
|
||||
if (const auto session = CGSessionCopyCurrentDictionary()) {
|
||||
const auto value = static_cast<CFBooleanRef>(CFDictionaryGetValue(session, CFSTR("CGSSessionScreenIsLocked")));
|
||||
locked = value && CFBooleanGetValue(value);
|
||||
CFRelease(session);
|
||||
}
|
||||
return locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disable local-event suppression on the mouse source.
|
||||
*/
|
||||
void configure_source() const {
|
||||
// Each move warps the cursor. The default 250ms suppression then drops the
|
||||
// following click and mouse-moved events. The cursor image still moves.
|
||||
if (source) {
|
||||
CGEventSourceSetLocalEventsSuppressionInterval(source, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -745,6 +803,8 @@ namespace lvh::detail {
|
||||
if (!open_) {
|
||||
return OperationStatus::failure(ErrorCode::device_closed, "macOS mouse is closed");
|
||||
}
|
||||
std::lock_guard source_lock {state_->source_mutex};
|
||||
state_->refresh_source_on_lock_change();
|
||||
if (!state_->source) {
|
||||
return OperationStatus::failure(ErrorCode::backend_failure, "macOS mouse event source is unavailable");
|
||||
}
|
||||
@@ -940,7 +1000,7 @@ namespace lvh::detail {
|
||||
return {OperationStatus::failure(ErrorCode::unsupported_profile, "device profile is not a mouse"), nullptr};
|
||||
}
|
||||
auto state = input_state();
|
||||
if (!state->source) {
|
||||
if (std::lock_guard source_lock {state->source_mutex}; !state->source) {
|
||||
return {OperationStatus::failure(ErrorCode::backend_failure, "macOS mouse event source is unavailable"), nullptr};
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,24 @@ namespace lvh::detail::test {
|
||||
std::size_t cursor_warps {}; ///< Calls to update the cursor after posting.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief One mouse submission made under a simulated session lock state.
|
||||
*/
|
||||
struct MacosMouseLockSubmission {
|
||||
bool screen_locked {}; ///< Whether the session reports a locked screen.
|
||||
bool poll_due {}; ///< Whether the backend's lock-state poll interval has elapsed.
|
||||
bool fail_source_creation {}; ///< Whether the next mouse event source creation fails.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Result of one mouse submission made under a simulated session lock state.
|
||||
*/
|
||||
struct MacosMouseLockSubmissionResult {
|
||||
OperationStatus status; ///< Status returned by the submission.
|
||||
std::size_t source_creations {}; ///< Mouse event source creations attempted so far.
|
||||
std::optional<double> suppression_interval; ///< Suppression interval on the posted event.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Portable view of one CoreGraphics keyboard event the macOS backend built.
|
||||
*/
|
||||
@@ -235,6 +253,16 @@ namespace lvh::detail::test {
|
||||
bool source_available = true
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Submit relative mouse motion across simulated session lock changes.
|
||||
*
|
||||
* @param submissions Lock state and poll timing for each submission.
|
||||
* @return Status, mouse event source count, and suppression interval after each submission.
|
||||
*/
|
||||
std::vector<MacosMouseLockSubmissionResult> macos_backend_mouse_lock_submissions(
|
||||
const std::vector<MacosMouseLockSubmission> &submissions
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Exercise macOS backend creation and unsupported-device paths.
|
||||
*
|
||||
|
||||
+68
@@ -63,8 +63,44 @@ namespace {
|
||||
std::optional<lvh::detail::test::MacosPoint> warped_location;
|
||||
lvh::detail::test::MacosMouseEventsResult *events = nullptr; ///< Optional capture of native event metadata.
|
||||
bool fail_first_creation = false; ///< Whether to fail the next mouse event allocation.
|
||||
bool screen_locked = false; ///< Session lock state reported to the backend.
|
||||
std::size_t source_creations {}; ///< HID-system-state event source creations the backend attempted.
|
||||
bool fail_next_source_creation = false; ///< Whether to fail the next mouse event source creation.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Report the scoped session lock state instead of the real login session.
|
||||
*
|
||||
* @return Session dictionary owned by the caller, or null when the session is unavailable.
|
||||
*/
|
||||
CFDictionaryRef mouse_session_dictionary() {
|
||||
if (!mouse_environment()) {
|
||||
return CGSessionCopyCurrentDictionary();
|
||||
}
|
||||
const auto session = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
||||
if (session) {
|
||||
CFDictionarySetValue(session, CFSTR("CGSSessionScreenIsLocked"), mouse_environment()->screen_locked ? kCFBooleanTrue : kCFBooleanFalse);
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Count mouse event source creations and optionally fail the next one.
|
||||
*
|
||||
* @param state Requested event source state.
|
||||
* @return New native event source, or null when creation fails.
|
||||
*/
|
||||
CGEventSourceRef mouse_create_event_source(CGEventSourceStateID state) {
|
||||
if (mouse_environment() && state == kCGEventSourceStateHIDSystemState) {
|
||||
++mouse_environment()->source_creations;
|
||||
if (mouse_environment()->fail_next_source_creation) {
|
||||
mouse_environment()->fail_next_source_creation = false;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return CGEventSourceCreate(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Age cursor snapshots while capturing fresh mouse event submissions.
|
||||
*
|
||||
@@ -179,9 +215,13 @@ namespace {
|
||||
#define CGEventGetLocation mouse_event_location
|
||||
#define CGEventPost mouse_event_post
|
||||
#define CGWarpMouseCursorPosition mouse_warp_cursor_position
|
||||
#define CGSessionCopyCurrentDictionary mouse_session_dictionary
|
||||
#define CGEventSourceCreate mouse_create_event_source
|
||||
#define create_platform_backend create_platform_backend_for_macos_backend_test_hooks
|
||||
#include "../../src/platform/macos/macos_backend.cpp"
|
||||
#undef create_platform_backend
|
||||
#undef CGEventSourceCreate
|
||||
#undef CGSessionCopyCurrentDictionary
|
||||
#undef CGWarpMouseCursorPosition
|
||||
#undef CGEventPost
|
||||
#undef CGEventGetLocation
|
||||
@@ -347,6 +387,34 @@ namespace lvh::detail::test {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<MacosMouseLockSubmissionResult> macos_backend_mouse_lock_submissions(
|
||||
const std::vector<MacosMouseLockSubmission> &submissions
|
||||
) {
|
||||
MacosMouseEventsResult captured;
|
||||
MouseEnvironment environment {{.origin_x = 0, .origin_y = 0, .width = 400, .height = 200}};
|
||||
environment.events = &captured;
|
||||
|
||||
const auto state = std::make_shared<macos::MacosInputState>();
|
||||
macos::MacosMouse mouse {state, {}};
|
||||
|
||||
std::vector<MacosMouseLockSubmissionResult> results;
|
||||
for (const auto &submission : submissions) {
|
||||
environment.screen_locked = submission.screen_locked;
|
||||
environment.fail_next_source_creation = submission.fail_source_creation;
|
||||
if (submission.poll_due) {
|
||||
state->last_lock_check = {};
|
||||
}
|
||||
captured.events.clear();
|
||||
const auto status = mouse.submit({.kind = MouseEventKind::relative_motion, .x = 1, .y = 1});
|
||||
results.push_back({
|
||||
.status = status,
|
||||
.source_creations = environment.source_creations,
|
||||
.suppression_interval = captured.events.empty() ? std::nullopt : captured.events.back().suppression_interval,
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
MacosBackendUtilityResult macos_backend_utilities() {
|
||||
auto backend = create_platform_backend_for_macos_backend_test_hooks();
|
||||
MacosBackendUtilityResult result;
|
||||
|
||||
@@ -498,6 +498,47 @@ TEST_F(MacosBackendTest, RejectsMouseSubmissionWithoutAnEventSource) {
|
||||
EXPECT_TRUE(result.events.empty());
|
||||
}
|
||||
|
||||
TEST_F(MacosBackendTest, RecreatesMouseEventSourceWhenSessionLockStateChanges) {
|
||||
const std::vector<lvh::detail::test::MacosMouseLockSubmission> submissions {
|
||||
{.screen_locked = false, .poll_due = true},
|
||||
{.screen_locked = true, .poll_due = false},
|
||||
{.screen_locked = true, .poll_due = true},
|
||||
{.screen_locked = true, .poll_due = true},
|
||||
{.screen_locked = false, .poll_due = true},
|
||||
};
|
||||
const auto results = lvh::detail::test::macos_backend_mouse_lock_submissions(submissions);
|
||||
|
||||
ASSERT_EQ(results.size(), submissions.size());
|
||||
const std::array<std::size_t, 5> expected_source_creations {1, 1, 2, 2, 3};
|
||||
for (std::size_t index = 0; index < results.size(); ++index) {
|
||||
SCOPED_TRACE(index);
|
||||
ASSERT_TRUE(results[index].status.ok()) << results[index].status.message();
|
||||
EXPECT_EQ(results[index].source_creations, expected_source_creations[index]);
|
||||
ASSERT_TRUE(results[index].suppression_interval.has_value());
|
||||
EXPECT_DOUBLE_EQ(*results[index].suppression_interval, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MacosBackendTest, RetriesMouseEventSourceRecreationAfterCreationFailure) {
|
||||
const std::vector<lvh::detail::test::MacosMouseLockSubmission> submissions {
|
||||
{.screen_locked = false, .poll_due = true},
|
||||
{.screen_locked = true, .poll_due = true, .fail_source_creation = true},
|
||||
{.screen_locked = true, .poll_due = true},
|
||||
{.screen_locked = true, .poll_due = true},
|
||||
};
|
||||
const auto results = lvh::detail::test::macos_backend_mouse_lock_submissions(submissions);
|
||||
|
||||
ASSERT_EQ(results.size(), submissions.size());
|
||||
const std::array<std::size_t, 4> expected_source_creations {1, 2, 3, 3};
|
||||
for (std::size_t index = 0; index < results.size(); ++index) {
|
||||
SCOPED_TRACE(index);
|
||||
ASSERT_TRUE(results[index].status.ok()) << results[index].status.message();
|
||||
EXPECT_EQ(results[index].source_creations, expected_source_creations[index]);
|
||||
ASSERT_TRUE(results[index].suppression_interval.has_value());
|
||||
EXPECT_DOUBLE_EQ(*results[index].suppression_interval, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MacosBackendTest, ReportsCapabilitiesAndUnsupportedDevices) {
|
||||
const auto result = lvh::detail::test::macos_backend_utilities();
|
||||
|
||||
|
||||
Reference in new issue
Block a user