forked from microsoft/terminal
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstate.cpp
More file actions
554 lines (502 loc) · 19 KB
/
state.cpp
File metadata and controls
554 lines (502 loc) · 19 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "vtrenderer.hpp"
#include "../../inc/conattrs.hpp"
#include "../../host/VtIo.hpp"
// For _vcprintf
#include <conio.h>
#include <cstdarg>
#pragma hdrstop
using namespace Microsoft::Console;
using namespace Microsoft::Console::Render;
using namespace Microsoft::Console::Types;
constexpr til::point 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)),
_usingLineRenditions(false),
_stopUsingLineRenditions(false),
_usingSoftFont(false),
_lastTextAttributes(INVALID_COLOR, INVALID_COLOR, INVALID_COLOR),
_lastViewport(initialViewport),
_pool(til::pmr::get_default_resource()),
_invalidMap(initialViewport.Dimensions(), false, &_pool),
_scrollDelta(0, 0),
_clearedAllThisFrame(false),
_cursorMoved(false),
_resized(false),
_suppressResizeRepaint(true),
_virtualTop(0),
_circled(false),
_firstPaint(true),
_skipCursor(false),
_terminalOwner{ nullptr },
_newBottomLine{ false },
_deferredCursorPos{ INVALID_COORDS },
_trace{},
_bufferLine{},
_buffer{},
_formatBuffer{},
_conversionBuffer{},
_pfnSetLookingForDSR{}
{
#ifndef UNIT_TESTING
// When unit testing, we can instantiate a VtEngine without a pipe.
THROW_HR_IF(E_HANDLE, !_hFile);
#else
// member is only defined when UNIT_TESTING is.
_usingTestCallback = false;
#endif
}
// Method Description:
// - Writes a fill of characters to our file handle (repeat of same character over and over)
[[nodiscard]] HRESULT VtEngine::_WriteFill(const size_t n, const char c) noexcept
try
{
_trace.TraceStringFill(n, c);
#ifdef UNIT_TESTING
if (_usingTestCallback)
{
const std::string str(n, c);
// 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
// TODO GH10001: Replace me with REP
_buffer.append(n, c);
return S_OK;
}
CATCH_RETURN();
// 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();
}
void VtEngine::_Flush() noexcept
{
if (_buffer.empty())
{
return;
}
if (!_corked)
{
_flushImpl();
return;
}
// Defer the flush until someone calls Cork(false).
_flushRequested = true;
}
// _corked is often true and separating _flushImpl() out allows _flush() to be inlined.
void VtEngine::_flushImpl() noexcept
{
if (_hFile)
{
const auto fSuccess = WriteFile(_hFile.get(), _buffer.data(), gsl::narrow_cast<DWORD>(_buffer.size()), nullptr, nullptr);
_buffer.clear();
_startOfFrameBufferIndex = 0;
if (!fSuccess)
{
LOG_LAST_ERROR();
_hFile.reset();
if (_terminalOwner)
{
_terminalOwner->CloseOutput();
}
}
}
}
// The name of this method is an analogy to TCP_CORK. It instructs
// the VT renderer to stop flushing its buffer to the output pipe.
// Don't forget to uncork it!
void VtEngine::Cork(bool corked) noexcept
{
_corked = corked;
// Now do the deferred flush from a previous call to _Flush().
if (!corked && _flushRequested)
{
_flushRequested = false;
_flushImpl();
}
}
// Method Description:
// - Wrapper for _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:
// - Writes a wstring to the tty when the characters are from the DRCS soft font.
// It is assumed that the character set has already been designated in the
// client terminal, so we just need to re-map our internal representation
// of the characters into ASCII.
// Arguments:
// - wstr - wstring of text to be written
// Return Value:
// - S_OK or suitable HRESULT error from writing pipe.
[[nodiscard]] HRESULT VtEngine::_WriteTerminalDrcs(const std::wstring_view wstr) noexcept
{
std::string needed;
needed.reserve(wstr.size());
for (const auto& wch : wstr)
{
// Our DRCS characters use the range U+EF20 to U+EF7F from the Unicode
// Private Use Area. To map them back to ASCII we just mask with 7F.
needed.push_back(wch & 0x7F);
}
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.
// Arguments:
// Does nothing for vt, the dpi is handed by the terminal.
// - 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 til::inclusive_rect& srNewViewport) noexcept
{
auto hr = S_OK;
const auto newView = Viewport::FromInclusive(srNewViewport);
const auto oldSize = _lastViewport.Dimensions();
const auto newSize = newView.Dimensions();
if (oldSize != newSize)
{
// Don't emit a resize event if we've requested it be suppressed
if (!_suppressResizeRepaint)
{
hr = _ResizeWindow(newSize.width, newSize.height);
}
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 its 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(newSize);
}
else
{
if (SUCCEEDED(hr))
{
_invalidMap.resize(newSize, true); // resize while filling in new space with repaint requests.
// Viewport is smaller now - just update it all.
if (oldSize.height > newSize.height || oldSize.width > newSize.width)
{
hr = InvalidateAll();
}
}
}
_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;
_lastViewport = newView;
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_ til::size* const pFontSize) noexcept
{
*pFontSize = { 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 til::point 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::VirtualTerminal::VtIo* 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());
_Flush();
return S_OK;
}
// Method Description:
// - Sends a notification through to the `VtInputThread` that it should
// watch for and capture the response from a DSR message we're about to send.
// This is typically `RequestCursor` at the time of writing this, but in theory
// could be another DSR as well.
// Arguments:
// - <none>
// Return Value:
// - S_OK if all goes well. Invalid state error if no notification function is installed.
// (see `SetLookingForDSRCallback` to install one.)
[[nodiscard]] HRESULT VtEngine::_ListenForDSR() noexcept
{
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !_pfnSetLookingForDSR);
_pfnSetLookingForDSR(true);
return S_OK;
}
// 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 its own buffer specifically for a
// conpty scenario.
// - See also: GH#3490, #4354, #4741
// Arguments:
// - resizeQuirk - True to turn on the quirk. False otherwise.
// Return Value:
// - true iff we were started with the `--resizeQuirk` flag enabled.
void VtEngine::SetResizeQuirk(const bool resizeQuirk)
{
_resizeQuirk = resizeQuirk;
}
void VtEngine::SetLookingForDSRCallback(std::function<void(bool)> pfnLooking) noexcept
{
_pfnSetLookingForDSR = pfnLooking;
}
void VtEngine::SetTerminalCursorTextPosition(const til::point cursor) noexcept
{
_lastText = cursor;
}
// 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
{
// On startup we request the modes we require for optimal functioning
// (namely win32 input mode and focus event mode).
//
// It's important that any additional modes set here are also mirrored in
// the AdaptDispatch::HardReset method, since that needs to re-enable them
// in the connected terminal after passing through an RIS sequence.
RETURN_IF_FAILED(_Write("\033[?9001h\033[?1004h"));
_Flush();
return S_OK;
}
HRESULT VtEngine::SwitchScreenBuffer(const bool useAltBuffer) noexcept
{
RETURN_IF_FAILED(_SwitchScreenBuffer(useAltBuffer));
_Flush();
return S_OK;
}
HRESULT VtEngine::RequestMouseMode(const bool enable) noexcept
{
const auto status = _WriteFormatted(FMT_COMPILE("\x1b[?1003;1006{}"), enable ? 'h' : 'l');
_Flush();
return status;
}