From 5b99238fca19e3996e40dc85b3ed082870e118c8 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Sat, 22 Apr 2017 23:41:22 -0700 Subject: [PATCH] Don't show the cursor if it is outside the console window Fixes https://github.com/rprichard/winpty/issues/113 --- src/agent/Scraper.cc | 28 ++++++++++++++++------------ src/agent/Scraper.h | 4 ++-- src/agent/SmallRect.h | 8 ++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/agent/Scraper.cc b/src/agent/Scraper.cc index 542d833..21f9c67 100755 --- a/src/agent/Scraper.cc +++ b/src/agent/Scraper.cc @@ -432,7 +432,7 @@ WORD Scraper::attributesMask() } void Scraper::directScrapeOutput(const ConsoleScreenBufferInfo &info, - bool cursorVisible) + bool consoleCursorVisible) { const SmallRect windowRect = info.windowRect(); @@ -446,11 +446,12 @@ void Scraper::directScrapeOutput(const ConsoleScreenBufferInfo &info, const int h = scrapeRect.height(); const Coord cursor = info.cursorPosition(); - const int cursorColumn = !cursorVisible ? -1 : - constrained(0, cursor.X - scrapeRect.Left, w - 1); - const int cursorLine = !cursorVisible ? -1 : - constrained(0, cursor.Y - scrapeRect.Top, h - 1); - if (!cursorVisible) { + const bool showTerminalCursor = + consoleCursorVisible && scrapeRect.contains(cursor); + const int cursorColumn = !showTerminalCursor ? -1 : cursor.X - scrapeRect.Left; + const int cursorLine = !showTerminalCursor ? -1 : cursor.Y - scrapeRect.Top; + + if (!showTerminalCursor) { m_terminal->hideTerminalCursor(); } @@ -467,13 +468,13 @@ void Scraper::directScrapeOutput(const ConsoleScreenBufferInfo &info, } } - if (cursorVisible) { + if (showTerminalCursor) { m_terminal->showTerminalCursor(cursorColumn, cursorLine); } } bool Scraper::scrollingScrapeOutput(const ConsoleScreenBufferInfo &info, - bool cursorVisible, + bool consoleCursorVisible, bool tentative) { const Coord cursor = info.cursorPosition(); @@ -605,9 +606,12 @@ bool Scraper::scrollingScrapeOutput(const ConsoleScreenBufferInfo &info, std::min(m_dirtyLineCount, windowRect.top() + windowRect.height()) + m_scrolledCount; - const int64_t cursorLine = !cursorVisible ? -1 : cursor.Y + m_scrolledCount; - const int cursorColumn = !cursorVisible ? -1 : cursor.X; - if (!cursorVisible) { + const bool showTerminalCursor = + consoleCursorVisible && windowRect.contains(cursor); + const int64_t cursorLine = !showTerminalCursor ? -1 : cursor.Y + m_scrolledCount; + const int cursorColumn = !showTerminalCursor ? -1 : cursor.X; + + if (!showTerminalCursor) { m_terminal->hideTerminalCursor(); } @@ -636,7 +640,7 @@ bool Scraper::scrollingScrapeOutput(const ConsoleScreenBufferInfo &info, m_scrapedLineCount = windowRect.top() + m_scrolledCount; - if (cursorVisible) { + if (showTerminalCursor) { m_terminal->showTerminalCursor(cursorColumn, cursorLine); } diff --git a/src/agent/Scraper.h b/src/agent/Scraper.h index 48314a6..9c10d80 100755 --- a/src/agent/Scraper.h +++ b/src/agent/Scraper.h @@ -73,9 +73,9 @@ private: ConsoleScreenBufferInfo &finalInfoOut); WORD attributesMask(); void directScrapeOutput(const ConsoleScreenBufferInfo &info, - bool cursorVisible); + bool consoleCursorVisible); bool scrollingScrapeOutput(const ConsoleScreenBufferInfo &info, - bool cursorVisible, + bool consoleCursorVisible, bool tentative); void syncMarkerText(CHAR_INFO (&output)[SYNC_MARKER_LEN]); int findSyncMarker(); diff --git a/src/agent/SmallRect.h b/src/agent/SmallRect.h index c159346..bad0b88 100644 --- a/src/agent/SmallRect.h +++ b/src/agent/SmallRect.h @@ -76,6 +76,14 @@ struct SmallRect : SMALL_RECT other.Bottom <= Bottom; } + bool contains(const Coord &other) const + { + return other.X >= Left && + other.X <= Right && + other.Y >= Top && + other.Y <= Bottom; + } + SmallRect intersected(const SmallRect &other) const { int x1 = std::max(Left, other.Left);