-
Notifications
You must be signed in to change notification settings - Fork 2
/
djl_con.hxx
759 lines (659 loc) · 28.5 KB
/
djl_con.hxx
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
#pragma once
#ifdef WATCOM
#include <conio.h>
#include <graph.h>
#include <dos.h>
class ConsoleConfiguration
{
private:
bool outputEstablished;
void (__interrupt __far * prev_int_23)();
static void __interrupt __far controlc_routine() {}
public:
ConsoleConfiguration() : outputEstablished( false ), prev_int_23( 0 ) {}
~ConsoleConfiguration() {}
void EstablishConsoleOutput( int16_t width = 80, int16_t height = 24 )
{
if ( outputEstablished )
return;
outputEstablished = true;
_setvideomode( 3 ); // 80x25 color text
// hook the ^C interrupt so the default handler doesn't terminate the app
prev_int_23 = _dos_getvect( 0x23 );
_dos_setvect( 0x23, controlc_routine );
} //EstablishConsoleOutput
static int portable_kbhit() { return kbhit(); }
static int throttled_kbhit() { return kbhit(); }
static int portable_getch() { return getch(); }
static char * portable_gets_s( char * buf, size_t bufsize ) { return gets( buf ); }
void RestoreConsoleInput()
{
if ( 0 != prev_int_23 )
{
_dos_setvect( 0x23, prev_int_23 );
prev_int_23 = 0;
}
} //RestoreConsoleInput
void RestoreConsoleOutput( bool clearScreen = true )
{
outputEstablished = false;
} //RestoreConsoleOutput
void RestoreConsole( bool clearScreen = true )
{
RestoreConsoleInput();
RestoreConsoleOutput( clearScreen );
} //RestoreConsole
bool IsOutputEstablished() { return outputEstablished; }
};
#else
#include <chrono>
using namespace std;
using namespace std::chrono;
class ConsoleConfiguration
{
private:
#ifdef _WIN32
HANDLE consoleOutputHandle;
HANDLE consoleInputHandle;
WINDOWPLACEMENT oldWindowPlacement;
CONSOLE_SCREEN_BUFFER_INFOEX oldScreenInfo;
DWORD oldOutputConsoleMode, oldInputConsoleMode;
CONSOLE_CURSOR_INFO oldCursorInfo;
int16_t setWidth;
UINT oldOutputCP;
static const size_t longestEscapeSequence = 10; // probably overkill
char aReady[ 1 + longestEscapeSequence ];
#else
#ifndef OLDGCC // the several-years-old Gnu C compiler for the RISC-V development boards
struct termios orig_termios;
#endif
#endif
bool inputEstablished, outputEstablished;
#ifdef _WIN32
// this function takes Windows keyboard input and produces Linux keyboard input
bool process_key_event( INPUT_RECORD & rec, char * pout )
{
*pout = 0;
if ( KEY_EVENT != rec.EventType )
return false;
if ( !rec.Event.KeyEvent.bKeyDown )
return false;
const uint8_t asc = rec.Event.KeyEvent.uChar.AsciiChar;
const uint8_t sc = (uint8_t) rec.Event.KeyEvent.wVirtualScanCode;
// don't pass back just an Alt/ctrl/shift/capslock without another character
if ( ( 0 == asc ) && ( 0x38 == sc || 0x1d == sc || 0x2a == sc || 0x3a == sc || 0x36 == sc ) )
return false;
bool fshift = ( 0 != ( rec.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED ) );
bool fctrl = ( 0 != ( rec.Event.KeyEvent.dwControlKeyState & ( LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED ) ) );
bool falt = ( 0 != ( rec.Event.KeyEvent.dwControlKeyState & ( LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED ) ) );
tracer.Trace( " process key event sc/asc %02x%02x, shift %d, ctrl %d, alt %d\n", sc, asc, fshift, fctrl, falt );
// control+ 1, 3-5, and 7-0 should be swallowed. 2 and 6 are allowed.
if ( fctrl && ( 2 == sc || 4 == sc || 5 == sc || 6 == sc || 8 == sc || 9 == sc || 0xa == sc || 0xb == sc ) )
return false;
// control+ =, ;, ', `, ,, ., / should be swallowed
if ( fctrl && ( 0x0d == sc || 0x27 == sc || 0x28 == sc || 0x29 == sc || 0x33 == sc || 0x34 == sc || 0x35 == sc ) )
return false;
// alt+ these characters has no meaning
if ( falt && ( '\'' == asc || '`' == asc || ',' == asc || '.' == asc || '/' == asc || 0x4c == sc ) )
return false;
pout[ 0 ] = asc;
pout[ 1 ] = 0;
if ( falt && asc >= ' ' && asc <= 0x7f )
{
sprintf( pout, "\033%c", asc );
}
else if ( 0x01 == sc ) // ESC
{
// can't test on Windows if ( falt ) ;
}
else if ( 0x0e == sc ) // Backspace
{
if ( falt )
{
pout[ 0 ] = 0x1b;
pout[ 1 ] = 0x7f;
pout[ 2 ] = 0;
}
else if ( fctrl )
pout[ 0 ] = 8;
else
pout[ 0 ] = 0x7f; // shift and no shift
}
else if ( 0x0f == sc ) // Tab
{
// can't test on Windows if ( falt ) ;
// can't test on Windows else if ( fctrl ) ;
if ( fshift ) strcpy( pout, "\033[Z" );
}
else if ( 0x35 == sc ) // keypad /
{
if ( falt ) strcpy( pout, "\033/" );
else if ( fctrl ) *pout = 31;
}
else if ( 0x37 == sc ) // keypad *
{
if ( falt ) strcpy( pout, "\033*" );
else if ( fctrl ) *pout = 10;
}
else if ( sc >= 0x3b && sc <= 0x44 ) // F1 - F10
{
if ( falt )
{
if ( sc >= 0x3b && sc <= 0x3e )
sprintf( pout, "\033[1;3%c", sc - 0x3b + 80 );
else if ( sc == 0x3f )
strcpy( pout, "\033[15;3~" );
else if ( sc >= 0x40 && sc <= 0x42 )
sprintf( pout, "\033[1%c;3~", 55 + sc - 0x40 );
else if ( sc >= 0x43 && sc <= 0x44 )
sprintf( pout, "\033[2%c;3~", 48 + sc - 0x43 );
}
else if ( fctrl )
{
if ( sc >= 0x3b && sc <= 0x3e )
sprintf( pout, "\033[1;5%c", sc - 0x3b + 80 );
else if ( sc == 0x3f )
strcpy( pout, "\033[15;5~" );
else if ( sc >= 0x40 && sc <= 0x42 )
sprintf( pout, "\033[2%c;5~", 55 + sc - 0x40 );
else if ( sc >= 0x43 && sc <= 0x44 )
sprintf( pout, "\033[2%c;5~", 48 + sc - 0x43 );
}
else if ( fshift )
{
if ( sc >= 0x3b && sc <= 0x3e )
sprintf( pout, "\033[1;2%c", sc - 0x3b + 80 );
else if ( sc == 0x3f )
strcpy( pout, "\033[15;2~" );
else if ( sc >= 0x40 && sc <= 0x42 )
sprintf( pout, "\033[1%c;2~", 55 + sc - 0x40 );
else if ( sc >= 0x43 && sc <= 0x44 )
sprintf( pout, "\033[2%c;2~", 48 + sc - 0x43 );
}
else
{
if ( sc >= 0x3b && sc <= 0x3e )
sprintf( pout, "\033O%c", 80 + sc - 0x3b );
else if ( sc == 0x3f )
strcpy( pout, "\033[15~" );
else if ( sc >= 0x40 && sc <= 0x42 )
sprintf( pout, "\033[1%c~", 55 + sc - 0x40 );
else
sprintf( pout, "\033[2%c~", 48 + sc - 0x43 );
}
}
else if ( 0x47 == sc ) // Home
{
if ( falt ) strcpy( pout, "\033[1;3H" );
else if ( fctrl ) strcpy( pout, "\033[1;5H" );
else if ( fshift ) strcpy( pout, "\033[1;2H" );
else strcpy( pout, "\033[H" );
}
else if ( 0x48 == sc ) // up arrow
{
if ( falt ) strcpy( pout, "\033[1;3A" );
else if ( fctrl ) strcpy( pout, "\033[1;5A" );
else if ( fshift ) strcpy( pout, "\033[1;2A" );
else strcpy( pout, "\033[A" );
}
else if ( 0x49 == sc ) // page up
{
if ( falt ) strcpy( pout, "\033[5;3~" );
else if ( fctrl ) strcpy( pout, "\033[5;5~" );
else if ( fshift ) strcpy( pout, "\033[5;2~" );
else strcpy( pout, "\033[5~" );
}
else if ( 0x4a == sc ) // keypad -
{
if ( falt ) strcpy( pout, "\033-" );
// can't test on Windows else if ( fctrl ) ;
}
else if ( 0x4b == sc ) // left arrow
{
if ( falt ) strcpy( pout, "\033[1;3D" );
else if ( fctrl ) strcpy( pout, "\033[1;5D" );
else if ( fshift ) strcpy( pout, "\033[1;2D" );
else strcpy( pout, "\033[D" );
}
else if ( 0x4c == sc ) // keypad 5
{
// no output on Linux if ( fctrl ) ;
// no output on Linux else if ( fshift ) ;
}
else if ( 0x4d == sc ) // right arrow
{
if ( falt ) strcpy( pout, "\033[1;3C" );
else if ( fctrl ) strcpy( pout, "\033[1;5C" );
else if ( fshift ) strcpy( pout, "\033[1;2C" );
else strcpy( pout, "\033[C" );
}
else if ( 0x4e == sc ) // keypad +
{
if ( falt ) strcpy( pout, "\033+" );
else if ( fshift ) *pout = 43;
else if ( fctrl ) *pout = 43;
else *pout = 43;
}
else if ( 0x4f == sc ) // End
{
if ( falt ) strcpy( pout, "\033[1;3F" );
else if ( fctrl ) strcpy( pout, "\033[1;5F" );
else if ( fshift ) strcpy( pout, "\033[1;2F" );
else strcpy( pout, "\033[F" );
}
else if ( 0x50 == sc ) // down arrow
{
if ( falt ) strcpy( pout, "\033[1;3B" );
else if ( fctrl ) strcpy( pout, "\033[1;5B" );
else if ( fshift ) strcpy( pout, "\033[1;2B" );
else strcpy( pout, "\033[B" );
}
else if ( 0x51 == sc ) // page down
{
if ( falt ) strcpy( pout, "\033[6;3~" );
else if ( fctrl ) strcpy( pout, "\033[6;5~" );
else if ( fshift ) strcpy( pout, "\033[6;2~" );
else strcpy( pout, "\033[6~" );
}
else if ( 0x52 == sc ) // INS
{
if ( falt ) strcpy( pout, "\033[2;3~" );
else if ( fctrl ) strcpy( pout, "\033[2;5~" );
else if ( fshift ) ; // can't repro on windows
else strcpy( pout, "\033[2~" );
}
else if ( 0x53 == sc ) // Del
{
if ( falt ) strcpy( pout, "\033[3;3~" );
else if ( fctrl ) strcpy( pout, "\033[3;5~" );
else if ( fshift ) strcpy( pout, "\033[3;2~" );
else strcpy( pout, "\033[3~" );
}
else if ( 0x57 == sc || 0x58 == sc ) // F11 - F12
{
if ( falt ) sprintf( pout, "\033[2%c;3~", sc - 0x57 + 51 );
else if ( fctrl ) sprintf( pout, "\033[2%c;5~", sc - 0x57 + 51 );
else if ( fshift ) sprintf( pout, "\033[2%c;2~", sc - 0x57 + 51 );
else sprintf( pout, "\033[2%c~", 51 + sc - 0x57 );
}
return true;
} //process_key_event
#endif // _WIN32
public:
#ifdef _WIN32
ConsoleConfiguration() : oldOutputConsoleMode( 0 ), oldInputConsoleMode( 0 ),
setWidth( 0 ), oldOutputCP( 0 ),
inputEstablished( false ), outputEstablished( false )
{
oldWindowPlacement = {0};
oldScreenInfo = {0};
oldCursorInfo = {0};
consoleOutputHandle = GetStdHandle( STD_OUTPUT_HANDLE );
consoleInputHandle = GetStdHandle( STD_INPUT_HANDLE );
EstablishConsoleInput();
memset( aReady, 0, sizeof( aReady ) );
} //ConsoleConfiguration
#else
ConsoleConfiguration() : inputEstablished( false ), outputEstablished( false )
{
EstablishConsoleInput();
} //ConsoleConfiguration
#endif
~ConsoleConfiguration()
{
RestoreConsole();
}
bool IsOutputEstablished() { return outputEstablished; }
#ifdef _WIN32
HANDLE GetOutputHandle() { return consoleOutputHandle; };
HANDLE GetInputHandle() { return consoleInputHandle; };
#endif
void SetCursorInfo( uint32_t size ) // 0 to 100
{
#ifdef _WIN32
if ( 0 != consoleOutputHandle )
{
CONSOLE_CURSOR_INFO info = {0};
if ( 0 == size )
{
info.dwSize = 1;
info.bVisible = FALSE;
}
else
{
info.dwSize = size;
info.bVisible = TRUE;
}
SetConsoleCursorInfo( consoleOutputHandle, &info );
}
#else
if ( outputEstablished )
{
if ( 0 == size )
printf( "\x1b[?25l" );
else
printf( "\x1b[?25h" );
fflush( stdout );
}
#endif
} //SetCursorInfo
void EstablishConsoleInput( void * pCtrlCRoutine = 0 )
{
if ( !isatty( fileno( stdin ) ) )
return;
if ( inputEstablished )
RestoreConsoleInput();
#ifdef _WIN32
GetConsoleMode( consoleOutputHandle, &oldInputConsoleMode );
if ( 0 == pCtrlCRoutine )
{
DWORD dwMode = oldInputConsoleMode;
dwMode &= ~ENABLE_PROCESSED_INPUT;
SetConsoleMode( consoleInputHandle, dwMode );
tracer.Trace( "old and new console input mode: %#x, %#x\n", oldInputConsoleMode, dwMode );
}
else
{
// don't automatically have ^c terminate the app. ^break will still terminate the app
PHANDLER_ROUTINE handler = (PHANDLER_ROUTINE) pCtrlCRoutine;
SetConsoleCtrlHandler( handler, TRUE );
}
#else
#ifndef OLDGCC // the several-years-old Gnu C compiler for the RISC-V development boards. that machine has no keyboard support
tcgetattr( 0, &orig_termios );
// make input raw so it's possible to peek to see if a keystroke is available
struct termios new_termios;
memcpy( &new_termios, &orig_termios, sizeof( new_termios ) );
cfmakeraw( &new_termios );
new_termios.c_oflag = orig_termios.c_oflag;
tcsetattr( 0, TCSANOW, &new_termios );
#endif
#endif
inputEstablished = true;
} //EstablishConsoleInput
void EstablishConsoleOutput( int16_t width = 80, int16_t height = 24 )
{
tracer.Trace( " EstablishConsoleOutput width %u height %u, outputEstablished %d\n", width, height, outputEstablished );
if ( outputEstablished )
return;
#ifdef _WIN32
GetConsoleCursorInfo( consoleOutputHandle, &oldCursorInfo );
if ( 0 != width )
{
oldWindowPlacement.length = sizeof oldWindowPlacement;
GetWindowPlacement( GetConsoleWindow(), &oldWindowPlacement );
oldOutputCP = GetConsoleOutputCP();
SetConsoleOutputCP( 437 );
oldScreenInfo.cbSize = sizeof oldScreenInfo;
GetConsoleScreenBufferInfoEx( consoleOutputHandle, &oldScreenInfo );
CONSOLE_SCREEN_BUFFER_INFOEX newInfo;
memcpy( &newInfo, &oldScreenInfo, sizeof newInfo );
setWidth = width;
newInfo.dwSize.X = width;
newInfo.dwSize.Y = height;
newInfo.dwMaximumWindowSize.X = width;
newInfo.dwMaximumWindowSize.Y = height;
newInfo.wAttributes = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
// legacy DOS RGB values
newInfo.ColorTable[ 0 ] = 0;
newInfo.ColorTable[ 1 ] = 0x800000;
newInfo.ColorTable[ 2 ] = 0x008000;
newInfo.ColorTable[ 3 ] = 0x808000;
newInfo.ColorTable[ 4 ] = 0x000080;
newInfo.ColorTable[ 5 ] = 0x800080;
newInfo.ColorTable[ 6 ] = 0x008080;
newInfo.ColorTable[ 7 ] = 0xc0c0c0;
newInfo.ColorTable[ 8 ] = 0x808080;
newInfo.ColorTable[ 9 ] = 0xff0000;
newInfo.ColorTable[ 10 ] = 0x00ff00;
newInfo.ColorTable[ 11 ] = 0xffff00;
newInfo.ColorTable[ 12 ] = 0x0000ff;
newInfo.ColorTable[ 13 ] = 0xff00ff;
newInfo.ColorTable[ 14 ] = 0x00ffff;
newInfo.ColorTable[ 15 ] = 0xffffff;
SetConsoleScreenBufferInfoEx( consoleOutputHandle, &newInfo );
COORD newSize = { width, height };
SetConsoleScreenBufferSize( consoleOutputHandle, newSize );
}
DWORD dwMode = 0;
GetConsoleMode( consoleOutputHandle, &dwMode );
oldOutputConsoleMode = dwMode;
dwMode |= ( ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_WINDOW_INPUT );
tracer.Trace( "old and new console output mode: %04x, %04x\n", oldOutputConsoleMode, dwMode );
SetConsoleMode( consoleOutputHandle, dwMode );
#else
if ( isatty( fileno( stdout ) ) )
{
printf( "%c[1 q", 27 ); // 1 == cursor blinking block.
fflush( stdout );
}
#endif
outputEstablished = true;
if ( 0 != width )
SendClsSequence();
} //EstablishConsoleOutput
void RestoreConsoleInput()
{
if ( inputEstablished )
{
#ifdef _WIN32
SetConsoleMode( consoleInputHandle, oldInputConsoleMode );
#else
#ifndef OLDGCC // the several-years-old Gnu C compiler for the RISC-V development boards
tcsetattr( 0, TCSANOW, &orig_termios );
#endif
#endif
inputEstablished = false;
}
} //RestoreConsoleInput
void RestoreConsoleOutput( bool clearScreen = true )
{
if ( outputEstablished )
{
#ifndef _WIN32
if ( isatty( fileno( stdout ) ) )
{
printf( "%c[0m", 27 ); // turn off display attributes
fflush( stdout );
}
#endif
if ( clearScreen )
SendClsSequence();
#ifdef _WIN32
SetConsoleOutputCP( oldOutputCP );
SetConsoleCursorInfo( consoleOutputHandle, & oldCursorInfo );
if ( 0 != setWidth )
{
SetConsoleScreenBufferInfoEx( consoleOutputHandle, & oldScreenInfo );
SetWindowPlacement( GetConsoleWindow(), & oldWindowPlacement );
}
SetConsoleMode( consoleOutputHandle, oldOutputConsoleMode );
#endif
outputEstablished = false;
}
} //RestoreConsoleOutput
void RestoreConsole( bool clearScreen = true )
{
RestoreConsoleInput();
RestoreConsoleOutput( clearScreen );
} //RestoreConsole
void SendClsSequence()
{
if ( isatty( fileno( stdout ) ) )
{
printf( "\x1b[2J" ); // clear the screen
printf( "\x1b[1G" ); // cursor to top line
printf( "\x1b[1d" ); // cursor to left side
fflush( stdout );
}
} //SendClsSequence
void ClearScreen()
{
#ifdef _WIN32
if ( 0 != consoleOutputHandle )
SendClsSequence();
else
{
HANDLE hcon = GetStdHandle( STD_OUTPUT_HANDLE );
DWORD dwMode = 0;
GetConsoleMode( hcon, &dwMode );
DWORD oldMode = dwMode;
dwMode |= ( ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_WINDOW_INPUT );
SetConsoleMode( hcon, dwMode );
SendClsSequence();
SetConsoleMode( hcon, oldMode );
}
#else
SendClsSequence();
#endif
} //ClearScreen
int portable_kbhit()
{
if ( !isatty( fileno( stdin ) ) )
return ( 0 == feof( stdin) );
#ifdef _WIN32
if ( 0 != aReady[ 0 ] )
return true;
return _kbhit();
#else
fd_set set;
FD_ZERO( &set );
FD_SET( STDIN_FILENO, &set );
struct timeval timeout = {0};
return ( select( 1, &set, NULL, NULL, &timeout ) > 0 );
#endif
} //portable_kbhit
static int redirected_getch()
{
assert( !isatty( fileno( stdin ) ) );
char data;
if ( 1 == read( 0, &data, 1 ) )
{
// for files with CR/LF, skip the CR and turn the LF into a CR
// for files with LF, turn the LF into a CR
if ( ( 0x0d == data ) && ( !feof( stdin ) ) )
{
if ( 0 == read( 0, &data, 1 ) ) // make gcc not complain by checking return code
data = 0x0d;
}
if ( 0x0a == data )
data = 0x0d;
return data;
}
return EOF;
} //redirected_getch()
#ifdef _WIN32
// behave like getch() on linux -- extended characters have escape sequences
int linux_getch()
{
if ( !isatty( fileno( stdin ) ) )
return redirected_getch();
size_t cReady = strlen( aReady );
if ( 0 != cReady )
{
int result = aReady[ 0 ];
memmove( & aReady[ 0 ], & aReady[ 1 ], cReady ); // may just be the null termination
return result;
}
HANDLE hConsoleInput = GetStdHandle( STD_INPUT_HANDLE );
do
{
DWORD available = 0;
BOOL ok = GetNumberOfConsoleInputEvents( hConsoleInput, &available );
if ( ok && ( 0 != available ) )
{
INPUT_RECORD records[ 1 ];
DWORD numRead = 0;
ok = ReadConsoleInput( hConsoleInput, records, 1, &numRead );
if ( ok )
{
for ( DWORD x = 0; x < numRead; x++ )
{
char acSequence[ longestEscapeSequence ] = {0};
bool used = process_key_event( records[ x ], acSequence );
if ( !used )
continue;
strcat( aReady, acSequence );
tracer.Trace( " consumed sequence of length %zd, total cached len %zd\n", strlen( acSequence ), strlen( aReady ) );
}
}
}
if ( 0 != aReady[ 0 ] )
return linux_getch();
} while( true );
tracer.Trace( " bug in linux_getch; returning nothing\n" );
assert( false );
return 0;
} //linux_getch
#endif // _WIN32
static int portable_getch()
{
if ( !isatty( fileno( stdin ) ) )
return redirected_getch();
#ifdef _WIN32
return _getch();
#else
int r;
unsigned char c;
do
{
if ( ( r = read( 0, &c, sizeof( c ) ) ) < 0 )
return r;
if ( 0 != r ) // Linux platforms I tested wait for a char to be available. MacOS returns 0 immediately.
break;
//tracer.Trace( " sleeping in portable_getch()\n" );
sleep_ms( 1 );
} while( true );
return c;
#endif
} //portable_getch
bool throttled_kbhit()
{
// _kbhit() does device I/O in Windows, which sleeps for a tiny amount waiting for a reply, so
// compute-bound mbasic.com apps run 10x slower than they should because mbasic polls for keyboard input.
// Workaround: only call _kbhit() if 50 milliseconds has gone by since the last call.
static high_resolution_clock::time_point last_call = high_resolution_clock::now();
high_resolution_clock::time_point tNow = high_resolution_clock::now();
long long difference = duration_cast<std::chrono::milliseconds>( tNow - last_call ).count();
if ( difference > 50 )
{
last_call = tNow;
return portable_kbhit();
}
return false;
} //throttled_kbhit
static char * portable_gets_s( char * buf, size_t bufsize )
{
size_t len = 0;
do
{
char ch = (char) portable_getch();
if ( '\n' == ch || '\r' == ch )
{
printf( "\n" );
fflush( stdout ); // fflush is required on linux or it'll be buffered not seen until the app ends.
break;
}
if ( len >= ( bufsize - 1 ) )
break;
if ( 0x7f == ch || 8 == ch ) // backspace (it's not 8 for some reason)
{
if ( len > 0 )
{
printf( "\x8 \x8" );
fflush( stdout );
len--;
}
}
else
{
printf( "%c", ch );
fflush( stdout );
buf[ len++ ] = ch;
}
} while( true );
buf[ len ] = 0;
return buf;
} //portable_gets_s
}; //ConsoleConfiguration
#endif // WATCOM