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

Render the cursor state to VT #2829

Merged
merged 7 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
65 changes: 14 additions & 51 deletions src/host/ut_host/VtRendererTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class Microsoft::Console::Render::VtRendererTest
std::deque<std::string> qExpectedInput;
bool WriteCallback(const char* const pch, size_t const cch);
void TestPaint(VtEngine& engine, std::function<void()> pfn);
void TestPaintXterm(XtermEngine& engine, std::function<void()> pfn);
Viewport SetUpViewport();
};

Expand Down Expand Up @@ -162,38 +161,6 @@ void VtRendererTest::TestPaint(VtEngine& engine, std::function<void()> pfn)
VERIFY_SUCCEEDED(engine.EndPaint());
}

// Function Description:
// - Small helper to do a series of testing wrapped by StartPaint/EndPaint calls
// Also expects \x1b[?25l and \x1b[?25h on start/stop, for cursor visibility
// Arguments:
// - engine: the engine to operate on
// - pfn: A function pointer to some test code to run.
// Return Value:
// - <none>
void VtRendererTest::TestPaintXterm(XtermEngine& engine, std::function<void()> pfn)
{
HRESULT hr = engine.StartPaint();
pfn();
// If we didn't have anything to do on this frame, still execute our
// callback, but don't check for the following ?25h
if (hr != S_FALSE)
{
// If the engine has decided that it needs to disble the cursor, it'll
// insert ?25l to the front of the buffer (which won't hit this
// callback) and write ?25h to the end of the frame
if (engine._needToDisableCursor)
{
qExpectedInput.push_back("\x1b[?25h");
}
}

VERIFY_SUCCEEDED(engine.EndPaint());

VERIFY_ARE_EQUAL(qExpectedInput.size(),
static_cast<size_t>(0),
L"Done painting, there shouldn't be any output we're still expecting");
}

void VtRendererTest::VtSequenceHelperTests()
{
wil::unique_hfile hFile = wil::unique_hfile(INVALID_HANDLE_VALUE);
Expand Down Expand Up @@ -287,7 +254,7 @@ void VtRendererTest::Xterm256TestInvalidate()
L"Make sure that scrolling only invalidates part of the viewport, and sends the right sequences"));
COORD scrollDelta = { 0, 1 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled one down, only top line is invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -303,7 +270,7 @@ void VtRendererTest::Xterm256TestInvalidate()
scrollDelta = { 0, 3 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));

TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three down, only top 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -317,7 +284,7 @@ void VtRendererTest::Xterm256TestInvalidate()

scrollDelta = { 0, -1 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled one up, only bottom line is invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -332,7 +299,7 @@ void VtRendererTest::Xterm256TestInvalidate()

scrollDelta = { 0, -3 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three up, only bottom 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -352,7 +319,7 @@ void VtRendererTest::Xterm256TestInvalidate()
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
scrollDelta = { 0, 2 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three down, only top 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand Down Expand Up @@ -546,8 +513,6 @@ void VtRendererTest::Xterm256TestCursor()
L"----move to the start of this line (y stays the same)----"));
qExpectedInput.push_back("\r");
VERIFY_SUCCEEDED(engine->_MoveCursor({ 0, 1 }));

qExpectedInput.push_back("\x1b[?25h");
});

TestPaint(*engine, [&]() {
Expand Down Expand Up @@ -618,15 +583,15 @@ void VtRendererTest::XtermTestInvalidate()
L"Make sure that invalidating anything only invalidates that portion"));
SMALL_RECT invalid = { 1, 1, 1, 1 };
VERIFY_SUCCEEDED(engine->Invalidate(&invalid));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
VERIFY_ARE_EQUAL(invalid, engine->_invalidRect.ToExclusive());
});

Log::Comment(NoThrowString().Format(
L"Make sure that scrolling only invalidates part of the viewport, and sends the right sequences"));
COORD scrollDelta = { 0, 1 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled one down, only top line is invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -641,7 +606,7 @@ void VtRendererTest::XtermTestInvalidate()

scrollDelta = { 0, 3 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three down, only top 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -655,7 +620,7 @@ void VtRendererTest::XtermTestInvalidate()

scrollDelta = { 0, -1 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled one up, only bottom line is invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -670,7 +635,7 @@ void VtRendererTest::XtermTestInvalidate()

scrollDelta = { 0, -3 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three up, only bottom 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand All @@ -690,7 +655,7 @@ void VtRendererTest::XtermTestInvalidate()
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
scrollDelta = { 0, 2 };
VERIFY_SUCCEEDED(engine->InvalidateScroll(&scrollDelta));
TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"---- Scrolled three down, only top 3 lines are invalid. ----"));
invalid = view.ToExclusive();
Expand Down Expand Up @@ -853,8 +818,6 @@ void VtRendererTest::XtermTestCursor()
L"----move to the start of this line (y stays the same)----"));
qExpectedInput.push_back("\r");
VERIFY_SUCCEEDED(engine->_MoveCursor({ 0, 1 }));

qExpectedInput.push_back("\x1b[?25h");
});

TestPaint(*engine, [&]() {
Expand Down Expand Up @@ -1119,14 +1082,14 @@ void VtRendererTest::TestWrapping()

Viewport view = SetUpViewport();

TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"Make sure the cursor is at 0,0"));
qExpectedInput.push_back("\x1b[H");
VERIFY_SUCCEEDED(engine->_MoveCursor({ 0, 0 }));
});

TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
Log::Comment(NoThrowString().Format(
L"Painting a line that wrapped, then painting another line, and "
L"making sure we don't manually move the cursor between those paints."));
Expand Down Expand Up @@ -1185,7 +1148,7 @@ void VtRendererTest::TestResize()

