Skip to content

Commit

Permalink
Introduce VTInt to represent VT parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Jan 21, 2022
1 parent 4e46c85 commit 64238ab
Show file tree
Hide file tree
Showing 41 changed files with 734 additions and 682 deletions.
10 changes: 5 additions & 5 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ namespace Microsoft::Terminal::Core
virtual void SetTextAttributes(const TextAttribute& attrs) noexcept = 0;

virtual Microsoft::Console::Types::Viewport GetBufferSize() noexcept = 0;
virtual bool SetCursorPosition(short x, short y) noexcept = 0;
virtual COORD GetCursorPosition() noexcept = 0;
virtual bool SetCursorPosition(til::point pos) noexcept = 0;
virtual til::point GetCursorPosition() noexcept = 0;
virtual bool SetCursorVisibility(const bool visible) noexcept = 0;
virtual bool CursorLineFeed(const bool withReturn) noexcept = 0;
virtual bool EnableCursorBlinking(const bool enable) noexcept = 0;

virtual bool DeleteCharacter(const size_t count) noexcept = 0;
virtual bool InsertCharacter(const size_t count) noexcept = 0;
virtual bool EraseCharacters(const size_t numChars) noexcept = 0;
virtual bool DeleteCharacter(const til::CoordType count) noexcept = 0;
virtual bool InsertCharacter(const til::CoordType count) noexcept = 0;
virtual bool EraseCharacters(const til::CoordType numChars) noexcept = 0;
virtual bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) noexcept = 0;
virtual bool EraseInDisplay(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) noexcept = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ bool Terminal::SendMouseEvent(const COORD viewportPos, const unsigned int uiButt
#pragma warning(suppress : 26496) // analysis can't tell we're assigning through a reference below
auto clampedPos{ viewportPos };
_mutableViewport.ToOrigin().Clamp(clampedPos);
return _terminalInput->HandleMouse(clampedPos, uiButton, GET_KEYSTATE_WPARAM(states.Value()), wheelDelta, state);
return _terminalInput->HandleMouse(til::point{ clampedPos }, uiButton, GET_KEYSTATE_WPARAM(states.Value()), wheelDelta, state);
}

// Method Description:
Expand Down
10 changes: 5 additions & 5 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ class Microsoft::Terminal::Core::Terminal final :
TextAttribute GetTextAttributes() const noexcept override;
void SetTextAttributes(const TextAttribute& attrs) noexcept override;
Microsoft::Console::Types::Viewport GetBufferSize() noexcept override;
bool SetCursorPosition(short x, short y) noexcept override;
COORD GetCursorPosition() noexcept override;
bool SetCursorPosition(til::point pos) noexcept override;
til::point GetCursorPosition() noexcept override;
bool SetCursorVisibility(const bool visible) noexcept override;
bool EnableCursorBlinking(const bool enable) noexcept override;
bool CursorLineFeed(const bool withReturn) noexcept override;
bool DeleteCharacter(const size_t count) noexcept override;
bool InsertCharacter(const size_t count) noexcept override;
bool EraseCharacters(const size_t numChars) noexcept override;
bool DeleteCharacter(const til::CoordType count) noexcept override;
bool InsertCharacter(const til::CoordType count) noexcept override;
bool EraseCharacters(const til::CoordType numChars) noexcept override;
bool EraseInLine(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) noexcept override;
bool EraseInDisplay(const ::Microsoft::Console::VirtualTerminal::DispatchTypes::EraseType eraseType) noexcept override;
bool WarningBell() noexcept override;
Expand Down
77 changes: 25 additions & 52 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,26 @@ Viewport Terminal::GetBufferSize() noexcept
return _buffer->GetSize();
}

bool Terminal::SetCursorPosition(short x, short y) noexcept
bool Terminal::SetCursorPosition(til::point pos) noexcept
try
{
const auto viewport = _GetMutableViewport();
const auto viewOrigin = viewport.Origin();
const short absoluteX = viewOrigin.X + x;
const short absoluteY = viewOrigin.Y + y;
COORD newPos{ absoluteX, absoluteY };
const til::point viewOrigin{ viewport.Origin() };
auto newPos = til::unwrap_coord(viewOrigin + pos);
viewport.Clamp(newPos);
_buffer->GetCursor().SetPosition(newPos);

return true;
}
CATCH_RETURN_FALSE()

