winpty/agent/SmallRect.h
Ryan Prichard d4640890cf Rewrite resize logic, introduce "direct mode", and tweak scroll scraping.
* Unfreeze the console while changing the buffer size.  Changing the
   buffer size hangs conhost.exe.  See:
    - https://github.com/rprichard/winpty/issues/31
    - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff

 * Detect buffer size changes and switch to a "direct mode".  Direct mode
   makes no attempt to track incremental console changes.  Instead, the
   content of the current console window is printed.  This mode is
   intended for full-screen apps that resize the console.

 * Reopen CONOUT$, which detects apps that change the active screen buffer.
   Fixes https://github.com/rprichard/winpty/issues/34.

 * In the scroll scraping (scrollingScrapeOutput), consider a line changed
   if the new content is truncated relative to the content previously
   output.  Previously, we only compared against the line-buffer up to the
   current console width.  e.g.

   If this:
      |C:\Program|

   turns into:
      |C:\Prog|
      |ram    |

   we previously left |C:\Program| in the line-buffer for the first line
   and did not re-output the first line.

   We *should* reoutput the first line at this point so that, if the line
   scrolls upward, and the terminal is later expanded, we will have
   output an "Erase in Line" CSI command to clear the obscured "ram" text.

   We need to update the line-buffer for the sake of Windows 10 combined
   with terminals like xterm and putty.  On such a terminal, if the
   terminal later widened, Windows 10 will restore the console to the
   first state.  At that point, we need to reoutput the line, because
   xterm and putty do not save and restore truncated line content extending
   past the current terminal width.
2015-09-29 02:32:49 -05:00

127 lines
3.9 KiB
C++

// Copyright (c) 2011-2012 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#ifndef SMALLRECT_H
#define SMALLRECT_H
#include <windows.h>
#include "Coord.h"
#include <algorithm>
#include <string>
struct SmallRect : SMALL_RECT
{
SmallRect()
{
Left = Right = Top = Bottom = 0;
}
SmallRect(SHORT x, SHORT y, SHORT width, SHORT height)
{
Left = x;
Top = y;
Right = x + width - 1;
Bottom = y + height - 1;
}
SmallRect(const COORD &topLeft, const COORD &size)
{
Left = topLeft.X;
Top = topLeft.Y;
Right = Left + size.X - 1;
Bottom = Top + size.Y - 1;
}
SmallRect(const SMALL_RECT &other)
{
*(SMALL_RECT*)this = other;
}
SmallRect(const SmallRect &other)
{
*(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
}
SmallRect &operator=(const SmallRect &other)
{
*(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
return *this;
}
bool contains(const SmallRect &other) const
{
return other.Left >= Left &&
other.Right <= Right &&
other.Top >= Top &&
other.Bottom <= Bottom;
}
SmallRect intersected(const SmallRect &other) const
{
int x1 = std::max(Left, other.Left);
int x2 = std::min(Right, other.Right);
int y1 = std::max(Top, other.Top);
int y2 = std::min(Bottom, other.Bottom);
return SmallRect(x1,
y1,
std::max(0, x2 - x1 + 1),
std::max(0, y2 - y1 + 1));
}
SmallRect ensureLineIncluded(SHORT line) const
{
const SHORT h = height();
if (line < Top) {
return SmallRect(Left, line, width(), h);
} else if (line > Bottom) {
return SmallRect(Left, line - h + 1, width(), h);
} else {
return *this;
}
}
SHORT top() const { return Top; }
SHORT left() const { return Left; }
SHORT width() const { return Right - Left + 1; }
SHORT height() const { return Bottom - Top + 1; }
void setTop(SHORT top) { Top = top; }
void setLeft(SHORT left) { Left = left; }
void setWidth(SHORT width) { Right = Left + width - 1; }
void setHeight(SHORT height) { Bottom = Top + height - 1; }
Coord size() const { return Coord(width(), height()); }
bool operator==(const SmallRect &other) const
{
return Left == other.Left &&
Right == other.Right &&
Top == other.Top &&
Bottom == other.Bottom;
}
bool operator!=(const SmallRect &other) const
{
return !(*this == other);
}
std::string toString() const;
};
#endif // SMALLRECT_H