-
Notifications
You must be signed in to change notification settings - Fork 64
/
refterm_example_terminal.c
1386 lines (1187 loc) · 50.2 KB
/
refterm_example_terminal.c
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
static terminal_buffer AllocateTerminalBuffer(int DimX, int DimY)
{
terminal_buffer Result = {0};
size_t TotalSize = sizeof(renderer_cell)*DimX*DimY;
Result.Cells = VirtualAlloc(0, TotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if(Result.Cells)
{
Result.DimX = DimX;
Result.DimY = DimY;
}
return Result;
}
static void DeallocateTerminalBuffer(terminal_buffer *Buffer)
{
if(Buffer && Buffer->Cells)
{
VirtualFree(Buffer->Cells, 0, MEM_RELEASE);
Buffer->DimX = Buffer->DimY = 0;
Buffer->Cells = 0;
}
}
static DWORD GetPipePendingDataCount(HANDLE Pipe)
{
DWORD Result = 0;
PeekNamedPipe(Pipe, 0, 0, 0, &Result, 0);
return Result;
}
static void UpdateLineEnd(example_terminal *Terminal, size_t ToP)
{
Terminal->Lines[Terminal->CurrentLineIndex].OnePastLastP = ToP;
}
static void LineFeed(example_terminal *Terminal, size_t AtP, size_t NextLineStart, glyph_props AtProps)
{
UpdateLineEnd(Terminal, AtP);
++Terminal->CurrentLineIndex;
if(Terminal->CurrentLineIndex >= Terminal->MaxLineCount)
{
Terminal->CurrentLineIndex = 0;
}
example_line *Line = Terminal->Lines + Terminal->CurrentLineIndex;
Line->FirstP = NextLineStart;
Line->OnePastLastP = NextLineStart;
Line->ContainsComplexChars = 0;
Line->StartingProps = AtProps;
if(Terminal->LineCount <= Terminal->CurrentLineIndex)
{
Terminal->LineCount = Terminal->CurrentLineIndex + 1;
}
}
static int IsInBounds(terminal_buffer *Buffer, terminal_point Point)
{
int Result = ((Point.X >= 0) && (Point.X < (int)Buffer->DimX) &&
(Point.Y >= 0) && (Point.Y < (int)Buffer->DimY));
return Result;
}
static uint32_t PackRGB(uint32_t R, uint32_t G, uint32_t B)
{
if(R > 255) R = 255;
if(G > 255) G = 255;
if(B > 255) B = 255;
uint32_t Result = ((B << 16) | (G << 8) | (R << 0));
return Result;
}
static int IsDigit(char Digit)
{
int Result = ((Digit >= '0') && (Digit <= '9'));
return Result;
}
static char PeekToken(source_buffer_range *Range, int Ordinal)
{
char Result = 0;
if(Ordinal < Range->Count)
{
Result = Range->Data[Ordinal];
}
return Result;
}
static char GetToken(source_buffer_range *Range)
{
char Result = 0;
if(Range->Count)
{
Result = Range->Data[0];
*Range = ConsumeCount(*Range, 1);
}
return Result;
}
static uint32_t ParseNumber(source_buffer_range *Range)
{
uint32_t Result = 0;
while(IsDigit(PeekToken(Range, 0)))
{
char Token = GetToken(Range);
Result = 10*Result + (Token - '0');
}
return Result;
}
static int AtEscape(source_buffer_range *Range)
{
int Result = ((PeekToken(Range, 0) == '\x1b') &&
(PeekToken(Range, 1) == '['));
return Result;
}
static int IsDirectCodepoint(wchar_t CodePoint)
{
int Result = ((CodePoint >= MinDirectCodepoint) &&
(CodePoint <= MaxDirectCodepoint));
return Result;
}
static renderer_cell *GetCell(terminal_buffer *Buffer, terminal_point Point)
{
renderer_cell *Result = IsInBounds(Buffer, Point) ? (Buffer->Cells + Point.Y*Buffer->DimX + Point.X) : 0;
return Result;
}
static void ClearCellCount(example_terminal *Terminal, int32_t Count, renderer_cell *Cell)
{
uint32_t Background = Terminal->DefaultBackgroundColor;
while(Count--)
{
Cell->GlyphIndex = 0;
Cell->Foreground = Background; // TODO(casey): Should be able to set this to 0, but need to make sure cache slot 0 never gets filled
Cell->Background = Background;
++Cell;
}
}
static void ClearLine(example_terminal *Terminal, terminal_buffer *Buffer, int32_t Y)
{
terminal_point Point = {0, Y};
if(IsInBounds(Buffer, Point))
{
ClearCellCount(Terminal, Buffer->DimX, GetCell(Buffer, Point));
}
}
static void Clear(example_terminal *Terminal, terminal_buffer *Buffer)
{
ClearCellCount(Terminal, Buffer->DimX*Buffer->DimY, Buffer->Cells);
}
static void AdvanceRowNoClear(example_terminal *Terminal, terminal_point *Point)
{
Point->X = 0;
++Point->Y;
if(Point->Y >= (int32_t)Terminal->ScreenBuffer.DimY)
{
Point->Y = 0;
}
}
static void AdvanceRow(example_terminal *Terminal, terminal_point *Point)
{
AdvanceRowNoClear(Terminal, Point);
ClearLine(Terminal, &Terminal->ScreenBuffer, Point->Y);
}
static void AdvanceColumn(example_terminal *Terminal, terminal_point *Point)
{
++Point->X;
if(Terminal->LineWrap && (Point->X >= (int32_t)Terminal->ScreenBuffer.DimX))
{
AdvanceRow(Terminal, Point);
}
}
static void SetCellDirect(gpu_glyph_index GPUIndex, glyph_props Props, renderer_cell *Dest)
{
Dest->GlyphIndex = GPUIndex.Value;
uint32_t Foreground = Props.Foreground;
uint32_t Background = Props.Background;
if(Props.Flags & TerminalCell_ReverseVideo)
{
Foreground = Props.Background;
Background = Props.Foreground;
}
if(Props.Flags & TerminalCell_Invisible)
{
Dest->GlyphIndex = 0;
}
Dest->Foreground = Foreground | (Props.Flags << 24);
Dest->Background = Background;
}
static void ClearProps(example_terminal *Terminal, glyph_props *Props)
{
Props->Foreground = Terminal->DefaultForegroundColor;
Props->Background = Terminal->DefaultBackgroundColor;
Props->Flags = 0;
}
static void ClearCursor(example_terminal *Terminal, cursor_state *Cursor)
{
Cursor->At.X = 0;
Cursor->At.Y = 0;
ClearProps(Terminal, &Cursor->Props);
}
static int ParseEscape(example_terminal *Terminal, source_buffer_range *Range, cursor_state *Cursor)
{
int MovedCursor = 0;
GetToken(Range);
GetToken(Range);
wchar_t Command = 0;
uint32_t ParamCount = 0;
uint32_t Params[8] = {0};
while((ParamCount < ArrayCount(Params)) && Range->Count)
{
char Token = PeekToken(Range, 0);
if(IsDigit(Token))
{
Params[ParamCount++] = ParseNumber(Range);
wchar_t Semi = GetToken(Range);
if(Semi != ';')
{
Command = Semi;
break;
}
}
else
{
Command = GetToken(Range);
}
}
switch(Command)
{
case 'H':
{
// NOTE(casey): Move cursor to X,Y position
Cursor->At.X = Params[1] - 1;
Cursor->At.Y = Params[0] - 1;
MovedCursor = 1;
} break;
case 'm':
{
// NOTE(casey): Set graphics mode
if(Params[0] == 0)
{
ClearProps(Terminal, &Cursor->Props);
}
if(Params[0] == 1) Cursor->Props.Flags |= TerminalCell_Bold;
if(Params[0] == 2) Cursor->Props.Flags |= TerminalCell_Dim;
if(Params[0] == 3) Cursor->Props.Flags |= TerminalCell_Italic;
if(Params[0] == 4) Cursor->Props.Flags |= TerminalCell_Underline;
if(Params[0] == 5) Cursor->Props.Flags |= TerminalCell_Blinking;
if(Params[0] == 7) Cursor->Props.Flags |= TerminalCell_ReverseVideo;
if(Params[0] == 8) Cursor->Props.Flags |= TerminalCell_Invisible;
if(Params[0] == 9) Cursor->Props.Flags |= TerminalCell_Strikethrough;
if((Params[0] == 38) && (Params[1] == 2)) Cursor->Props.Foreground = PackRGB(Params[2], Params[3], Params[4]);
if((Params[0] == 48) && (Params[1] == 2)) Cursor->Props.Background = PackRGB(Params[2], Params[3], Params[4]);
} break;
}
return MovedCursor;
}
static size_t GetLineLength(example_line *Line)
{
Assert(Line->OnePastLastP >= Line->FirstP);
size_t Result = Line->OnePastLastP - Line->FirstP;
return Result;
}
static void ParseLines(example_terminal *Terminal, source_buffer_range Range, cursor_state *Cursor)
{
/* TODO(casey): Currently, if the commit of line data _straddles_ a control code boundary
this code does not properly _stop_ the processing cursor. This can cause an edge case
where a VT code that splits a line _doesn't_ split the line as it should. To fix this
the ending code just needs to check to see if the reason it couldn't parse an escape
code in "AtEscape" was that it ran out of characters, and if so, don't advance the parser
past that point.
*/
__m128i Carriage = _mm_set1_epi8('\n');
__m128i Escape = _mm_set1_epi8('\x1b');
__m128i Complex = _mm_set1_epi8(0x80);
size_t SplitLineAtCount = 4096;
size_t LastP = Range.AbsoluteP;
while(Range.Count)
{
__m128i ContainsComplex = _mm_setzero_si128();
size_t Count = Range.Count;
if(Count > SplitLineAtCount) Count = SplitLineAtCount;
char *Data = Range.Data;
while(Count >= 16)
{
__m128i Batch = _mm_loadu_si128((__m128i *)Data);
__m128i TestC = _mm_cmpeq_epi8(Batch, Carriage);
__m128i TestE = _mm_cmpeq_epi8(Batch, Escape);
__m128i TestX = _mm_and_si128(Batch, Complex);
__m128i Test = _mm_or_si128(TestC, TestE);
int Check = _mm_movemask_epi8(Test);
if(Check)
{
int Advance = _tzcnt_u32(Check);
__m128i MaskX = _mm_loadu_si128((__m128i *)(OverhangMask + 16 - Advance));
TestX = _mm_and_si128(MaskX, TestX);
ContainsComplex = _mm_or_si128(ContainsComplex, TestX);
Count -= Advance;
Data += Advance;
break;
}
ContainsComplex = _mm_or_si128(ContainsComplex, TestX);
Count -= 16;
Data += 16;
}
Range = ConsumeCount(Range, Data - Range.Data);
Terminal->Lines[Terminal->CurrentLineIndex].ContainsComplexChars |=
_mm_movemask_epi8(ContainsComplex);
if(AtEscape(&Range))
{
size_t FeedAt = Range.AbsoluteP;
if(ParseEscape(Terminal, &Range, Cursor))
{
LineFeed(Terminal, FeedAt, FeedAt, Cursor->Props);
}
}
else
{
char Token = GetToken(&Range);
if(Token == '\n')
{
LineFeed(Terminal, Range.AbsoluteP, Range.AbsoluteP, Cursor->Props);
}
else if(Token < 0) // TODO(casey): Not sure what is a "combining char" here, really, but this is a rough test
{
Terminal->Lines[Terminal->CurrentLineIndex].ContainsComplexChars = 1;
}
}
UpdateLineEnd(Terminal, Range.AbsoluteP);
if(GetLineLength(&Terminal->Lines[Terminal->CurrentLineIndex]) > SplitLineAtCount)
{
LineFeed(Terminal, Range.AbsoluteP, Range.AbsoluteP, Cursor->Props);
}
}
}
static void ParseWithUniscribe(example_terminal *Terminal, source_buffer_range UTF8Range, cursor_state *Cursor)
{
/* TODO(casey): This code is absolutely horrible - Uniscribe is basically unusable as an API, because it doesn't support
a clean state-machine way of feeding things to it. So I don't even know how you would really use it in a way
that guaranteed you didn't have buffer-too-small problems. It's just horrible.
Plus, it is UTF16, which means we have to do an entire conversion here before we even call it :(
I would rather get rid of this function altogether and move to something that supports UTF8, because we never
actually want to have to do this garbage - it's just a giant hack for no reason. Basically everything in this
function should be replaced by something that can turn UTF8 into chunks that need to be rasterized together.
That's all we need here, and it could be done very efficiently with sensible code.
*/
example_partitioner *Partitioner = &Terminal->Partitioner;
DWORD Count = MultiByteToWideChar(CP_UTF8, 0, UTF8Range.Data, (DWORD)UTF8Range.Count,
Partitioner->Expansion, ArrayCount(Partitioner->Expansion));
wchar_t *Data = Partitioner->Expansion;
int ItemCount = 0;
ScriptItemize(Data, Count, ArrayCount(Partitioner->Items), &Partitioner->UniControl, &Partitioner->UniState, Partitioner->Items, &ItemCount);
int Segment = 0;
for(int ItemIndex = 0;
ItemIndex < ItemCount;
++ItemIndex)
{
SCRIPT_ITEM *Item = Partitioner->Items + ItemIndex;
Assert((DWORD)Item->iCharPos < Count);
DWORD StrCount = Count - Item->iCharPos;
if((ItemIndex + 1) < ItemCount)
{
Assert(Item[1].iCharPos >= Item[0].iCharPos);
StrCount = (Item[1].iCharPos - Item[0].iCharPos);
}
wchar_t *Str = Data + Item->iCharPos;
int IsComplex = (ScriptIsComplex(Str, StrCount, SIC_COMPLEX) == S_OK);
ScriptBreak(Str, StrCount, &Item->a, Partitioner->Log);
int SegCount = 0;
Partitioner->SegP[SegCount++] = 0;
for(uint32_t CheckIndex = 0;
CheckIndex < StrCount;
++CheckIndex)
{
SCRIPT_LOGATTR Attr = Partitioner->Log[CheckIndex];
int ShouldBreak = (Str[CheckIndex] == ' ');;
if(IsComplex)
{
ShouldBreak |= Attr.fSoftBreak;
}
else
{
ShouldBreak |= Attr.fCharStop;
}
if(ShouldBreak) Partitioner->SegP[SegCount++] = CheckIndex;
}
Partitioner->SegP[SegCount++] = StrCount;
int dSeg = 1;
int SegStart = 0;
int SegStop = SegCount - 1;
if(Item->a.fRTL || Item->a.fLayoutRTL)
{
dSeg = -1;
SegStart = SegCount - 2;
SegStop = -1;
}
for(int SegIndex = SegStart;
SegIndex != SegStop;
SegIndex += dSeg)
{
size_t Start = Partitioner->SegP[SegIndex];
size_t End = Partitioner->SegP[SegIndex + 1];
size_t ThisCount = (End - Start);
if(ThisCount)
{
wchar_t *Run = Str + Start;
wchar_t CodePoint = Run[0];
if((ThisCount == 1) && IsDirectCodepoint(CodePoint))
{
renderer_cell *Cell = GetCell(&Terminal->ScreenBuffer, Cursor->At);
if(Cell)
{
glyph_props Props = Cursor->Props;
if(Terminal->DebugHighlighting)
{
Props.Background = 0x00800000;
}
SetCellDirect(Terminal->ReservedTileTable[CodePoint - MinDirectCodepoint], Props, Cell);
}
AdvanceColumn(Terminal, &Cursor->At);
}
else
{
// TODO(casey): This wastes a lookup on the tile count.
// It should save the entry somehow, and roll it into the first cell.
int Prepped = 0;
glyph_hash RunHash = ComputeGlyphHash(2*ThisCount, (char unsigned *)Run, DefaultSeed);
glyph_dim GlyphDim = GetGlyphDim(&Terminal->GlyphGen, Terminal->GlyphTable, ThisCount, Run, RunHash);
for(uint32_t TileIndex = 0;
TileIndex < GlyphDim.TileCount;
++TileIndex)
{
renderer_cell *Cell = GetCell(&Terminal->ScreenBuffer, Cursor->At);
if(Cell)
{
glyph_hash TileHash = ComputeHashForTileIndex(RunHash, TileIndex);
glyph_state Entry = FindGlyphEntryByHash(Terminal->GlyphTable, TileHash);
if(Entry.FilledState != GlyphState_Rasterized)
{
if(!Prepped)
{
PrepareTilesForTransfer(&Terminal->GlyphGen, &Terminal->Renderer, ThisCount, Run, GlyphDim);
Prepped = 1;
}
TransferTile(&Terminal->GlyphGen, &Terminal->Renderer, TileIndex, Entry.GPUIndex);
UpdateGlyphCacheEntry(Terminal->GlyphTable, Entry.ID, GlyphState_Rasterized, Entry.DimX, Entry.DimY);
}
glyph_props Props = Cursor->Props;
if(Terminal->DebugHighlighting)
{
Props.Background = Segment ? 0x0008080 : 0x00000080;
Segment = !Segment;
}
SetCellDirect(Entry.GPUIndex, Props, Cell);
}
AdvanceColumn(Terminal, &Cursor->At);
}
}
}
}
}
}
static int ParseLineIntoGlyphs(example_terminal *Terminal, source_buffer_range Range,
cursor_state *Cursor, int ContainsComplexChars)
{
int CursorJumped = 0;
while(Range.Count)
{
// NOTE(casey): Eat all non-Unicode
char Peek = PeekToken(&Range, 0);
if((Peek == '\x1b') && AtEscape(&Range))
{
if(ParseEscape(Terminal, &Range, Cursor))
{
CursorJumped = 1;
}
}
else if(Peek == '\r')
{
GetToken(&Range);
Cursor->At.X = 0;
}
else if(Peek == '\n')
{
GetToken(&Range);
AdvanceRow(Terminal, &Cursor->At);
}
else if(ContainsComplexChars)
{
/* TODO(casey): Currently, if you have a long line that force-splits, it will not
recombine properly with Unicode. I _DO NOT_ think this should be fixed in
the line parser. Instead, the fix should be what should happen here to begin
with, which is that the glyph chunking should happen in a state machine,
NOT using buffer runs like Uniscribe does.
So I believe the _correct_ design here is that you have a state machine instead
of Uniscribe for complex grapheme clusters, and _that_ will "just work" here
as well as being much much faster than the current path, which is very slow
because of Uniscribe _and_ is limited to intermediate buffer sizes.
*/
// NOTE(casey): If it's not an escape, and this line contains fancy Unicode stuff,
// it's something we need to pass to a shaper to find out how it
// has to be segmented. Which sadly is Uniscribe at this point :(
// Putting something actually good in here would probably be a massive improvement.
// NOTE(casey): Scan for the next escape code (which Uniscribe helpfully totally fails to handle)
source_buffer_range SubRange = Range;
do
{
Range = ConsumeCount(Range, 1);
} while(Range.Count &&
(Range.Data[0] != '\n') &&
(Range.Data[0] != '\r') &&
(Range.Data[0] != '\x1b'));
// NOTE(casey): Pass the range between the escape codes to Uniscribe
SubRange.Count = Range.AbsoluteP - SubRange.AbsoluteP;
ParseWithUniscribe(Terminal, SubRange, Cursor);
}
else
{
// NOTE(casey): It's not an escape, and we know there are only simple characters on the line.
wchar_t CodePoint = GetToken(&Range);
renderer_cell *Cell = GetCell(&Terminal->ScreenBuffer, Cursor->At);
if(Cell)
{
gpu_glyph_index GPUIndex = {0};
if(IsDirectCodepoint(CodePoint))
{
GPUIndex = Terminal->ReservedTileTable[CodePoint - MinDirectCodepoint];
}
else
{
Assert(CodePoint <= 127);
glyph_hash RunHash = ComputeGlyphHash(2, (char unsigned *)&CodePoint, DefaultSeed);
glyph_state Entry = FindGlyphEntryByHash(Terminal->GlyphTable, RunHash);
if(Entry.FilledState != GlyphState_Rasterized)
{
PrepareTilesForTransfer(&Terminal->GlyphGen, &Terminal->Renderer, 1, &CodePoint, GetSingleTileUnitDim());
TransferTile(&Terminal->GlyphGen, &Terminal->Renderer, 0, Entry.GPUIndex);
UpdateGlyphCacheEntry(Terminal->GlyphTable, Entry.ID, GlyphState_Rasterized, Entry.DimX, Entry.DimY);
}
GPUIndex = Entry.GPUIndex;
}
SetCellDirect(GPUIndex, Cursor->Props, Cell);
}
AdvanceColumn(Terminal, &Cursor->At);
}
}
return CursorJumped;
}
static void CloseProcess(example_terminal *Terminal)
{
CloseHandle(Terminal->ChildProcess);
CloseHandle(Terminal->Legacy_WriteStdIn);
CloseHandle(Terminal->Legacy_ReadStdOut);
CloseHandle(Terminal->Legacy_ReadStdError);
CloseHandle(Terminal->FastPipe);
Terminal->ChildProcess = INVALID_HANDLE_VALUE;
Terminal->Legacy_WriteStdIn = INVALID_HANDLE_VALUE;
Terminal->Legacy_ReadStdOut = INVALID_HANDLE_VALUE;
Terminal->Legacy_ReadStdError = INVALID_HANDLE_VALUE;
Terminal->FastPipe = INVALID_HANDLE_VALUE;
}
static void KillProcess(example_terminal *Terminal)
{
if(Terminal->ChildProcess != INVALID_HANDLE_VALUE)
{
TerminateProcess(Terminal->ChildProcess, 0);
CloseProcess(Terminal);
}
}
static void AppendOutput(example_terminal *Terminal, char *Format, ...)
{
// TODO(casey): This is all garbage code. You need a checked printf here, and of
// course there isn't one of those. Ideally this would change over to using
// a real concatenator here, like with a #define system, but this is just
// a hack for now to do basic printing from the internal code.
source_buffer_range Dest = GetNextWritableRange(&Terminal->ScrollBackBuffer, LARGEST_AVAILABLE);
va_list ArgList;
va_start(ArgList, Format);
int Used = wvsprintfA(Dest.Data, Format, ArgList);
va_end(ArgList);
Dest.Count = Used;
CommitWrite(&Terminal->ScrollBackBuffer, Dest.Count);
ParseLines(Terminal, Dest, &Terminal->RunningCursor);
}
static int UpdateTerminalBuffer(example_terminal *Terminal, HANDLE FromPipe)
{
int Result = 0;
if(FromPipe != INVALID_HANDLE_VALUE)
{
Result = 1;
terminal_buffer *Term = &Terminal->ScreenBuffer;
DWORD PendingCount = GetPipePendingDataCount(FromPipe);
if(PendingCount)
{
source_buffer_range Dest = GetNextWritableRange(&Terminal->ScrollBackBuffer, PendingCount);
DWORD ReadCount = 0;
if(ReadFile(FromPipe, Dest.Data, (DWORD)Dest.Count, &ReadCount, 0))
{
Assert(ReadCount <= Dest.Count);
Dest.Count = ReadCount;
CommitWrite(&Terminal->ScrollBackBuffer, Dest.Count);
ParseLines(Terminal, Dest, &Terminal->RunningCursor);
}
}
else
{
DWORD Error = GetLastError();
if((Error == ERROR_BROKEN_PIPE) ||
(Error == ERROR_INVALID_HANDLE))
{
Result = 0;
}
}
}
return Result;
}
static void LayoutLines(example_terminal *Terminal)
{
// TODO(casey): Probably want to do something better here - this over-clears, since we clear
// the whole thing and then also each line, for no real reason other than to make line wrapping
// simpler.
Clear(Terminal, &Terminal->ScreenBuffer);
//
// TODO(casey): This code is super bad, and there's no need for it to keep repeating itself.
//
// TODO(casey): How do we know how far back to go, for control chars?
int32_t LineCount = 2*Terminal->ScreenBuffer.DimY;
int32_t LineOffset = Terminal->CurrentLineIndex + Terminal->ViewingLineOffset - LineCount;
int CursorJumped = 0;
cursor_state Cursor = {0};
ClearCursor(Terminal, &Cursor);
for(int32_t LineIndexIndex = 0;
LineIndexIndex < LineCount;
++LineIndexIndex)
{
int32_t LineIndex = (LineOffset + LineIndexIndex) % Terminal->MaxLineCount;
if(LineIndex < 0) LineIndex += Terminal->MaxLineCount;
example_line Line = Terminal->Lines[LineIndex];
source_buffer_range Range = ReadSourceAt(&Terminal->ScrollBackBuffer, Line.FirstP, Line.OnePastLastP - Line.FirstP);
Cursor.Props = Line.StartingProps;
if(ParseLineIntoGlyphs(Terminal, Range, &Cursor, Line.ContainsComplexChars))
{
CursorJumped = 1;
}
}
if(CursorJumped)
{
Cursor.At.X = 0;
Cursor.At.Y = Terminal->ScreenBuffer.DimY - 4;
}
AdvanceRow(Terminal, &Cursor.At);
ClearProps(Terminal, &Cursor.Props);
#if 0
uint32_t CLCount = Terminal->CommandLineCount;
source_buffer_range CommandLineRange = {0};
CommandLineRange.AbsoluteP = 0;
CommandLineRange.Count = CLCount;
CommandLineRange.Data = (char *)Terminal->CommandLine;
#else
#endif
char Prompt[] = {'>', ' '};
source_buffer_range PromptRange = {0};
PromptRange.Count = ArrayCount(Prompt);
PromptRange.Data = Prompt;
ParseLineIntoGlyphs(Terminal, PromptRange, &Cursor, 0);
source_buffer_range CommandLineRange = {0};
CommandLineRange.Count = Terminal->CommandLineCount;
CommandLineRange.Data = Terminal->CommandLine;
ParseLineIntoGlyphs(Terminal, CommandLineRange, &Cursor, 1);
char CursorCode[] = {'\x1b', '[', '5', 'm', 0xe2, 0x96, 0x88};
source_buffer_range CursorRange = {0};
CursorRange.Count = ArrayCount(CursorCode);
CursorRange.Data = CursorCode;
ParseLineIntoGlyphs(Terminal, CursorRange, &Cursor, 1);
AdvanceRowNoClear(Terminal, &Cursor.At);
Terminal->ScreenBuffer.FirstLineY = CursorJumped ? 0 : Cursor.At.Y;
}
static int ExecuteSubProcess(example_terminal *Terminal, char *ProcessName, char *ProcessCommandLine)
{
if(Terminal->ChildProcess != INVALID_HANDLE_VALUE)
{
KillProcess(Terminal);
}
PROCESS_INFORMATION ProcessInfo = {0};
STARTUPINFOA StartupInfo = {sizeof(StartupInfo)};
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
SECURITY_ATTRIBUTES Inherit = {sizeof(Inherit)};
Inherit.bInheritHandle = TRUE;
CreatePipe(&StartupInfo.hStdInput, &Terminal->Legacy_WriteStdIn, &Inherit, 0);
CreatePipe(&Terminal->Legacy_ReadStdOut, &StartupInfo.hStdOutput, &Inherit, 0);
CreatePipe(&Terminal->Legacy_ReadStdError, &StartupInfo.hStdError, &Inherit, 0);
SetHandleInformation(Terminal->Legacy_WriteStdIn, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(Terminal->Legacy_ReadStdOut, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(Terminal->Legacy_ReadStdError, HANDLE_FLAG_INHERIT, 0);
int Result = 0;
char *ProcessDir = ".\\";
if(CreateProcessA(
ProcessName,
ProcessCommandLine,
0,
0,
TRUE,
CREATE_NO_WINDOW|CREATE_SUSPENDED,
0,
ProcessDir,
&StartupInfo,
&ProcessInfo))
{
if(Terminal->EnableFastPipe)
{
wchar_t PipeName[64];
wsprintfW(PipeName, L"\\\\.\\pipe\\fastpipe%x", ProcessInfo.dwProcessId);
Terminal->FastPipe = CreateNamedPipeW(PipeName, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED, 0, 1,
Terminal->PipeSize, Terminal->PipeSize, 0, 0);
// TODO(casey): Should give this its own event / overlapped
ConnectNamedPipe(Terminal->FastPipe, &Terminal->FastPipeTrigger);
DWORD Error = GetLastError();
Assert(Error == ERROR_IO_PENDING);
}
ResumeThread(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hThread);
Terminal->ChildProcess = ProcessInfo.hProcess;
Result = 1;
}
CloseHandle(StartupInfo.hStdInput);
CloseHandle(StartupInfo.hStdOutput);
CloseHandle(StartupInfo.hStdError);
return Result;
}
static int StringsAreEqual(char *A, char *B)
{
if(A && B)
{
while(*A && *B && (*A == *B))
{
++A;
++B;
}
return(*A == *B);
}
else
{
return(A == B);
}
}
static void
RevertToDefaultFont(example_terminal *Terminal)
{
#if 0
wsprintfW(Terminal->RequestedFontName, L"%s", L"Courier New");
Terminal->RequestedFontHeight = 25;
#else
wsprintfW(Terminal->RequestedFontName, L"%s", L"Consolas");
Terminal->RequestedFontHeight = 17;
#endif
}
static int
RefreshFont(example_terminal *Terminal)
{
int Result = 0;
//
// NOTE(casey): Set up the mapping table between run-hashes and glyphs
//
glyph_table_params Params = {0};
// NOTE(casey): An additional tile is reserved for position 0, so it can be "empty",
// in case the space glyph is not actually empty.
Params.ReservedTileCount = ArrayCount(Terminal->ReservedTileTable) + 1;
// NOTE(casey): We have to shrink the font size until it fits in the glyph texture,
// to prevent large fonts from overflowing.
for(int Try = 0; Try <= 1; ++Try)
{
Result = SetFont(&Terminal->GlyphGen, Terminal->RequestedFontName, Terminal->RequestedFontHeight);
if(Result)
{
Params.CacheTileCountInX = SafeRatio1(Terminal->REFTERM_TEXTURE_WIDTH, Terminal->GlyphGen.FontWidth);
Params.EntryCount = GetExpectedTileCountForDimension(&Terminal->GlyphGen, Terminal->REFTERM_TEXTURE_WIDTH, Terminal->REFTERM_TEXTURE_HEIGHT);
Params.HashCount = 4096;
if(Params.EntryCount > Params.ReservedTileCount)
{
Params.EntryCount -= Params.ReservedTileCount;
break;
}
}
RevertToDefaultFont(Terminal);
}
// TODO(casey): In theory, this VirtualAlloc could fail, so it may be a better idea to
// just use a reserved memory footprint here and always use the same size. It is not
// a very large amount of memory, so picking a maximum and sticking with it is probably
// better. You can cap the size of the cache and there is no real penalty for doing
// that, since it's a cache, so it'd just be a better idea all around.
if(Terminal->GlyphTableMem)
{
VirtualFree(Terminal->GlyphTableMem, 0, MEM_RELEASE);
Terminal->GlyphTableMem = 0;
}
Terminal->GlyphTableMem = VirtualAlloc(0, GetGlyphTableFootprint(Params), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
Terminal->GlyphTable = PlaceGlyphTableInMemory(Params, Terminal->GlyphTableMem);
InitializeDirectGlyphTable(Params, Terminal->ReservedTileTable, 1);
//
// NOTE(casey): Pre-rasterize all the ASCII characters, since they are directly mapped rather than hash-mapped.
//
glyph_dim UnitDim = GetSingleTileUnitDim();
for(uint32_t TileIndex = 0;
TileIndex < ArrayCount(Terminal->ReservedTileTable);
++TileIndex)
{
wchar_t Letter = MinDirectCodepoint + TileIndex;
PrepareTilesForTransfer(&Terminal->GlyphGen, &Terminal->Renderer, 1, &Letter, UnitDim);
TransferTile(&Terminal->GlyphGen, &Terminal->Renderer, 0, Terminal->ReservedTileTable[TileIndex]);
}
// NOTE(casey): Clear the reserved 0 tile
wchar_t Nothing = 0;
gpu_glyph_index ZeroTile = {0};
PrepareTilesForTransfer(&Terminal->GlyphGen, &Terminal->Renderer, 0, &Nothing, UnitDim);
TransferTile(&Terminal->GlyphGen, &Terminal->Renderer, 0, ZeroTile);
return Result;
}
static void ExecuteCommandLine(example_terminal *Terminal)
{
// TODO(casey): All of this is complete garbage and should never ever be used.
Terminal->CommandLine[Terminal->CommandLineCount] = 0;
uint32_t ParamStart = 0;
while(ParamStart < Terminal->CommandLineCount)
{
if(Terminal->CommandLine[ParamStart] == ' ') break;
++ParamStart;
}
char *A = Terminal->CommandLine;
char *B = Terminal->CommandLine + ParamStart;
*B = 0;
if(ParamStart < Terminal->CommandLineCount)
{
++ParamStart;
++B;
}
source_buffer_range ParamRange = {0};
ParamRange.Data = B;
ParamRange.Count = Terminal->CommandLineCount - ParamStart;
// TODO(casey): Collapse all these options into a little array, so there's no
// copy-pasta.
AppendOutput(Terminal, "\n");
if(StringsAreEqual(Terminal->CommandLine, "status"))
{
ClearProps(Terminal, &Terminal->RunningCursor.Props);
AppendOutput(Terminal, "RefTerm v%u\n", REFTERM_VERSION);
AppendOutput(Terminal, "Size: %u x %u\n", Terminal->ScreenBuffer.DimX, Terminal->ScreenBuffer.DimY);
AppendOutput(Terminal, "Fast pipe: %s\n", Terminal->EnableFastPipe ? "ON" : "off");
AppendOutput(Terminal, "Font: %S %u\n", Terminal->RequestedFontName, Terminal->RequestedFontHeight);
AppendOutput(Terminal, "Line Wrap: %s\n", Terminal->LineWrap ? "ON" : "off");
AppendOutput(Terminal, "Debug: %s\n", Terminal->DebugHighlighting ? "ON" : "off");
AppendOutput(Terminal, "Throttling: %s\n", !Terminal->NoThrottle ? "ON" : "off");
}
else if(StringsAreEqual(Terminal->CommandLine, "fastpipe"))
{
Terminal->EnableFastPipe = !Terminal->EnableFastPipe;
AppendOutput(Terminal, "Fast pipe: %s\n", Terminal->EnableFastPipe ? "ON" : "off");
}
else if(StringsAreEqual(Terminal->CommandLine, "linewrap"))
{
Terminal->LineWrap = !Terminal->LineWrap;
AppendOutput(Terminal, "LineWrap: %s\n", Terminal->LineWrap ? "ON" : "off");
}
else if(StringsAreEqual(Terminal->CommandLine, "debug"))
{
Terminal->DebugHighlighting = !Terminal->DebugHighlighting;
AppendOutput(Terminal, "Debug: %s\n", Terminal->DebugHighlighting ? "ON" : "off");
}
else if(StringsAreEqual(Terminal->CommandLine, "throttle"))
{