Skip to content

fix: decode SGR mouse reports with a negative coordinate - #224

Open
Gaurav-Gosain wants to merge 1 commit into
charmbracelet:mainfrom
Gaurav-Gosain:fix/sgr-mouse-negative
Open

Gaurav-Gosain wants to merge 1 commit into
charmbracelet:mainfrom
Gaurav-Gosain:fix/sgr-mouse-negative

Conversation

@Gaurav-Gosain

Copy link
Copy Markdown

Hey! I hit this one in tuios: with SGR-pixel mouse mode (1016) on, Ghostty and kitty don't clamp the pointer to the grid, so a pointer in the left padding (or outside the window while dragging) comes in with a negative x, and one above the first row with a negative y, like ESC [ < 32 ; -5 ; 328 M.

parseCsi only takes 0x30-0x3F as parameter bytes, so the scan stops at the -. The decoder returns UnknownEvent("\x1b[<32;-") and the rest, 5;328M, comes through as typed keys. In my case that meant dragging a window to the left edge typed digits and M/m into the app. It also stalls input: the reader holds the unknown event until the escape timeout, and every new read resets it, so while the pointer keeps moving past the edge nothing gets through. I measured 41 motion reports written over 320 ms arriving all at once at 374 ms.

The fix is a small dedicated parser, parseSignedSGRMouse, reached only from the branch where the generic scan already fails (< prefix and the scan stopped at a -), so the hot path doesn't change. ansi.Params can't carry a negative value since the sign bit is the more-flag, which is why it parses the three numbers itself. An incomplete report still returns an UnknownEvent covering the whole buffer, so the reader waits for the rest like before. Positions stay 0-based after the usual -1, so a report at x = -5 becomes X = -6.

How I tested it:

  • A table test, TestSignedSGRMouse: negative x, negative y, both, press, release, motion, wheel, modifiers, the 8-bit CSI form, a report followed by more input, incomplete reports, and a few malformed ones.
  • go test ./... and go vet . pass.
  • In tuios with Ghostty and 1016 on, a drag past the left edge now decodes as mouse events instead of keys.

Sources for the terminal behaviour: Ghostty's posToPixels in src/input/mouse_encode.zig notes the value "may be negative", and kitty computes global_x = mouse_x - left before clamping in kitty/mouse.c.

A terminal in SGR-pixel mode (1016), such as Ghostty or kitty, does not clamp the pointer to the grid. A pointer in the left padding, or outside the window during a drag, is reported at a negative x, and above the first row at a negative y. '-' is not a CSI parameter byte, so the report decoded as an unknown event followed by typed keys, and the reader held the unknown event until the escape timeout, stalling input while the pointer moved past the edge. Parse those reports on the path where the generic scan already fails, so the hot path does not change.
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.

1 participant