Repository navigation
fix: decode SGR mouse reports with a negative coordinate - #224
Open
Gaurav-Gosain wants to merge 1 commit into
Open
Gaurav-Gosain wants to merge 1 commit into
Gaurav-Gosain wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.parseCsionly takes 0x30-0x3F as parameter bytes, so the scan stops at the-. The decoder returnsUnknownEvent("\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 andM/minto 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.Paramscan'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 anUnknownEventcovering 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:
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 ./...andgo vet .pass.Sources for the terminal behaviour: Ghostty's
posToPixelsinsrc/input/mouse_encode.zignotes the value "may be negative", and kitty computesglobal_x = mouse_x - leftbefore clamping inkitty/mouse.c.