Fix SGR mouse movement reports (#18864)

According to the documentation, the final character of an SGR mouse
report is meant to be `M` for a button press and `m` for a button
release. However it isn't clear what the final character should be for
motion events, and we were using an `m` if there weren't any buttons
held down at the time, while all other terminals used an `M`, regardless
of the button state.

This PR updates our implementation to match what everyone else is doing,
since our interpretation of the spec was causing problems for some apps.

## Validation Steps Performed

I've manually tested the new behavior in Vttest, and confirmed that our
mouse reports now match Xterm more closely, and I've tested with v0.42.0
of Zellij, which was previous glitching badly in Windows Terminal, but
now works correctly.

I've also updated our unit tests for the SGR mouse mode to reflect the
correct report format.

Closes #18712
This commit is contained in:
James Holderness 2025-05-02 23:36:45 +01:00 committed by GitHub
parent 58092f142f
commit 865f5e5239
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 32 deletions

View File

@ -99,7 +99,7 @@ public:
TerminalInput::StringType str; TerminalInput::StringType str;
str.append(std::wstring_view{ buffer }); str.append(std::wstring_view{ buffer });
str[str.size() - 1] = IsButtonDown(uiButton) ? L'M' : L'm'; str[str.size() - 1] = IsButtonUp(uiButton) ? L'm' : L'M';
return str; return str;
} }
@ -182,23 +182,18 @@ public:
return result; return result;
} }
bool IsButtonDown(unsigned int uiButton) bool IsButtonUp(unsigned int uiButton)
{ {
auto fIsDown = false;
switch (uiButton) switch (uiButton)
{ {
case WM_LBUTTONDBLCLK: case WM_LBUTTONUP:
case WM_LBUTTONDOWN: case WM_RBUTTONUP:
case WM_RBUTTONDOWN: case WM_MBUTTONUP:
case WM_RBUTTONDBLCLK: case WM_XBUTTONUP:
case WM_MBUTTONDOWN: return true;
case WM_MBUTTONDBLCLK: default:
case WM_MOUSEWHEEL: return false;
case WM_MOUSEHWHEEL:
fIsDown = true;
break;
} }
return fIsDown;
} }
/* From winuser.h - Needed to manually specify the test properties /* From winuser.h - Needed to manually specify the test properties

View File

@ -63,24 +63,19 @@ static constexpr bool _isWheelMsg(const unsigned int buttonCode) noexcept
} }
// Routine Description: // Routine Description:
// - Determines if the input windows message code describes a button press // - Determines if the input windows message code describes a button release
// (either down or doubleclick)
// Parameters: // Parameters:
// - button - the message to decode. // - button - the message to decode.
// Return value: // Return value:
// - true if button is a button down event // - true if button is a button up event
static constexpr bool _isButtonDown(const unsigned int button) noexcept static constexpr bool _isButtonUp(const unsigned int button) noexcept
{ {
switch (button) switch (button)
{ {
case WM_LBUTTONDBLCLK: case WM_LBUTTONUP:
case WM_LBUTTONDOWN: case WM_RBUTTONUP:
case WM_RBUTTONDOWN: case WM_MBUTTONUP:
case WM_RBUTTONDBLCLK: case WM_XBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
return true; return true;
default: default:
return false; return false;
@ -372,7 +367,7 @@ TerminalInput::OutputType TerminalInput::HandleMouse(const til::point position,
// then we want to handle hovers with WM_MOUSEMOVE. // then we want to handle hovers with WM_MOUSEMOVE.
// However, if we're dragging (WM_MOUSEMOVE with a button pressed), // However, if we're dragging (WM_MOUSEMOVE with a button pressed),
// then use that pressed button instead. // then use that pressed button instead.
return _GenerateSGRSequence(position, physicalButtonPressed ? realButton : button, _isButtonDown(realButton), isHover, modifierKeyState, delta); return _GenerateSGRSequence(position, physicalButtonPressed ? realButton : button, _isButtonUp(button), isHover, modifierKeyState, delta);
} }
else else
{ {
@ -463,18 +458,18 @@ TerminalInput::OutputType TerminalInput::_GenerateUtf8Sequence(const til::point
// Parameters: // Parameters:
// - position - The windows coordinates (top,left = 0,0) of the mouse event // - position - The windows coordinates (top,left = 0,0) of the mouse event
// - button - the message to decode. WM_MOUSEMOVE is used for mouse hovers with no buttons pressed. // - button - the message to decode. WM_MOUSEMOVE is used for mouse hovers with no buttons pressed.
// - isDown - true if a mouse button was pressed. // - isRelease - true if a mouse button was released.
// - isHover - true if the sequence is generated in response to a mouse hover // - isHover - true if the sequence is generated in response to a mouse hover
// - modifierKeyState - the modifier keys pressed with this button // - modifierKeyState - the modifier keys pressed with this button
// - delta - the amount that the scroll wheel changed (should be 0 unless button is a WM_MOUSE*WHEEL) // - delta - the amount that the scroll wheel changed (should be 0 unless button is a WM_MOUSE*WHEEL)
// - ppwchSequence - On success, where to put the pointer to the generated sequence // - ppwchSequence - On success, where to put the pointer to the generated sequence
// - pcchLength - On success, where to put the length of the generated sequence // - pcchLength - On success, where to put the length of the generated sequence
TerminalInput::OutputType TerminalInput::_GenerateSGRSequence(const til::point position, const unsigned int button, const bool isDown, const bool isHover, const short modifierKeyState, const short delta) TerminalInput::OutputType TerminalInput::_GenerateSGRSequence(const til::point position, const unsigned int button, const bool isRelease, const bool isHover, const short modifierKeyState, const short delta)
{ {
// Format for SGR events is: // Format for SGR events is:
// "\x1b[<%d;%d;%d;%c", xButton, x+1, y+1, fButtonDown? 'M' : 'm' // "\x1b[<%d;%d;%d;%c", xButton, x+1, y+1, isRelease? 'm' : 'M'
const auto xbutton = _windowsButtonToSGREncoding(button, isHover, modifierKeyState, delta); const auto xbutton = _windowsButtonToSGREncoding(button, isHover, modifierKeyState, delta);
return fmt::format(FMT_COMPILE(L"{}<{};{};{}{}"), _csi, xbutton, position.x + 1, position.y + 1, isDown ? L'M' : L'm'); return fmt::format(FMT_COMPILE(L"{}<{};{};{}{}"), _csi, xbutton, position.x + 1, position.y + 1, isRelease ? L'm' : L'M');
} }
// Routine Description: // Routine Description:

View File

@ -109,7 +109,7 @@ namespace Microsoft::Console::VirtualTerminal
#pragma region MouseInput #pragma region MouseInput
[[nodiscard]] OutputType _GenerateDefaultSequence(til::point position, unsigned int button, bool isHover, short modifierKeyState, short delta); [[nodiscard]] OutputType _GenerateDefaultSequence(til::point position, unsigned int button, bool isHover, short modifierKeyState, short delta);
[[nodiscard]] OutputType _GenerateUtf8Sequence(til::point position, unsigned int button, bool isHover, short modifierKeyState, short delta); [[nodiscard]] OutputType _GenerateUtf8Sequence(til::point position, unsigned int button, bool isHover, short modifierKeyState, short delta);
[[nodiscard]] OutputType _GenerateSGRSequence(til::point position, unsigned int button, bool isDown, bool isHover, short modifierKeyState, short delta); [[nodiscard]] OutputType _GenerateSGRSequence(til::point position, unsigned int button, bool isRelease, bool isHover, short modifierKeyState, short delta);
[[nodiscard]] OutputType _makeAlternateScrollOutput(short delta) const; [[nodiscard]] OutputType _makeAlternateScrollOutput(short delta) const;