-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
OutputStateMachineEngine.cpp
1741 lines (1611 loc) · 62.1 KB
/
OutputStateMachineEngine.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "stateMachine.hpp"
#include "OutputStateMachineEngine.hpp"
#include "ascii.hpp"
using namespace Microsoft::Console;
using namespace Microsoft::Console::VirtualTerminal;
// takes ownership of pDispatch
OutputStateMachineEngine::OutputStateMachineEngine(std::unique_ptr<ITermDispatch> pDispatch) :
_dispatch(std::move(pDispatch)),
_pfnFlushToTerminal(nullptr),
_pTtyConnection(nullptr),
_lastPrintedChar(AsciiChars::NUL)
{
THROW_HR_IF_NULL(E_INVALIDARG, _dispatch.get());
}
const ITermDispatch& OutputStateMachineEngine::Dispatch() const noexcept
{
return *_dispatch;
}
ITermDispatch& OutputStateMachineEngine::Dispatch() noexcept
{
return *_dispatch;
}
// Routine Description:
// - Triggers the Execute action to indicate that the listener should
// immediately respond to a C0 control character.
// Arguments:
// - wch - Character to dispatch.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionExecute(const wchar_t wch)
{
switch (wch)
{
case AsciiChars::NUL:
// microsoft/terminal#1825 - VT applications expect to be able to write NUL
// and have _nothing_ happen. Filter the NULs here, so they don't fill the
// buffer with empty spaces.
break;
case AsciiChars::BEL:
_dispatch->WarningBell();
// microsoft/terminal#2952
// If we're attached to a terminal, let's also pass the BEL through.
if (_pfnFlushToTerminal != nullptr)
{
_pfnFlushToTerminal();
}
break;
case AsciiChars::BS:
_dispatch->CursorBackward(1);
break;
case AsciiChars::TAB:
_dispatch->ForwardTab(1);
break;
case AsciiChars::CR:
_dispatch->CarriageReturn();
break;
case AsciiChars::LF:
case AsciiChars::FF:
case AsciiChars::VT:
// LF, FF, and VT are identical in function.
_dispatch->LineFeed(DispatchTypes::LineFeedType::DependsOnMode);
break;
default:
_dispatch->Execute(wch);
break;
}
_ClearLastChar();
return true;
}
// Routine Description:
// - Triggers the Execute action to indicate that the listener should
// immediately respond to a C0 control character.
// This is called from the Escape state in the state machine, indicating the
// immediately previous character was an 0x1b. The output state machine
// does not treat this any differently than a normal ActionExecute.
// Arguments:
// - wch - Character to dispatch.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionExecuteFromEscape(const wchar_t wch)
{
return ActionExecute(wch);
}
// Routine Description:
// - Triggers the Print action to indicate that the listener should render the
// character given.
// Arguments:
// - wch - Character to dispatch.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionPrint(const wchar_t wch)
{
// Stash the last character of the string, if it's a graphical character
if (wch >= AsciiChars::SPC)
{
_lastPrintedChar = wch;
}
_dispatch->Print(wch); // call print
return true;
}
// Routine Description:
// - Triggers the Print action to indicate that the listener should render the
// string of characters given.
// Arguments:
// - string - string to dispatch.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionPrintString(const std::wstring_view string)
{
if (string.empty())
{
return true;
}
// Stash the last character of the string, if it's a graphical character
const wchar_t wch = string.back();
if (wch >= AsciiChars::SPC)
{
_lastPrintedChar = wch;
}
_dispatch->PrintString(string); // call print
return true;
}
// Routine Description:
// This is called when we have determined that we don't understand a particular
// sequence, or the adapter has determined that the string is intended for
// the actual terminal (when we're acting as a pty).
// - Pass the string through to the target terminal application. If we're a pty,
// then we'll have a TerminalConnection that we'll write the string to.
// Otherwise, we're the terminal device, and we'll eat the string (because
// we don't know what to do with it)
// Arguments:
// - string - string to dispatch.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionPassThroughString(const std::wstring_view string)
{
bool success = true;
if (_pTtyConnection != nullptr)
{
const auto hr = _pTtyConnection->WriteTerminalW(string);
LOG_IF_FAILED(hr);
success = SUCCEEDED(hr);
}
// If there's not a TTY connection, our previous behavior was to eat the string.
return success;
}
// Routine Description:
// - Triggers the EscDispatch action to indicate that the listener should handle
// a simple escape sequence. These sequences traditionally start with ESC
// and a simple letter. No complicated parameters.
// Arguments:
// - wch - Character to dispatch.
// - intermediates - Intermediate characters in the sequence
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionEscDispatch(const wchar_t wch,
const std::basic_string_view<wchar_t> intermediates)
{
bool success = false;
// no intermediates.
if (intermediates.empty())
{
switch (wch)
{
case VTActionCodes::DECSC_CursorSave:
success = _dispatch->CursorSaveState();
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECSC);
break;
case VTActionCodes::DECRC_CursorRestore:
success = _dispatch->CursorRestoreState();
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECRC);
break;
case VTActionCodes::DECKPAM_KeypadApplicationMode:
success = _dispatch->SetKeypadMode(true);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECKPAM);
break;
case VTActionCodes::DECKPNM_KeypadNumericMode:
success = _dispatch->SetKeypadMode(false);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECKPNM);
break;
case VTActionCodes::NEL_NextLine:
success = _dispatch->LineFeed(DispatchTypes::LineFeedType::WithReturn);
TermTelemetry::Instance().Log(TermTelemetry::Codes::NEL);
break;
case VTActionCodes::IND_Index:
success = _dispatch->LineFeed(DispatchTypes::LineFeedType::WithoutReturn);
TermTelemetry::Instance().Log(TermTelemetry::Codes::IND);
break;
case VTActionCodes::RI_ReverseLineFeed:
success = _dispatch->ReverseLineFeed();
TermTelemetry::Instance().Log(TermTelemetry::Codes::RI);
break;
case VTActionCodes::HTS_HorizontalTabSet:
success = _dispatch->HorizontalTabSet();
TermTelemetry::Instance().Log(TermTelemetry::Codes::HTS);
break;
case VTActionCodes::RIS_ResetToInitialState:
success = _dispatch->HardReset();
TermTelemetry::Instance().Log(TermTelemetry::Codes::RIS);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
else if (intermediates.size() == 1)
{
const auto value = til::at(intermediates, 0);
DesignateCharsetTypes designateType = DefaultDesignateCharsetType;
success = _GetDesignateType(value, designateType);
if (success)
{
switch (designateType)
{
case DesignateCharsetTypes::G0:
success = _dispatch->DesignateCharset(wch);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DesignateG0);
break;
case DesignateCharsetTypes::G1:
success = false;
TermTelemetry::Instance().Log(TermTelemetry::Codes::DesignateG1);
break;
case DesignateCharsetTypes::G2:
success = false;
TermTelemetry::Instance().Log(TermTelemetry::Codes::DesignateG2);
break;
case DesignateCharsetTypes::G3:
success = false;
TermTelemetry::Instance().Log(TermTelemetry::Codes::DesignateG3);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
else if (value == L'#')
{
switch (wch)
{
case VTActionCodes::DECALN_ScreenAlignmentPattern:
success = _dispatch->ScreenAlignmentPattern();
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECALN);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
}
// If we were unable to process the string, and there's a TTY attached to us,
// trigger the state machine to flush the string to the terminal.
if (_pfnFlushToTerminal != nullptr && !success)
{
success = _pfnFlushToTerminal();
}
_ClearLastChar();
return success;
}
// Routine Description:
// - Triggers the CsiDispatch action to indicate that the listener should handle
// a control sequence. These sequences perform various API-type commands
// that can include many parameters.
// Arguments:
// - wch - Character to dispatch.
// - intermediates - Intermediate characters in the sequence
// - parameters - set of numeric parameters collected while parsing the sequence.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
const std::basic_string_view<wchar_t> intermediates,
std::basic_string_view<size_t> parameters)
{
bool success = false;
size_t distance = 0;
size_t line = 0;
size_t column = 0;
size_t topMargin = 0;
size_t bottomMargin = 0;
size_t numTabs = 0;
size_t clearType = 0;
unsigned int function = 0;
DispatchTypes::EraseType eraseType = DispatchTypes::EraseType::ToEnd;
std::vector<DispatchTypes::GraphicsOptions> graphicsOptions;
DispatchTypes::AnsiStatusType deviceStatusType = static_cast<DispatchTypes::AnsiStatusType>(0); // there is no default status type.
size_t repeatCount = 0;
// This is all the args after the first arg, and the count of args not including the first one.
const auto remainingParams = parameters.size() > 1 ? parameters.substr(1) : std::basic_string_view<size_t>{};
if (intermediates.empty())
{
// fill params
switch (wch)
{
case VTActionCodes::CUU_CursorUp:
case VTActionCodes::CUD_CursorDown:
case VTActionCodes::CUF_CursorForward:
case VTActionCodes::CUB_CursorBackward:
case VTActionCodes::CNL_CursorNextLine:
case VTActionCodes::CPL_CursorPrevLine:
case VTActionCodes::CHA_CursorHorizontalAbsolute:
case VTActionCodes::HPA_HorizontalPositionAbsolute:
case VTActionCodes::VPA_VerticalLinePositionAbsolute:
case VTActionCodes::HPR_HorizontalPositionRelative:
case VTActionCodes::VPR_VerticalPositionRelative:
case VTActionCodes::ICH_InsertCharacter:
case VTActionCodes::DCH_DeleteCharacter:
case VTActionCodes::ECH_EraseCharacters:
success = _GetCursorDistance(parameters, distance);
break;
case VTActionCodes::HVP_HorizontalVerticalPosition:
case VTActionCodes::CUP_CursorPosition:
success = _GetXYPosition(parameters, line, column);
break;
case VTActionCodes::DECSTBM_SetScrollingRegion:
success = _GetTopBottomMargins(parameters, topMargin, bottomMargin);
break;
case VTActionCodes::ED_EraseDisplay:
case VTActionCodes::EL_EraseLine:
success = _GetEraseOperation(parameters, eraseType);
break;
case VTActionCodes::SGR_SetGraphicsRendition:
success = _GetGraphicsOptions(parameters, graphicsOptions);
break;
case VTActionCodes::DSR_DeviceStatusReport:
success = _GetDeviceStatusOperation(parameters, deviceStatusType);
break;
case VTActionCodes::DA_DeviceAttributes:
success = _VerifyDeviceAttributesParams(parameters);
break;
case VTActionCodes::SU_ScrollUp:
case VTActionCodes::SD_ScrollDown:
success = _GetScrollDistance(parameters, distance);
break;
case VTActionCodes::ANSISYSSC_CursorSave:
case VTActionCodes::ANSISYSRC_CursorRestore:
success = _VerifyHasNoParameters(parameters);
break;
case VTActionCodes::IL_InsertLine:
case VTActionCodes::DL_DeleteLine:
success = _GetScrollDistance(parameters, distance);
break;
case VTActionCodes::CHT_CursorForwardTab:
case VTActionCodes::CBT_CursorBackTab:
success = _GetTabDistance(parameters, numTabs);
break;
case VTActionCodes::TBC_TabClear:
success = _GetTabClearType(parameters, clearType);
break;
case VTActionCodes::DTTERM_WindowManipulation:
success = _GetWindowManipulationType(parameters, function);
break;
case VTActionCodes::REP_RepeatCharacter:
success = _GetRepeatCount(parameters, repeatCount);
break;
default:
// If no params to fill, param filling was successful.
success = true;
break;
}
// if param filling successful, try to dispatch
if (success)
{
switch (wch)
{
case VTActionCodes::CUU_CursorUp:
success = _dispatch->CursorUp(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CUU);
break;
case VTActionCodes::CUD_CursorDown:
success = _dispatch->CursorDown(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CUD);
break;
case VTActionCodes::CUF_CursorForward:
success = _dispatch->CursorForward(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CUF);
break;
case VTActionCodes::CUB_CursorBackward:
success = _dispatch->CursorBackward(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CUB);
break;
case VTActionCodes::CNL_CursorNextLine:
success = _dispatch->CursorNextLine(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CNL);
break;
case VTActionCodes::CPL_CursorPrevLine:
success = _dispatch->CursorPrevLine(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CPL);
break;
case VTActionCodes::CHA_CursorHorizontalAbsolute:
case VTActionCodes::HPA_HorizontalPositionAbsolute:
success = _dispatch->CursorHorizontalPositionAbsolute(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CHA);
break;
case VTActionCodes::VPA_VerticalLinePositionAbsolute:
success = _dispatch->VerticalLinePositionAbsolute(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::VPA);
break;
case VTActionCodes::HPR_HorizontalPositionRelative:
success = _dispatch->HorizontalPositionRelative(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::HPR);
break;
case VTActionCodes::VPR_VerticalPositionRelative:
success = _dispatch->VerticalPositionRelative(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::VPR);
break;
case VTActionCodes::CUP_CursorPosition:
case VTActionCodes::HVP_HorizontalVerticalPosition:
success = _dispatch->CursorPosition(line, column);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CUP);
break;
case VTActionCodes::DECSTBM_SetScrollingRegion:
success = _dispatch->SetTopBottomScrollingMargins(topMargin, bottomMargin);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECSTBM);
break;
case VTActionCodes::ICH_InsertCharacter:
success = _dispatch->InsertCharacter(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::ICH);
break;
case VTActionCodes::DCH_DeleteCharacter:
success = _dispatch->DeleteCharacter(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DCH);
break;
case VTActionCodes::ED_EraseDisplay:
success = _dispatch->EraseInDisplay(eraseType);
TermTelemetry::Instance().Log(TermTelemetry::Codes::ED);
break;
case VTActionCodes::EL_EraseLine:
success = _dispatch->EraseInLine(eraseType);
TermTelemetry::Instance().Log(TermTelemetry::Codes::EL);
break;
case VTActionCodes::SGR_SetGraphicsRendition:
success = _dispatch->SetGraphicsRendition({ graphicsOptions.data(), graphicsOptions.size() });
TermTelemetry::Instance().Log(TermTelemetry::Codes::SGR);
break;
case VTActionCodes::DSR_DeviceStatusReport:
success = _dispatch->DeviceStatusReport(deviceStatusType);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DSR);
break;
case VTActionCodes::DA_DeviceAttributes:
success = _dispatch->DeviceAttributes();
TermTelemetry::Instance().Log(TermTelemetry::Codes::DA);
break;
case VTActionCodes::SU_ScrollUp:
success = _dispatch->ScrollUp(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::SU);
break;
case VTActionCodes::SD_ScrollDown:
success = _dispatch->ScrollDown(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::SD);
break;
case VTActionCodes::ANSISYSSC_CursorSave:
success = _dispatch->CursorSaveState();
TermTelemetry::Instance().Log(TermTelemetry::Codes::ANSISYSSC);
break;
case VTActionCodes::ANSISYSRC_CursorRestore:
success = _dispatch->CursorRestoreState();
TermTelemetry::Instance().Log(TermTelemetry::Codes::ANSISYSRC);
break;
case VTActionCodes::IL_InsertLine:
success = _dispatch->InsertLine(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::IL);
break;
case VTActionCodes::DL_DeleteLine:
success = _dispatch->DeleteLine(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DL);
break;
case VTActionCodes::CHT_CursorForwardTab:
success = _dispatch->ForwardTab(numTabs);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CHT);
break;
case VTActionCodes::CBT_CursorBackTab:
success = _dispatch->BackwardsTab(numTabs);
TermTelemetry::Instance().Log(TermTelemetry::Codes::CBT);
break;
case VTActionCodes::TBC_TabClear:
success = _dispatch->TabClear(clearType);
TermTelemetry::Instance().Log(TermTelemetry::Codes::TBC);
break;
case VTActionCodes::ECH_EraseCharacters:
success = _dispatch->EraseCharacters(distance);
TermTelemetry::Instance().Log(TermTelemetry::Codes::ECH);
break;
case VTActionCodes::DTTERM_WindowManipulation:
success = _dispatch->WindowManipulation(static_cast<DispatchTypes::WindowManipulationType>(function),
remainingParams);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DTTERM_WM);
break;
case VTActionCodes::REP_RepeatCharacter:
// Handled w/o the dispatch. This function is unique in that way
// If this were in the ITerminalDispatch, then each
// implementation would effectively be the same, calling only
// functions that are already part of the interface.
// Print the last graphical character a number of times.
if (_lastPrintedChar != AsciiChars::NUL)
{
std::wstring wstr(repeatCount, _lastPrintedChar);
_dispatch->PrintString(wstr);
}
success = true;
TermTelemetry::Instance().Log(TermTelemetry::Codes::REP);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
}
else if (intermediates.size() == 1)
{
const auto value = til::at(intermediates, 0);
switch (value)
{
case L'?':
success = _IntermediateQuestionMarkDispatch(wch, parameters);
break;
case L'!':
success = _IntermediateExclamationDispatch(wch);
break;
case L' ':
success = _IntermediateSpaceDispatch(wch, parameters);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
// If we were unable to process the string, and there's a TTY attached to us,
// trigger the state machine to flush the string to the terminal.
if (_pfnFlushToTerminal != nullptr && !success)
{
success = _pfnFlushToTerminal();
}
_ClearLastChar();
return success;
}
// Routine Description:
// - Handles actions that have postfix params on an intermediate '?', such as DECTCEM, DECCOLM, ATT610
// Arguments:
// - wch - Character to dispatch.
// - parameters - set of numeric parameters collected while parsing the sequence.
// Return Value:
// - True if handled successfully. False otherwise.
bool OutputStateMachineEngine::_IntermediateQuestionMarkDispatch(const wchar_t wchAction,
const std::basic_string_view<size_t> parameters)
{
bool success = false;
std::vector<DispatchTypes::PrivateModeParams> privateModeParams;
// Ensure that there was the right number of params
switch (wchAction)
{
case VTActionCodes::DECSET_PrivateModeSet:
case VTActionCodes::DECRST_PrivateModeReset:
success = _GetPrivateModeParams(parameters, privateModeParams);
break;
default:
// If no params to fill, param filling was successful.
success = true;
break;
}
if (success)
{
switch (wchAction)
{
case VTActionCodes::DECSET_PrivateModeSet:
success = _dispatch->SetPrivateModes({ privateModeParams.data(), privateModeParams.size() });
//TODO: MSFT:6367459 Add specific logging for each of the DECSET/DECRST codes
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECSET);
break;
case VTActionCodes::DECRST_PrivateModeReset:
success = _dispatch->ResetPrivateModes({ privateModeParams.data(), privateModeParams.size() });
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECRST);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
return success;
}
// Routine Description:
// - Handles actions that have an intermediate '!', such as DECSTR
// Arguments:
// - wch - Character to dispatch.
// Return Value:
// - True if handled successfully. False otherwise.
bool OutputStateMachineEngine::_IntermediateExclamationDispatch(const wchar_t wchAction)
{
bool success = false;
switch (wchAction)
{
case VTActionCodes::DECSTR_SoftReset:
success = _dispatch->SoftReset();
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECSTR);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
return success;
}
// Routine Description:
// - Handles actions that have an intermediate ' ' (0x20), such as DECSCUSR
// Arguments:
// - wch - Character to dispatch.
// - parameters - set of numeric parameters collected while parsing the sequence.
// Return Value:
// - True if handled successfully. False otherwise.
bool OutputStateMachineEngine::_IntermediateSpaceDispatch(const wchar_t wchAction,
const std::basic_string_view<size_t> parameters)
{
bool success = false;
DispatchTypes::CursorStyle cursorStyle = DefaultCursorStyle;
// Parse params
switch (wchAction)
{
case VTActionCodes::DECSCUSR_SetCursorStyle:
success = _GetCursorStyle(parameters, cursorStyle);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
// if param filling successful, try to dispatch
if (success)
{
switch (wchAction)
{
case VTActionCodes::DECSCUSR_SetCursorStyle:
success = _dispatch->SetCursorStyle(cursorStyle);
TermTelemetry::Instance().Log(TermTelemetry::Codes::DECSCUSR);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
return success;
}
// Routine Description:
// - Triggers the Clear action to indicate that the state machine should erase
// all internal state.
// Arguments:
// - <none>
// Return Value:
// - <none>
bool OutputStateMachineEngine::ActionClear() noexcept
{
// do nothing.
return true;
}
// Routine Description:
// - Triggers the Ignore action to indicate that the state machine should eat
// this character and say nothing.
// Arguments:
// - <none>
// Return Value:
// - <none>
bool OutputStateMachineEngine::ActionIgnore() noexcept
{
// do nothing.
return true;
}
// Routine Description:
// - Triggers the OscDispatch action to indicate that the listener should handle a control sequence.
// These sequences perform various API-type commands that can include many parameters.
// Arguments:
// - wch - Character to dispatch. This will be a BEL or ST char.
// - parameter - identifier of the OSC action to perform
// - string - OSC string we've collected. NOT null terminated.
// Return Value:
// - true if we handled the dispatch.
bool OutputStateMachineEngine::ActionOscDispatch(const wchar_t /*wch*/,
const size_t parameter,
const std::wstring_view string)
{
bool success = false;
std::wstring title;
size_t tableIndex = 0;
DWORD color = 0;
switch (parameter)
{
case OscActionCodes::SetIconAndWindowTitle:
case OscActionCodes::SetWindowIcon:
case OscActionCodes::SetWindowTitle:
success = _GetOscTitle(string, title);
break;
case OscActionCodes::SetColor:
success = _GetOscSetColorTable(string, tableIndex, color);
break;
case OscActionCodes::SetForegroundColor:
case OscActionCodes::SetBackgroundColor:
case OscActionCodes::SetCursorColor:
success = _GetOscSetColor(string, color);
break;
case OscActionCodes::ResetCursorColor:
// the console uses 0xffffffff as an "invalid color" value
color = 0xffffffff;
success = true;
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
if (success)
{
switch (parameter)
{
case OscActionCodes::SetIconAndWindowTitle:
case OscActionCodes::SetWindowIcon:
case OscActionCodes::SetWindowTitle:
success = _dispatch->SetWindowTitle(title);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCWT);
break;
case OscActionCodes::SetColor:
success = _dispatch->SetColorTableEntry(tableIndex, color);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCCT);
break;
case OscActionCodes::SetForegroundColor:
success = _dispatch->SetDefaultForeground(color);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCFG);
break;
case OscActionCodes::SetBackgroundColor:
success = _dispatch->SetDefaultBackground(color);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCBG);
break;
case OscActionCodes::SetCursorColor:
success = _dispatch->SetCursorColor(color);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCSCC);
break;
case OscActionCodes::ResetCursorColor:
success = _dispatch->SetCursorColor(color);
TermTelemetry::Instance().Log(TermTelemetry::Codes::OSCRCC);
break;
default:
// If no functions to call, overall dispatch was a failure.
success = false;
break;
}
}
// If we were unable to process the string, and there's a TTY attached to us,
// trigger the state machine to flush the string to the terminal.
if (_pfnFlushToTerminal != nullptr && !success)
{
success = _pfnFlushToTerminal();
}
_ClearLastChar();
return success;
}
// Routine Description:
// - Triggers the Ss3Dispatch action to indicate that the listener should handle
// a control sequence. These sequences perform various API-type commands
// that can include many parameters.
// Arguments:
// - wch - Character to dispatch.
// - parameters - set of numeric parameters collected while parsing the sequence.
// Return Value:
// - true iff we successfully dispatched the sequence.
bool OutputStateMachineEngine::ActionSs3Dispatch(const wchar_t /*wch*/,
const std::basic_string_view<size_t> /*parameters*/) noexcept
{
// The output engine doesn't handle any SS3 sequences.
_ClearLastChar();
return false;
}
// Routine Description:
// - Retrieves the listed graphics options to be applied in order to the "font style" of the next characters inserted into the buffer.
// Arguments:
// - parameters - The parameters to parse
// - options - Space that will be filled with valid options from the GraphicsOptions enum
// Return Value:
// - True if we successfully retrieved an array of valid graphics options from the parameters we've stored. False otherwise.
bool OutputStateMachineEngine::_GetGraphicsOptions(const std::basic_string_view<size_t> parameters,
std::vector<DispatchTypes::GraphicsOptions>& options) const
{
bool success = false;
if (parameters.empty())
{
options.push_back(DefaultGraphicsOption);
success = true;
}
else
{
for (const auto& p : parameters)
{
options.push_back((DispatchTypes::GraphicsOptions)p);
}
success = true;
}
// If we were unable to process the string, and there's a TTY attached to us,
// trigger the state machine to flush the string to the terminal.
if (_pfnFlushToTerminal != nullptr && !success)
{
success = _pfnFlushToTerminal();
}
return success;
}
// Routine Description:
// - Retrieves the erase type parameter for an upcoming operation.
// Arguments:
// - parameters - The parameters to parse
// - eraseType - Receives the erase type parameter
// Return Value:
// - True if we successfully pulled an erase type from the parameters we've stored. False otherwise.
bool OutputStateMachineEngine::_GetEraseOperation(const std::basic_string_view<size_t> parameters,
DispatchTypes::EraseType& eraseType) const noexcept
{
bool success = false; // If we have too many parameters or don't know what to do with the given value, return false.
eraseType = DefaultEraseType; // if we fail, just put the default type in.
if (parameters.empty())
{
// Empty parameter sequences should use the default
eraseType = DefaultEraseType;
success = true;
}
else if (parameters.size() == 1)
{
// If there's one parameter, attempt to match it to the values we accept.
const auto param = static_cast<DispatchTypes::EraseType>(til::at(parameters, 0));
switch (param)
{
case DispatchTypes::EraseType::ToEnd:
case DispatchTypes::EraseType::FromBeginning:
case DispatchTypes::EraseType::All:
case DispatchTypes::EraseType::Scrollback:
eraseType = param;
success = true;
break;
}
}
return success;
}
// Routine Description:
// - Retrieves a distance for a cursor operation from the parameter pool stored during Param actions.
// Arguments:
// - parameters - The parameters to parse
// - distance - Receives the distance
// Return Value:
// - True if we successfully pulled the cursor distance from the parameters we've stored. False otherwise.
bool OutputStateMachineEngine::_GetCursorDistance(const std::basic_string_view<size_t> parameters,
size_t& distance) const noexcept
{
bool success = false;
distance = DefaultCursorDistance;
if (parameters.empty())
{
// Empty parameter sequences should use the default
success = true;
}
else if (parameters.size() == 1)
{
// If there's one parameter, use it.
distance = til::at(parameters, 0);
success = true;
}
// Distances of 0 should be changed to 1.
if (distance == 0)
{
distance = DefaultCursorDistance;
}
return success;
}
// Routine Description:
// - Retrieves a distance for a scroll operation from the parameter pool stored during Param actions.
// Arguments:
// - parameters - The parameters to parse
// - distance - Receives the distance
// Return Value:
// - True if we successfully pulled the scroll distance from the parameters we've stored. False otherwise.
bool OutputStateMachineEngine::_GetScrollDistance(const std::basic_string_view<size_t> parameters,
size_t& distance) const noexcept
{
bool success = false;
distance = DefaultScrollDistance;
if (parameters.empty())
{
// Empty parameter sequences should use the default
success = true;
}
else if (parameters.size() == 1)
{
// If there's one parameter, use it.
distance = til::at(parameters, 0);
success = true;
}
// Distances of 0 should be changed to 1.
if (distance == 0)
{
distance = DefaultScrollDistance;
}
return success;
}
// Routine Description:
// - Retrieves a width for the console window from the parameter pool stored during Param actions.
// Arguments:
// - parameters - The parameters to parse
// - consoleWidth - Receives the width
// Return Value:
// - True if we successfully pulled the width from the parameters we've stored. False otherwise.
bool OutputStateMachineEngine::_GetConsoleWidth(const std::basic_string_view<size_t> parameters,
size_t& consoleWidth) const noexcept
{
bool success = false;
consoleWidth = DefaultConsoleWidth;
if (parameters.empty())
{
// Empty parameter sequences should use the default
success = true;
}
else if (parameters.size() == 1)
{
// If there's one parameter, use it.
consoleWidth = til::at(parameters, 0);
success = true;
}
// Distances of 0 should be changed to 80.
if (consoleWidth == 0)
{
consoleWidth = DefaultConsoleWidth;
}
return success;
}
// Routine Description: