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