Skip to content

Commit 04c2d6b

Browse files
j4jamesDHowett
authored andcommitted
Remove unwanted DECSTBM clipping (#2764)
The `DECSTBM` margins are meant to define the range of lines within which certain vertical scrolling operations take place. However, we were applying these margin restrictions in the `ScrollRegion` function, which is also used in a number of places that shouldn't be affected by `DECSTBM`. This includes the `ICH` and `DCH` escape sequences (which are only affected by the horizontal margins, which we don't yet support), the `ScrollConsoleScreenBuffer` API (which is public Console API, not meant to be affected by the VT terminal emulation), and the `CSI 3 J` erase scrollback extension (which isn't really scrolling as such, but uses the `ScrollRegion` function to manipulate the scrollback buffer). This commit moves the margin clipping out of the `ScrollRegion` function, so it can be applied exclusively in the places that need it. With the margin clipping removed from the `ScrollRegion` function, it now had to be applied manually in the places it was actually required. This included: * The `DoSrvPrivateReverseLineFeed` function (for the `RI` control): This was * just a matter of updating the bottom of the scroll rect to the bottom margin * (at least when the margins were actually set), since the top of the scroll * rect would always be the top of the viewport. The * `DoSrvPrivateModifyLinesImpl` function (for the `IL` and `DL` commands): * Again this was just a matter of updating the bottom of the scroll rect, since * the cursor position would always determine the top of the scroll rect. The * `AdaptDispatch::_ScrollMovement` method (for the `SU` and `SD` commands): * This required updating both the top and bottom coordinates of the scroll * rect, and also a simpler destination Y coordinate (the way the `ScrollRegion` * function worked before, the caller was expected to take the margins into * account when determining the destination). On the plus side, there was now no longer a need to override the margins when calling `ScrollRegion` in the `AdjustCursorPosition` function. In the first case, the margins had needed to be cleared (_stream.cpp 143-145), but that is now the default behaviour. In the second case, there had been a more complicated adjustment of the margins (_stream.cpp 196-209), but that code was never actually used so could be removed completely (to get to that point either _fScrollUp_ was true, so _scrollDownAtTop_ couldn't also be true, or _fScrollDown_ was true, but in that case there is a check to make sure _scrollDownAtTop_ is false). While testing, I also noticed that one of the `ScrollRegion` calls in the `AdjustCursorPosition` function was not setting the horizontal range correctly - the scrolling should always affect the full buffer width rather than just the viewport width - so I've fixed that now as well. ## Validation Steps Performed For commands like `RI`, `IL`, `DL`, etc. where we've changed the implementation but not the behaviour, there were already unit tests that could confirm that the new implementation was still producing the correct results. Where there has been a change in behaviour - namely for the `ICH` and `DCH` commands, and the `ScrollConsoleScreenBuffer` API - I've extended the existing unit tests to check that they still function correctly even when the `DECSTBM` margins are set (which would previously have caused them to fail). I've also tested manually with the test cases in issues #2543 and #2659, and confirmed that they now work as expected. Closes #2543 Closes #2659 (cherry picked from commit 0c8a4df)
1 parent 7e1e719 commit 04c2d6b

File tree

6 files changed

+56
-55
lines changed

6 files changed

+56
-55
lines changed

src/host/_stream.cpp

+3-27
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,11 @@ using Microsoft::Console::VirtualTerminal::StateMachine;
140140
const COORD newPostMarginsOrigin = { 0, moveToYPosition };
141141
const COORD newViewOrigin = { 0, newViewTop };
142142

143-
// Unset the margins to scroll the content below the margins,
144-
// then restore them after.
145-
screenInfo.SetScrollMargins(Viewport::FromInclusive({ 0 }));
146143
try
147144
{
148145
ScrollRegion(screenInfo, scrollRect, std::nullopt, newPostMarginsOrigin, UNICODE_SPACE, bufferAttributes);
149146
}
150147
CATCH_LOG();
151-
screenInfo.SetScrollMargins(relativeMargins);
152148

