- Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathoutputStream.cpp
456 lines (409 loc) · 15.9 KB
/
outputStream.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include"precomp.h"
#include"outputStream.hpp"
#include"_stream.h"
#include"getset.h"
#include"handle.h"
#include"directio.h"
#include"output.h"
#include"../interactivity/inc/ServiceLocator.hpp"
#pragma hdrstop
usingnamespaceMicrosoft::Console;
usingnamespaceMicrosoft::Console::VirtualTerminal;
using Microsoft::Console::Interactivity::ServiceLocator;
ConhostInternalGetSet::ConhostInternalGetSet(_In_ IIoProvider& io) :
_io{ io }
{
}
// - Sends a string response to the input stream of the console.
// - Used by various commands where the program attached would like a reply to one of the commands issued.
// - This will generate two "key presses" (one down, one up) for every character in the string and place them into the head of the console's input stream.
// Arguments:
// - response - The response string to transmit back to the input stream
// Return Value:
// - <none>
voidConhostInternalGetSet::ReturnResponse(const std::wstring_view response)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// ConPTY should not respond to requests. That's the job of the terminal.
if (gci.IsInVtIoMode())
{
return;
}
// TODO GH#4954 During the input refactor we may want to add a "priority" input list
// to make sure that "response" input is spooled directly into the application.
// We switched this to an append (vs. a prepend) to fix GH#1637, a bug where two CPR
// could collide with each other.
_io.GetActiveInputBuffer()->WriteString(response);
}
// Routine Description:
// - Retrieves the state machine for the active output buffer.
// Arguments:
// - <none>
// Return Value:
// - a reference to the StateMachine instance.
StateMachine& ConhostInternalGetSet::GetStateMachine()
{
return _io.GetActiveOutputBuffer().GetStateMachine();
}
// Routine Description:
// - Retrieves the text buffer and virtual viewport for the active output
// buffer. Also returns a flag indicating whether it's the main buffer.
// Arguments:
// - <none>
// Return Value:
// - a tuple with the buffer reference, viewport, and main buffer flag.
ITerminalApi::BufferState ConhostInternalGetSet::GetBufferAndViewport()
{
auto& info = _io.GetActiveOutputBuffer();
return { info.GetTextBuffer(), info.GetVirtualViewport().ToExclusive(), info.Next == nullptr };
}
// Routine Description:
// - Sets the position of the window viewport.
// Arguments:
// - position - the new position of the viewport.
// Return Value:
// - <none>
voidConhostInternalGetSet::SetViewportPosition(const til::point position)
{
auto& info = _io.GetActiveOutputBuffer();
THROW_IF_FAILED(info.SetViewportOrigin(true, position, true));
// SetViewportOrigin() only updates the virtual bottom (the bottom coordinate of the area
// in the text buffer a VT client writes its output into) when it's moving downwards.
// But this function is meant to truly move the viewport no matter what. Otherwise `tput reset` breaks.
info.UpdateBottom();
}
// Routine Description:
// - Sets the state of one of the system modes.
// Arguments:
// - mode - The mode being updated.
// - enabled - True to enable the mode, false to disable it.
// Return Value:
// - <none>
voidConhostInternalGetSet::SetSystemMode(const Mode mode, constbool enabled)
{
switch (mode)
{
case Mode::AutoWrap:
WI_UpdateFlag(_io.GetActiveOutputBuffer().OutputMode, ENABLE_WRAP_AT_EOL_OUTPUT, enabled);
break;
case Mode::LineFeed:
WI_UpdateFlag(_io.GetActiveOutputBuffer().OutputMode, DISABLE_NEWLINE_AUTO_RETURN, !enabled);
break;
case Mode::BracketedPaste:
ServiceLocator::LocateGlobals().getConsoleInformation().SetBracketedPasteMode(enabled);
break;
default:
THROW_HR(E_INVALIDARG);
}
}
// Routine Description:
// - Retrieves the current state of one of the system modes.
// Arguments:
// - mode - The mode being queried.
// Return Value:
// - true if the mode is enabled. false otherwise.
boolConhostInternalGetSet::GetSystemMode(const Mode mode) const
{
switch (mode)
{
case Mode::AutoWrap:
returnWI_IsFlagSet(_io.GetActiveOutputBuffer().OutputMode, ENABLE_WRAP_AT_EOL_OUTPUT);
case Mode::LineFeed:
returnWI_IsFlagClear(_io.GetActiveOutputBuffer().OutputMode, DISABLE_NEWLINE_AUTO_RETURN);
case Mode::BracketedPaste:
returnServiceLocator::LocateGlobals().getConsoleInformation().GetBracketedPasteMode();
default:
THROW_HR(E_INVALIDARG);
}
}
// Routine Description:
// - Sends the configured answerback message in response to an ENQ query.
// Return Value:
// - <none>
voidConhostInternalGetSet::ReturnAnswerback()
{
constauto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
ReturnResponse(gci.GetAnswerbackMessage());
}
// Routine Description:
// - Sends a notify message to play the "SystemHand" sound event.
// Return Value:
// - <none>
voidConhostInternalGetSet::WarningBell()
{
_io.GetActiveOutputBuffer().SendNotifyBeep();
}
// Routine Description:
// - Sets the title of the console window.
// Arguments:
// - title - The string to set as the window title
// Return Value:
// - <none>
voidConhostInternalGetSet::SetWindowTitle(std::wstring_view title)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
gci.SetTitle(title.empty() ? gci.GetOriginalTitle() : title);
}
// Routine Description:
// - Swaps to the alternate screen buffer. In virtual terminals, there exists both a "main"
// screen buffer and an alternate. This creates a new alternate, and switches to it.
// If there is an already existing alternate, it is discarded.
// Arguments:
// - attrs - the attributes the buffer is initialized with.
// Return Value:
// - <none>
voidConhostInternalGetSet::UseAlternateScreenBuffer(const TextAttribute& attrs)
{
THROW_IF_NTSTATUS_FAILED(_io.GetActiveOutputBuffer().UseAlternateScreenBuffer(attrs));
}
// Routine Description:
// - Swaps to the main screen buffer. From the alternate buffer, returns to the main screen
// buffer. From the main screen buffer, does nothing. The alternate is discarded.
// Return Value:
// - <none>
voidConhostInternalGetSet::UseMainScreenBuffer()
{
_io.GetActiveOutputBuffer().UseMainScreenBuffer();
}
// Method Description:
// - Retrieves the current user default cursor style.
// Arguments:
// - <none>
// Return Value:
// - the default cursor style.
CursorType ConhostInternalGetSet::GetUserDefaultCursorStyle() const
{
constauto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
return gci.GetCursorType();
}
// Routine Description:
// - Shows or hides the active window when asked.
// Arguments:
// - showOrHide - True for show, False for hide. Matching WM_SHOWWINDOW lParam.
// Return Value:
// - <none>
voidConhostInternalGetSet::ShowWindow(bool showOrHide)
{
// ConPTY is supposed to be "transparent" to the VT application. Any VT it processes is given to the terminal.
// As such, it must not react to this "CSI 1 t" or "CSI 2 t" sequence. That's the job of the terminal.
// If the terminal encounters such a sequence, it can show/hide itself and let ConPTY know via its signal API.
constauto window = ServiceLocator::LocateConsoleWindow();
if (!window)
{
return;
}
// GH#13301 - When we send this ShowWindow message, if we send it to the
// conhost HWND, it's going to need to get processed by the window message
// thread before returning.
// However, ShowWindowAsync doesn't have this problem. It'll post the
// message to the window thread, then immediately return, so we don't have
// to worry about deadlocking.
constauto hwnd = window->GetWindowHandle();
::ShowWindowAsync(hwnd, showOrHide ? SW_SHOWNOACTIVATE : SW_MINIMIZE);
}
// Routine Description:
// - Set the code page used for translating text when calling A versions of I/O functions.
// Arguments:
// - codepage - the new code page of the console.
// Return Value:
// - <none>
voidConhostInternalGetSet::SetCodePage(constunsignedint codepage)
{
LOG_IF_FAILED(DoSrvSetConsoleOutputCodePage(codepage));
LOG_IF_FAILED(DoSrvSetConsoleInputCodePage(codepage));
}
// Routine Description:
// - Reset the code pages to their default values.
// Arguments:
// - <none>
// Return Value:
// - <none>
voidConhostInternalGetSet::ResetCodePage()
{
constauto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LOG_IF_FAILED(DoSrvSetConsoleOutputCodePage(gci.DefaultOutputCP));
LOG_IF_FAILED(DoSrvSetConsoleInputCodePage(gci.DefaultCP));
}
// Routine Description:
// - Gets the code page used for translating text when calling A versions of output functions.
// Arguments:
// - <none>
// Return Value:
// - the output code page of the console.
unsignedintConhostInternalGetSet::GetOutputCodePage() const
{
returnServiceLocator::LocateGlobals().getConsoleInformation().OutputCP;
}
// Routine Description:
// - Gets the code page used for translating text when calling A versions of input functions.
// Arguments:
// - <none>
// Return Value:
// - the input code page of the console.
unsignedintConhostInternalGetSet::GetInputCodePage() const
{
returnServiceLocator::LocateGlobals().getConsoleInformation().CP;
}
// Routine Description:
// - Copies the given content to the clipboard.
// Arguments:
// - content - the text to be copied.
// Return Value:
// - <none>
voidConhostInternalGetSet::CopyToClipboard(const wil::zwstring_view /*content*/)
{
// TODO
}
// Routine Description:
// - Updates the taskbar progress indicator.
// Arguments:
// - state: indicates the progress state
// - progress: indicates the progress value
// Return Value:
// - <none>
voidConhostInternalGetSet::SetTaskbarProgress(const DispatchTypes::TaskbarState /*state*/, constsize_t/*progress*/)
{
// TODO
}
// Routine Description:
// - Set the active working directory. Not used in conhost.
// Arguments:
// - content - the text to be copied.
// Return Value:
// - <none>
voidConhostInternalGetSet::SetWorkingDirectory(const std::wstring_view /*uri*/)
{
}
// Routine Description:
// - Plays a single MIDI note, blocking for the duration.
// Arguments:
// - noteNumber - The MIDI note number to be played (0 - 127).
// - velocity - The force with which the note should be played (0 - 127).
// - duration - How long the note should be sustained (in milliseconds).
// Return value:
// - true if successful. false otherwise.
voidConhostInternalGetSet::PlayMidiNote(constint noteNumber, constint velocity, const std::chrono::microseconds duration)
{
constauto window = ServiceLocator::LocateConsoleWindow();
if (!window)
{
return;
}
// Unlock the console, so the UI doesn't hang while we're busy.
UnlockConsole();
// This call will block for the duration, unless shutdown early.
constauto windowHandle = window->GetWindowHandle();
auto& midiAudio = ServiceLocator::LocateGlobals().getConsoleInformation().GetMidiAudio();
midiAudio.PlayNote(windowHandle, noteNumber, velocity, std::chrono::duration_cast<std::chrono::milliseconds>(duration));
LockConsole();
}
// Routine Description:
// - Resizes the window to the specified dimensions, in characters.
// Arguments:
// - width: The new width of the window, in columns
// - height: The new height of the window, in rows
// Return Value:
// - True if handled successfully. False otherwise.
boolConhostInternalGetSet::ResizeWindow(const til::CoordType sColumns, const til::CoordType sRows)
{
// Ensure we can safely use gsl::narrow_cast<short>(...).
if (sColumns <= 0 || sRows <= 0 || sColumns > SHRT_MAX || sRows > SHRT_MAX)
{
returnfalse;
}
auto api = ServiceLocator::LocateGlobals().api;
auto& screenInfo = _io.GetActiveOutputBuffer();
// We need to save the attributes separately, since the wAttributes field in
// CONSOLE_SCREEN_BUFFER_INFOEX is not capable of representing the extended
// attribute values, and can end up corrupting that data when restored.
constauto attributes = screenInfo.GetTextBuffer().GetCurrentAttributes();
constauto restoreAttributes = wil::scope_exit([&] {
screenInfo.GetTextBuffer().SetCurrentAttributes(attributes);
});
CONSOLE_SCREEN_BUFFER_INFOEX csbiex = { 0 };
csbiex.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
api->GetConsoleScreenBufferInfoExImpl(screenInfo, csbiex);
constauto oldViewport = screenInfo.GetVirtualViewport();
auto newViewport = Viewport::FromDimensions(oldViewport.Origin(), { sColumns, sRows });
// Always resize the width of the console
csbiex.dwSize.X = gsl::narrow_cast<short>(sColumns);
// Only set the screen buffer's height if it's currently less than
// what we're requesting.
if (sRows > csbiex.dwSize.Y)
{
csbiex.dwSize.Y = gsl::narrow_cast<short>(sRows);
}
// If the cursor row is now past the bottom of the viewport, we'll have to
// move the viewport down to bring it back into view.
constauto cursorOverflow = csbiex.dwCursorPosition.Y - newViewport.BottomInclusive();
if (cursorOverflow > 0)
{
newViewport = Viewport::Offset(newViewport, { 0, cursorOverflow });
}
// SetWindowInfo expect inclusive rects
constauto sri = newViewport.ToInclusive();
// SetConsoleScreenBufferInfoEx however expects exclusive rects
constauto sre = newViewport.ToExclusive();
csbiex.srWindow = til::unwrap_exclusive_small_rect(sre);
THROW_IF_FAILED(api->SetConsoleScreenBufferInfoExImpl(screenInfo, csbiex));
THROW_IF_FAILED(api->SetConsoleWindowInfoImpl(screenInfo, true, sri));
returntrue;
}
// Routine Description:
// - Checks if the InputBuffer is willing to accept VT Input directly
// IsVtInputEnabled is an internal-only "API" call that the vt commands can execute,
// but it is not represented as a function call on our public API surface.
// Arguments:
// - <none>
// Return value:
// - true if enabled (see IsInVirtualTerminalInputMode). false otherwise.
boolConhostInternalGetSet::IsVtInputEnabled() const
{
return _io.GetActiveInputBuffer()->IsInVirtualTerminalInputMode();
}
// Routine Description:
// - Lets accessibility apps know when an area of the screen has changed.
// Arguments:
// - changedRect - the area that has changed.
// Return value:
// - <none>
voidConhostInternalGetSet::NotifyAccessibilityChange(const til::rect& changedRect)
{
auto& screenInfo = _io.GetActiveOutputBuffer();
if (screenInfo.HasAccessibilityEventing() && changedRect)
{
screenInfo.NotifyAccessibilityEventing(
changedRect.left,
changedRect.top,
changedRect.right - 1,
changedRect.bottom - 1);
}
}
// Routine Description:
// - Implements conhost-specific behavior when the buffer is rotated.
// Arguments:
// - delta - the number of cycles that the buffer has rotated.
// Return value:
// - <none>
voidConhostInternalGetSet::NotifyBufferRotation(constint delta)
{
auto& screenInfo = _io.GetActiveOutputBuffer();
if (screenInfo.IsActiveScreenBuffer())
{
auto pNotifier = ServiceLocator::LocateAccessibilityNotifier();
if (pNotifier)
{
pNotifier->NotifyConsoleUpdateScrollEvent(0, -delta);
}
}
}
voidConhostInternalGetSet::InvokeCompletions(std::wstring_view /*menuJson*/, unsignedint/*replaceLength*/)
{
// Not implemented for conhost.
}
voidConhostInternalGetSet::SearchMissingCommand(std::wstring_view /*missingCommand*/)
{
// Not implemented for conhost.
}