Repository navigation
fix(macos): recreate mouse event source when the session lock state changes - #239
pilou33620 wants to merge 2 commits into
Conversation
…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.
6add74d to
b7d47e5
Compare
ReenigneArcher
left a comment
There was a problem hiding this comment.
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
| last_screen_locked = locked; | ||
|
|
||
| if (source) { | ||
| CFRelease(source); | ||
| } | ||
| source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); | ||
| configure_source(); |
There was a problem hiding this comment.
[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:
| 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.
|
|
Thanks, good catch. Applied your suggestion as-is in 35c9f43 and added a regression test |



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.
MacosInputStatecreates itskCGEventSourceStateHIDSystemStatemouse event source once at startup. That source nolonger 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:
CGSessionCopyCurrentDictionary()(CGSSessionScreenIsLocked), at most once every500 ms, from
MacosMouse::submit().local-event suppression disabled as before.
source_mutexguards 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
MacosBackendTest.RecreatesMouseEventSourceWhenSessionLockStateChanges. It simulates unlocked → locked →unlocked through new test hooks for
CGSessionCopyCurrentDictionaryandCGEventSourceCreate, and checks that themouse 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_libvirtualhidpasses on a macOS 15 Intel GitHub runner (Debug, Ninja,-DBUILD_TESTS=ON), run from my fork.Manual
libvirtualhid, Moonlight on iPad.
after unlock, over several lock/unlock cycles without restarting Sunshine.
Limitations
session.
Type of Change
Checklist
AI Usage
See our AI usage policy.
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