Skip to content

Only observe drag events when a configured feature needs them - #1381

Closed
eret9616 wants to merge 1 commit into
linearmouse:mainfrom
eret9616:fix/skip-unneeded-drag-events
Closed

eret9616 wants to merge 1 commit into
linearmouse:mainfrom
eret9616:fix/skip-unneeded-drag-events

Conversation

@eret9616

@eret9616 eret9616 commented Sep 4, 2026

Copy link
Copy Markdown

Fixes #1380

Problem

GlobalEventTap observes EventType.all, which unconditionally includes leftMouseDragged, rightMouseDragged and otherMouseDragged. The tap is a synchronous .defaultTap at the HID level, so WindowServer has to round-trip every one of those events through LinearMouse before delivering it. With a 4000–8000 Hz mouse that is up to 8000 blocking round trips per second while a window is being dragged, and dragging stutters visibly (plain pointer movement is fine because mouseMoved is not observed). Quitting LinearMouse makes it smooth again instantly.

In many configurations nothing can consume drag events at all — only pointer/scrolling settings are in use — so observing them is pure overhead.

Change

  • EventType.required(for: schemes) derives the tap's event mask from the configuration:
    • the base set (scroll wheel, button down/up, key down/up, flags changed) is always observed;
    • *MouseDragged is only observed for buttons that some scheme can actually watch while held: trigger buttons of buttons.mappings (including simultaneous / whileHeld), left + right when switchPrimaryButtonAndSecondaryButtons is on, back/forward for universalBackForward, and the autoScroll / gesture trigger buttons (defaulting to the middle button, matching the transformers);
    • mouseMoved follows the existing rule (redirectsToScroll, autoScroll, gesture).
  • GlobalEventTap subscribes to ConfigurationState.$configuration and recreates the tap when the required set changes (debounced, on the main run loop). As a side effect, enabling autoScroll / gesture / redirectsToScroll now starts observing mouseMoved without restarting the app.
  • The observed event types are logged at info level when the tap is created, which makes this easy to check with log show.

The mask is still global (one tap), so a mapping on button 3 for one device keeps otherMouseDragged observed for all devices — exactly what happens today for those buttons; the change only ever removes drag types that no feature could consume.

Testing

  • xcodebuild test: 841 tests, 0 failures.
  • Manual, macOS 15.3.1, Keychron M6 8K wired at 8000 Hz, config with only pointer/scrolling settings for that device plus button mappings (buttons 2/3) on other devices:
    • stock v0.11.4: window dragging stutters 100% of the time; this branch: smooth.
    • CGGetEventTapList shows the tap mask going from 0xe401cde to 0xe401c1e (leftMouseDragged / rightMouseDragged dropped, otherMouseDragged kept because of the button 2/3 mappings).
    • Scrolling, the Command scroll-speed modifier and the button mappings keep working.
  • Note: main currently needs the Xcode 26 SDK because of glassEffect in SecondaryButtonStyle.swift; I built and ran the tests with Xcode 16.4 by stubbing that single line locally (not part of this PR).

🤖 Generated with Claude Code

The global event tap is synchronous, so every observed event costs a
WindowServer round trip through LinearMouse. It observed all
*MouseDragged events unconditionally, which makes click-and-drag stutter
with 4000-8000 Hz mice even when no feature can consume those events.

Derive the tap's event mask from the configuration instead: drag events
are only observed for buttons that button mappings, primary/secondary
button switching, universal back/forward, auto scroll or gesture can
watch while held, and the tap is recreated when the required set
changes.

Fixes linearmouse#1380

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@lujjjh

lujjjh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for investigating this. The optimization makes sense as a workaround, although the performance issue still remains when these features are actually in use.

There is also a correctness issue in EventType.required(for:). It examines each raw scheme independently, while Auto Scroll settings are merged field by field. For example, if a base scheme enables Auto Scroll with the middle button and a conditional scheme overrides only the trigger to the right button, the effective configuration uses the right button. However, the conditional scheme has no explicit enabled: true, so it is skipped and rightMouseDragged is omitted from the event mask.

More generally, EventType and GlobalEventTap should not need to duplicate the configuration rules of every feature. It may be cleaner for transformers to declare their required event types through the same path used to build them, with the global event tap observing the resulting union across the configuration.

lujjjh added a commit that referenced this pull request Oct 3, 2026
Enable motion events only while needed by configuration or active
interactions. Skip unrelated transformer work and update the Auto Scroll
indicator at 60 Hz.

Validated with 966 unit tests and manual testing.

Supersedes #1381. Related to #1380 and #915.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Performance**
* Reduced unnecessary pointer-motion event processing, which may improve
responsiveness during mouse movement and dragging.
* **Bug Fixes**
* Improved event tap recovery and interaction handling, including
maintaining button releases during overlapping interactions.
* **Testing**
* Added coverage for pointer-motion handling, deferred events, and
performance.
* Isolated test configuration from personal settings when running the
test suite.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@lujjjh

lujjjh commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Closing this in favor of #1412.

@lujjjh lujjjh closed this Oct 3, 2026
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.

Click-and-drag stutters with an 8000 Hz mouse: the synchronous event tap observes every drag event even when no feature needs them

2 participants