Skip to content

fix(macos): recreate mouse event source when the session lock state changes - #239

Open
pilou33620 wants to merge 2 commits into
LizardByte:masterfrom
pilou33620:claude/quirky-pasteur-6gbj5v
Open

pilou33620 wants to merge 2 commits into
LizardByte:masterfrom
pilou33620:claude/quirky-pasteur-6gbj5v

Conversation

@pilou33620

Copy link
Copy Markdown

Description

On macOS, the virtual mouse stops moving the cursor as soon as the session locks. Clicks and keyboard input keep
working, and a physical mouse still moves the cursor. Restarting the host while the Mac is locked makes the virtual
mouse work again, including after unlocking, until the next lock.

MacosInputState creates its kCGEventSourceStateHIDSystemState mouse event source once at startup. That source no
longer moves the cursor after the session lock state changes. The keyboard uses its own private source and is not
affected.

This PR recreates the mouse event source when the session lock state changes:

  • The lock state is read from CGSessionCopyCurrentDictionary() (CGSSessionScreenIsLocked), at most once every
    500 ms, from MacosMouse::submit().
  • When it differs from the state the current source was created in, the source is released and created again, with
    local-event suppression disabled as before.
  • A new source_mutex guards the source so several mouse devices sharing the state cannot use it while it is replaced.

Behavior is unchanged while the lock state does not change. A missing source still rejects submissions.

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

Automated

  • Added MacosBackendTest.RecreatesMouseEventSourceWhenSessionLockStateChanges. It simulates unlocked → locked →
    unlocked through new test hooks for CGSessionCopyCurrentDictionary and CGEventSourceCreate, and checks that the
    mouse source is created again on each lock-state change, not before the poll interval has elapsed, not while the
    state is stable, and that posted events still carry a zero suppression interval. Without the fix the source count
    stays at 1.
  • ./test_libvirtualhid passes on a macOS 15 Intel GitHub runner (Debug, Ninja, -DBUILD_TESTS=ON), run from my fork.

Manual

  • Mac mini (Apple Silicon), macOS 27.2.1 (26B131), Sunshine built from current master with this branch of
    libvirtualhid, Moonlight on iPad.
  • Before: after locking (Ctrl+Cmd+Q) the cursor does not move; restarting Sunshine is required.
  • After: the cursor moves on the lock screen, the session can be unlocked from Moonlight, and the mouse keeps working
    after unlock, over several lock/unlock cycles without restarting Sunshine.

Limitations

  • The unit test checks that the source is recreated; it cannot reproduce the real stale-source behavior of a locked
    session.
  • Not run: the full CI matrix (Linux, Windows, FreeBSD), and the macOS arm64 unit tests.
  • Not tested: screen saver without lock, fast user switching.

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Tests have been added or updated for new or modified functionality, including regression tests for bug fixes

AI Usage

See our AI usage policy.

  • AI tools were used while preparing this PR

The diagnosis came from manual testing on my Mac. The code change, the regression test and this description were
written with an AI agent; I validated the fix manually as described above.

🤖 Generated with Claude Code

https://claude.ai/code/session_013w5cvghYsf8sPGsBVvMMJa

@CLAassistant

CLAassistant commented Nov 27, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…hanges

The HID-system-state event source created at startup stops moving the
cursor once the session locks; clicks and keyboard keep working.
Recreate it whenever the session lock state changes, and cover the
behavior with a regression test.
@pilou33620
pilou33620 force-pushed the claude/quirky-pasteur-6gbj5v branch from 6add74d to b7d47e5 Compare November 27, 2026 22:34

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Requesting changes for one recovery regression: a transient mouse-source creation failure disables further mouse submissions without retrying. The inline comment includes a tested correction.

Validation on macOS arm64: the Debug CMake/Ninja build and all 21 existing MacosBackendTest.* tests passed at b7d47e5c3864704657fff00539b885fed0adadd1; git diff --check and clang-format checks passed. A supplemental failure-injection regression fails on this head and passes with the suggested correction (22/22 macOS backend tests).

Verification limits: GitHub Actions workflows await maintainer approval; the GH Pages Read the Docs failure is a timeout waiting for the unstarted Jekyll job. I did not reproduce the real lock-screen cursor behavior or run the other platform test suites. GitHub reports this head mergeable against master.

Review completed by Codex | Model: gpt-6.1-sol | Thinking: xhigh

Comment thread src/platform/macos/macos_backend.cpp Outdated
Comment on lines +535 to +541
last_screen_locked = locked;

if (source) {
CFRelease(source);
}
source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
configure_source();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Keep failed mouse-source recreation retryable

If CGEventSourceCreate() returns nullptr during a lock-state change, the old source has already been released and last_screen_locked has already been updated. Every subsequent poll sees the same lock state and returns before trying to create another source, so mouse movement, clicks, and scrolling all fail until another lock-state transition or a backend restart. Creating a new mouse cannot recover either, because the backend retains the same shared input state.

Create the replacement before releasing the old source, and record the lock state only after creation succeeds:

Suggested change
last_screen_locked = locked;
if (source) {
CFRelease(source);
}
source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
configure_source();
const auto refreshed_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
if (!refreshed_source) {
return;
}
if (source) {
CFRelease(source);
}
source = refreshed_source;
configure_source();
last_screen_locked = locked;

I verified this with a supplemental test that fails source creation once on the unlocked → locked transition, then submits twice more with the screen still locked and polling due. The current code stays at two creation attempts and returns backend_failure both times. With this suggestion, the next poll retries successfully and all 22 macOS backend tests pass. Please include that failure-and-retry scenario in the regression coverage.

Review completed by Codex | Model: gpt-6.1-sol | Thinking: xhigh

Create the replacement before releasing the current source and record
the lock state only once creation succeeded, so a transient failure is
retried on the next poll.
@sonarqubecloud

Copy link
Copy Markdown

@pilou33620

Copy link
Copy Markdown
Author

Thanks, good catch. Applied your suggestion as-is in 35c9f43 and added a regression test
(RetriesMouseEventSourceRecreationAfterCreationFailure) covering the failed creation on
unlocked → locked followed by a retry on the next poll. I could not run the macOS tests
for this last commit on my side, so CI will be the first run.

This branch has not been deployed

No deployments
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.

3 participants