153149
// Move the viewport down
154150
auto hr = screenInfo.SetViewportOrigin(true, newViewOrigin, true);
@@ -186,39 +182,19 @@ using Microsoft::Console::VirtualTerminal::StateMachine;
186182
SMALL_RECT scrollRect = { 0 };
187183
scrollRect.Top = srMargins.Top;
188184
scrollRect.Bottom = srMargins.Bottom;
189-
scrollRect.Left = screenInfo.GetViewport().Left(); // NOTE: Left/Right Scroll margins don't do anything currently.
190-
scrollRect.Right = screenInfo.GetViewport().RightInclusive();
185+
scrollRect.Left = 0; // NOTE: Left/Right Scroll margins don't do anything currently.
186+
scrollRect.Right = bufferSize.X - 1; // -1, otherwise this would be an exclusive rect.
191187

192188
COORD dest;
193189
dest.X = scrollRect.Left;
194190
dest.Y = scrollRect.Top - diff;
195191

196-
SMALL_RECT clipRect = scrollRect;
197-
// Typically ScrollRegion() clips by the scroll margins. However, if
198-
// we're scrolling down at the top of the viewport, we'll need to
199-
// not clip at the margins, instead move the contents of the margins
200-
// up above the viewport. So we'll clear out the current margins, and
201-
// set them to the viewport+(#diff rows above the viewport).
202-
if (scrollDownAtTop)
203-
{
204-
clipRect.Top -= diff;
205-
auto fakeMargins = srMargins;
206-
fakeMargins.Top -= diff;
207-
auto fakeRelative = viewport.ConvertToOrigin(Viewport::FromInclusive(fakeMargins));
208-
screenInfo.SetScrollMargins(fakeRelative);
209-
}
210-
211192
try
212193
{
213-
ScrollRegion(screenInfo, scrollRect, clipRect, dest, UNICODE_SPACE, bufferAttributes);
194+
ScrollRegion(screenInfo, scrollRect, scrollRect, dest, UNICODE_SPACE, bufferAttributes);
214195
}
215196
CATCH_LOG();
216197

217-
if (scrollDownAtTop)
218-
{
219-
// Undo the fake margins we set above
220-
screenInfo.SetScrollMargins(relativeMargins);
221-
}
222198
coordCursor.Y -= diff;
223199
}
224200