VERIFY_SUCCEEDED(engine->UpdateViewport(newView.ToInclusive()));

TestPaintXterm(*engine, [&]() {
TestPaint(*engine, [&]() {
VERIFY_ARE_EQUAL(newView, engine->_invalidRect);
VERIFY_IS_FALSE(engine->_firstPaint);
VERIFY_IS_FALSE(engine->_suppressResizeRepaint);
Expand Down
69 changes: 52 additions & 17 deletions src/renderer/vt/XtermEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ XtermEngine::XtermEngine(_In_ wil::unique_hfile hPipe,

_trace.TraceLastText(_lastText);

// Prep us to think that the cursor is not visible this frame. If it _is_
// visible, then PaintCursor will be called, and we'll set this to true
// during the frame.
_nextCursorIsVisible = false;

if (_firstPaint)
{
// MSFT:17815688
Expand Down Expand Up @@ -72,14 +77,7 @@ XtermEngine::XtermEngine(_In_ wil::unique_hfile hPipe,

if (!_quickReturn)
{
if (!_WillWriteSingleChar())
{
// MSFT:TODO:20331739
// Make sure to match the cursor visibility in the terminal to the console's
// // Turn off cursor
// RETURN_IF_FAILED(_HideCursor());
}
else
if (_WillWriteSingleChar())
{
// Don't re-enable the cursor.
_quickReturn = true;
Expand All @@ -98,22 +96,38 @@ XtermEngine::XtermEngine(_In_ wil::unique_hfile hPipe,
// - S_OK if we succeeded, else an appropriate HRESULT for failing to allocate or write.
[[nodiscard]] HRESULT XtermEngine::EndPaint() noexcept
{
// MSFT:TODO:20331739
// Make sure to match the cursor visibility in the terminal to the console's
// if (!_quickReturn)
// {
// // Turn on cursor
// RETURN_IF_FAILED(_ShowCursor());
// }

// If during the frame we determined that the cursor needed to be disabled,
// then insert a cursor off at the start of the buffer, and re-enable
// the cursor here.
if (_needToDisableCursor)
{
_buffer.insert(0, "\x1b[25l");
// If the cursor was previously visible, let's hide it for this frame,
// by prepending a cursor off.
if (_lastCursorIsVisible)
{
_buffer.insert(0, "\x1b[25l");
_lastCursorIsVisible = false;
}
// If the cursor was NOT previously visible, then that's fine! we don't
// need to worry, it's already off.
}

// If the cursor is moving from off -> on (including cases where we just
// disabled if for this frame), show the cursor at the end of the frame
if (_nextCursorIsVisible && !_lastCursorIsVisible)
{
RETURN_IF_FAILED(_ShowCursor());
}
// Otherwise, if the cursor previously was visible, and it should be hidden
// (on -> off), hide it at the end of the frame.
else if (!_nextCursorIsVisible && _lastCursorIsVisible)
{
RETURN_IF_FAILED(_HideCursor());
}

// Update our tracker of what we thought the last cursor state of the
// terminal was.
_lastCursorIsVisible = _nextCursorIsVisible;

RETURN_IF_FAILED(VtEngine::EndPaint());

Expand Down Expand Up @@ -177,6 +191,27 @@ XtermEngine::XtermEngine(_In_ wil::unique_hfile hPipe,
return VtEngine::_16ColorUpdateDrawingBrushes(colorForeground, colorBackground, isBold, _ColorTable, _cColorTable);
}

// Routine Description:
// - Draws the cursor on the screen
// Arguments:
// - options - Options that affect the presentation of the cursor
// Return Value:
// - S_OK or suitable HRESULT error from writing pipe.
[[nodiscard]] HRESULT XtermEngine::PaintCursor(const IRenderEngine::CursorOptions& options) noexcept
{
// PaintCursor is only called when the cursor is in fact visible in a single
// frame. When this is called, mark _nextCursorIsVisible as true. At the end
// of the frame, we'll decide to either turn the cursor on or not, based
// upon the previous state.

// When this method is not called during a frame, it's because the cursor
// was not visible. In that case, at the end of the frame,
// _nextCursorIsVisible will still be false (from when we set it during
// StartPaint)
_nextCursorIsVisible = true;
return VtEngine::PaintCursor(options);
}

// Routine Description:
// - Write a VT sequence to move the cursor to the specified coordinates. We
// also store the last place we left the cursor for future optimizations.
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/vt/XtermEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Microsoft::Console::Render
[[nodiscard]] HRESULT StartPaint() noexcept override;
[[nodiscard]] HRESULT EndPaint() noexcept override;

[[nodiscard]] HRESULT PaintCursor(const CursorOptions& options) noexcept override;

[[nodiscard]] virtual HRESULT UpdateDrawingBrushes(const COLORREF colorForeground,
const COLORREF colorBackground,
const WORD legacyColorAttribute,
Expand All @@ -60,6 +62,8 @@ namespace Microsoft::Console::Render
bool _previousLineWrapped;
bool _usingUnderLine;
bool _needToDisableCursor;
bool _lastCursorIsVisible = false;
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
bool _nextCursorIsVisible = true;

[[nodiscard]] HRESULT _MoveCursor(const COORD coord) noexcept override;

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/vt/vtrenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Microsoft::Console::Render
const COORD coordTarget) noexcept override;
[[nodiscard]] HRESULT PaintSelection(const SMALL_RECT rect) noexcept override;

[[nodiscard]] HRESULT PaintCursor(const CursorOptions& options) noexcept override;
[[nodiscard]] virtual HRESULT PaintCursor(const CursorOptions& options) noexcept override;

[[nodiscard]] virtual HRESULT UpdateDrawingBrushes(const COLORREF colorForeground,
const COLORREF colorBackground,
Expand Down