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
|
{%mainunit fpgfx.pas}
// fpGFX internal message queue implementation
type
// a simlpe linked list implementation
TMessageListElement = class(TObject)
protected
Next: TMessageListElement;
Prev: TMessageListElement;
public
msg: TfpgMessageRec;
end;
var
// to make things easier we have a few global variable that will get set
UsedFirstMessage: TMessageListElement;
UsedLastMessage: TMessageListElement;
FreeFirstMessage: TMessageListElement;
FreeLastMessage: TMessageListElement;
procedure MsgListInsertElement(Elem: TMessageListElement; var First: TMessageListElement; var Last: TMessageListElement);
begin
Elem.Prev := nil;
Elem.Next := nil;
if First = nil then
begin
First := Elem;
Last := Elem;
end
else
begin
Last.Next := Elem;
Elem.Prev := Last;
Last := Elem;
end;
end;
procedure MsgListRemoveElement(Elem: TMessageListElement; var First: TMessageListElement; var Last: TMessageListElement);
begin
if Elem = First then
First := Elem.Next;
if Elem = Last then
Last := Elem.Prev;
if Elem.Next <> nil then
Elem.Next.Prev := Elem.Prev;
if Elem.Prev <> nil then
Elem.Prev.Next := Elem.Next;
end;
function fpgAllocateMessage: PfpgMessageRec;
var
e: TMessageListElement;
begin
e := FreeFirstMessage;
if e <> nil then
begin
MsgListRemoveElement(e, FreeFirstMessage, FreeLastMessage);
MsgListInsertElement(e, UsedFirstMessage, UsedLastMessage);
result := @(e.msg);
end
else
result := nil;
end;
procedure fpgDeleteFirstMessage;
var
e: TMessageListElement;
begin
e := UsedFirstMessage;
if e <> nil then
begin
MsgListRemoveElement(e, UsedFirstMessage, UsedLastMessage);
MsgListInsertElement(e, FreeFirstMessage, FreeLastMessage);
end;
end;
function fpgGetFirstMessage: PfpgMessageRec;
begin
if UsedFirstMessage <> nil then
Result := @(UsedFirstMessage.msg)
else
Result := nil;
end;
procedure fpgInitMsgQueue;
var
n: integer;
e: TMessageListElement;
begin
UsedFirstMessage := nil;
UsedLastMessage := nil;
FreeFirstMessage := nil;
FreeLastMessage := nil;
uMsgQueueList := TList.Create;
for n := 1 to cMessageQueueSize do
begin
e := TMessageListElement.Create;
uMsgQueueList.Add(e); // so we can free it off later
MsgListInsertElement(e,FreeFirstMessage,FreeLastMessage);
end;
end;
procedure fpgPostMessage(Sender, Dest: TObject; MsgCode: integer; var aparams: TfpgMessageParams); overload;
var
p: PfpgMessageRec;
begin
if Dest = nil then
Exit; //==>
p := fpgAllocateMessage;
if p <> nil then
begin
p^.MsgCode := MsgCode;
p^.Sender := Sender;
p^.Dest := Dest;
p^.Params := aparams;
end
else
Writeln('THE MESSAGE QUEUE IS FULL.');
end;
procedure fpgPostMessage(Sender, Dest: TObject; MsgCode: integer); overload;
var
p: PfpgMessageRec;
begin
if Dest = nil then
Exit; //==>
p := fpgAllocateMessage;
if p <> nil then
begin
p^.MsgCode := MsgCode;
p^.Sender := Sender;
p^.Dest := Dest;
p^.Stop := False;
end
else
Writeln('THE MESSAGE QUEUE IS FULL.');
end;
procedure fpgSendMessage(Sender, Dest: TObject; MsgCode: integer; var aparams: TfpgMessageParams); overload;
var
m: TfpgMessageRec;
begin
if Dest = nil then
Exit; //==>
m.MsgCode := MsgCode;
m.Sender := Sender;
m.Dest := Dest;
m.Params := aparams;
try
m.Dest.Dispatch(m)
except
on E: Exception do
{$IFDEF DEBUG}writeln('fpgSendMessage Caught Exception: ' + E.Message){$ENDIF};
end;
end;
procedure fpgSendMessage(Sender, Dest: TObject; MsgCode: integer); overload;
var
m: TfpgMessageRec;
begin
if Dest = nil then
Exit; //==>
m.MsgCode := MsgCode;
m.Sender := Sender;
m.Dest := Dest;
try
m.Dest.Dispatch(m)
except
on E: Exception do
{$IFDEF DEBUG}writeln('fpgSendMessage Caught Exception: ' + E.Message){$ENDIF};
end;
end;
procedure fpgDeliverMessage(var msg: TfpgMessageRec);
var
oItem: TMsgHookItem;
i: integer;
begin
if msg.MsgCode = FPGM_KILLME then
msg.Dest.Free
else
begin
try
msg.Dest.Dispatch(msg);
if fpgApplication.FMessageHookList.Count > 0 then
begin
for i := 0 to fpgApplication.FMessageHookList.Count - 1 do
begin
oItem := TMsgHookItem(fpgApplication.FMessageHookList.Items[i]);
if (msg.Dest = oItem.Dest) and (msg.MsgCode = oItem.MsgCode) then
oItem.Listener.Dispatch(msg);
end;
end;
except
on E: Exception do
{$IFDEF DEBUG}writeln('fpgDeliverMessage Caught Exception: ' + E.Message){$ENDIF};
end;
end;
end;
procedure fpgDeliverMessages;
var
mp: PfpgMessageRec;
m: TfpgMessageRec;
begin
repeat
mp := fpgGetFirstMessage;
if mp <> nil then
begin
m := mp^;
fpgDeleteFirstMessage;
fpgDeliverMessage(m);
end;
until mp = nil;
end;
|