src/host/getset.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,11 @@ void DoSrvPrivateAllowCursorBlinking(SCREEN_INFORMATION& screenInfo, const bool
13681368
srScroll.Right = SHORT_MAX;
13691369
srScroll.Top = viewport.Top;
13701370
srScroll.Bottom = viewport.Bottom;
1371+
// Clip to the DECSTBM margin boundary
1372+
if (screenInfo.AreMarginsSet())
1373+
{
1374+
srScroll.Bottom = screenInfo.GetAbsoluteScrollMargins().BottomInclusive();
1375+
}
13711376
// Paste coordinate for cut text above
13721377
COORD coordDestination;
13731378
coordDestination.X = 0;
@@ -2044,6 +2049,11 @@ void DoSrvPrivateModifyLinesImpl(const unsigned int count, const bool insert)
20442049
srScroll.Right = SHORT_MAX;
20452050
srScroll.Top = cursorPosition.Y;
20462051
srScroll.Bottom = screenInfo.GetViewport().BottomInclusive();
2052+
// Clip to the DECSTBM margin boundary
2053+
if (screenInfo.AreMarginsSet())
2054+
{
2055+
srScroll.Bottom = screenInfo.GetAbsoluteScrollMargins().BottomInclusive();
2056+
}
20472057
// Paste coordinate for cut text above
20482058
COORD coordDestination;
20492059
coordDestination.X = 0;

src/host/output.cpp

-25
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,6 @@ void ScrollRegion(SCREEN_INFORMATION& screenInfo,
361361
// If there was no clip rect, we'll clip to the entire buffer size.
362362
auto clip = Viewport::FromInclusive(clipRectGiven.value_or(buffer.ToInclusive()));
363363

364-
// Account for the scroll margins set by DECSTBM
365-
// DECSTBM command can sometimes apply a clipping behavior as well. Check if we have any
366-
// margins defined by DECSTBM and further restrict the clipping area here.
367-
if (screenInfo.AreMarginsSet())
368-
{
369-
const auto margin = screenInfo.GetScrollingRegion();
370-
371-
// Update the clip rectangle to only include the area that is also in the margin.
372-
clip = Viewport::Intersect(clip, margin);
373-
374-
// We'll also need to update the source rectangle, but we need to do that later.
375-
}
376-
377364
// OK, make sure that the clip rectangle also fits inside the buffer
378365
clip = Viewport::Intersect(buffer, clip);
379366

@@ -416,18 +403,6 @@ void ScrollRegion(SCREEN_INFORMATION& screenInfo,
416403
targetOrigin.Y += currentSourceOrigin.Y - originalSourceOrigin.Y;
417404
}
418405

419-
// See MSFT:20204600 - Update the source rectangle to only include the region
420-
// inside the scroll margins. We need to do this AFTER we calculate the
421-
// delta between the currentSourceOrigin and the originalSourceOrigin.
422-
// Don't combine this with the above block, because if there are margins set
423-
// and the source rectangle was clipped by the buffer, we still want to
424-
// adjust the target origin point based on the clipping of the buffer.
425-
if (screenInfo.AreMarginsSet())
426-
{
427-
const auto margin = screenInfo.GetScrollingRegion();
428-
source = Viewport::Intersect(source, margin);
429-
}
430-
431406
// And now the target viewport is the same size as the source viewport but at the different position.
432407
auto target = Viewport::FromDimensions(targetOrigin, source.Dimensions());
433408

src/host/ut_host/ApiRoutinesTests.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,12 @@ class ApiRoutinesTests
594594
TEST_METHOD(ApiScrollConsoleScreenBufferW)
595595
{
596596
BEGIN_TEST_METHOD_PROPERTIES()
597+
TEST_METHOD_PROPERTY(L"data:setMargins", L"{false, true}")
597598
TEST_METHOD_PROPERTY(L"data:checkClipped", L"{false, true}")
598599
END_TEST_METHOD_PROPERTIES();
599600

601+
bool setMargins;
602+
VERIFY_SUCCEEDED(TestData::TryGetValue(L"setMargins", setMargins), L"Get whether or not we should set the DECSTBM margins.");
600603
bool checkClipped;
601604
VERIFY_SUCCEEDED(TestData::TryGetValue(L"checkClipped", checkClipped), L"Get whether or not we should check all the options using a clipping rectangle.");
602605

@@ -605,6 +608,13 @@ class ApiRoutinesTests
605608

606609
VERIFY_SUCCEEDED(si.GetTextBuffer().ResizeTraditional({ 5, 5 }), L"Make the buffer small so this doesn't take forever.");
607610

611+
// Tests are run both with and without the DECSTBM margins set. This should not alter
612+
// the results, since ScrollConsoleScreenBuffer should not be affected by VT margins.
613+
auto& stateMachine = si.GetStateMachine();
614+
stateMachine.ProcessString(setMargins ? L"\x1b[2;4r" : L"\x1b[r");
615+
// Make sure we clear the margins on exit so they can't break other tests.
616+
auto clearMargins = wil::scope_exit([&] { stateMachine.ProcessString(L"\x1b[r"); });
617+
608618
gci.LockConsole();
609619
auto Unlock = wil::scope_exit([&] { gci.UnlockConsole(); });
610620

src/host/ut_host/ScreenBufferTests.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,13 @@ void ScreenBufferTests::ScrollOperations()
32823282

32833283
void ScreenBufferTests::InsertChars()
32843284
{
3285+
BEGIN_TEST_METHOD_PROPERTIES()
3286+
TEST_METHOD_PROPERTY(L"Data:setMargins", L"{false, true}")
3287+
END_TEST_METHOD_PROPERTIES();
3288+
3289+
bool setMargins;
3290+
VERIFY_SUCCEEDED(TestData::TryGetValue(L"setMargins", setMargins));
3291+
32853292
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
32863293
auto& si = gci.GetActiveOutputBuffer().GetActiveBuffer();
32873294
auto& stateMachine = si.GetStateMachine();
@@ -3295,6 +3302,12 @@ void ScreenBufferTests::InsertChars()
32953302
VERIFY_SUCCEEDED(si.ResizeScreenBuffer({ bufferWidth, bufferHeight }, false));
32963303
si.SetViewport(Viewport::FromExclusive({ viewportStart, 0, viewportEnd, 25 }), true);
32973304

3305+
// Tests are run both with and without the DECSTBM margins set. This should not alter
3306+
// the results, since the ICH operation is not affected by vertical margins.
3307+
stateMachine.ProcessString(setMargins ? L"\x1b[15;20r" : L"\x1b[r");
3308+
// Make sure we clear the margins on exit so they can't break other tests.
3309+
auto clearMargins = wil::scope_exit([&] { stateMachine.ProcessString(L"\x1b[r"); });
3310+
32983311
Log::Comment(
32993312
L"Test 1: Fill the line with Qs. Write some text within the viewport boundaries. "
33003313
L"Then insert 5 spaces at the cursor. Watch spaces get inserted, text slides right "
@@ -3425,6 +3438,13 @@ void ScreenBufferTests::InsertChars()
34253438

34263439
void ScreenBufferTests::DeleteChars()
34273440
{
3441+
BEGIN_TEST_METHOD_PROPERTIES()
3442+
TEST_METHOD_PROPERTY(L"Data:setMargins", L"{false, true}")
3443+
END_TEST_METHOD_PROPERTIES();
3444+
3445+
bool setMargins;
3446+
VERIFY_SUCCEEDED(TestData::TryGetValue(L"setMargins", setMargins));
3447+
34283448
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
34293449
auto& si = gci.GetActiveOutputBuffer().GetActiveBuffer();
34303450
auto& stateMachine = si.GetStateMachine();
@@ -3438,6 +3458,12 @@ void ScreenBufferTests::DeleteChars()
34383458
VERIFY_SUCCEEDED(si.ResizeScreenBuffer({ bufferWidth, bufferHeight }, false));
34393459
si.SetViewport(Viewport::FromExclusive({ viewportStart, 0, viewportEnd, 25 }), true);
34403460

3461+
// Tests are run both with and without the DECSTBM margins set. This should not alter
3462+
// the results, since the DCH operation is not affected by vertical margins.
3463+
stateMachine.ProcessString(setMargins ? L"\x1b[15;20r" : L"\x1b[r");
3464+
// Make sure we clear the margins on exit so they can't break other tests.
3465+
auto clearMargins = wil::scope_exit([&] { stateMachine.ProcessString(L"\x1b[r"); });
3466+
34413467
Log::Comment(
34423468
L"Test 1: Fill the line with Qs. Write some text within the viewport boundaries. "
34433469
L"Then delete 5 characters at the cursor. Watch the rest of the line slide left, "

src/terminal/adapter/adaptDispatch.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -963,13 +963,17 @@ bool AdaptDispatch::_ScrollMovement(const ScrollDirection sdDirection, _In_ unsi
963963
srScreen.Right = SHORT_MAX;
964964
srScreen.Top = csbiex.srWindow.Top;
965965
srScreen.Bottom = csbiex.srWindow.Bottom - 1; // srWindow is exclusive, hence the - 1
966+
// Clip to the DECSTBM margin boundaries
967+
if (_srScrollMargins.Top < _srScrollMargins.Bottom)
968+
{
969+
srScreen.Top = csbiex.srWindow.Top + _srScrollMargins.Top;
970+
srScreen.Bottom = csbiex.srWindow.Top + _srScrollMargins.Bottom;
971+
}
966972

967973
// Paste coordinate for cut text above
968974
COORD coordDestination;
969975
coordDestination.X = srScreen.Left;
970-
// Scroll starting from the top of the scroll margins.
971-
coordDestination.Y = (_srScrollMargins.Top + srScreen.Top) + sDistance * (sdDirection == ScrollDirection::Up ? -1 : 1);
972-
// We don't need to worry about clipping the margins at all, ScrollRegion inside conhost will do that correctly for us
976+
coordDestination.Y = srScreen.Top + sDistance * (sdDirection == ScrollDirection::Up ? -1 : 1);
973977

974978
// Fill character for remaining space left behind by "cut" operation (or for fill if we "cut" the entire line)
975979
CHAR_INFO ciFill;

0 commit comments

Comments
 (0)