COORD Terminal::GetCursorPosition() noexcept
til::point Terminal::GetCursorPosition() noexcept
{
const auto absoluteCursorPos = _buffer->GetCursor().GetPosition();
const til::point absoluteCursorPos{ _buffer->GetCursor().GetPosition() };
const auto viewport = _GetMutableViewport();
const auto viewOrigin = viewport.Origin();
const short relativeX = absoluteCursorPos.X - viewOrigin.X;
const short relativeY = absoluteCursorPos.Y - viewOrigin.Y;
COORD newPos{ relativeX, relativeY };

const til::point viewOrigin{ viewport.Origin() };
// TODO assert that the coord is > (0, 0) && <(view.W, view.H)
return newPos;
return absoluteCursorPos - viewOrigin;
}

// Method Description:
Expand Down Expand Up @@ -106,26 +100,16 @@ CATCH_RETURN_FALSE()
// - count, the number of characters to delete
// Return value:
// - true if succeeded, false otherwise
bool Terminal::DeleteCharacter(const size_t count) noexcept
bool Terminal::DeleteCharacter(const til::CoordType count) noexcept
try
{
SHORT dist;
if (!SUCCEEDED(SizeTToShort(count, &dist)))
{
return false;
}
const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto copyToPos = cursorPos;
const COORD copyFromPos{ cursorPos.X + dist, cursorPos.Y };
const til::point copyFromPos{ cursorPos.X + count, cursorPos.Y };
const auto sourceWidth = _mutableViewport.RightExclusive() - copyFromPos.X;
SHORT width;
if (!SUCCEEDED(UIntToShort(sourceWidth, &width)))
{
return false;
}

// Get a rectangle of the source
auto source = Viewport::FromDimensions(copyFromPos, width, 1);
auto source = Viewport::FromDimensions(til::unwrap_coord(copyFromPos), gsl::narrow<short>(sourceWidth), 1);

// Get a rectangle of the target
const auto target = Viewport::FromDimensions(copyToPos, source.Dimensions());
Expand Down Expand Up @@ -154,34 +138,24 @@ CATCH_RETURN_FALSE()
// - count, the number of spaces to insert
// Return value:
// - true if succeeded, false otherwise
bool Terminal::InsertCharacter(const size_t count) noexcept
bool Terminal::InsertCharacter(const til::CoordType count) noexcept
try
{
// NOTE: the code below is _extremely_ similar to DeleteCharacter
// We will want to use this same logic and implement a helper function instead
// that does the 'move a region from here to there' operation
// TODO: Github issue #2163
SHORT dist;
if (!SUCCEEDED(SizeTToShort(count, &dist)))
{
return false;
}

const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto copyFromPos = cursorPos;
const COORD copyToPos{ cursorPos.X + dist, cursorPos.Y };
const til::point copyToPos{ cursorPos.X + count, cursorPos.Y };
const auto sourceWidth = _mutableViewport.RightExclusive() - copyFromPos.X;
SHORT width;
if (!SUCCEEDED(UIntToShort(sourceWidth, &width)))
{
return false;
}

// Get a rectangle of the source
auto source = Viewport::FromDimensions(copyFromPos, width, 1);
const auto sourceOrigin = source.Origin();
auto source = Viewport::FromDimensions(copyFromPos, gsl::narrow<short>(sourceWidth), 1);

// Get a rectangle of the target
const auto target = Viewport::FromDimensions(copyToPos, source.Dimensions());
const auto target = Viewport::FromDimensions(til::unwrap_coord(copyToPos), source.Dimensions());
const auto walkDirection = Viewport::DetermineWalkDirection(source, target);

auto sourcePos = source.GetWalkOrigin(walkDirection);
Expand All @@ -193,20 +167,20 @@ try
const auto data = OutputCell(*(_buffer->GetCellDataAt(sourcePos)));
_buffer->Write(OutputCellIterator({ &data, 1 }), targetPos);
} while (source.WalkInBounds(sourcePos, walkDirection) && target.WalkInBounds(targetPos, walkDirection));
const auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), dist);
const auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), count);
_buffer->Write(eraseIter, cursorPos);

return true;
}
CATCH_RETURN_FALSE()

