forked from jackokring/rub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
w32smsg.inc
190 lines (162 loc) · 5.2 KB
/
w32smsg.inc
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
{
System independent system interface for win32/win64
Copyright (c) 2000 by Pierre Muller
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
uses
windows,dos,winevent;
var
ChangeSystemEvents : TCriticalSection;
Const
SystemEventActive : Boolean = false;
procedure SystemEventHandler(var ir:INPUT_RECORD);
var
e : TSystemEvent;
begin
{ WINDOW_BUFFER_SIZE_EVENT is triggered by buffer size changes
but we are interested in console size changes, thus its handled below
in PollSystemEvent }
if (ir.EventType in [FOCUS_EVENT{,WINDOW_BUFFER_SIZE_EVENT}]) then
begin
EnterCriticalSection(ChangeSystemEvents);
if (ir.EventType=FOCUS_EVENT) then
begin
if ir.Event.FocusEvent.bSetFocus then
e.typ:=SysSetFocus
else
e.typ:=SysReleaseFocus;
end
else
begin
e.typ:=SysResize;
e.x:=ir.Event.WindowBufferSizeEvent.dwSize.x;
e.y:=ir.Event.WindowBufferSizeEvent.dwSize.y;
end;
PutSystemEvent(e);
LeaveCriticalSection(ChangeSystemEvents);
end;
end;
var
Xsize, YSize : longint;
function HandleConsoleCtrl(typ : dword) : BOOL; stdcall;
var
SE : TSystemEvent;
begin
HandleConsoleCtrl:=false;
case typ of
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT,
CTRL_SHUTDOWN_EVENT :
begin
SE.typ:=SysClose;
SE.CloseTyp:=typ;
PutSystemEvent(SE);
HandleConsoleCtrl:=true;
end;
end;
end;
procedure InitSystemMsg;
var
mode : dword;
ConsoleScreenBufferInfo : Console_screen_buffer_info;
begin
if SystemEventActive then
exit;
// enable Window events
GetConsoleMode(TextRec(Input).Handle,@mode);
mode:=mode or ENABLE_WINDOW_INPUT;
SetConsoleMode(TextRec(Input).Handle,mode);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
@ConsoleScreenBufferInfo);
XSize:=ConsoleScreenBufferInfo.srWindow.right-ConsoleScreenBufferInfo.srWindow.left+1;
YSize:=ConsoleScreenBufferInfo.srWindow.bottom-ConsoleScreenBufferInfo.srWindow.top+1;
PendingSystemHead:=@PendingSystemEvent;
PendingSystemTail:=@PendingSystemEvent;
PendingSystemEvents:=0;
FillChar(LastSystemEvent,sizeof(TSystemEvent),0);
InitializeCriticalSection(ChangeSystemEvents);
SetResizeEventHandler(@SystemEventHandler);
SetFocusEventHandler(@SystemEventHandler);
SetConsoleCtrlHandler(@HandleConsoleCtrl,true);
SystemEventActive:=true;
end;
procedure DoneSystemMsg;
var
mode : dword;
begin
if not SystemEventActive then
exit;
// disable System events
GetConsoleMode(TextRec(Input).Handle,@mode);
mode:=mode and (not ENABLE_WINDOW_INPUT);
SetConsoleMode(TextRec(Input).Handle,mode);
SetResizeEventHandler(nil);
SetFocusEventHandler(nil);
DeleteCriticalSection(ChangeSystemEvents);
SetConsoleCtrlHandler(@HandleConsoleCtrl,false);
SystemEventActive:=false;
end;
procedure GetSystemEvent(var SystemEvent: TSystemEvent);
var
b : byte;
begin
repeat
EnterCriticalSection(ChangeSystemEvents);
b:=PendingSystemEvents;
LeaveCriticalSection(ChangeSystemEvents);
if b>0 then
break
else
sleep(50);
until false;
EnterCriticalSection(ChangeSystemEvents);
SystemEvent:=PendingSystemHead^;
inc(PendingSystemHead);
if longint(PendingSystemHead)=longint(@PendingSystemEvent)+sizeof(PendingSystemEvent) then
PendingSystemHead:=@PendingSystemEvent;
dec(PendingSystemEvents);
LastSystemEvent:=SystemEvent;
LeaveCriticalSection(ChangeSystemEvents);
end;
function PollSystemEvent(var SystemEvent: TSystemEvent):boolean;
var
ConsoleScreenBufferInfo : Console_screen_buffer_info;
NewXSize, NewYSize : longint;
begin
EnterCriticalSection(ChangeSystemEvents);
if PendingSystemEvents>0 then
begin
SystemEvent:=PendingSystemHead^;
PollSystemEvent:=true;
end
else
begin
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
@ConsoleScreenBufferInfo);
NewXSize:=ConsoleScreenBufferInfo.srWindow.right-ConsoleScreenBufferInfo.srWindow.left+1;
NewYSize:=ConsoleScreenBufferInfo.srWindow.bottom-ConsoleScreenBufferInfo.srWindow.top+1;
if (XSize<>NewXSize) or (YSize<>NewYSize) then
begin
SystemEvent.typ:=SysResize;
SystemEvent.x:=NewXSize;
SystemEvent.y:=NewYSize;
PutSystemEvent(SystemEvent);
XSize:=NewXSize;
YSize:=NewYSize;
PollSystemEvent:=true;
end
else
PollSystemEvent:=false;
end;
LeaveCriticalSection(ChangeSystemEvents);
end;