Merge: Give top-level methods some rules
[nit.git] / lib / android / native_app_glue.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Some documentation of this module has been adapted from the documentation
18 # of the Android NDK projet.
19
20 # Wrapper of the Android native_app_glue framework to implement app.nit
21 #
22 # The framework provides 3 different structures for a single C application
23 # under Android. We use all 3 structures in this API to implement app.nit
24 # on Android. Each structure is wrapped in a Nit extern class:
25 #
26 # * `NativeAppGlue` is the lowest level class, it is implemented by the C
27 # structure `android_app`. It offers features on the main Android thread
28 # (not on the same thread as Nit). For this reason, prefer to use
29 # `NdkNativeActivity`.
30 #
31 # * `NdkNativeActivity` is implemented by the C structure `ANativeActivity`. It
32 # is on the same thread as Nit and manages the synchronization with the
33 # main Android thread.
34 #
35 # * `NativeActivity` is implemented in Java by `android.app.NativeActivity`,
36 # which is a subclass of `Activity` and `Context` (in Java). It represent
37 # main activity of the running application. Use it to get anything related
38 # to the `Context` and as anchor to execute Java UI code.
39 module native_app_glue
40
41 import platform
42 import log
43
44 in "C header" `{
45 #include <android_native_app_glue.h>
46 `}
47
48 in "C body" `{
49 struct android_app* native_app_glue_data;
50
51 // Entry point called by the native_app_glue_framework framework
52 // We relay the call to the Nit application.
53 void android_main(struct android_app* app) {
54 native_app_glue_data = app;
55 app_dummy();
56 main(0, NULL);
57 }
58
59 // Main callback on the native_app_glue framework
60 //
61 // We relay everything to our App.
62 static void app_cmd_handler(struct android_app* app, int32_t cmd) {
63 App nit_app = app->userData;
64 switch (cmd) {
65 case APP_CMD_SAVE_STATE:
66 App_save_state(nit_app);
67 break;
68 case APP_CMD_INIT_WINDOW:
69 App_init_window(nit_app);
70 break;
71 case APP_CMD_TERM_WINDOW:
72 App_term_window(nit_app);
73 break;
74 case APP_CMD_GAINED_FOCUS:
75 App_gained_focus(nit_app);
76 break;
77 case APP_CMD_LOST_FOCUS:
78 App_lost_focus(nit_app);
79 break;
80 case APP_CMD_PAUSE:
81 App_pause(nit_app);
82 break;
83 case APP_CMD_STOP:
84 App_stop(nit_app);
85 break;
86 case APP_CMD_DESTROY:
87 App_destroy(nit_app);
88 break;
89 case APP_CMD_START:
90 App_start(nit_app);
91 break;
92 case APP_CMD_RESUME:
93 App_resume(nit_app);
94 break;
95 case APP_CMD_LOW_MEMORY:
96 App_low_memory(nit_app);
97 break;
98 case APP_CMD_CONFIG_CHANGED:
99 App_config_changed(nit_app);
100 break;
101 case APP_CMD_INPUT_CHANGED:
102 App_input_changed(nit_app);
103 break;
104 case APP_CMD_WINDOW_RESIZED:
105 App_window_resized(nit_app);
106 break;
107 case APP_CMD_WINDOW_REDRAW_NEEDED:
108 App_window_redraw_needed(nit_app);
109 break;
110 case APP_CMD_CONTENT_RECT_CHANGED:
111 App_content_rect_changed(nit_app);
112 break;
113 }
114 }
115 `}
116
117 # Android SDK's `android.app.NativeActivity`.
118 #
119 # Can be used to get anything related to the `Context` of the activity in Java
120 # and as anchor to execute Java UI code.
121 extern class NativeActivity in "Java" `{ android.app.NativeActivity `}
122 super JavaObject
123 end
124
125 redef class App
126 redef fun setup
127 do
128 var native_app_glue = native_app_glue
129 native_app_glue.user_data = self
130
131 set_as_cmd_handler(native_app_glue)
132 end
133
134 # The underlying implementation using the Android native_app_glue framework
135 fun native_app_glue: NativeAppGlue `{ return native_app_glue_data; `}
136
137 # The main Java Activity of this application
138 fun native_activity: NativeActivity do return native_app_glue.ndk_native_activity.java_native_activity
139
140 # Set `native_app_glue` command handler to our C implementation which
141 # will callback self.
142 private fun set_as_cmd_handler(native_app_glue: NativeAppGlue) import save_state,
143 init_window, term_window, gained_focus, lost_focus, pause, stop, destroy,
144 start, resume, low_memory, config_changed, input_changed, window_resized,
145 window_redraw_needed, content_rect_changed `{
146 native_app_glue->onAppCmd = &app_cmd_handler;
147 `}
148
149 # Notification from the Android framework to generate a new saved state
150 #
151 # You can use the `shared_preferences` module or `NativeAppGlue::saved_state`.
152 fun save_state do end
153
154 # Notification from the native_app glue framework, a new ANativeWindow is ready
155 #
156 # When called, `NativeAppGlue::window` returns a poiter to the new window surface.
157 fun init_window do end
158
159 # Notification from the native_app glue framework, the existing window needs to be terminated
160 #
161 # Upon receiving this command, `native_app_glue.window` still contains the existing window.
162 fun term_window do end
163
164 # Notification from the Android framework, `native_activity` has gained focus
165 fun gained_focus do end
166
167 # Notification from the Android framework, `native_activity` has lost focus
168 fun lost_focus do end
169
170 # Notification from the Android framework, your app has been paused
171 fun pause do end
172
173 # Notification from the Android framework, your app has been stopped
174 fun stop do end
175
176 # Notification from the Android framework, `native_activity` is being destroyed
177 #
178 # Clean up and exit.
179 fun destroy do end
180
181 # Notification from the Android framework, `native_activity` has been started
182 fun start do end
183
184 # Notification from the Android framework, `native_activity` has been resumed
185 fun resume do end
186
187 # Notification from the Android framework, the system is running low on memory
188 #
189 # Try to reduce your memory use.
190 fun low_memory do end
191
192 # Notification from the Android framework, the current device configuration has changed
193 fun config_changed do end
194
195 # Notification from the Android framework, `native_app_glue.input_queue` has changed
196 fun input_changed do end
197
198 # Notification from the Android framework, the window has been resized.
199 #
200 # Please redraw with its new size.
201 fun window_resized do end
202
203 # Notification from the Android framework, the current ANativeWindow must be redrawn
204 fun window_redraw_needed do end
205
206 # Notification from the Android framework, the content area of the window has changed
207 #
208 # Raised when the soft input window being shown or hidden, and similar events.
209 fun content_rect_changed do end
210
211 # Call the `ALooper` to retrieve events and callback the application
212 fun poll_looper(timeout_ms: Int) import handle_looper_event `{
213 int ident;
214 int event;
215 void* source;
216 while ((ident=ALooper_pollAll(timeout_ms, NULL, &event, &source)) >= 0) {
217 App_handle_looper_event(recv, ident, event, source);
218 }
219 `}
220
221 # Handle an event retrieved by the `ALooper` and `poll_looper` without a callback
222 protected fun handle_looper_event(ident, event: Int, data: Pointer) import native_app_glue,
223 save_state, init_window, term_window, gained_focus, lost_focus, pause, stop,
224 destroy, start, resume, low_memory, config_changed, input_changed,
225 window_resized, window_redraw_needed, content_rect_changed `{
226
227 struct android_app *app_glue = App_native_app_glue(recv);
228 struct android_poll_source* source = (struct android_poll_source*)data;
229
230 // Process this event.
231 if (source != NULL) source->process(app_glue, source);
232 `}
233 end
234
235 # An Android activity implemented in C. This is the C part of `NativeActivity`
236 # which is the Java part.
237 #
238 # The callbacks at this level are synchronous on the UI thread. Thus app.nit
239 # do not use them, and instead rely on `NativeAppGlue`.
240 extern class NdkNativeActivity `{ ANativeActivity * `}
241
242 # Callbacks on the main thread
243 # FIXME This would not yet be usable, to implement when Nit has threads
244 #fun set_callbacks_handler(handler: App) or callbacks= ...
245
246 # Java VM associated to `self`
247 fun vm: JavaVM `{ return recv->vm; `}
248
249 # JNI environmnet associated to `self`
250 fun env: JniEnv `{ return recv->env; `}
251
252 # The `NativeActivity`, as in the Java object, associated to `self`
253 fun java_native_activity: NativeActivity `{ return recv->clazz; `}
254
255 # Path to this application's internal data directory.
256 fun internal_data_path: NativeString `{ return (char*)recv->internalDataPath; `}
257
258 # Path to this application's external (removable/mountable) data directory.
259 fun external_data_path: NativeString `{ return (char*)recv->externalDataPath; `}
260
261 # The platform's SDK version code.
262 fun sdk_version: Int `{ return recv->sdkVersion; `}
263
264 # This is the native instance of the application. It is not used by
265 # the framework, but can be set by the application to its own instance
266 # state.
267 fun instance: Pointer `{ return recv->instance; `}
268
269 # Pointer to the Asset Manager instance for the application. The application
270 # uses this to access binary assets bundled inside its own .apk file.
271 #
272 # TODO activate in a future `asset_manager` module if it cannot be done in Java
273 #fun asset_manager: AssetManager `{ return recv->assetManager; `}
274
275 # Available starting with Honeycomb: path to the directory containing
276 # the application's OBB files (if any). If the app doesn't have any
277 # OBB files, this directory may not exist.
278 # api?
279 #
280 # TODO activate in a future module at API 11
281 #fun obb_path: NativeString `{ return (char*)recv->obbPath; `}
282 end
283
284 # This is the interface for the standard glue code of a threaded
285 # application. In this model, the application's code is running
286 # in its own thread separate from the main thread of the process.
287 # It is not required that this thread be associated with the Java
288 # VM, although it will need to be in order to make JNI calls any
289 # Java objects.
290 extern class NativeAppGlue `{ struct android_app* `}
291 # We use the `userData` field of the C structure to store an handle to
292 # the associated App
293 private fun user_data: App `{ return recv->userData; `}
294 private fun user_data=(val: App) `{ recv->userData = val; `}
295
296 # Fill this in with the function to process input events. At this point
297 # the event has already been pre-dispatched, and it will be finished upon
298 # return. Return 1 if you have handled the event, 0 for any default
299 # dispatching.
300 #int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
301 #fun set_input_event_handler(handler: App) `{ `}
302
303 # The ANativeActivity object instance that this app is running in.
304 fun ndk_native_activity: NdkNativeActivity `{ return recv->activity; `}
305
306 # The current configuration the app is running in.
307 fun config: AConfiguration `{ return recv->config; `}
308
309 # This is the last instance's saved state, as provided at creation time.
310 # It is NULL if there was no state. You can use this as you need; the
311 # memory will remain around until you call android_app_exec_cmd() for
312 # APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
313 # These variables should only be changed when processing a APP_CMD_SAVE_STATE,
314 # at which point they will be initialized to NULL and you can malloc your
315 # state and place the information here. In that case the memory will be
316 # freed for you later.
317 fun saved_state: Pointer `{ return recv->savedState; `}
318 fun saved_state_size: Int `{ return recv->savedStateSize; `}
319
320 # The ALooper associated with the app's thread.
321 fun looper: ALooper `{ return recv->looper; `}
322
323 # When non-NULL, this is the input queue from which the app will
324 # receive user input events.
325 fun input_queue: AInputQueue `{ return recv->inputQueue; `}
326
327 # When non-NULL, this is the window surface that the app can draw in.
328 fun window: ANativeWindow `{ return recv->window; `}
329
330 # Current content rectangle of the window; this is the area where the
331 # window's content should be placed to be seen by the user.
332 #
333 # TODO activate when we know what to return (returns a struct not a pointer)
334 #fun content_recv: ARect `{ return recv->contentRect; `}
335
336 # Current state of the app's activity. May be either APP_CMD_START,
337 # APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below.
338 fun activity_state: Int `{ return recv->activityState; `}
339
340 # This is non-zero when the application's NativeActivity is being
341 # destroyed and waiting for the app thread to complete.
342 fun detroy_request: Bool `{ return recv->destroyRequested; `}
343 end
344
345 # Android NDK's struture holding configurations of the native app
346 extern class AConfiguration `{ AConfiguration* `}
347 end
348
349 # Android NDK's structure to handle events synchronously
350 extern class ALooper `{ ALooper* `}
351 # Returns the looper associated with the calling thread, or NULL if there is not one
352 new for_thread `{ return ALooper_forThread(); `}
353 end
354
355 # Android NDK's struture to handle input events synchronously
356 extern class AInputQueue `{ AInputQueue* `}
357 end
358
359 # Android NDK's structure to control the native window for drawing
360 extern class ANativeWindow `{ ANativeWindow* `}
361 end