| 1 |
// Copyright (c) 2011-2016 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#ifndef AGENT_SCRAPER_H |
| 22 |
#define AGENT_SCRAPER_H |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
|
| 26 |
#include <stdint.h> |
| 27 |
|
| 28 |
#include <memory> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "ConsoleLine.h" |
| 32 |
#include "Coord.h" |
| 33 |
#include "LargeConsoleRead.h" |
| 34 |
#include "SmallRect.h" |
| 35 |
#include "Terminal.h" |
| 36 |
|
| 37 |
class ConsoleScreenBufferInfo; |
| 38 |
class Win32Console; |
| 39 |
class Win32ConsoleBuffer; |
| 40 |
|
| 41 |
// We must be able to issue a single ReadConsoleOutputW call of |
| 42 |
// MAX_CONSOLE_WIDTH characters, and a single read of approximately several |
| 43 |
// hundred fewer characters than BUFFER_LINE_COUNT. |
| 44 |
const int BUFFER_LINE_COUNT = 3000; |
| 45 |
const int MAX_CONSOLE_WIDTH = 2500; |
| 46 |
const int MAX_CONSOLE_HEIGHT = 2000; |
| 47 |
const int SYNC_MARKER_LEN = 16; |
| 48 |
const int SYNC_MARKER_MARGIN = 200; |
| 49 |
|
| 50 |
class Scraper { |
| 51 |
public: |
| 52 |
Scraper( |
| 53 |
Win32Console &console, |
| 54 |
Win32ConsoleBuffer &buffer, |
| 55 |
std::unique_ptr<Terminal> terminal, |
| 56 |
Coord initialSize); |
| 57 |
~Scraper(); |
| 58 |
void resizeWindow(Win32ConsoleBuffer &buffer, |
| 59 |
Coord newSize, |
| 60 |
ConsoleScreenBufferInfo &finalInfoOut); |
| 61 |
void scrapeBuffer(Win32ConsoleBuffer &buffer, |
| 62 |
ConsoleScreenBufferInfo &finalInfoOut); |
| 63 |
Terminal &terminal() { return *m_terminal; } |
| 64 |
|
| 65 |
private: |
| 66 |
void resetConsoleTracking( |
| 67 |
Terminal::SendClearFlag sendClear, int64_t scrapedLineCount); |
| 68 |
void markEntireWindowDirty(const SmallRect &windowRect); |
| 69 |
void scanForDirtyLines(const SmallRect &windowRect); |
| 70 |
void clearBufferLines(int firstRow, int count); |
| 71 |
void resizeImpl(const ConsoleScreenBufferInfo &origInfo); |
| 72 |
void syncConsoleContentAndSize(bool forceResize, |
| 73 |
ConsoleScreenBufferInfo &finalInfoOut); |
| 74 |
WORD attributesMask(); |
| 75 |
void directScrapeOutput(const ConsoleScreenBufferInfo &info, |
| 76 |
bool consoleCursorVisible); |
| 77 |
bool scrollingScrapeOutput(const ConsoleScreenBufferInfo &info, |
| 78 |
bool consoleCursorVisible, |
| 79 |
bool tentative); |
| 80 |
void syncMarkerText(CHAR_INFO (&output)[SYNC_MARKER_LEN]); |
| 81 |
int findSyncMarker(); |
| 82 |
void createSyncMarker(int row); |
| 83 |
|
| 84 |
private: |
| 85 |
Win32Console &m_console; |
| 86 |
Win32ConsoleBuffer *m_consoleBuffer = nullptr; |
| 87 |
std::unique_ptr<Terminal> m_terminal; |
| 88 |
|
| 89 |
int m_syncRow = -1; |
| 90 |
unsigned int m_syncCounter = 0; |
| 91 |
|
| 92 |
bool m_directMode = false; |
| 93 |
Coord m_ptySize; |
| 94 |
int64_t m_scrapedLineCount = 0; |
| 95 |
int64_t m_scrolledCount = 0; |
| 96 |
int64_t m_maxBufferedLine = -1; |
| 97 |
LargeConsoleReadBuffer m_readBuffer; |
| 98 |
std::vector<ConsoleLine> m_bufferData; |
| 99 |
int m_dirtyWindowTop = -1; |
| 100 |
int m_dirtyLineCount = 0; |
| 101 |
}; |
| 102 |
|
| 103 |
#endif // AGENT_SCRAPER_H |