input-inject: wheel remainder, release buttons at the real pointer

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 02:52:40 -07:00
1 parent f6fc2c5bd3
commit ccb058ed3a
2 files changed
+37 -12

No files matched your search

+29 -10
View File
@@ -56,6 +56,8 @@ public final class Injector: @unchecked Sendable {
private var state = KeyState()
private var clicks: ClickTracker
private var lastPosted: CGPoint?
private var verticalWheelRemainder: Int32 = 0
private var horizontalWheelRemainder: Int32 = 0
private var dropped: UInt64 = 0
/// An injector whose events are tagged so our own tap passes them through
@@ -278,11 +280,15 @@ public final class Injector: @unchecked Sendable {
}
/// `MOUSEEVENTF_WHEEL` / `HWHEEL` (`InputSimulation.cs:204-209`). Windows
/// counts in 120ths of a notch and macOS counts lines, so the packet's
/// `WheelDelta` divides straight down (docs/protocol.md §5.2).
/// counts in 120ths of a notch and macOS counts lines (docs/protocol.md
/// §5.2), so 120 units become one line.
///
/// A high-resolution wheel sends fractions of a notch, which Windows
/// accumulates inside `SendInput`; here the fraction is carried in a
/// remainder so a slow scroll is not silently thrown away.
private func postWheel(vertical: Int32, horizontal: Int32, at point: CGPoint) {
let lines = vertical / WindowsMessage.wheelDelta
let columns = horizontal / WindowsMessage.wheelDelta
let lines = Injector.notches(vertical, remainder: &verticalWheelRemainder)
let columns = Injector.notches(horizontal, remainder: &horizontalWheelRemainder)
guard lines != 0 || columns != 0 else { return }
guard
let event = CGEvent(
@@ -315,11 +321,11 @@ public final class Injector: @unchecked Sendable {
}
postKey(keycode: keycode, vk: vk, down: false)
}
for button in state.buttonReleaseOrder() {
let buttons = state.buttonReleaseOrder()
let at = buttons.isEmpty ? CGPoint(x: 0, y: 0) : (currentPointer() ?? CGPoint(x: 0, y: 0))
for button in buttons {
_ = state.buttonUp(button.number)
postButtonEvent(
button.number, down: false, at: lastPosted ?? CGPoint(x: 0, y: 0),
clickState: button.clickState)
postButtonEvent(button.number, down: false, at: at, clickState: button.clickState)
}
clicks.reset()
// `ResetSystemKeyFlags` (`InputSimulation.cs:477-480`). Windows leaves the
@@ -367,11 +373,24 @@ public final class Injector: @unchecked Sendable {
// MARK: - Helpers
/// Whole lines out of a stream of 120ths, keeping the fraction for next time.
private static func notches(_ delta: Int32, remainder: inout Int32) -> Int32 {
remainder &+= delta
let whole = remainder / WindowsMessage.wheelDelta
remainder &-= whole &* WindowsMessage.wheelDelta
return whole
}
/// The pointer right now. Windows reads it with `GetCursorPos` on the same
/// paths (`Receiver.cs:149, 156`).
private func pointer(layout: ScreenLayout) -> CGPoint {
if let here = CGEvent(source: nil)?.location { return here }
return lastPosted ?? Coordinates.centre(of: layout.primary)
currentPointer() ?? Coordinates.centre(of: layout.primary)
}
/// The pointer right now, for the paths that have no `ScreenLayout` to fall
/// back on — `releaseAll()` is called from recovery, not from a packet.
private func currentPointer() -> CGPoint? {
CGEvent(source: nil)?.location ?? lastPosted
}
/// Keeps an injected point on a real display. Windows relies on `SendInput`