Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid encoding VT via win32 input mode #16407

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions src/host/inputBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,27 @@ size_t InputBuffer::Write(const std::span<const INPUT_RECORD>& inEvents)
}
}

void InputBuffer::WriteString(const std::wstring_view& text)
try
{
if (text.empty())
{
return;
}

const auto initiallyEmptyQueue = _storage.empty();

_writeString(text);

if (initiallyEmptyQueue && !_storage.empty())
{
ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
}

WakeUpReadersWaitingForData();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_HandleTerminalInputCallback doesn't wake up readers if there were no bits - should we do that here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit afraid of outright using _HandleTerminalInputCallback, because InputBuffer::Write actually does it exactly like that: It always calls WakeUpReadersWaitingForData(). In the future, if I ever get to rewrite InputBuffer with proper string support, I was hoping to really understand how this works and to properly unify the event/wakeup code.

}
CATCH_LOG()

// This can be considered a "privileged" variant of Write() which allows FOCUS_EVENTs to generate focus VT sequences.
// If we didn't do this, someone could write a FOCUS_EVENT_RECORD with WriteConsoleInput, exit without flushing the
// input buffer and the next application will suddenly get a "\x1b[I" sequence in their input. See GH#13238.
Expand Down Expand Up @@ -828,21 +849,7 @@ void InputBuffer::_HandleTerminalInputCallback(const TerminalInput::StringType&
return;
}

for (const auto& wch : text)
{
if (wch == UNICODE_NULL)
{
// Convert null byte back to input event with proper control state
const auto zeroKey = OneCoreSafeVkKeyScanW(0);
uint32_t ctrlState = 0;
WI_SetFlagIf(ctrlState, SHIFT_PRESSED, WI_IsFlagSet(zeroKey, 0x100));
WI_SetFlagIf(ctrlState, LEFT_CTRL_PRESSED, WI_IsFlagSet(zeroKey, 0x200));
WI_SetFlagIf(ctrlState, LEFT_ALT_PRESSED, WI_IsFlagSet(zeroKey, 0x400));
_storage.push_back(SynthesizeKeyEvent(true, 1, LOBYTE(zeroKey), 0, wch, ctrlState));
continue;
}
_storage.push_back(SynthesizeKeyEvent(true, 1, 0, 0, wch, 0));
}
_writeString(text);

if (!_vtInputShouldSuppress)
{
Expand All @@ -856,6 +863,25 @@ void InputBuffer::_HandleTerminalInputCallback(const TerminalInput::StringType&
}
}

void InputBuffer::_writeString(const std::wstring_view& text)
{
for (const auto& wch : text)
{
if (wch == UNICODE_NULL)
{
// Convert null byte back to input event with proper control state
const auto zeroKey = OneCoreSafeVkKeyScanW(0);
uint32_t ctrlState = 0;
WI_SetFlagIf(ctrlState, SHIFT_PRESSED, WI_IsFlagSet(zeroKey, 0x100));
WI_SetFlagIf(ctrlState, LEFT_CTRL_PRESSED, WI_IsFlagSet(zeroKey, 0x200));
WI_SetFlagIf(ctrlState, LEFT_ALT_PRESSED, WI_IsFlagSet(zeroKey, 0x400));
_storage.push_back(SynthesizeKeyEvent(true, 1, LOBYTE(zeroKey), 0, wch, ctrlState));
continue;
}
_storage.push_back(SynthesizeKeyEvent(true, 1, 0, 0, wch, 0));
}
}

TerminalInput& InputBuffer::GetTerminalInput()
{
return _termInput;
Expand Down
2 changes: 2 additions & 0 deletions src/host/inputBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class InputBuffer final : public ConsoleObjectHeader
size_t Prepend(const std::span<const INPUT_RECORD>& inEvents);
size_t Write(const INPUT_RECORD& inEvent);
size_t Write(const std::span<const INPUT_RECORD>& inEvents);
void WriteString(const std::wstring_view& text);
void WriteFocusEvent(bool focused) noexcept;
bool WriteMouseEvent(til::point position, unsigned int button, short keyState, short wheelDelta);

Expand Down Expand Up @@ -96,6 +97,7 @@ class InputBuffer final : public ConsoleObjectHeader
void _WriteBuffer(const std::span<const INPUT_RECORD>& inRecords, _Out_ size_t& eventsWritten, _Out_ bool& setWaitEvent);
bool _CoalesceEvent(const INPUT_RECORD& inEvent) noexcept;
void _HandleTerminalInputCallback(const Microsoft::Console::VirtualTerminal::TerminalInput::StringType& text);
void _writeString(const std::wstring_view& text);

#ifdef UNIT_TESTING
friend class InputBufferTests;
Expand Down
15 changes: 1 addition & 14 deletions src/host/outputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,11 @@
// - <none>
void ConhostInternalGetSet::ReturnResponse(const std::wstring_view response)
{
InputEventQueue inEvents;

// generate a paired key down and key up event for every
// character to be sent into the console's input buffer
for (const auto& wch : response)
{
// This wasn't from a real keyboard, so we're leaving key/scan codes blank.
auto keyEvent = SynthesizeKeyEvent(true, 1, 0, 0, wch, 0);
inEvents.push_back(keyEvent);
keyEvent.Event.KeyEvent.bKeyDown = false;
inEvents.push_back(keyEvent);
}

// TODO GH#4954 During the input refactor we may want to add a "priority" input list
// to make sure that "response" input is spooled directly into the application.
// We switched this to an append (vs. a prepend) to fix GH#1637, a bug where two CPR
// could collide with each other.
_io.GetActiveInputBuffer()->Write(inEvents);
_io.GetActiveInputBuffer()->WriteString(response);
}

// Routine Description:
Expand Down Expand Up @@ -98,7 +85,7 @@
THROW_IF_FAILED(info.SetViewportOrigin(true, position, false));
// SetViewportOrigin() only updates the virtual bottom (the bottom coordinate of the area
// in the text buffer a VT client writes its output into) when it's moving downwards.
// But this function is meant to truly move the viewport no matter what. Otherwise `tput reset` breaks.

Check notice on line 88 in src/host/outputStream.cpp

View workflow job for this annotation

GitHub Actions / Spell checking

`Line` matches candidate pattern `\btput\s+(?:(?:-[SV]|-T\s*\w+)\s+)*\w{3,5}\b` (candidate-pattern)
info.UpdateBottom();
}

Expand Down
Loading