-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathstate.cpp
457 lines (416 loc) · 15.7 KB
/
state.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "vtrenderer.hpp"
#include "../../inc/conattrs.hpp"
#include "../../types/inc/convert.hpp"
// For _vcprintf
#include <conio.h>
#include <cstdarg>
#pragma hdrstop
using namespace Microsoft::Console;
using namespace Microsoft::Console::Render;
using namespace Microsoft::Console::Types;
const COORD VtEngine::INVALID_COORDS = { -1, -1 };
// Routine Description:
// - Creates a new VT-based rendering engine
// - NOTE: Will throw if initialization failure. Caller must catch.
// Arguments:
// - <none>
// Return Value:
// - An instance of a Renderer.
VtEngine::VtEngine(_In_ wil::unique_hfile pipe,
const Viewport initialViewport) :
RenderEngineBase(),
_hFile(std::move(pipe)),
_lastTextAttributes(INVALID_COLOR, INVALID_COLOR),
_lastViewport(initialViewport),
_pool(til::pmr::get_default_resource()),
_invalidMap(initialViewport.Dimensions(), false, &_pool),
_lastText({ 0 }),
_scrollDelta({ 0, 0 }),
_quickReturn(false),
_clearedAllThisFrame(false),
_cursorMoved(false),
_resized(false),
_suppressResizeRepaint(true),
_virtualTop(0),
_circled(false),
_firstPaint(true),
_skipCursor(false),
_pipeBroken(false),
_exitResult{ S_OK },
_terminalOwner{ nullptr },
_newBottomLine{ false },
_deferredCursorPos{ INVALID_COORDS },
_inResizeRequest{ false },
_trace{},
_bufferLine{},
_buffer{},
_formatBuffer{},
_conversionBuffer{}
{
#ifndef UNIT_TESTING
// When unit testing, we can instantiate a VtEngine without a pipe.
THROW_HR_IF(E_HANDLE, _hFile.get() == INVALID_HANDLE_VALUE);
#else
// member is only defined when UNIT_TESTING is.
_usingTestCallback = false;
#endif
}
// Method Description:
// - Writes the characters to our file handle. If we're building the unit tests,
// we can instead write to the test callback, in order to avoid needing to
// set up pipes and threads for unit tests.
// Arguments:
// - str: The buffer to write to the pipe. Might have nulls in it.
// Return Value:
// - S_OK or suitable HRESULT error from writing pipe.
[[nodiscard]] HRESULT VtEngine::_Write(std::string_view const str) noexcept
{
_trace.TraceString(str);
#ifdef UNIT_TESTING
if (_usingTestCallback)
{
// Try to get the last error. If that wasn't set, then the test probably
// doesn't set last error. No matter. We'll just return with E_FAIL
// then. This is a unit test, we don't particularly care.
const auto succeeded = _pfnTestCallback(str.data(), str.size());
auto hr = E_FAIL;
if (!succeeded)
{
const auto err = ::GetLastError();
// If there wasn't an error in GLE, just use E_FAIL
hr = SUCCEEDED_WIN32(err) ? hr : HRESULT_FROM_WIN32(err);
}
return succeeded ? S_OK : hr;
}
#endif
try
{
_buffer.append(str);
return S_OK;
}
CATCH_RETURN();
}
[[nodiscard]] HRESULT VtEngine::_Flush() noexcept
{
#ifdef UNIT_TESTING
if (_hFile.get() == INVALID_HANDLE_VALUE)
{
// Do not flush during Unit Testing because we won't have a valid file.
return S_OK;
}
#endif
if (!_pipeBroken)
{
bool fSuccess = !!WriteFile(_hFile.get(), _buffer.data(), gsl::narrow_cast<DWORD>(_buffer.size()), nullptr, nullptr);
_buffer.clear();
if (!fSuccess)
{
_exitResult = HRESULT_FROM_WIN32(GetLastError());
_pipeBroken = true;
if (_terminalOwner)
{
_terminalOwner->CloseOutput();
}
return _exitResult;
}
}
return S_OK;
}
// Method Description:
// - Wrapper for ITerminalOutputConnection. See _Write.
[[nodiscard]] HRESULT VtEngine::WriteTerminalUtf8(const std::string_view str) noexcept
{
return _Write(str);
}
// Method Description:
// - Writes a wstring to the tty, encoded as full utf-8. This is one
// implementation of the WriteTerminalW method.
// Arguments:
// - wstr - wstring of text to be written
// Return Value:
// - S_OK or suitable HRESULT error from either conversion or writing pipe.
[[nodiscard]] HRESULT VtEngine::_WriteTerminalUtf8(const std::wstring_view wstr) noexcept
{
RETURN_IF_FAILED(til::u16u8(wstr, _conversionBuffer));
return _Write(_conversionBuffer);
}
// Method Description:
// - Writes a wstring to the tty, encoded as "utf-8" where characters that are
// outside the ASCII range are encoded as '?'
// This mainly exists to maintain compatibility with the inbox telnet client.
// This is one implementation of the WriteTerminalW method.
// Arguments:
// - wstr - wstring of text to be written
// Return Value:
// - S_OK or suitable HRESULT error from writing pipe.
[[nodiscard]] HRESULT VtEngine::_WriteTerminalAscii(const std::wstring_view wstr) noexcept
{
std::string needed;
needed.reserve(wstr.size());
for (const auto& wch : wstr)
{
// We're explicitly replacing characters outside ASCII with a ? because
// that's what telnet wants.
needed.push_back((wch > L'\x7f') ? '?' : static_cast<char>(wch));
}
return _Write(needed);
}
// Method Description:
// - This method will update the active font on the current device context
// Does nothing for vt, the font is handed by the terminal.
// Arguments:
// - FontDesired - reference to font information we should use while instantiating a font.
// - Font - reference to font information where the chosen font information will be populated.
// Return Value:
// - HRESULT S_OK
[[nodiscard]] HRESULT VtEngine::UpdateFont(const FontInfoDesired& /*pfiFontDesired*/,
_Out_ FontInfo& /*pfiFont*/) noexcept
{
return S_OK;
}
// Method Description:
// - This method will modify the DPI we're using for scaling calculations.
// Does nothing for vt, the dpi is handed by the terminal.
// Arguments:
// - iDpi - The Dots Per Inch to use for scaling. We will use this relative to
// the system default DPI defined in Windows headers as a constant.
// Return Value:
// - HRESULT S_OK
[[nodiscard]] HRESULT VtEngine::UpdateDpi(const int /*iDpi*/) noexcept
{
return S_OK;
}
// Method Description:
// - This method will update our internal reference for how big the viewport is.
// If the viewport has changed size, then we'll need to send an update to
// the terminal.
// Arguments:
// - srNewViewport - The bounds of the new viewport.
// Return Value:
// - HRESULT S_OK
[[nodiscard]] HRESULT VtEngine::UpdateViewport(const SMALL_RECT srNewViewport) noexcept
{
HRESULT hr = S_OK;
const Viewport oldView = _lastViewport;
const Viewport newView = Viewport::FromInclusive(srNewViewport);
_lastViewport = newView;
if ((oldView.Height() != newView.Height()) || (oldView.Width() != newView.Width()))
{
// Don't emit a resize event if we've requested it be suppressed
if (!_suppressResizeRepaint)
{
hr = _ResizeWindow(newView.Width(), newView.Height());
}
_resized = true;
}
// See MSFT:19408543
// Always clear the suppression request, even if the new size was the same
// as the last size. We're always going to get a UpdateViewport call
// for our first frame. However, we start with _suppressResizeRepaint set,
// to prevent that first UpdateViewport call from emitting our size.
// If we only clear the flag when the new viewport is different, this can
// lead to the first _actual_ resize being suppressed.
_suppressResizeRepaint = false;
if (_resizeQuirk)
{
// GH#3490 - When the viewport width changed, don't do anything extra here.
// If the buffer had areas that were invalid due to the resize, then the
// buffer will have triggered it's own invalidations for what it knows is
// invalid. Previously, we'd invalidate everything if the width changed,
// because we couldn't be sure if lines were reflowed.
_invalidMap.resize(newView.Dimensions());
}
else
{
if (SUCCEEDED(hr))
{
_invalidMap.resize(newView.Dimensions(), true); // resize while filling in new space with repaint requests.
// Viewport is smaller now - just update it all.
if (oldView.Height() > newView.Height() || oldView.Width() > newView.Width())
{
hr = InvalidateAll();
}
}
}
return hr;
}
// Method Description:
// - This method will figure out what the new font should be given the starting font information and a DPI.
// - When the final font is determined, the FontInfo structure given will be updated with the actual resulting font chosen as the nearest match.
// - NOTE: It is left up to the underling rendering system to choose the nearest font. Please ask for the font dimensions if they are required using the interface. Do not use the size you requested with this structure.
// - If the intent is to immediately turn around and use this font, pass the optional handle parameter and use it immediately.
// Does nothing for vt, the font is handed by the terminal.
// Arguments:
// - FontDesired - reference to font information we should use while instantiating a font.
// - Font - reference to font information where the chosen font information will be populated.
// - iDpi - The DPI we will have when rendering
// Return Value:
// - S_FALSE: This is unsupported by the VT Renderer and should use another engine's value.
[[nodiscard]] HRESULT VtEngine::GetProposedFont(const FontInfoDesired& /*pfiFontDesired*/,
_Out_ FontInfo& /*pfiFont*/,
const int /*iDpi*/) noexcept
{
return S_FALSE;
}
// Method Description:
// - Retrieves the current pixel size of the font we have selected for drawing.
// Arguments:
// - pFontSize - receives the current X by Y size of the font.
// Return Value:
// - S_FALSE: This is unsupported by the VT Renderer and should use another engine's value.
[[nodiscard]] HRESULT VtEngine::GetFontSize(_Out_ COORD* const pFontSize) noexcept
{
*pFontSize = COORD({ 1, 1 });
return S_FALSE;
}
// Method Description:
// - Sets the test callback for this instance. Instead of rendering to a pipe,
// this instance will instead render to a callback for testing.
// Arguments:
// - pfn: a callback to call instead of writing to the pipe.
// Return Value:
// - <none>
void VtEngine::SetTestCallback(_In_ std::function<bool(const char* const, size_t const)> pfn)
{
#ifdef UNIT_TESTING
_pfnTestCallback = pfn;
_usingTestCallback = true;
#else
THROW_HR(E_FAIL);
#endif
}
// Method Description:
// - Returns true if the entire viewport has been invalidated. That signals we
// should use a VT Clear Screen sequence as an optimization.
// Arguments:
// - <none>
// Return Value:
// - true if the entire viewport has been invalidated
bool VtEngine::_AllIsInvalid() const
{
return _invalidMap.all();
}
// Method Description:
// - Prevent the renderer from emitting output on the next resize. This prevents
// the host from echoing a resize to the terminal that requested it.
// Arguments:
// - <none>
// Return Value:
// - S_OK
[[nodiscard]] HRESULT VtEngine::SuppressResizeRepaint() noexcept
{
_suppressResizeRepaint = true;
return S_OK;
}
// Method Description:
// - "Inherit" the cursor at the given position. We won't need to move it
// anywhere, so update where we last thought the cursor was.
// Also update our "virtual top", indicating where should clip all updates to
// (we don't want to paint the empty region above the inherited cursor).
// Also ignore the next InvalidateCursor call.
// Arguments:
// - coordCursor: The cursor position to inherit from.
// Return Value:
// - S_OK
[[nodiscard]] HRESULT VtEngine::InheritCursor(const COORD coordCursor) noexcept
{
_virtualTop = coordCursor.Y;
_lastText = coordCursor;
_skipCursor = true;
// Prevent us from clearing the entire viewport on the first paint
_firstPaint = false;
return S_OK;
}
void VtEngine::SetTerminalOwner(Microsoft::Console::ITerminalOwner* const terminalOwner)
{
_terminalOwner = terminalOwner;
}
// Method Description:
// - sends a sequence to request the end terminal to tell us the
// cursor position. The terminal will reply back on the vt input handle.
// Flushes the buffer as well, to make sure the request is sent to the terminal.
// Arguments:
// - <none>
// Return Value:
// - S_OK if we succeeded, else an appropriate HRESULT for failing to allocate or write.
HRESULT VtEngine::RequestCursor() noexcept
{
RETURN_IF_FAILED(_RequestCursor());
RETURN_IF_FAILED(_Flush());
return S_OK;
}
// Method Description:
// - Tell the vt renderer to begin a resize operation. During a resize
// operation, the vt renderer should _not_ request to be repainted during a
// text buffer circling event. Any callers of this method should make sure to
// call EndResize to make sure the renderer returns to normal behavior.
// See GH#1795 for context on this method.
// Arguments:
// - <none>
// Return Value:
// - <none>
void VtEngine::BeginResizeRequest()
{
_inResizeRequest = true;
}
// Method Description:
// - Tell the vt renderer to end a resize operation.
// See BeginResize for more details.
// See GH#1795 for context on this method.
// Arguments:
// - <none>
// Return Value:
// - <none>
void VtEngine::EndResizeRequest()
{
_inResizeRequest = false;
}
// Method Description:
// - Configure the renderer for the resize quirk. This changes the behavior of
// conpty to _not_ InvalidateAll the entire viewport on a resize operation.
// This is used by the Windows Terminal, because it is prepared to be
// connected to a conpty, and handles it's own buffer specifically for a
// conpty scenario.
// - See also: GH#3490, #4354, #4741
// Arguments:
// - <none>
// Return Value:
// - true iff we were started with the `--resizeQuirk` flag enabled.
void VtEngine::SetResizeQuirk(const bool resizeQuirk)
{
_resizeQuirk = resizeQuirk;
}
// Method Description:
// - Manually emit a "Erase Scrollback" sequence to the connected terminal. We
// need to do this in certain cases that we've identified where we believe the
// client wanted the entire terminal buffer cleared, not just the viewport.
// For more information, see GH#3126.
// - This is unimplemented in the win-telnet, xterm-ascii renderers - inbox
// telnet.exe doesn't know how to handle a ^[[3J. This _is_ implemented in the
// Xterm256Engine.
// Arguments:
// - <none>
// Return Value:
// - S_OK if we wrote the sequences successfully, otherwise an appropriate HRESULT
[[nodiscard]] HRESULT VtEngine::ManuallyClearScrollback() noexcept
{
return S_OK;
}
// Method Description:
// - Send a sequence to the connected terminal to request win32-input-mode from
// them. This will enable the connected terminal to send us full INPUT_RECORDs
// as input. If the terminal doesn't understand this sequence, it'll just
// ignore it.
// Arguments:
// - <none>
// Return Value:
// - S_OK if we succeeded, else an appropriate HRESULT for failing to allocate or write.
HRESULT VtEngine::RequestWin32Input() noexcept
{
RETURN_IF_FAILED(_RequestWin32Input());
RETURN_IF_FAILED(_Flush());
return S_OK;
}