bool Terminal::EraseCharacters(const size_t numChars) noexcept
bool Terminal::EraseCharacters(const til::CoordType numChars) noexcept
try
{
const auto absoluteCursorPos = _buffer->GetCursor().GetPosition();
const auto viewport = _GetMutableViewport();
const short distanceToRight = viewport.RightExclusive() - absoluteCursorPos.X;
const short fillLimit = std::min(static_cast<short>(numChars), distanceToRight);
const auto distanceToRight = viewport.RightExclusive() - absoluteCursorPos.X;
const auto fillLimit = std::min(numChars, distanceToRight);
const auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), fillLimit);
_buffer->Write(eraseIter, absoluteCursorPos);
return true;
Expand All @@ -228,7 +202,7 @@ try
{
const auto cursorPos = _buffer->GetCursor().GetPosition();
const auto viewport = _GetMutableViewport();
COORD startPos = { 0 };
til::point startPos;
startPos.Y = cursorPos.Y;
// nlength determines the number of spaces we need to write
DWORD nlength = 0;
Expand All @@ -254,7 +228,7 @@ try
const auto eraseIter = OutputCellIterator(UNICODE_SPACE, _buffer->GetCurrentAttributes(), nlength);

// Explicitly turn off end-of-line wrap-flag-setting when erasing cells.
_buffer->Write(eraseIter, startPos, false);
_buffer->Write(eraseIter, til::unwrap_coord(startPos), false);
return true;
}
CATCH_RETURN_FALSE()
Expand All @@ -271,8 +245,7 @@ bool Terminal::EraseInDisplay(const DispatchTypes::EraseType eraseType) noexcept
try
{
// Store the relative cursor position so we can restore it later after we move the viewport
const auto cursorPos = _buffer->GetCursor().GetPosition();
#pragma warning(suppress : 26496) // This is written by ConvertToOrigin, cpp core checks is wrong saying it should be const.
const auto cursorPos{ _buffer->GetCursor().GetPosition() };
auto relativeCursor = cursorPos;
_mutableViewport.ConvertToOrigin(&relativeCursor);

Expand All @@ -296,7 +269,7 @@ try
short sNewTop = coordLastChar.Y + 1;

// Increment the circular buffer only if the new location of the viewport would be 'below' the buffer
const short delta = (sNewTop + _mutableViewport.Height()) - (_buffer->GetSize().Height());
const auto delta = (sNewTop + _mutableViewport.Height()) - (_buffer->GetSize().Height());
for (auto i = 0; i < delta; i++)
{
_buffer->IncrementCircularBuffer();
Expand All @@ -318,7 +291,7 @@ try
// and we have to make sure we erase that text
const auto eraseStart = _mutableViewport.Height();
const auto eraseEnd = _buffer->GetLastNonSpaceCharacter(_mutableViewport).Y;
for (SHORT i = eraseStart; i <= eraseEnd; i++)
for (auto i = eraseStart; i <= eraseEnd; i++)
{
_buffer->GetRowByOffset(i).Reset(_buffer->GetCurrentAttributes());
}
Expand All @@ -337,7 +310,7 @@ try
// Move the viewport, adjust the scroll bar if needed, and restore the old cursor position
_mutableViewport = Viewport::FromExclusive(newWin);
Terminal::_NotifyScrollEvent();
SetCursorPosition(relativeCursor.X, relativeCursor.Y);
SetCursorPosition(til::point{ relativeCursor });

return true;
}
Expand Down
56 changes: 24 additions & 32 deletions src/cascadia/TerminalCore/TerminalDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@ void TerminalDispatch::PrintString(const std::wstring_view string) noexcept
_terminalApi.PrintString(string);
}

bool TerminalDispatch::CursorPosition(const size_t line,
const size_t column) noexcept
try
bool TerminalDispatch::CursorPosition(VTInt line,
VTInt column) noexcept
{
SHORT x{ 0 };
SHORT y{ 0 };

RETURN_BOOL_IF_FALSE(SUCCEEDED(SizeTToShort(column, &x)) &&
SUCCEEDED(SizeTToShort(line, &y)));
RETURN_BOOL_IF_FALSE(SUCCEEDED(IntSub(line, 1, &line)) &&
SUCCEEDED(IntSub(column, 1, &column)));

RETURN_BOOL_IF_FALSE(SUCCEEDED(ShortSub(x, 1, &x)) &&
SUCCEEDED(ShortSub(y, 1, &y)));

return _terminalApi.SetCursorPosition(x, y);
return _terminalApi.SetCursorPosition({ column, line });
}
CATCH_LOG_RETURN_FALSE()

bool TerminalDispatch::CursorVisibility(const bool isVisible) noexcept
{
Expand All @@ -61,30 +53,30 @@ bool TerminalDispatch::EnableCursorBlinking(const bool enable) noexcept
return _terminalApi.EnableCursorBlinking(enable);
}

bool TerminalDispatch::CursorForward(const size_t distance) noexcept
bool TerminalDispatch::CursorForward(const VTInt distance) noexcept
try
{
const auto cursorPos = _terminalApi.GetCursorPosition();
const COORD newCursorPos{ cursorPos.X + gsl::narrow<short>(distance), cursorPos.Y };
return _terminalApi.SetCursorPosition(newCursorPos.X, newCursorPos.Y);
const til::point newCursorPos{ cursorPos.X + distance, cursorPos.Y };
return _terminalApi.SetCursorPosition(newCursorPos);
}
CATCH_LOG_RETURN_FALSE()

