-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy path_output.cpp
499 lines (437 loc) · 20.8 KB
/
_output.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "_output.h"
#include "directio.h"
#include "handle.h"
#include "misc.h"
#include "_stream.h"
#include "../interactivity/inc/ServiceLocator.hpp"
#include "../types/inc/convert.hpp"
#include "../types/inc/GlyphWidth.hpp"
#include "../types/inc/Viewport.hpp"
using namespace Microsoft::Console::Types;
using Microsoft::Console::Interactivity::ServiceLocator;
// Routine Description:
// - This routine writes a screen buffer region to the screen.
// Arguments:
// - screenInfo - reference to screen buffer information.
// - srRegion - Region to write in screen buffer coordinates. Region is inclusive
// Return Value:
// - <none>
void WriteToScreen(SCREEN_INFORMATION& screenInfo, const Viewport& region)
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// update to screen, if we're not iconic.
if (!screenInfo.IsActiveScreenBuffer() || WI_IsFlagSet(gci.Flags, CONSOLE_IS_ICONIC))
{
return;
}
// clip region to fit within the viewport
const auto clippedRegion = screenInfo.GetViewport().Clamp(region);
if (!clippedRegion.IsValid())
{
return;
}
if (screenInfo.IsActiveScreenBuffer())
{
if (ServiceLocator::LocateGlobals().pRender != nullptr)
{
ServiceLocator::LocateGlobals().pRender->TriggerRedraw(region);
}
}
}
enum class FillConsoleMode
{
WriteAttribute,
FillAttribute,
WriteCharacter,
FillCharacter,
};
struct FillConsoleResult
{
size_t lengthRead = 0;
til::CoordType cellsModified = 0;
};
static FillConsoleResult FillConsoleImpl(SCREEN_INFORMATION& screenInfo, FillConsoleMode mode, const void* data, const size_t lengthToWrite, const til::point startingCoordinate)
{
if (lengthToWrite == 0)
{
return {};
}
LockConsole();
const auto unlock = wil::scope_exit([&] { UnlockConsole(); });
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto& screenBuffer = screenInfo.GetActiveBuffer();
const auto bufferSize = screenBuffer.GetBufferSize();
FillConsoleResult result;
if (!bufferSize.IsInBounds(startingCoordinate))
{
return {};
}
if (auto writer = gci.GetVtWriterForBuffer(&screenInfo))
{
writer.BackupCursor();
const auto h = bufferSize.Height();
const auto w = bufferSize.Width();
auto y = startingCoordinate.y;
auto input = static_cast<const uint16_t*>(data);
size_t inputPos = 0;
til::small_vector<CHAR_INFO, 1024> infoBuffer;
Viewport unused;
infoBuffer.resize(gsl::narrow_cast<size_t>(w));
while (y < h && inputPos < lengthToWrite)
{
const auto beg = y == startingCoordinate.y ? startingCoordinate.x : 0;
const auto columnsAvailable = w - beg;
til::CoordType columns = 0;
const auto readViewport = Viewport::FromInclusive({ beg, y, w - 1, y });
THROW_IF_FAILED(ReadConsoleOutputWImplHelper(screenInfo, infoBuffer, readViewport, unused));
switch (mode)
{
case FillConsoleMode::WriteAttribute:
for (; columns < columnsAvailable && inputPos < lengthToWrite; ++columns, ++inputPos)
{
infoBuffer[columns].Attributes = input[inputPos];
}
break;
case FillConsoleMode::FillAttribute:
for (const auto attr = input[0]; columns < columnsAvailable && inputPos < lengthToWrite; ++columns, ++inputPos)
{
infoBuffer[columns].Attributes = attr;
}
break;
case FillConsoleMode::WriteCharacter:
for (; columns < columnsAvailable && inputPos < lengthToWrite; ++inputPos)
{
const auto ch = input[inputPos];
if (ch >= 0x80 && IsGlyphFullWidth(ch))
{
// If the wide glyph doesn't fit into the last column, pad it with whitespace.
if ((columns + 1) >= columnsAvailable)
{
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = L' ';
lead.Attributes = lead.Attributes & ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE);
break;
}
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = ch;
lead.Attributes = lead.Attributes & ~COMMON_LVB_TRAILING_BYTE | COMMON_LVB_LEADING_BYTE;
auto& trail = infoBuffer[columns++];
trail.Char.UnicodeChar = ch;
trail.Attributes = trail.Attributes & ~COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE;
}
else
{
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = ch;
lead.Attributes = lead.Attributes & ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE);
}
}
break;
case FillConsoleMode::FillCharacter:
// Identical to WriteCharacter above, but with the if() and for() swapped.
if (const auto ch = input[0]; ch >= 0x80 && IsGlyphFullWidth(ch))
{
for (; columns < columnsAvailable && inputPos < lengthToWrite; ++inputPos)
{
// If the wide glyph doesn't fit into the last column, pad it with whitespace.
if ((columns + 1) >= columnsAvailable)
{
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = L' ';
lead.Attributes = lead.Attributes & ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE);
break;
}
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = ch;
lead.Attributes = lead.Attributes & ~COMMON_LVB_TRAILING_BYTE | COMMON_LVB_LEADING_BYTE;
auto& trail = infoBuffer[columns++];
trail.Char.UnicodeChar = ch;
trail.Attributes = trail.Attributes & ~COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE;
}
}
else
{
for (; columns < columnsAvailable && inputPos < lengthToWrite; ++inputPos)
{
auto& lead = infoBuffer[columns++];
lead.Char.UnicodeChar = ch;
lead.Attributes = lead.Attributes & ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE);
}
}
break;
}
const auto writeViewport = Viewport::FromInclusive({ beg, y, beg + columns - 1, y });
THROW_IF_FAILED(WriteConsoleOutputWImplHelper(screenInfo, infoBuffer, w, writeViewport, unused));
y += 1;
result.cellsModified += columns;
}
result.lengthRead = inputPos;
writer.Submit();
}
else
{
// Technically we could always pass `data` as `uint16_t*`, because `wchar_t` is guaranteed to be 16 bits large.
// However, OutputCellIterator is terrifyingly unsafe code and so we don't do that.
//
// Constructing an OutputCellIterator with a `wchar_t` takes the `wchar_t` by reference, so that it can reference
// it in a `wstring_view` forever. That's of course really bad because passing a `const uint16_t&` to a
// `const wchar_t&` argument implicitly converts the types. To do so, the implicit conversion allocates a
// `wchar_t` value on the stack. The lifetime of that copy DOES NOT get extended beyond the constructor call.
// The result is that OutputCellIterator would read random data from the stack.
//
// Don't ever assume the lifetime of implicitly convertible types given by reference.
// Ironically that's a bug that cannot happen with C pointers. To no ones surprise, C keeps on winning.
auto attrs = static_cast<const uint16_t*>(data);
auto chars = static_cast<const wchar_t*>(data);
OutputCellIterator it;
switch (mode)
{
case FillConsoleMode::WriteAttribute:
it = OutputCellIterator({ attrs, lengthToWrite });
break;
case FillConsoleMode::WriteCharacter:
it = OutputCellIterator({ chars, lengthToWrite });
break;
case FillConsoleMode::FillAttribute:
it = OutputCellIterator(TextAttribute(*attrs), lengthToWrite);
break;
case FillConsoleMode::FillCharacter:
it = OutputCellIterator(*chars, lengthToWrite);
break;
default:
__assume(false);
}
const auto done = screenBuffer.Write(it, startingCoordinate, false);
result.lengthRead = done.GetInputDistance(it);
result.cellsModified = done.GetCellDistance(it);
// If we've overwritten image content, it needs to be erased.
ImageSlice::EraseCells(screenInfo.GetTextBuffer(), startingCoordinate, result.cellsModified);
}
if (screenBuffer.HasAccessibilityEventing())
{
// Notify accessibility
auto endingCoordinate = startingCoordinate;
bufferSize.WalkInBounds(endingCoordinate, result.cellsModified);
screenBuffer.NotifyAccessibilityEventing(startingCoordinate.x, startingCoordinate.y, endingCoordinate.x, endingCoordinate.y);
}
return result;
}
// Routine Description:
// - writes text attributes to the screen
// Arguments:
// - OutContext - the screen info to write to
// - attrs - the attrs to write to the screen
// - target - the starting coordinate in the screen
// - used - number of elements written
// Return Value:
// - S_OK, E_INVALIDARG or similar HRESULT error.
[[nodiscard]] HRESULT ApiRoutines::WriteConsoleOutputAttributeImpl(IConsoleOutputObject& OutContext,
const std::span<const WORD> attrs,
const til::point target,
size_t& used) noexcept
{
// Set used to 0 from the beginning in case we exit early.
used = 0;
if (attrs.empty())
{
return S_OK;
}
try
{
LockConsole();
const auto unlock = wil::scope_exit([&] { UnlockConsole(); });
used = FillConsoleImpl(OutContext, FillConsoleMode::WriteAttribute, attrs.data(), attrs.size(), target).cellsModified;
return S_OK;
}
CATCH_RETURN();
}
// Routine Description:
// - writes text to the screen
// Arguments:
// - screenInfo - the screen info to write to
// - chars - the text to write to the screen
// - target - the starting coordinate in the screen
// - used - number of elements written
// Return Value:
// - S_OK, E_INVALIDARG or similar HRESULT error.
[[nodiscard]] HRESULT ApiRoutines::WriteConsoleOutputCharacterWImpl(IConsoleOutputObject& OutContext,
const std::wstring_view chars,
const til::point target,
size_t& used) noexcept
{
// Set used to 0 from the beginning in case we exit early.
used = 0;
if (chars.empty())
{
return S_OK;
}
try
{
LockConsole();
const auto unlock = wil::scope_exit([&] { UnlockConsole(); });
used = FillConsoleImpl(OutContext, FillConsoleMode::WriteCharacter, chars.data(), chars.size(), target).lengthRead;
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - writes text to the screen
// Arguments:
// - screenInfo - the screen info to write to
// - chars - the text to write to the screen
// - target - the starting coordinate in the screen
// - used - number of elements written
// Return Value:
// - S_OK, E_INVALIDARG or similar HRESULT error.
[[nodiscard]] HRESULT ApiRoutines::WriteConsoleOutputCharacterAImpl(IConsoleOutputObject& OutContext,
const std::string_view chars,
const til::point target,
size_t& used) noexcept
{
// Set used to 0 from the beginning in case we exit early.
used = 0;
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto codepage = gci.OutputCP;
try
{
// convert to wide chars so we can call the W version of this function
const auto wideChars = ConvertToW(codepage, chars);
size_t wideCharsWritten = 0;
RETURN_IF_FAILED(WriteConsoleOutputCharacterWImpl(OutContext, wideChars, target, wideCharsWritten));
// Create a view over the wide chars and reduce it to the amount actually written (do in two steps to enforce bounds)
std::wstring_view writtenView(wideChars);
writtenView = writtenView.substr(0, wideCharsWritten);
// Look over written wide chars to find equivalent count of ascii chars so we can properly report back
// how many elements were actually written
used = GetALengthFromW(codepage, writtenView);
}
CATCH_RETURN();
return S_OK;
}
// Routine Description:
// - fills the screen buffer with the specified text attribute
// Arguments:
// - OutContext - reference to screen buffer information.
// - attribute - the text attribute to use to fill
// - lengthToWrite - the number of elements to write
// - startingCoordinate - Screen buffer coordinate to begin writing to.
// - cellsModified - the number of elements written
// Return Value:
// - S_OK or suitable HRESULT code from failure to write (memory issues, invalid arg, etc.)
[[nodiscard]] HRESULT ApiRoutines::FillConsoleOutputAttributeImpl(IConsoleOutputObject& OutContext,
const WORD attribute,
const size_t lengthToWrite,
const til::point startingCoordinate,
size_t& cellsModified,
const bool enablePowershellShim) noexcept
{
try
{
LockConsole();
const auto unlock = wil::scope_exit([&] { UnlockConsole(); });
// See FillConsoleOutputCharacterWImpl and its identical code.
if (enablePowershellShim)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (const auto writer = gci.GetVtWriterForBuffer(&OutContext))
{
const auto currentBufferDimensions{ OutContext.GetBufferSize().Dimensions() };
const auto wroteWholeBuffer = lengthToWrite == (currentBufferDimensions.area<size_t>());
const auto startedAtOrigin = startingCoordinate == til::point{ 0, 0 };
const auto wroteSpaces = attribute == (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
if (wroteWholeBuffer && startedAtOrigin && wroteSpaces)
{
// PowerShell has previously called FillConsoleOutputCharacterW() which triggered a call to WriteClearScreen().
cellsModified = lengthToWrite;
return S_OK;
}
}
}
cellsModified = FillConsoleImpl(OutContext, FillConsoleMode::FillAttribute, &attribute, lengthToWrite, startingCoordinate).cellsModified;
return S_OK;
}
CATCH_RETURN();
}
// Routine Description:
// - fills the screen buffer with the specified wchar
// Arguments:
// - OutContext - reference to screen buffer information.
// - character - wchar to fill with
// - lengthToWrite - the number of elements to write
// - startingCoordinate - Screen buffer coordinate to begin writing to.
// - cellsModified - the number of elements written
// - enablePowershellShim - true iff the client process that's calling this
// method is "powershell.exe". Used to enable certain compatibility shims for
// conpty mode. See GH#3126.
// Return Value:
// - S_OK or suitable HRESULT code from failure to write (memory issues, invalid arg, etc.)
[[nodiscard]] HRESULT ApiRoutines::FillConsoleOutputCharacterWImpl(IConsoleOutputObject& OutContext,
const wchar_t character,
const size_t lengthToWrite,
const til::point startingCoordinate,
size_t& cellsModified,
const bool enablePowershellShim) noexcept
{
try
{
LockConsole();
const auto unlock = wil::scope_exit([&] { UnlockConsole(); });
// GH#3126 - This is a shim for powershell's `Clear-Host` function. In
// the vintage console, `Clear-Host` is supposed to clear the entire
// buffer. In conpty however, there's no difference between the viewport
// and the entirety of the buffer. We're going to see if this API call
// exactly matched the way we expect powershell to call it. If it does,
// then let's manually emit a ^[[3J to the connected terminal, so that
// their entire buffer will be cleared as well.
if (enablePowershellShim)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (auto writer = gci.GetVtWriterForBuffer(&OutContext))
{
const auto currentBufferDimensions{ OutContext.GetBufferSize().Dimensions() };
const auto wroteWholeBuffer = lengthToWrite == (currentBufferDimensions.area<size_t>());
const auto startedAtOrigin = startingCoordinate == til::point{ 0, 0 };
const auto wroteSpaces = character == UNICODE_SPACE;
if (wroteWholeBuffer && startedAtOrigin && wroteSpaces)
{
WriteClearScreen(OutContext);
writer.Submit();
cellsModified = lengthToWrite;
return S_OK;
}
}
}
cellsModified = FillConsoleImpl(OutContext, FillConsoleMode::FillCharacter, &character, lengthToWrite, startingCoordinate).lengthRead;
return S_OK;
}
CATCH_RETURN();
}
// Routine Description:
// - fills the screen buffer with the specified char
// Arguments:
// - OutContext - reference to screen buffer information.
// - character - ascii character to fill with
// - lengthToWrite - the number of elements to write
// - startingCoordinate - Screen buffer coordinate to begin writing to.
// - cellsModified - the number of elements written
// Return Value:
// - S_OK or suitable HRESULT code from failure to write (memory issues, invalid arg, etc.)
[[nodiscard]] HRESULT ApiRoutines::FillConsoleOutputCharacterAImpl(IConsoleOutputObject& OutContext,
const char character,
const size_t lengthToWrite,
const til::point startingCoordinate,
size_t& cellsModified) noexcept
try
{
// In case ConvertToW throws causing an early return, set modified cells to 0.
cellsModified = 0;
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// convert to wide chars and call W version
const auto wchs = ConvertToW(gci.OutputCP, { &character, 1 });
LOG_HR_IF(E_UNEXPECTED, wchs.size() > 1);
return FillConsoleOutputCharacterWImpl(OutContext, wchs.at(0), lengthToWrite, startingCoordinate, cellsModified);
}
CATCH_RETURN()