1 | unit Xvcl.Kernel;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Xvcl.Generics, Xvcl.Classes, Xvcl.Forms, Xvcl.Controls, Xvcl.Graphics;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TKernel = class;
|
---|
10 | TScreen = class;
|
---|
11 |
|
---|
12 | TProcessState = (psReady, psRunning, psWaiting, psFinished);
|
---|
13 |
|
---|
14 | TProcess = class
|
---|
15 | Id: Integer;
|
---|
16 | Name: string;
|
---|
17 | Priority: Integer;
|
---|
18 | State: TProcessState;
|
---|
19 | procedure Execute; virtual;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | TDriver = class
|
---|
23 | Name: string;
|
---|
24 | Kernel: TKernel;
|
---|
25 | procedure Initialize; virtual;
|
---|
26 | procedure Finalize; virtual;
|
---|
27 | end;
|
---|
28 |
|
---|
29 | TScreenCanvas = class(TCanvas)
|
---|
30 | Screen: TScreen;
|
---|
31 | function GetVideoDevice: TVideoDevice; override;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | TScreen = class(TComponent)
|
---|
35 | Canvas: TScreenCanvas;
|
---|
36 | Size: TPoint;
|
---|
37 | Forms: TList<TForm>;
|
---|
38 | VideoDevice: TVideoDevice;
|
---|
39 | procedure Paint;
|
---|
40 | procedure FocusForm(Form: TForm);
|
---|
41 | constructor Create; override;
|
---|
42 | destructor Destroy; override;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | TKeyboard = class
|
---|
46 | Kernel: TKernel;
|
---|
47 | KeysState: array[Word] of Boolean;
|
---|
48 | function ReadKey: Char;
|
---|
49 | function KeyPressed: Boolean;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | TMouse = class
|
---|
53 | Kernel: TKernel;
|
---|
54 | procedure HandleMove(Position: TPoint);
|
---|
55 | procedure HandleDown(Position: TPoint);
|
---|
56 | procedure HandleUp(Position: TPoint);
|
---|
57 | end;
|
---|
58 |
|
---|
59 | TScheduler = class
|
---|
60 | Kernel: TKernel;
|
---|
61 | procedure Reschedule;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | TKernel = class
|
---|
65 | private
|
---|
66 | FOnAfterDriverInit: TNotifyEvent;
|
---|
67 | FOnTick: TNotifyEvent;
|
---|
68 | public
|
---|
69 | Processes: TList<TProcess>;
|
---|
70 | Drivers: TList<TDriver>;
|
---|
71 | StartOnBoot: TList<TApplication>;
|
---|
72 | Screen: TScreen;
|
---|
73 | Keyboard: TKeyboard;
|
---|
74 | Mouse: TMouse;
|
---|
75 | Scheduler: TScheduler;
|
---|
76 | Terminated: Boolean;
|
---|
77 | procedure Boot;
|
---|
78 | procedure PowerOff;
|
---|
79 | procedure Reboot;
|
---|
80 | constructor Create;
|
---|
81 | destructor Destroy; override;
|
---|
82 | property OnAfterDriverInit: TNotifyEvent read FOnAfterDriverInit
|
---|
83 | write FOnAfterDriverInit;
|
---|
84 | property OnTick: TNotifyEvent read FOnTick write FOnTick;
|
---|
85 | end;
|
---|
86 |
|
---|
87 |
|
---|
88 | implementation
|
---|
89 |
|
---|
90 | { TKernel }
|
---|
91 |
|
---|
92 | procedure TKernel.Boot;
|
---|
93 | var
|
---|
94 | Driver: TDriver;
|
---|
95 | App: TApplication;
|
---|
96 | begin
|
---|
97 | for Driver in Drivers do
|
---|
98 | Driver.Initialize;
|
---|
99 |
|
---|
100 | for App in StartOnBoot do
|
---|
101 | App.Run;
|
---|
102 |
|
---|
103 | repeat
|
---|
104 | Scheduler.Reschedule;
|
---|
105 | if Assigned(FOnTick) then FOnTick(Self);
|
---|
106 | until Terminated;
|
---|
107 |
|
---|
108 | for Driver in Drivers do
|
---|
109 | Driver.Finalize;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | constructor TKernel.Create;
|
---|
113 | begin
|
---|
114 | Processes := TList<TProcess>.Create;
|
---|
115 | Drivers := TList<TDriver>.Create;
|
---|
116 | Screen := TScreen.Create;
|
---|
117 | StartOnBoot := TList<TApplication>.Create;
|
---|
118 | Scheduler := TScheduler.Create;
|
---|
119 | Keyboard := TKeyboard.Create;
|
---|
120 | Keyboard.Kernel := Self;
|
---|
121 | Mouse := TMouse.Create;
|
---|
122 | Mouse.Kernel := Self;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | destructor TKernel.Destroy;
|
---|
126 | begin
|
---|
127 | Mouse.Destroy;
|
---|
128 | Keyboard.Destroy;
|
---|
129 | Scheduler.Destroy;
|
---|
130 | StartOnBoot.Destroy;
|
---|
131 | Screen.Destroy;
|
---|
132 | Drivers.Destroy;
|
---|
133 | Processes.Destroy;
|
---|
134 | inherited;
|
---|
135 | end;
|
---|
136 |
|
---|
137 |
|
---|
138 | procedure TKernel.PowerOff;
|
---|
139 | begin
|
---|
140 |
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TKernel.Reboot;
|
---|
144 | begin
|
---|
145 |
|
---|
146 | end;
|
---|
147 |
|
---|
148 | { TScreen }
|
---|
149 |
|
---|
150 | constructor TScreen.Create;
|
---|
151 | begin
|
---|
152 | inherited;
|
---|
153 | Forms := TList<TForm>.Create;
|
---|
154 | Canvas := TScreenCanvas.Create;
|
---|
155 | Canvas.Screen := Self;
|
---|
156 | end;
|
---|
157 |
|
---|
158 | destructor TScreen.Destroy;
|
---|
159 | begin
|
---|
160 | Canvas.Destroy;
|
---|
161 | Forms.Destroy;
|
---|
162 | inherited;
|
---|
163 | end;
|
---|
164 |
|
---|
165 | procedure TScreen.FocusForm(Form: TForm);
|
---|
166 | var
|
---|
167 | I: Integer;
|
---|
168 | FormIndex: Integer;
|
---|
169 | begin
|
---|
170 | FormIndex := Forms.IndexOf(Form);
|
---|
171 | for I := 0 to Forms.Count - 1 do
|
---|
172 | Forms[I].Focused := I = FormIndex;
|
---|
173 |
|
---|
174 | Forms.Move(FormsIndex, Forms.Count - 1);
|
---|
175 | Paint;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TScreen.Paint;
|
---|
179 | var
|
---|
180 | Form: TForm;
|
---|
181 | begin
|
---|
182 | Canvas.Brush.Color := clWhite;
|
---|
183 | Canvas.FillRect(TRectangle.Create(0, 0, Size.X, Size.Y));
|
---|
184 | for Form in Forms do Form.Paint;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | { TDriver }
|
---|
188 |
|
---|
189 | procedure TDriver.Finalize;
|
---|
190 | begin
|
---|
191 |
|
---|
192 | end;
|
---|
193 |
|
---|
194 | procedure TDriver.Initialize;
|
---|
195 | begin
|
---|
196 |
|
---|
197 | end;
|
---|
198 |
|
---|
199 | { TScheduler }
|
---|
200 |
|
---|
201 | procedure TScheduler.Reschedule;
|
---|
202 | begin
|
---|
203 |
|
---|
204 | end;
|
---|
205 |
|
---|
206 | { TKeyboard }
|
---|
207 |
|
---|
208 | function TKeyboard.KeyPressed: Boolean;
|
---|
209 | begin
|
---|
210 | end;
|
---|
211 |
|
---|
212 | function TKeyboard.ReadKey: Char;
|
---|
213 | begin
|
---|
214 |
|
---|
215 | end;
|
---|
216 |
|
---|
217 | { TProcess }
|
---|
218 |
|
---|
219 | procedure TProcess.Execute;
|
---|
220 | begin
|
---|
221 |
|
---|
222 | end;
|
---|
223 |
|
---|
224 | { TMouse }
|
---|
225 |
|
---|
226 | procedure TMouse.HandleDown(Position: TPoint);
|
---|
227 | var
|
---|
228 | Form: TForm;
|
---|
229 | NewMessage: TMessageMouseDown;
|
---|
230 | begin
|
---|
231 | NewMessage := TMessageMouseDown.Create;
|
---|
232 | NewMessage.Position := Position;
|
---|
233 | try
|
---|
234 | for Form in Kernel.Screen.Forms do
|
---|
235 | if Form.Bounds.Contains(Position) then begin
|
---|
236 | if Form.HandleMessage(NewMessage) then begin
|
---|
237 | Break;
|
---|
238 | end;
|
---|
239 | end;
|
---|
240 | finally
|
---|
241 | NewMessage.Destroy;
|
---|
242 | end;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TMouse.HandleMove(Position: TPoint);
|
---|
246 | var
|
---|
247 | Form: TForm;
|
---|
248 | NewMessage: TMessageMouseMove;
|
---|
249 | begin
|
---|
250 | NewMessage := TMessageMouseMove.Create;
|
---|
251 | NewMessage.Position := Position;
|
---|
252 | try
|
---|
253 | for Form in Kernel.Screen.Forms do
|
---|
254 | if Form.Bounds.Contains(Position) then begin
|
---|
255 | if Form.HandleMessage(NewMessage) then begin
|
---|
256 | Break;
|
---|
257 | end;
|
---|
258 | end;
|
---|
259 | finally
|
---|
260 | NewMessage.Destroy;
|
---|
261 | end;
|
---|
262 | end;
|
---|
263 |
|
---|
264 | procedure TMouse.HandleUp(Position: TPoint);
|
---|
265 | var
|
---|
266 | Form: TForm;
|
---|
267 | NewMessage: TMessageMouseUp;
|
---|
268 | begin
|
---|
269 | NewMessage := TMessageMouseUp.Create;
|
---|
270 | NewMessage.Position := Position;
|
---|
271 | try
|
---|
272 | for Form in Kernel.Screen.Forms do
|
---|
273 | if Form.Bounds.Contains(Position) then begin
|
---|
274 | if Form.HandleMessage(NewMessage) then begin
|
---|
275 | Break;
|
---|
276 | end;
|
---|
277 | end;
|
---|
278 | finally
|
---|
279 | NewMessage.Destroy;
|
---|
280 | end;
|
---|
281 | end;
|
---|
282 |
|
---|
283 | { TScrenCanvas }
|
---|
284 |
|
---|
285 | function TScreenCanvas.GetVideoDevice: TVideoDevice;
|
---|
286 | begin
|
---|
287 | if Assigned(Screen) then Result := Screen.VideoDevice
|
---|
288 | else Result := nil;
|
---|
289 | end;
|
---|
290 |
|
---|
291 | end.
|
---|