-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCrt.cs
1999 lines (1849 loc) · 79 KB
/
Crt.cs
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
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Collections.Generic;
namespace RandM.RMLib
{
/// <summary>
/// A static class for manipulating a console window
/// Compatibility with the Borland Pascal CRT unit was attempted, along with a few new additions
/// </summary>
public static class Crt
{
/* Private variables */
private static Queue<char> _KeyBuf = new Queue<char>();
private static object _Lock = new object();
private static int _NormAttr = LightGray;
private static NativeMethods.COORD _ScreenSize = new NativeMethods.COORD();
private static IntPtr _StdInputHandle = IntPtr.Zero;
private static IntPtr _StdOutputHandle = IntPtr.Zero;
private static int _TextAttr = LightGray;
private static int _TextMode = CO80;
/* Color Constants
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Use these color constants with SetPalette, SetAllPalette, TextColor, and
TextBackground:
*/
public const int Black = 0;
public const int Blue = 1;
public const int Green = 2;
public const int Cyan = 3;
public const int Red = 4;
public const int Magenta = 5;
public const int Brown = 6;
public const int LightGray = 7;
public const int DarkGray = 8;
public const int LightBlue = 9;
public const int LightGreen = 10;
public const int LightCyan = 11;
public const int LightRed = 12;
public const int LightMagenta = 13;
public const int Yellow = 14;
public const int White = 15;
public const int Blink = 128;
/* Constants Used as Parameters to TextMode (Crt unit)
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
public const int BW40 = 0; // 40x25 B/W ¦ CGA
public const int CO40 = 1; // 40x25 Color ¦ CGA
public const int BW80 = 2; // 80x25 B/W ¦ CGA
public const int CO80 = 3; // 80x25 Color ¦ CGA
public const int Mono = 7; // 80x25 B/W on MDA ¦ HGC
public const int Font8x8 = 256; // 43-/50-line mode ¦ EGA/VGA
public const int C40 = CO40; // 3.0 compatibility
public const int C80 = CO80; // 3.0 compatibility
#region ReadKey() Key Mappings
private static int[] TKeys = {
0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 13, 0, 0, // 0..15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, // 16..31
32, 1073, 1081, 1079, 1071, 1075, 1072, 1077, 1080, 0, 0, 0, 0, 1082, 1083, 0, // 32..47
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 0, 0, 0, 0, 0, // 48..63
0, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, // 64..79
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 0, 0, 0, 0, 0, // 80..95
1082, 1079, 1080, 1081, 1075, 1076, 1077, 1071, 1072, 1073, 42, 43, 0, 45, 46, 47, // 96..111
1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1133, 1134, 0, 0, 0, 0, // 112..127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128..143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144..159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160..175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 61, 44, 45, 46, 47, // 176..191
96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192..207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 92, 93, 39, 0, // 208..223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223..239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 240..255
private static int[] TAltKeys = {
0, 0, 0, 0, 0, 0, 0, 0, 1008, 0, 0, 0, 0, 0, 0, 0, // 0..15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16..31
0, 1153, 1161, 1159, 1151, 1155, 1152, 1157, 1160, 0, 0, 0, 0, 1162, 1163, 0, // 32..47
1129, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 0, 0, 0, 0, 0, 0, // 48..63
0, 1030, 1048, 1046, 1032, 1018, 1033, 1034, 1035, 1023, 1036, 1037, 1038, 1050, 1049, 1024, // 64..79
1025, 1016, 1019, 1031, 1020, 1022, 1047, 1017, 1045, 1021, 1044, 0, 0, 0, 0, 0, // 80..95
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1055, 1078, 0, 1074, 0, 1164, // 96..111
1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1139, 1140, 0, 0, 0, 0, // 112..127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128..143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144..159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160..175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1039, 1131, 1051, 1130, 1052, 1053, // 176..191
1041, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192..207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1026, 1043, 1027, 1040, 0, // 208..223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223..239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 240..255
private static int[] TCtrlKeys = {
0, 0, 0, 0, 0, 0, 0, 0, 127, 1148, 0, 0, 0, 10, 0, 0, // 0..15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16..31
32, 1132, 1118, 1117, 1119, 1115, 1141, 1116, 1145, 0, 0, 0, 0, 1004, 1006, 0, // 32..47
0, 0, 1003, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 48..63
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 64..79
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 0, // 80..95
1004, 1117, 1145, 1118, 1115, 1143, 1116, 1119, 1141, 1132, 1150, 1144, 0, 1142, 1006, 1149, // 96..111
1094, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1137, 1138, 0, 0, 0, 0, // 112..127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128..143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144..159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160..175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 0, 0, // 176..191
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192..207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 28, 29, 0, 0, // 208..223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223..239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 240..255
private static int[] TShiftKeys = {
0, 0, 0, 0, 0, 0, 0, 0, 8, 1015, 0, 0, 0, 13, 0, 0, // 0..15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, // 16..31
32, 1073, 1081, 1079, 1071, 1075, 1072, 1077, 1080, 0, 0, 0, 0, 1005, 1007, 0, // 32..47
41, 33, 64, 35, 36, 37, 94, 38, 42, 40, 0, 0, 0, 0, 0, 0, // 48..63
0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 64..79
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 0, 0, 0, 0, 0, // 80..95
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 42, 43, 0, 45, 46, 47, // 96..111
1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1135, 1136, 0, 0, 0, 0, // 112..127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128..143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144..159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160..175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 43, 60, 95, 62, 63, // 176..191
126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192..207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 124, 125, 34, 0, // 208..223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223..239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 240..255
#endregion
static Crt()
{
CheckBreak = true;
CheckEof = false;
DirectVideo = true;
LastMode = CO80;
WindMax = 79 + (24 << 8);
WindMin = 0;
// Get the standard input/output handles
if (OSUtils.IsWindows)
{
_StdInputHandle = NativeMethods.GetStdHandle(NativeMethods.STD_INPUT_HANDLE);
_StdOutputHandle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
}
// Get the console screen buffer info and eliminate the scroll buffer
if (OSUtils.IsWindows)
{
NativeMethods.CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo = new NativeMethods.CONSOLE_SCREEN_BUFFER_INFO();
NativeMethods.GetConsoleScreenBufferInfo(_StdOutputHandle, out ConsoleScreenBufferInfo);
_ScreenSize.X = (short)(ConsoleScreenBufferInfo.srWindow.Right - ConsoleScreenBufferInfo.srWindow.Left + 1);
_ScreenSize.Y = (short)(ConsoleScreenBufferInfo.srWindow.Bottom - ConsoleScreenBufferInfo.srWindow.Top + 1);
NativeMethods.SetConsoleScreenBufferSize(_StdOutputHandle, _ScreenSize);
}
else
{
try
{
_ScreenSize.X = (short)Console.WindowWidth;
_ScreenSize.Y = (short)Console.WindowHeight;
Console.SetBufferSize(_ScreenSize.X, _ScreenSize.Y);
}
catch (NotImplementedException)
{
// Can't do that
}
}
// Find the WindMin/WindMax records
WindMin = 0;
WindMax = (_ScreenSize.X - 1) | ((_ScreenSize.Y - 1) << 8);
// Determine the starting text attribute
_NormAttr = GetAttrAt(WhereX(), WhereY());
// Initialize the text attribute
NormVideo();
}
/// <summary>
/// Associates a text file with the CRT window.
/// </summary>
/// <remarks>
/// AssignCrt works exactly like the Assign standard procedure except that no
/// file name is specified. Instead, the text file is associated with the CRT.
///
/// This allows faster output (and input) than would normally be possible using
/// standard output (or input).
/// </remarks>
/// <param name="f">The text file to associate with the CRT window</param>
public static void AssignCrt(object f)
{
lock (_Lock)
{
// Not going to implement
}
}
/// <summary>
/// Controls user termination of an application using the CRT window.
/// </summary>
/// <remarks>
/// When CheckBreak is True, the user can terminate the application at any time
/// by
/// - choosing the Close command on the CRT window's Control menu
/// - double-clicking the window's Control-menu box
/// - pressing Alt+F4
/// pressing Ctrl-Break
///
/// The user can also press Ctrl+C or Ctrl+Break at any time to halt the
/// application and force the CRT window into its inactive state.
///
/// All of these features are disabled when CheckBreak is False.
///
/// At run time, Crt stores the old Ctrl-Break interrupt vector, $1B, in a
/// global pointer called SaveInt1B.
/// </remarks>
public static bool CheckBreak { get; set; }
/// <summary>
/// Controls the end-of-file character checking in the CRT window.
/// </summary>
/// <remarks>
/// When CheckEOF is True, an end-of-file marker is generated when the user
/// presses Ctrl+Z while reading from a file assigned to the CRT window.
///
/// When CheckEOF is False, pressing Ctrl+Z has no effect.
///
/// CheckEOF is False by default.
/// </remarks>
public static bool CheckEof { get; set; }
/// <summary>
/// Clears all characters from the cursor position to the start of the line
/// without moving the cursor.
/// </summary>
/// <remarks>
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the current cursor
/// position to the left edge becomes the background color.
///
/// ClrBol is window-relative.
/// </remarks>
public static void ClrBol()
{
lock (_Lock)
{
FastWrite(new string(' ', WhereX()), WindMinX + 1, WhereYA(), _TextAttr);
}
}
/// <summary>
/// Clears the active window from the cursor's current line to the start of the window
/// </summary>
/// <remarks>
/// Sets all character positions from the cursor's current line to the start of the window
/// to blanks with the currently defined text attributes. Thus, if TextBackground is not
/// black, the entire screen becomes the background color. This also applies to characters
/// cleared by ClrEol, InsLine, and DelLine, and to empty lines created by scrolling.
///
/// ClrBos is window-relative.
/// </remarks>
public static void ClrBos()
{
lock (_Lock)
{
// Clear rows before current row
ScrollUpWindow(WhereY() - 1);
ScrollDownWindow(WhereY() - 1);
// Clear start of current row
ClrBol();
}
}
/// <summary>
/// Clears all characters from the cursor position to the end of the line
/// without moving the cursor.
/// </summary>
/// <remarks>
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the current cursor
/// position to the right edge becomes the background color.
///
/// ClrEol is window-relative.
/// </remarks>
public static void ClrEol()
{
lock (_Lock)
{
FastWrite(new string(' ', (WindMaxX + 1) - WhereX() + 1), WhereXA(), WhereYA(), _TextAttr);
}
}
/// <summary>
/// Clears the active window from the cursor's current line to the end of the window
/// </summary>
/// <remarks>
/// Sets all character positions from the cursor's current line to the end of the window
/// to blanks with the currently defined text attributes. Thus, if TextBackground is not
/// black, the entire screen becomes the background color. This also applies to characters
/// cleared by ClrEol, InsLine, and DelLine, and to empty lines created by scrolling.
///
/// ClrEos is window-relative.
/// </remarks>
public static void ClrEos()
{
lock (_Lock)
{
// Clear rows after current row
ScrollDownWindow(WindRows - WhereY());
ScrollUpWindow(WindRows - WhereY());
// Clear rest of current row
ClrEol();
}
}
/// <summary>
/// Clears all characters from the cursor position's current line
/// without moving the cursor.
/// </summary>
/// <remarks>
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the current cursor
/// position's line becomes the background color.
///
/// ClrLine is window-relative.
/// </remarks>
public static void ClrLine()
{
lock (_Lock)
{
FastWrite(new string(' ', WindCols), WindMinX + 1, WhereYA(), _TextAttr);
}
}
/// <summary>
/// Clears the active windows and returns the cursor to the upper-left corner.
/// </summary>
/// <remarks>
/// Sets all character positions to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the entire screen becomes
/// the background color. This also applies to characters cleared by ClrEol,
/// InsLine, and DelLine, and to empty lines created by scrolling.
///
/// ClrScr is window-relative.
/// </remarks>
public static void ClrScr()
{
lock (_Lock)
{
if (OSUtils.IsWindows)
{
ScrollUpWindow(WindRows);
}
else
{
try
{
Console.Clear();
}
catch (NotImplementedException)
{
// Can't do that
}
}
GotoXY(1, 1);
}
}
/// <summary>
/// Sets the foreground colour to be the same as the background colour
/// </summary>
public static void Conceal()
{
lock (_Lock)
{
TextColor((_TextAttr & 0xF0) >> 4);
}
}
/// <summary>
/// Delays a specified number of milliseconds.
/// </summary>
/// <remarks>
/// Ms specifies the number of milliseconds to wait.
///
/// Delay is an approximation, so the delay period will not last exactly Ms
/// milliseconds.
/// </remarks>
/// <param name="milliseconds">The number of milliseconds to wait</param>
public static void Delay(int milliseconds)
{
Thread.Sleep(milliseconds);
}
public static void DelChar()
{
DelChar(1);
}
public static void DelChar(int count)
{
for (int i = WhereXA(); i <= WindMinX + WindCols - count; i++)
{
FastWrite(GetCharAt(i + count, WhereYA()).ToString(), i, WhereYA(), GetAttrAt(i + count, WhereYA()));
}
for (int i = WindMinX + WindCols + 1 - count; i <= WindMinX + WindCols; i++)
{
FastWrite(" ", i, WhereYA(), _TextAttr);
}
}
/// <summary>
/// Deletes the line containing the cursor.
/// </summary>
/// <remarks>
/// The line containing the cursor is deleted, and all lines below are moved one
/// line up (using the BIOS scroll routine). A new line is added at the bottom.
///
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the new line becomes the
/// background color.
/// </remarks>
public static void DelLine()
{
lock (_Lock)
{
DelLine(1);
}
}
/// <summary>
/// Deletes the line containing the cursor.
/// </summary>
/// <remarks>
/// The line containing the cursor is deleted, and all lines below are moved one
/// line up (using the BIOS scroll routine). A new line is added at the bottom.
///
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the new line becomes the
/// background color.
/// </remarks>
/// <param name="count">The number of lines to delete</param>
public static void DelLine(int count)
{
lock (_Lock)
{
ScrollUpCustom(WindMinX + 1, WhereYA(), WindMaxX + 1, WindMaxY + 1, count, ' ', _TextAttr);
}
}
/// <summary>
/// Enables and disables direct memory access for Write and WriteLn statements
/// that output to the screen
/// </summary>
/// <remarks>
/// When DirectVideo is True, Writes and WriteLns to files associated with the
/// CRT will store characters directly in video memory, instead of calling the
/// BIOS to display them.
///
/// When DirectVideo is False, all characters are written through BIOS calls, a
/// significantly slower process.
///
/// DirectVideo always defaults to True.
///
/// If you want characters displayed through BIOS calls, set DirectVideo to
/// False at the beginning of your program and after each call to TextMode.
/// </remarks>
public static bool DirectVideo { get; set; }
public static void FastWrite(string text, int column, int row, int foregroundColour, int backgroundColour)
{
lock (_Lock)
{
FastWrite(text, column, row, foregroundColour | (backgroundColour << 4));
}
}
/// <summary>
/// Writes a string of text at the desired X/Y coordinate with the given text attribute.
///
/// FastWrite is not window-relative, and it does not wrap text that goes beyond the right edge of the screen.
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="column">The 1-based column to start the text</param>
/// <param name="row">The 1-based row to start the text</param>
/// <param name="attribute">The text attribute to colour the text</param>
public static void FastWrite(string text, int column, int row, int attribute)
{
lock (_Lock)
{
if ((column <= _ScreenSize.X) && (row <= _ScreenSize.Y))
{
if (OSUtils.IsWindows)
{
NativeMethods.CHAR_INFO[] Buffer = new NativeMethods.CHAR_INFO[text.Length];
for (int i = 0; i < text.Length; i++)
{
Buffer[i].Attributes = (ushort)attribute;
Buffer[i].AsciiChar = text[i];
}
NativeMethods.COORD BufferCoord = new NativeMethods.COORD();
BufferCoord.X = 0;
BufferCoord.Y = 0;
NativeMethods.COORD BufferSize = new NativeMethods.COORD();
BufferSize.X = (short)text.Length;
BufferSize.Y = 1;
NativeMethods.SMALL_RECT WriteRegion = new NativeMethods.SMALL_RECT();
WriteRegion.Bottom = (short)(row - 1);
WriteRegion.Left = (short)(column - 1);
WriteRegion.Right = (short)((column - 1) + text.Length);
WriteRegion.Top = (short)(row - 1);
NativeMethods.WriteConsoleOutput(_StdOutputHandle, Buffer, BufferSize, BufferCoord, ref WriteRegion);
}
else
{
try
{
int OldX = Console.CursorLeft;
int OldY = Console.CursorTop;
ConsoleColor OldFore = Console.ForegroundColor;
ConsoleColor OldBack = Console.BackgroundColor;
Console.SetCursorPosition(column - 1, row - 1);
//Console.ForegroundColor =
GetConsoleColour(attribute % 16);
//Console.BackgroundColor =
GetConsoleColour(attribute / 16);
Console.Write(text);
// TODO Keep text in buffer for SaveScreen() and RestoreScreen()
Console.SetCursorPosition(OldX, OldY);
Console.ForegroundColor = OldFore;
Console.BackgroundColor = OldBack;
}
catch (NotImplementedException)
{
// Can't do that
}
}
}
}
}
public static void FillScreen(char ch)
{
string Line = new string(ch, ScreenCols);
for (int Y = 1; Y <= ScreenRows; Y++)
{
FastWrite(Line, 1, Y, _TextAttr);
}
}
/// <summary>
/// Retrieves the text attribute at the given X/Y coordinate.
///
/// GetAttrAt is not window-relative.
/// </summary>
/// <param name="column">The 1-based column to look at</param>
/// <param name="row">The 1-based row to look at</param>
/// <returns>The text attribute at the given X/Y coordinate</returns>
public static int GetAttrAt(int column, int row)
{
lock (_Lock)
{
if ((column <= _ScreenSize.X) && (row <= _ScreenSize.Y))
{
if (OSUtils.IsWindows)
{
NativeMethods.COORD ReadCoord = new NativeMethods.COORD();
ReadCoord.X = (short)(column - 1);
ReadCoord.Y = (short)(row - 1);
ushort[] Attribute = new ushort[1];
uint NumberOfAttrsRead = 0;
NativeMethods.ReadConsoleOutputAttribute(_StdOutputHandle, Attribute, 1, ReadCoord, out NumberOfAttrsRead);
return Attribute[0];
}
else
{
return LightGray;
}
}
else
{
return LightGray;
}
}
}
/// <summary>
/// Retrieves the character at the given X/Y coordinate.
///
/// GetCharAt is not window-relative.
/// </summary>
/// <param name="column">The 1-based column to look at</param>
/// <param name="row">The 1-based row to look at</param>
/// <returns>The character at the given X/Y coordinate</returns>
public static char GetCharAt(int column, int row)
{
lock (_Lock)
{
if ((column <= _ScreenSize.X) && (row <= _ScreenSize.Y))
{
if (OSUtils.IsWindows)
{
NativeMethods.COORD ReadCoord = new NativeMethods.COORD();
ReadCoord.X = (short)(column - 1);
ReadCoord.Y = (short)(row - 1);
StringBuilder Character = new StringBuilder();
uint NumberOfCharsRead = 0;
NativeMethods.ReadConsoleOutputCharacter(_StdOutputHandle, Character, 1, ReadCoord, out NumberOfCharsRead);
return Character[0];
}
else
{
return ' ';
}
}
else
{
return ' ';
}
}
}
private static ConsoleColor GetConsoleColour(int AColour)
{
lock (_Lock)
{
switch (AColour)
{
case 0: return ConsoleColor.Black;
case 1: return ConsoleColor.DarkBlue;
case 2: return ConsoleColor.DarkGreen;
case 3: return ConsoleColor.DarkCyan;
case 4: return ConsoleColor.DarkRed;
case 5: return ConsoleColor.DarkMagenta;
case 6: return ConsoleColor.DarkYellow;
case 7: return ConsoleColor.Gray;
case 8: return ConsoleColor.DarkGray;
case 9: return ConsoleColor.Blue;
case 10: return ConsoleColor.Green;
case 11: return ConsoleColor.Cyan;
case 12: return ConsoleColor.Red;
case 13: return ConsoleColor.Magenta;
case 14: return ConsoleColor.Yellow;
case 15: return ConsoleColor.White;
}
return ConsoleColor.Black;
}
}
/// <summary>
/// Moves the cursor to the given coordinates within the virtual screen.
/// </summary>
/// <remarks>
/// The upper-left corner of the virtual screen corresponds to (1, 1).
///
/// GotoXY is window-relative.
/// </remarks>
/// <param name="column">The 1-based column to move to</param>
/// <param name="row">The 1-based row to move to</param>
public static void GotoXY(int column, int row)
{
lock (_Lock)
{
if ((column > 0) && (row > 0))
{
int X = column - 1 + WindMinX;
int Y = row - 1 + WindMinY;
if ((X <= WindMaxX) && (Y <= WindMaxY))
{
if (OSUtils.IsWindows)
{
NativeMethods.COORD CursorPosition = new NativeMethods.COORD();
CursorPosition.X = (short)X;
CursorPosition.Y = (short)Y;
NativeMethods.SetConsoleCursorPosition(_StdOutputHandle, CursorPosition);
}
else
{
try
{
Console.SetCursorPosition(X, Y);
}
catch (NotImplementedException)
{
// Can't do that
}
}
}
}
}
}
/// <summary>
/// Hides the console window (restore it with Show())
/// </summary>
public static void HideConsole()
{
lock (_Lock)
{
if (OSUtils.IsWindows)
{
IntPtr hWnd = NativeMethods.GetConsoleWindow();
if (hWnd != IntPtr.Zero)
{
NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE);
}
}
}
}
public static void HideCursor()
{
lock (_Lock)
{
if (OSUtils.IsWindows)
{
NativeMethods.CONSOLE_CURSOR_INFO CCI;
if (NativeMethods.GetConsoleCursorInfo(_StdOutputHandle, out CCI))
{
CCI.Visible = false;
NativeMethods.SetConsoleCursorInfo(_StdOutputHandle, ref CCI);
}
}
else
{
try
{
Console.CursorVisible = false;
}
catch (NotImplementedException)
{
// Can't do that
}
}
}
}
/// <summary>
/// Selects high-intensity characters.
/// </summary>
/// <remarks>
/// There is a Byte variable in Crt TextAttr that is used to hold the current
/// video attribute. HighVideo sets the high intensity bit of TextAttr's
/// fore-ground color, thus mapping colors 0-7 onto colors 8-15.
/// </remarks>
public static void HighVideo()
{
lock (_Lock)
{
_TextAttr |= 0x08;
}
}
public static void InsChar(char ch)
{
InsChar(ch, 1);
}
public static void InsChar(char ch, int count)
{
// First make room for the new char(s)
for (int i = WindMinX + WindCols; i >= WhereXA() + count; i--)
{
FastWrite(GetCharAt(i - count, WhereYA()).ToString(), i, WhereYA(), GetAttrAt(i - count, WhereYA()));
}
// Then write the new char(s)
for (int i = WhereXA(); i < WhereXA() + count; i++)
{
FastWrite(ch.ToString(), i, WhereYA(), _TextAttr);
}
}
/// <summary>
/// Inserts an empty line at the cursor position.
/// </summary>
/// <remarks>
/// All lines below the inserted line are moved down one line, and the bottom
/// line scrolls off the screen (using the BIOS scroll routine).
///
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the new line becomes the
/// background color.
///
/// InsLine is window-relative.
/// </remarks>
public static void InsLine()
{
lock (_Lock)
{
InsLine(1);
}
}
/// <summary>
/// Inserts an empty line at the cursor position.
/// </summary>
/// <remarks>
/// All lines below the inserted line are moved down one line, and the bottom
/// line scrolls off the screen (using the BIOS scroll routine).
///
/// All character positions are set to blanks with the currently defined text
/// attributes. Thus, if TextBackground is not black, the new line becomes the
/// background color.
///
/// InsLine is window-relative.
/// </remarks>
/// <param name="count">The number of lines to insert</param>
public static void InsLine(int count)
{
lock (_Lock)
{
ScrollDownCustom(WindMinX + 1, WhereYA(), WindMaxX + 1, WindMaxY + 1, count, ' ', _TextAttr);
}
}
/// <summary>
/// Determines if a key has been pressed on the keyboard.
/// </summary>
/// The key can be read using the ReadKey function.
/// <remarks>
/// </remarks>
/// <returns>True If key has been pressed False If key has not been pressed</returns>
public static bool KeyPressed()
{
lock (_Lock)
{
// Check if we have a key in the buffer already
if (_KeyBuf.Count > 0) return true;
uint NumberOfEvents = 0;
do
{
if (OSUtils.IsWindows)
{
NativeMethods.GetNumberOfConsoleInputEvents(_StdInputHandle, out NumberOfEvents);
if (NumberOfEvents > 0)
{
NativeMethods.INPUT_RECORD[] Buffer = new NativeMethods.INPUT_RECORD[1];
uint NumberOfEventsRead = 0;
if (NativeMethods.ReadConsoleInput(_StdInputHandle, Buffer, 1, out NumberOfEventsRead))
{
if ((Buffer[0].EventType == NativeMethods.KEY_EVENT) && (Buffer[0].KeyEvent.bKeyDown))
{
bool Alt = (NativeMethods.GetKeyState(NativeMethods.VirtualKeyStates.VK_MENU) & 0x8000) == 0x8000;
bool Ctrl = (NativeMethods.GetKeyState(NativeMethods.VirtualKeyStates.VK_CONTROL) & 0x8000) == 0x8000;
bool Shift = (NativeMethods.GetKeyState(NativeMethods.VirtualKeyStates.VK_SHIFT) & 0x8000) == 0x8000;
if ((Buffer[0].KeyEvent.wVirtualKeyCode >= 65) && (Buffer[0].KeyEvent.wVirtualKeyCode <= 90))
{
bool CapsLock = (NativeMethods.GetKeyState(NativeMethods.VirtualKeyStates.VK_CAPITAL) & 0x0001) == 0x0001;
Shift ^= CapsLock;
}
if ((Buffer[0].KeyEvent.wVirtualKeyCode >= 96) && (Buffer[0].KeyEvent.wVirtualKeyCode <= 105))
{
bool NumLock = (NativeMethods.GetKeyState(NativeMethods.VirtualKeyStates.VK_NUMLOCK) & 0x0001) == 0x0001;
Shift |= NumLock;
}
int Key = 0;
if (Alt)
{
Key = TAltKeys[Buffer[0].KeyEvent.wVirtualKeyCode];
}
else if (Ctrl)
{
Key = TCtrlKeys[Buffer[0].KeyEvent.wVirtualKeyCode];
}
else if (Shift)
{
Key = TShiftKeys[Buffer[0].KeyEvent.wVirtualKeyCode];
}
else
{
Key = TKeys[Buffer[0].KeyEvent.wVirtualKeyCode];
}
if (Key >= 1000)
{
_KeyBuf.Enqueue((char)0);
_KeyBuf.Enqueue((char)(Key - 1000));
}
else if (Key > 0)
{
_KeyBuf.Enqueue((char)Key);
}
}
}
}
}
else
{
try
{
NumberOfEvents = (uint)(Console.KeyAvailable ? 1 : 0);
if (NumberOfEvents > 0)
{
ConsoleKeyInfo CKI = Console.ReadKey(true);
bool Alt = (CKI.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt;
bool Ctrl = (CKI.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control;
bool Shift = (CKI.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift;
if (((int)CKI.Key >= 65) && ((int)CKI.Key <= 90))
{
try
{
Shift ^= Console.CapsLock;
}
catch (NotSupportedException)
{
// Looks like mono doesn't support this
}
}
if (((int)CKI.Key >= 96) && ((int)CKI.Key <= 105))
{
Shift |= Console.NumberLock;
}
int Key = 0;
if (Alt)
{
Key = TAltKeys[(int)CKI.Key];
}
else if (Ctrl)
{
Key = TCtrlKeys[(int)CKI.Key];
}
else if (Shift)
{
Key = TShiftKeys[(int)CKI.Key];
}
else
{
Key = TKeys[(int)CKI.Key];
}
if (Key >= 1000)
{
_KeyBuf.Enqueue((char)0);
_KeyBuf.Enqueue((char)(Key - 1000));
}
else if (Key > 0)
{
_KeyBuf.Enqueue((char)Key);
}
}
}
catch (NotImplementedException)
{
// Can't do that
NumberOfEvents = 0;
}
}
} while (NumberOfEvents > 0);
return false;
}
}
/// <summary>
/// Each time TextMode is called, the current video mode is stored in LastMode.
/// </summary>
/// <remarks>