source: branches/Independent/Os.pas

Last change on this file was 68, checked in by chronos, 7 months ago
  • Added: Mouse API support.
File size: 3.7 KB
Line 
1unit Os;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, DateUtils, Api, Console, FileSystem,
7 Mouse;
8
9type
10 TSystem = class;
11
12 TRunningApp = class;
13
14 { TAppThread }
15
16 TAppThread = class(TThread)
17 App: TRunningApp;
18 procedure Execute; override;
19 end;
20
21 { TRunningApp }
22
23 TRunningApp = class
24 private
25 FApp: TApp;
26 procedure SetApp(AValue: TApp);
27 public
28 Context: TAppContext;
29 AppThread: TAppThread;
30 procedure Run;
31 constructor Create;
32 destructor Destroy; override;
33 property App: TApp read FApp write SetApp;
34 end;
35
36 { TRunningApps }
37
38 TRunningApps = class(TObjectList<TRunningApp>)
39 function AddNew(App: TApp; System: TSystem): TRunningApp;
40 end;
41
42 { TWindow }
43
44 TWindow = class
45 Handle: THandle;
46 Name: string;
47 Visible: Boolean;
48 Rect: TRect;
49 end;
50
51 { TWindows }
52
53 TWindows = class(TObjectList<TWindow>)
54 function AddNew: TWindow;
55 function SearchByHandle(Handle: THandle): TWindow;
56 end;
57
58 { TSystem }
59
60 TSystem = class
61 private
62 FOnDraw: TNotifyEvent;
63 public
64 Console: TConsole;
65 FileSystem: TFileSystem;
66 Mouse: TMouse;
67 Apps: TApps;
68 RunningApps: TRunningApps;
69 Windows: TWindows;
70 Handles: THandles;
71 procedure Redraw;
72 procedure RunApp(Name: string);
73 procedure Start;
74 constructor Create;
75 destructor Destroy; override;
76 property OnDraw: TNotifyEvent read FOnDraw write FOnDraw;
77 end;
78
79
80implementation
81
82uses
83 AppCode, AppTest, SystemApi;
84
85{ TAppThread }
86
87procedure TAppThread.Execute;
88begin
89 App.App.Run(App.Context);
90end;
91
92{ TRunningApp }
93
94procedure TRunningApp.SetApp(AValue: TApp);
95begin
96 if FApp = AValue then Exit;
97 FApp := AValue;
98end;
99
100procedure TRunningApp.Run;
101begin
102 AppThread.Start;
103end;
104
105constructor TRunningApp.Create;
106begin
107 Context := TAppContext.Create;
108 AppThread := TAppThread.Create(True);
109 AppThread.App := Self;
110end;
111
112destructor TRunningApp.Destroy;
113begin
114 AppThread.Terminate;
115 AppThread.WaitFor;
116 FreeAndNil(AppThread);
117 FreeAndNil(Context);
118 inherited;
119end;
120
121{ TRunningApps }
122
123function TRunningApps.AddNew(App: TApp; System: TSystem): TRunningApp;
124var
125 SystemApi: TSystemApi;
126begin
127 Result := TRunningApp.Create;
128 SystemApi := TSystemApi.Create;
129 SystemApi.App := Result;
130 SystemApi.System := System;
131 Result.Context.Api := SystemApi;
132 Result.App := App;
133 Add(Result);
134end;
135
136{ TWindows }
137
138function TWindows.AddNew: TWindow;
139begin
140 Result := TWindow.Create;
141 Add(Result);
142end;
143
144function TWindows.SearchByHandle(Handle: THandle): TWindow;
145var
146 I: Integer;
147begin
148 I := 0;
149 while (I < Count) and (Items[I].Handle <> Handle) do Inc(I);
150 if I < Count then Result := Items[I]
151 else Result := nil;
152end;
153
154{ TSystem }
155
156procedure TSystem.Redraw;
157begin
158 if Assigned(FOnDraw) then FOnDraw(Self);
159end;
160
161procedure TSystem.RunApp(Name: string);
162var
163 App: TApp;
164 RunningApp: TRunningApp;
165begin
166 App := Apps.SearchByName(Name);
167 if Assigned(App) then begin
168 RunningApp := RunningApps.AddNew(App, Self);
169 RunningApp.Run;
170 end else raise Exception.Create('App ' + Name + ' not found.');
171end;
172
173procedure TSystem.Start;
174var
175 AppCode: TAppCode;
176begin
177 Apps.AddNew(TAppTest);
178 AppCode := TAppCode(Apps.AddNew(TAppCode));
179 AppCode.Code.Text := FileSystem.LoadFile('Code.exe');
180
181 RunApp('Test');
182 RunApp('Test');
183 RunApp('Code');
184end;
185
186constructor TSystem.Create;
187begin
188 Handles := THandles.Create;
189 Windows := TWindows.Create;
190 Console := TConsole.Create;
191 Mouse := TMouse.Create;
192 FileSystem := TFileSystem.Create;
193 RunningApps := TRunningApps.Create;
194 Apps := TApps.Create;
195end;
196
197destructor TSystem.Destroy;
198begin
199 FreeAndNil(Apps);
200 FreeAndNil(RunningApps);
201 FreeAndNil(Console);
202 FreeAndNil(Mouse);
203 FreeAndNil(FileSystem);
204 FreeAndNil(Windows);
205 FreeAndNil(Handles);
206 inherited;
207end;
208
209end.
210
Note: See TracBrowser for help on using the repository browser.