bool TerminalDispatch::CursorBackward(const size_t distance) noexcept
bool TerminalDispatch::CursorBackward(const VTInt distance) noexcept
try
{
const auto cursorPos = _terminalApi.GetCursorPosition();
const COORD newCursorPos{ cursorPos.X - gsl::narrow<short>(distance), cursorPos.Y };
return _terminalApi.SetCursorPosition(newCursorPos.X, newCursorPos.Y);
const til::point newCursorPos{ cursorPos.X - distance, cursorPos.Y };
return _terminalApi.SetCursorPosition(newCursorPos);
}
CATCH_LOG_RETURN_FALSE()

bool TerminalDispatch::CursorUp(const size_t distance) noexcept
bool TerminalDispatch::CursorUp(const VTInt distance) noexcept
try
{
const auto cursorPos = _terminalApi.GetCursorPosition();
const COORD newCursorPos{ cursorPos.X, cursorPos.Y + gsl::narrow<short>(distance) };
return _terminalApi.SetCursorPosition(newCursorPos.X, newCursorPos.Y);
const til::point newCursorPos{ cursorPos.X, cursorPos.Y + distance };
return _terminalApi.SetCursorPosition(newCursorPos);
}
CATCH_LOG_RETURN_FALSE()

Expand All @@ -106,7 +98,7 @@ try
}
CATCH_LOG_RETURN_FALSE()

bool TerminalDispatch::EraseCharacters(const size_t numChars) noexcept
bool TerminalDispatch::EraseCharacters(const VTInt numChars) noexcept
try
{
return _terminalApi.EraseCharacters(numChars);
Expand All @@ -124,7 +116,7 @@ bool TerminalDispatch::CarriageReturn() noexcept
try
{
const auto cursorPos = _terminalApi.GetCursorPosition();
return _terminalApi.SetCursorPosition(0, cursorPos.Y);
return _terminalApi.SetCursorPosition({ 0, cursorPos.Y });
}
CATCH_LOG_RETURN_FALSE()

Expand All @@ -145,13 +137,13 @@ bool TerminalDispatch::HorizontalTabSet() noexcept
return true;
}

bool TerminalDispatch::ForwardTab(const size_t numTabs) noexcept
bool TerminalDispatch::ForwardTab(const VTInt numTabs) noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto cursorPosition = _terminalApi.GetCursorPosition();
auto column = cursorPosition.X;
const auto row = cursorPosition.Y;
auto tabsPerformed = 0u;
VTInt tabsPerformed = 0;
_InitTabStopsForWidth(width);
while (column + 1 < width && tabsPerformed < numTabs)
{
Expand All @@ -162,16 +154,16 @@ bool TerminalDispatch::ForwardTab(const size_t numTabs) noexcept
}
}

return _terminalApi.SetCursorPosition(column, row);
return _terminalApi.SetCursorPosition({ column, row });
}

bool TerminalDispatch::BackwardsTab(const size_t numTabs) noexcept
bool TerminalDispatch::BackwardsTab(const VTInt numTabs) noexcept
{
const auto width = _terminalApi.GetBufferSize().Dimensions().X;
const auto cursorPosition = _terminalApi.GetCursorPosition();
auto column = cursorPosition.X;
const auto row = cursorPosition.Y;
auto tabsPerformed = 0u;
VTInt tabsPerformed = 0;
_InitTabStopsForWidth(width);
while (column > 0 && tabsPerformed < numTabs)
{
Expand All @@ -182,7 +174,7 @@ bool TerminalDispatch::BackwardsTab(const size_t numTabs) noexcept
}
}

return _terminalApi.SetCursorPosition(column, row);
return _terminalApi.SetCursorPosition({ column, row });
}

bool TerminalDispatch::TabClear(const DispatchTypes::TabClearType clearType) noexcept
Expand Down Expand Up @@ -286,7 +278,7 @@ CATCH_LOG_RETURN_FALSE()
// - count, the number of characters to delete
// Return Value:
// True if handled successfully. False otherwise.
bool TerminalDispatch::DeleteCharacter(const size_t count) noexcept
bool TerminalDispatch::DeleteCharacter(const VTInt count) noexcept
try
{
return _terminalApi.DeleteCharacter(count);
Expand All @@ -299,7 +291,7 @@ CATCH_LOG_RETURN_FALSE()
// - count, the number of spaces to add
// Return Value:
// True if handled successfully, false otherwise
bool TerminalDispatch::InsertCharacter(const size_t count) noexcept
bool TerminalDispatch::InsertCharacter(const VTInt count) noexcept
try
{
return _terminalApi.InsertCharacter(count);
Expand Down
Loading

0 comments on commit 64238ab

Please sign in to comment.