-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
App.re
253 lines (212 loc) · 7.04 KB
/
App.re
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
module Log = (val Log.withNamespace("Revery.App"));
type delegatedFunc = unit => unit;
type idleFunc = unit => unit;
type canIdleFunc = unit => bool;
let noop = () => ();
type t = {
mutable idleCount: int,
mutable isFirstRender: bool,
windows: Hashtbl.t(int, Window.t),
onIdle: idleFunc,
canIdle: ref(canIdleFunc),
};
type initFunc = t => unit;
let framesToIdle = 10;
type appInitFunc = t => unit;
let getWindows = (app: t) => {
Hashtbl.to_seq_values(app.windows) |> List.of_seq;
};
let getWindowById = (app: t, id: int) => {
Hashtbl.find_opt(app.windows, id);
};
let _tryToClose = (app: t, window: Window.t) => {
let uniqueId = Window.getUniqueId(window);
if (Window.canQuit(window)) {
Log.debugf(m => m("canQuit is true for window %i", uniqueId));
Hashtbl.remove(app.windows, uniqueId);
} else {
Log.debugf(m => m("canQuit is false for window %i", uniqueId));
};
};
let _tryToCloseAll = (app: t) => {
let windows = Hashtbl.to_seq_values(app.windows);
Seq.iter(w => _tryToClose(app, w), windows);
};
let quit = (~askNicely=false, ~code=0, app: t) => {
if (askNicely) {
_tryToCloseAll(app);
};
Revery_Native.uninit();
if (Hashtbl.length(app.windows) == 0 || !askNicely) {
Log.info("Quitting");
exit(code);
};
};
let isIdle = (app: t) => app.idleCount >= framesToIdle;
let _mainThreadMutex = Mutex.create();
/* A list of pending functions the main thread will need to run */
let _mainThreadPendingFunctions: ref(list(delegatedFunc)) = ref([]);
let _anyPendingWork: ref(bool) = ref(false);
let runOnMainThread = f => {
Mutex.lock(_mainThreadMutex);
_mainThreadPendingFunctions := [f, ..._mainThreadPendingFunctions^];
_anyPendingWork := true;
Mutex.unlock(_mainThreadMutex);
// If we're 'idle' - in a [waitTimeout], dispatch an event to wake up the main thread
Sdl2.Event.push();
};
let _anyPendingMainThreadJobs = () => {
_anyPendingWork^;
};
let setCanIdle = (f: canIdleFunc, app: t) => {
app.canIdle := f;
};
/* Execute any pending main thread jobs */
let _doPendingMainThreadJobs = () => {
let jobs = {
Mutex.lock(_mainThreadMutex);
let ret = _mainThreadPendingFunctions^;
_anyPendingWork := false;
_mainThreadPendingFunctions := [];
Mutex.unlock(_mainThreadMutex);
ret;
};
jobs |> List.rev |> List.iter(f => f());
};
let flushPendingCallbacks = () => _doPendingMainThreadJobs();
let createWindow =
(~createOptions=WindowCreateOptions.default, app: t, windowName) => {
let w = Window.create(windowName, createOptions);
/* Window.render(w) */
let uniqueId = Window.getUniqueId(w);
Hashtbl.add(app.windows, uniqueId, w);
w;
};
let _anyWindowsDirty = (app: t) =>
List.fold_left(
(prev, w) => prev || Window.isDirty(w),
false,
getWindows(app),
);
let initConsole = () =>
if (Sys.win32) {
// First, try attaching to an existing console.
let attachResult = Sdl2.Platform.win32AttachConsole();
// If that wasn't available - try to allocate a new one.
let _code =
if (attachResult == 0) {
Sdl2.Platform.win32AllocConsole();
} else {
attachResult;
};
();
};
let start = (~onIdle=noop, initFunc: appInitFunc) => {
let appInstance: t = {
windows: Hashtbl.create(1),
idleCount: 0,
isFirstRender: true,
onIdle,
canIdle: ref(() => true),
};
let _ = Sdl2.init();
let _dispose = initFunc(appInstance);
let _ = Sdl2.ScreenSaver.enable();
let _handleEvent = evt => {
let handleEvent = windowID => {
let window = getWindowById(appInstance, windowID);
switch (window) {
| Some(win) => Window.handleEvent(evt, win)
| None =>
Log.errorf(m =>
m(
"Unable to find window with ID: %i - event: %s",
windowID,
Sdl2.Event.show(evt),
)
)
};
};
switch (evt) {
| Sdl2.Event.MouseButtonUp({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.MouseButtonDown({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.MouseMotion({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.MouseWheel({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.KeyDown({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.KeyUp({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.TextInput({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.TextEditing({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowResized({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowSizeChanged({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowExposed({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowFocusGained({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowFocusLost({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowMaximized({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowMinimized({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowRestored({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowMoved({windowID, _}) => handleEvent(windowID)
| Sdl2.Event.WindowEnter({windowID}) => handleEvent(windowID)
| Sdl2.Event.WindowLeave({windowID}) => handleEvent(windowID)
| Sdl2.Event.WindowClosed({windowID, _}) =>
Log.debugf(m => m("Got WindowClosed event for %i", windowID));
handleEvent(windowID);
switch (getWindowById(appInstance, windowID)) {
| None => ()
| Some(win) => _tryToClose(appInstance, win)
};
| Sdl2.Event.Quit =>
// Sometimes, on Mac, we could get a 'quit' without a
// corresponding WindowClosed event - this can happen
// if Command+Q is pressed. In that case, we'll try
// closing all the windows - and if they all close,
// we'll exit the app.
quit(~askNicely=true, ~code=0, appInstance)
| _ => ()
};
};
let _flushEvents = () => {
let processingEvents = ref(true);
while (processingEvents^) {
let evt = Sdl2.Event.poll();
switch (evt) {
| None => processingEvents := false
| Some(v) => _handleEvent(v)
};
};
};
Revery_Native.init();
let appLoop = () => {
_flushEvents();
Tick.Default.pump();
if (appInstance.isFirstRender
|| _anyWindowsDirty(appInstance)
|| _anyPendingMainThreadJobs()
|| !appInstance.canIdle^()) {
if (appInstance.idleCount > 0) {
Log.debug("Upshifting into active state.");
};
Performance.bench("_doPendingMainThreadJobs", () =>
_doPendingMainThreadJobs()
);
Performance.bench("renderWindows", () => {
List.iter(w => Window.render(w), getWindows(appInstance))
});
appInstance.idleCount = 0;
appInstance.isFirstRender = false;
} else {
appInstance.idleCount = appInstance.idleCount + 1;
if (appInstance.idleCount === framesToIdle) {
Log.debug("Downshifting into idle state...");
appInstance.onIdle();
};
let evt = Sdl2.Event.waitTimeout(250);
switch (evt) {
| None => ()
| Some(evt) => _handleEvent(evt)
};
};
Environment.yield();
false;
};
Sdl2.renderLoop(appLoop);
};