| 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_WIN32_CONSOLE_BUFFER_H |
| 22 |
#define AGENT_WIN32_CONSOLE_BUFFER_H |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <memory> |
| 29 |
|
| 30 |
#include "Coord.h" |
| 31 |
#include "SmallRect.h" |
| 32 |
|
| 33 |
class ConsoleScreenBufferInfo : public CONSOLE_SCREEN_BUFFER_INFO { |
| 34 |
public: |
| 35 |
ConsoleScreenBufferInfo() |
| 36 |
{ |
| 37 |
memset(this, 0, sizeof(*this)); |
| 38 |
} |
| 39 |
|
| 40 |
Coord bufferSize() const { return dwSize; } |
| 41 |
SmallRect windowRect() const { return srWindow; } |
| 42 |
Coord cursorPosition() const { return dwCursorPosition; } |
| 43 |
}; |
| 44 |
|
| 45 |
class Win32ConsoleBuffer { |
| 46 |
private: |
| 47 |
Win32ConsoleBuffer(HANDLE conout, bool owned) : |
| 48 |
m_conout(conout), m_owned(owned) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
public: |
| 53 |
static const int kDefaultAttributes = 7; |
| 54 |
|
| 55 |
~Win32ConsoleBuffer() { |
| 56 |
if (m_owned) { |
| 57 |
CloseHandle(m_conout); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static std::unique_ptr<Win32ConsoleBuffer> openStdout(); |
| 62 |
static std::unique_ptr<Win32ConsoleBuffer> openConout(); |
| 63 |
static std::unique_ptr<Win32ConsoleBuffer> createErrorBuffer(); |
| 64 |
|
| 65 |
Win32ConsoleBuffer(const Win32ConsoleBuffer &other) = delete; |
| 66 |
Win32ConsoleBuffer &operator=(const Win32ConsoleBuffer &other) = delete; |
| 67 |
|
| 68 |
HANDLE conout(); |
| 69 |
void clearLines(int row, int count, const ConsoleScreenBufferInfo &info); |
| 70 |
void clearAllLines(const ConsoleScreenBufferInfo &info); |
| 71 |
|
| 72 |
// Buffer and window sizes. |
| 73 |
ConsoleScreenBufferInfo bufferInfo(); |
| 74 |
Coord bufferSize(); |
| 75 |
SmallRect windowRect(); |
| 76 |
void resizeBuffer(const Coord &size); |
| 77 |
bool resizeBufferRange(const Coord &initialSize, Coord &finalSize); |
| 78 |
bool resizeBufferRange(const Coord &initialSize) { |
| 79 |
Coord dummy; |
| 80 |
return resizeBufferRange(initialSize, dummy); |
| 81 |
} |
| 82 |
void moveWindow(const SmallRect &rect); |
| 83 |
|
| 84 |
// Cursor. |
| 85 |
Coord cursorPosition(); |
| 86 |
void setCursorPosition(const Coord &point); |
| 87 |
|
| 88 |
// Screen content. |
| 89 |
void read(const SmallRect &rect, CHAR_INFO *data); |
| 90 |
void write(const SmallRect &rect, const CHAR_INFO *data); |
| 91 |
|
| 92 |
void setTextAttribute(WORD attributes); |
| 93 |
|
| 94 |
private: |
| 95 |
HANDLE m_conout = nullptr; |
| 96 |
bool m_owned = false; |
| 97 |
}; |
| 98 |
|
| 99 |
#endif // AGENT_WIN32_CONSOLE_BUFFER_H |