rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 * Copyright (C) 2010 The Android Open Source Project
|
nuclear@0
|
3 *
|
nuclear@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
nuclear@0
|
5 * you may not use this file except in compliance with the License.
|
nuclear@0
|
6 * You may obtain a copy of the License at
|
nuclear@0
|
7 *
|
nuclear@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
nuclear@0
|
9 *
|
nuclear@0
|
10 * Unless required by applicable law or agreed to in writing, software
|
nuclear@0
|
11 * distributed under the License is distributed on an "AS IS" BASIS,
|
nuclear@0
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nuclear@0
|
13 * See the License for the specific language governing permissions and
|
nuclear@0
|
14 * limitations under the License.
|
nuclear@0
|
15 *
|
nuclear@0
|
16 */
|
nuclear@0
|
17
|
nuclear@0
|
18 #ifndef _ANDROID_NATIVE_APP_GLUE_H
|
nuclear@0
|
19 #define _ANDROID_NATIVE_APP_GLUE_H
|
nuclear@0
|
20
|
nuclear@0
|
21 #include <poll.h>
|
nuclear@0
|
22 #include <pthread.h>
|
nuclear@0
|
23 #include <sched.h>
|
nuclear@0
|
24
|
nuclear@0
|
25 #include <android/configuration.h>
|
nuclear@0
|
26 #include <android/looper.h>
|
nuclear@0
|
27 #include <android/native_activity.h>
|
nuclear@0
|
28
|
nuclear@0
|
29 #ifdef __cplusplus
|
nuclear@0
|
30 extern "C" {
|
nuclear@0
|
31 #endif
|
nuclear@0
|
32
|
nuclear@0
|
33 /**
|
nuclear@0
|
34 * The native activity interface provided by <android/native_activity.h>
|
nuclear@0
|
35 * is based on a set of application-provided callbacks that will be called
|
nuclear@0
|
36 * by the Activity's main thread when certain events occur.
|
nuclear@0
|
37 *
|
nuclear@0
|
38 * This means that each one of this callbacks _should_ _not_ block, or they
|
nuclear@0
|
39 * risk having the system force-close the application. This programming
|
nuclear@0
|
40 * model is direct, lightweight, but constraining.
|
nuclear@0
|
41 *
|
nuclear@0
|
42 * The 'android_native_app_glue' static library is used to provide a different
|
nuclear@0
|
43 * execution model where the application can implement its own main event
|
nuclear@0
|
44 * loop in a different thread instead. Here's how it works:
|
nuclear@0
|
45 *
|
nuclear@0
|
46 * 1/ The application must provide a function named "android_main()" that
|
nuclear@0
|
47 * will be called when the activity is created, in a new thread that is
|
nuclear@0
|
48 * distinct from the activity's main thread.
|
nuclear@0
|
49 *
|
nuclear@0
|
50 * 2/ android_main() receives a pointer to a valid "android_app" structure
|
nuclear@0
|
51 * that contains references to other important objects, e.g. the
|
nuclear@0
|
52 * ANativeActivity obejct instance the application is running in.
|
nuclear@0
|
53 *
|
nuclear@0
|
54 * 3/ the "android_app" object holds an ALooper instance that already
|
nuclear@0
|
55 * listens to two important things:
|
nuclear@0
|
56 *
|
nuclear@0
|
57 * - activity lifecycle events (e.g. "pause", "resume"). See APP_CMD_XXX
|
nuclear@0
|
58 * declarations below.
|
nuclear@0
|
59 *
|
nuclear@0
|
60 * - input events coming from the AInputQueue attached to the activity.
|
nuclear@0
|
61 *
|
nuclear@0
|
62 * Each of these correspond to an ALooper identifier returned by
|
nuclear@0
|
63 * ALooper_pollOnce with values of LOOPER_ID_MAIN and LOOPER_ID_INPUT,
|
nuclear@0
|
64 * respectively.
|
nuclear@0
|
65 *
|
nuclear@0
|
66 * Your application can use the same ALooper to listen to additional
|
nuclear@0
|
67 * file-descriptors. They can either be callback based, or with return
|
nuclear@0
|
68 * identifiers starting with LOOPER_ID_USER.
|
nuclear@0
|
69 *
|
nuclear@0
|
70 * 4/ Whenever you receive a LOOPER_ID_MAIN or LOOPER_ID_INPUT event,
|
nuclear@0
|
71 * the returned data will point to an android_poll_source structure. You
|
nuclear@0
|
72 * can call the process() function on it, and fill in android_app->onAppCmd
|
nuclear@0
|
73 * and android_app->onInputEvent to be called for your own processing
|
nuclear@0
|
74 * of the event.
|
nuclear@0
|
75 *
|
nuclear@0
|
76 * Alternatively, you can call the low-level functions to read and process
|
nuclear@0
|
77 * the data directly... look at the process_cmd() and process_input()
|
nuclear@0
|
78 * implementations in the glue to see how to do this.
|
nuclear@0
|
79 *
|
nuclear@0
|
80 * See the sample named "native-activity" that comes with the NDK with a
|
nuclear@0
|
81 * full usage example. Also look at the JavaDoc of NativeActivity.
|
nuclear@0
|
82 */
|
nuclear@0
|
83
|
nuclear@0
|
84 struct android_app;
|
nuclear@0
|
85
|
nuclear@0
|
86 /**
|
nuclear@0
|
87 * Data associated with an ALooper fd that will be returned as the "outData"
|
nuclear@0
|
88 * when that source has data ready.
|
nuclear@0
|
89 */
|
nuclear@0
|
90 struct android_poll_source {
|
nuclear@0
|
91 // The identifier of this source. May be LOOPER_ID_MAIN or
|
nuclear@0
|
92 // LOOPER_ID_INPUT.
|
nuclear@0
|
93 int32_t id;
|
nuclear@0
|
94
|
nuclear@0
|
95 // The android_app this ident is associated with.
|
nuclear@0
|
96 struct android_app* app;
|
nuclear@0
|
97
|
nuclear@0
|
98 // Function to call to perform the standard processing of data from
|
nuclear@0
|
99 // this source.
|
nuclear@0
|
100 void (*process)(struct android_app* app, struct android_poll_source* source);
|
nuclear@0
|
101 };
|
nuclear@0
|
102
|
nuclear@0
|
103 /**
|
nuclear@0
|
104 * This is the interface for the standard glue code of a threaded
|
nuclear@0
|
105 * application. In this model, the application's code is running
|
nuclear@0
|
106 * in its own thread separate from the main thread of the process.
|
nuclear@0
|
107 * It is not required that this thread be associated with the Java
|
nuclear@0
|
108 * VM, although it will need to be in order to make JNI calls any
|
nuclear@0
|
109 * Java objects.
|
nuclear@0
|
110 */
|
nuclear@0
|
111 struct android_app {
|
nuclear@0
|
112 // The application can place a pointer to its own state object
|
nuclear@0
|
113 // here if it likes.
|
nuclear@0
|
114 void* userData;
|
nuclear@0
|
115
|
nuclear@0
|
116 // Fill this in with the function to process main app commands (APP_CMD_*)
|
nuclear@0
|
117 void (*onAppCmd)(struct android_app* app, int32_t cmd);
|
nuclear@0
|
118
|
nuclear@0
|
119 // Fill this in with the function to process input events. At this point
|
nuclear@0
|
120 // the event has already been pre-dispatched, and it will be finished upon
|
nuclear@0
|
121 // return. Return 1 if you have handled the event, 0 for any default
|
nuclear@0
|
122 // dispatching.
|
nuclear@0
|
123 int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
|
nuclear@0
|
124
|
nuclear@0
|
125 // The ANativeActivity object instance that this app is running in.
|
nuclear@0
|
126 ANativeActivity* activity;
|
nuclear@0
|
127
|
nuclear@0
|
128 // The current configuration the app is running in.
|
nuclear@0
|
129 AConfiguration* config;
|
nuclear@0
|
130
|
nuclear@0
|
131 // This is the last instance's saved state, as provided at creation time.
|
nuclear@0
|
132 // It is NULL if there was no state. You can use this as you need; the
|
nuclear@0
|
133 // memory will remain around until you call android_app_exec_cmd() for
|
nuclear@0
|
134 // APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
|
nuclear@0
|
135 // These variables should only be changed when processing a APP_CMD_SAVE_STATE,
|
nuclear@0
|
136 // at which point they will be initialized to NULL and you can malloc your
|
nuclear@0
|
137 // state and place the information here. In that case the memory will be
|
nuclear@0
|
138 // freed for you later.
|
nuclear@0
|
139 void* savedState;
|
nuclear@0
|
140 size_t savedStateSize;
|
nuclear@0
|
141
|
nuclear@0
|
142 // The ALooper associated with the app's thread.
|
nuclear@0
|
143 ALooper* looper;
|
nuclear@0
|
144
|
nuclear@0
|
145 // When non-NULL, this is the input queue from which the app will
|
nuclear@0
|
146 // receive user input events.
|
nuclear@0
|
147 AInputQueue* inputQueue;
|
nuclear@0
|
148
|
nuclear@0
|
149 // When non-NULL, this is the window surface that the app can draw in.
|
nuclear@0
|
150 ANativeWindow* window;
|
nuclear@0
|
151
|
nuclear@0
|
152 // Current content rectangle of the window; this is the area where the
|
nuclear@0
|
153 // window's content should be placed to be seen by the user.
|
nuclear@0
|
154 ARect contentRect;
|
nuclear@0
|
155
|
nuclear@0
|
156 // Current state of the app's activity. May be either APP_CMD_START,
|
nuclear@0
|
157 // APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below.
|
nuclear@0
|
158 int activityState;
|
nuclear@0
|
159
|
nuclear@0
|
160 // This is non-zero when the application's NativeActivity is being
|
nuclear@0
|
161 // destroyed and waiting for the app thread to complete.
|
nuclear@0
|
162 int destroyRequested;
|
nuclear@0
|
163
|
nuclear@0
|
164 // -------------------------------------------------
|
nuclear@0
|
165 // Below are "private" implementation of the glue code.
|
nuclear@0
|
166
|
nuclear@0
|
167 pthread_mutex_t mutex;
|
nuclear@0
|
168 pthread_cond_t cond;
|
nuclear@0
|
169
|
nuclear@0
|
170 int msgread;
|
nuclear@0
|
171 int msgwrite;
|
nuclear@0
|
172
|
nuclear@0
|
173 pthread_t thread;
|
nuclear@0
|
174
|
nuclear@0
|
175 struct android_poll_source cmdPollSource;
|
nuclear@0
|
176 struct android_poll_source inputPollSource;
|
nuclear@0
|
177
|
nuclear@0
|
178 int running;
|
nuclear@0
|
179 int stateSaved;
|
nuclear@0
|
180 int destroyed;
|
nuclear@0
|
181 int redrawNeeded;
|
nuclear@0
|
182 AInputQueue* pendingInputQueue;
|
nuclear@0
|
183 ANativeWindow* pendingWindow;
|
nuclear@0
|
184 ARect pendingContentRect;
|
nuclear@0
|
185 };
|
nuclear@0
|
186
|
nuclear@0
|
187 enum {
|
nuclear@0
|
188 /**
|
nuclear@0
|
189 * Looper data ID of commands coming from the app's main thread, which
|
nuclear@0
|
190 * is returned as an identifier from ALooper_pollOnce(). The data for this
|
nuclear@0
|
191 * identifier is a pointer to an android_poll_source structure.
|
nuclear@0
|
192 * These can be retrieved and processed with android_app_read_cmd()
|
nuclear@0
|
193 * and android_app_exec_cmd().
|
nuclear@0
|
194 */
|
nuclear@0
|
195 LOOPER_ID_MAIN = 1,
|
nuclear@0
|
196
|
nuclear@0
|
197 /**
|
nuclear@0
|
198 * Looper data ID of events coming from the AInputQueue of the
|
nuclear@0
|
199 * application's window, which is returned as an identifier from
|
nuclear@0
|
200 * ALooper_pollOnce(). The data for this identifier is a pointer to an
|
nuclear@0
|
201 * android_poll_source structure. These can be read via the inputQueue
|
nuclear@0
|
202 * object of android_app.
|
nuclear@0
|
203 */
|
nuclear@0
|
204 LOOPER_ID_INPUT = 2,
|
nuclear@0
|
205
|
nuclear@0
|
206 /**
|
nuclear@0
|
207 * Start of user-defined ALooper identifiers.
|
nuclear@0
|
208 */
|
nuclear@0
|
209 LOOPER_ID_USER = 3,
|
nuclear@0
|
210 };
|
nuclear@0
|
211
|
nuclear@0
|
212 enum {
|
nuclear@0
|
213 /**
|
nuclear@0
|
214 * Command from main thread: the AInputQueue has changed. Upon processing
|
nuclear@0
|
215 * this command, android_app->inputQueue will be updated to the new queue
|
nuclear@0
|
216 * (or NULL).
|
nuclear@0
|
217 */
|
nuclear@0
|
218 APP_CMD_INPUT_CHANGED,
|
nuclear@0
|
219
|
nuclear@0
|
220 /**
|
nuclear@0
|
221 * Command from main thread: a new ANativeWindow is ready for use. Upon
|
nuclear@0
|
222 * receiving this command, android_app->window will contain the new window
|
nuclear@0
|
223 * surface.
|
nuclear@0
|
224 */
|
nuclear@0
|
225 APP_CMD_INIT_WINDOW,
|
nuclear@0
|
226
|
nuclear@0
|
227 /**
|
nuclear@0
|
228 * Command from main thread: the existing ANativeWindow needs to be
|
nuclear@0
|
229 * terminated. Upon receiving this command, android_app->window still
|
nuclear@0
|
230 * contains the existing window; after calling android_app_exec_cmd
|
nuclear@0
|
231 * it will be set to NULL.
|
nuclear@0
|
232 */
|
nuclear@0
|
233 APP_CMD_TERM_WINDOW,
|
nuclear@0
|
234
|
nuclear@0
|
235 /**
|
nuclear@0
|
236 * Command from main thread: the current ANativeWindow has been resized.
|
nuclear@0
|
237 * Please redraw with its new size.
|
nuclear@0
|
238 */
|
nuclear@0
|
239 APP_CMD_WINDOW_RESIZED,
|
nuclear@0
|
240
|
nuclear@0
|
241 /**
|
nuclear@0
|
242 * Command from main thread: the system needs that the current ANativeWindow
|
nuclear@0
|
243 * be redrawn. You should redraw the window before handing this to
|
nuclear@0
|
244 * android_app_exec_cmd() in order to avoid transient drawing glitches.
|
nuclear@0
|
245 */
|
nuclear@0
|
246 APP_CMD_WINDOW_REDRAW_NEEDED,
|
nuclear@0
|
247
|
nuclear@0
|
248 /**
|
nuclear@0
|
249 * Command from main thread: the content area of the window has changed,
|
nuclear@0
|
250 * such as from the soft input window being shown or hidden. You can
|
nuclear@0
|
251 * find the new content rect in android_app::contentRect.
|
nuclear@0
|
252 */
|
nuclear@0
|
253 APP_CMD_CONTENT_RECT_CHANGED,
|
nuclear@0
|
254
|
nuclear@0
|
255 /**
|
nuclear@0
|
256 * Command from main thread: the app's activity window has gained
|
nuclear@0
|
257 * input focus.
|
nuclear@0
|
258 */
|
nuclear@0
|
259 APP_CMD_GAINED_FOCUS,
|
nuclear@0
|
260
|
nuclear@0
|
261 /**
|
nuclear@0
|
262 * Command from main thread: the app's activity window has lost
|
nuclear@0
|
263 * input focus.
|
nuclear@0
|
264 */
|
nuclear@0
|
265 APP_CMD_LOST_FOCUS,
|
nuclear@0
|
266
|
nuclear@0
|
267 /**
|
nuclear@0
|
268 * Command from main thread: the current device configuration has changed.
|
nuclear@0
|
269 */
|
nuclear@0
|
270 APP_CMD_CONFIG_CHANGED,
|
nuclear@0
|
271
|
nuclear@0
|
272 /**
|
nuclear@0
|
273 * Command from main thread: the system is running low on memory.
|
nuclear@0
|
274 * Try to reduce your memory use.
|
nuclear@0
|
275 */
|
nuclear@0
|
276 APP_CMD_LOW_MEMORY,
|
nuclear@0
|
277
|
nuclear@0
|
278 /**
|
nuclear@0
|
279 * Command from main thread: the app's activity has been started.
|
nuclear@0
|
280 */
|
nuclear@0
|
281 APP_CMD_START,
|
nuclear@0
|
282
|
nuclear@0
|
283 /**
|
nuclear@0
|
284 * Command from main thread: the app's activity has been resumed.
|
nuclear@0
|
285 */
|
nuclear@0
|
286 APP_CMD_RESUME,
|
nuclear@0
|
287
|
nuclear@0
|
288 /**
|
nuclear@0
|
289 * Command from main thread: the app should generate a new saved state
|
nuclear@0
|
290 * for itself, to restore from later if needed. If you have saved state,
|
nuclear@0
|
291 * allocate it with malloc and place it in android_app.savedState with
|
nuclear@0
|
292 * the size in android_app.savedStateSize. The will be freed for you
|
nuclear@0
|
293 * later.
|
nuclear@0
|
294 */
|
nuclear@0
|
295 APP_CMD_SAVE_STATE,
|
nuclear@0
|
296
|
nuclear@0
|
297 /**
|
nuclear@0
|
298 * Command from main thread: the app's activity has been paused.
|
nuclear@0
|
299 */
|
nuclear@0
|
300 APP_CMD_PAUSE,
|
nuclear@0
|
301
|
nuclear@0
|
302 /**
|
nuclear@0
|
303 * Command from main thread: the app's activity has been stopped.
|
nuclear@0
|
304 */
|
nuclear@0
|
305 APP_CMD_STOP,
|
nuclear@0
|
306
|
nuclear@0
|
307 /**
|
nuclear@0
|
308 * Command from main thread: the app's activity is being destroyed,
|
nuclear@0
|
309 * and waiting for the app thread to clean up and exit before proceeding.
|
nuclear@0
|
310 */
|
nuclear@0
|
311 APP_CMD_DESTROY,
|
nuclear@0
|
312 };
|
nuclear@0
|
313
|
nuclear@0
|
314 /**
|
nuclear@0
|
315 * Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next
|
nuclear@0
|
316 * app command message.
|
nuclear@0
|
317 */
|
nuclear@0
|
318 int8_t android_app_read_cmd(struct android_app* android_app);
|
nuclear@0
|
319
|
nuclear@0
|
320 /**
|
nuclear@0
|
321 * Call with the command returned by android_app_read_cmd() to do the
|
nuclear@0
|
322 * initial pre-processing of the given command. You can perform your own
|
nuclear@0
|
323 * actions for the command after calling this function.
|
nuclear@0
|
324 */
|
nuclear@0
|
325 void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd);
|
nuclear@0
|
326
|
nuclear@0
|
327 /**
|
nuclear@0
|
328 * Call with the command returned by android_app_read_cmd() to do the
|
nuclear@0
|
329 * final post-processing of the given command. You must have done your own
|
nuclear@0
|
330 * actions for the command before calling this function.
|
nuclear@0
|
331 */
|
nuclear@0
|
332 void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd);
|
nuclear@0
|
333
|
nuclear@0
|
334 /**
|
nuclear@0
|
335 * Dummy function you can call to ensure glue code isn't stripped.
|
nuclear@0
|
336 */
|
nuclear@0
|
337 void app_dummy();
|
nuclear@0
|
338
|
nuclear@0
|
339 /**
|
nuclear@0
|
340 * This is the function that application code must implement, representing
|
nuclear@0
|
341 * the main entry to the app.
|
nuclear@0
|
342 */
|
nuclear@0
|
343 extern void android_main(struct android_app* app);
|
nuclear@0
|
344
|
nuclear@0
|
345 #ifdef __cplusplus
|
nuclear@0
|
346 }
|
nuclear@0
|
347 #endif
|
nuclear@0
|
348
|
nuclear@0
|
349 #endif /* _ANDROID_NATIVE_APP_GLUE_H */
|