-
Notifications
You must be signed in to change notification settings - Fork 90
/
Grijjy.System.Console.pas
90 lines (74 loc) · 1.67 KB
/
Grijjy.System.Console.pas
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
unit Grijjy.System.Console;
{$INCLUDE 'Grijjy.inc'}
interface
uses
{$IFDEF LINUX}
Posix.Signal,
{$ENDIF}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
System.SysUtils,
System.SyncObjs;
function WaitForCtrlC(const ATimeout: Cardinal = INFINITE): Boolean;
var
CtrlC: Boolean = False;
implementation
var
CtrlC_Event: TEvent;
{$IFDEF MSWINDOWS}
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
begin
CtrlC_Event.SetEvent;
CtrlC := True;
end;
Result := True;
end;
{$ENDIF}
{$IFDEF LINUX}
var
sigIntHandler: sigaction_t;
procedure SigHandler(SigNum: Integer); cdecl;
begin
CtrlC_Event.SetEvent;
end;
{$ENDIF}
function WaitForCtrlC(const ATimeout: Cardinal): Boolean;
begin
Result := (CtrlC_Event.WaitFor(ATimeout) = TWaitResult.wrTimeout);
end;
{$IFDEF MSWINDOWS}
procedure DisableQuickEdit;
const
ENABLE_QUICK_EDIT = $40;
ENABLE_EXTENDED_FLAGS = $80;
var
StdHandle: THandle;
Mode: UInt32;
begin
StdHandle := GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(StdHandle, Mode);
Mode := Mode and not ENABLE_QUICK_EDIT;
Mode := Mode and not ENABLE_LINE_INPUT;
Mode := Mode and not ENABLE_MOUSE_INPUT;
Mode := Mode and not ENABLE_EXTENDED_FLAGS;
SetConsoleMode(StdHandle, Mode);
end;
{$ENDIF}
initialization
CtrlC_Event := TEvent.Create(nil, True, False, '');
{$IFDEF MSWINDOWS}
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
DisableQuickEdit;
{$ENDIF}
{$IFDEF LINUX}
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
{$ENDIF}
finalization
CtrlC_Event.Free;
end.