/
OutputStateMachineEngine.hpp
233 lines (200 loc) · 9.07 KB
/
OutputStateMachineEngine.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/*
Module Name:
- OutputStateMachineEngine.hpp
Abstract:
- This is the implementation of the client VT output state machine engine.
*/
#pragma once
#include <functional>
#include "../adapter/termDispatch.hpp"
#include "telemetry.hpp"
#include "IStateMachineEngine.hpp"
namespace Microsoft::Console::Render
{
class VtEngine;
}
namespace Microsoft::Console::VirtualTerminal
{
class OutputStateMachineEngine : public IStateMachineEngine
{
public:
static constexpr size_t MAX_URL_LENGTH = 2 * 1048576; // 2MB, like iTerm2
OutputStateMachineEngine(std::unique_ptr<ITermDispatch> pDispatch);
bool ActionExecute(const wchar_t wch) override;
bool ActionExecuteFromEscape(const wchar_t wch) override;
bool ActionPrint(const wchar_t wch) override;
bool ActionPrintString(const std::wstring_view string) override;
bool ActionPassThroughString(const std::wstring_view string) override;
bool ActionEscDispatch(const VTID id) override;
bool ActionVt52EscDispatch(const VTID id, const VTParameters parameters) override;
bool ActionCsiDispatch(const VTID id, const VTParameters parameters) override;
StringHandler ActionDcsDispatch(const VTID id, const VTParameters parameters) override;
bool ActionClear() noexcept override;
bool ActionIgnore() noexcept override;
bool ActionOscDispatch(const wchar_t wch,
const size_t parameter,
const std::wstring_view string) override;
bool ActionSs3Dispatch(const wchar_t wch, const VTParameters parameters) noexcept override;
void SetTerminalConnection(Microsoft::Console::Render::VtEngine* const pTtyConnection,
std::function<bool()> pfnFlushToTerminal);
const ITermDispatch& Dispatch() const noexcept;
ITermDispatch& Dispatch() noexcept;
private:
std::unique_ptr<ITermDispatch> _dispatch;
Microsoft::Console::Render::VtEngine* _pTtyConnection;
std::function<bool()> _pfnFlushToTerminal;
wchar_t _lastPrintedChar;
enum EscActionCodes : uint64_t
{
DECSC_CursorSave = VTID("7"),
DECRC_CursorRestore = VTID("8"),
DECKPAM_KeypadApplicationMode = VTID("="),
DECKPNM_KeypadNumericMode = VTID(">"),
IND_Index = VTID("D"),
NEL_NextLine = VTID("E"),
HTS_HorizontalTabSet = VTID("H"),
RI_ReverseLineFeed = VTID("M"),
SS2_SingleShift = VTID("N"),
SS3_SingleShift = VTID("O"),
DECID_IdentifyDevice = VTID("Z"),
ST_StringTerminator = VTID("\\"),
RIS_ResetToInitialState = VTID("c"),
LS2_LockingShift = VTID("n"),
LS3_LockingShift = VTID("o"),
LS1R_LockingShift = VTID("~"),
LS2R_LockingShift = VTID("}"),
LS3R_LockingShift = VTID("|"),
DECAC1_AcceptC1Controls = VTID(" 7"),
DECDHL_DoubleHeightLineTop = VTID("#3"),
DECDHL_DoubleHeightLineBottom = VTID("#4"),
DECSWL_SingleWidthLine = VTID("#5"),
DECDWL_DoubleWidthLine = VTID("#6"),
DECALN_ScreenAlignmentPattern = VTID("#8")
};
enum CsiActionCodes : uint64_t
{
ICH_InsertCharacter = VTID("@"),
CUU_CursorUp = VTID("A"),
CUD_CursorDown = VTID("B"),
CUF_CursorForward = VTID("C"),
CUB_CursorBackward = VTID("D"),
CNL_CursorNextLine = VTID("E"),
CPL_CursorPrevLine = VTID("F"),
CHA_CursorHorizontalAbsolute = VTID("G"),
CUP_CursorPosition = VTID("H"),
CHT_CursorForwardTab = VTID("I"),
ED_EraseDisplay = VTID("J"),
DECSED_SelectiveEraseDisplay = VTID("?J"),
EL_EraseLine = VTID("K"),
DECSEL_SelectiveEraseLine = VTID("?K"),
IL_InsertLine = VTID("L"),
DL_DeleteLine = VTID("M"),
DCH_DeleteCharacter = VTID("P"),
SU_ScrollUp = VTID("S"),
SD_ScrollDown = VTID("T"),
ECH_EraseCharacters = VTID("X"),
CBT_CursorBackTab = VTID("Z"),
HPA_HorizontalPositionAbsolute = VTID("`"),
HPR_HorizontalPositionRelative = VTID("a"),
REP_RepeatCharacter = VTID("b"),
DA_DeviceAttributes = VTID("c"),
DA2_SecondaryDeviceAttributes = VTID(">c"),
DA3_TertiaryDeviceAttributes = VTID("=c"),
VPA_VerticalLinePositionAbsolute = VTID("d"),
VPR_VerticalPositionRelative = VTID("e"),
HVP_HorizontalVerticalPosition = VTID("f"),
TBC_TabClear = VTID("g"),
DECSET_PrivateModeSet = VTID("?h"),
DECRST_PrivateModeReset = VTID("?l"),
SGR_SetGraphicsRendition = VTID("m"),
DSR_DeviceStatusReport = VTID("n"),
DSR_PrivateDeviceStatusReport = VTID("?n"),
DECSTBM_SetScrollingRegion = VTID("r"),
ANSISYSSC_CursorSave = VTID("s"), // NOTE: Overlaps with DECLRMM/DECSLRM. Fix when/if implemented.
DTTERM_WindowManipulation = VTID("t"), // NOTE: Overlaps with DECSLPP. Fix when/if implemented.
ANSISYSRC_CursorRestore = VTID("u"),
DECREQTPARM_RequestTerminalParameters = VTID("x"),
DECSCUSR_SetCursorStyle = VTID(" q"),
DECSTR_SoftReset = VTID("!p"),
DECSCA_SetCharacterProtectionAttribute = VTID("\"q"),
XT_PushSgrAlias = VTID("#p"),
XT_PopSgrAlias = VTID("#q"),
XT_PushSgr = VTID("#{"),
XT_PopSgr = VTID("#}"),
DECRQM_RequestMode = VTID("$p"),
DECRQM_PrivateRequestMode = VTID("?$p"),
DECCARA_ChangeAttributesRectangularArea = VTID("$r"),
DECRARA_ReverseAttributesRectangularArea = VTID("$t"),
DECCRA_CopyRectangularArea = VTID("$v"),
DECFRA_FillRectangularArea = VTID("$x"),
DECERA_EraseRectangularArea = VTID("$z"),
DECSERA_SelectiveEraseRectangularArea = VTID("${"),
DECSCPP_SetColumnsPerPage = VTID("$|"),
DECSACE_SelectAttributeChangeExtent = VTID("*x"),
DECINVM_InvokeMacro = VTID("*z"),
DECAC_AssignColor = VTID(",|"),
DECPS_PlaySound = VTID(",~")
};
enum DcsActionCodes : uint64_t
{
DECDLD_DownloadDRCS = VTID("{"),
DECDMAC_DefineMacro = VTID("!z"),
DECRSTS_RestoreTerminalState = VTID("$p"),
DECRQSS_RequestSetting = VTID("$q")
};
enum Vt52ActionCodes : uint64_t
{
CursorUp = VTID("A"),
CursorDown = VTID("B"),
CursorRight = VTID("C"),
CursorLeft = VTID("D"),
EnterGraphicsMode = VTID("F"),
ExitGraphicsMode = VTID("G"),
CursorToHome = VTID("H"),
ReverseLineFeed = VTID("I"),
EraseToEndOfScreen = VTID("J"),
EraseToEndOfLine = VTID("K"),
DirectCursorAddress = VTID("Y"),
Identify = VTID("Z"),
EnterAlternateKeypadMode = VTID("="),
ExitAlternateKeypadMode = VTID(">"),
ExitVt52Mode = VTID("<")
};
enum OscActionCodes : unsigned int
{
SetIconAndWindowTitle = 0,
SetWindowIcon = 1,
SetWindowTitle = 2,
SetWindowProperty = 3, // Not implemented
SetColor = 4,
Hyperlink = 8,
ConEmuAction = 9,
SetForegroundColor = 10,
SetBackgroundColor = 11,
SetCursorColor = 12,
SetClipboard = 52,
ResetForegroundColor = 110, // Not implemented
ResetBackgroundColor = 111, // Not implemented
ResetCursorColor = 112,
FinalTermAction = 133,
ITerm2Action = 1337,
};
bool _GetOscTitle(const std::wstring_view string,
std::wstring& title) const;
bool _GetOscSetColorTable(const std::wstring_view string,
std::vector<size_t>& tableIndexes,
std::vector<DWORD>& rgbs) const;
bool _GetOscSetColor(const std::wstring_view string,
std::vector<DWORD>& rgbs) const;
bool _GetOscSetClipboard(const std::wstring_view string,
std::wstring& content,
bool& queryClipboard) const noexcept;
static constexpr std::wstring_view hyperlinkIDParameter{ L"id=" };
bool _ParseHyperlink(const std::wstring_view string,
std::wstring& params,
std::wstring& uri) const;
void _ClearLastChar() noexcept;
};
}