apk build (not done)
[andemo] / src / android / android_native_app_glue.c
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <jni.h>
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/resource.h>
25
26 #include "android_native_app_glue.h"
27 #include <android/log.h>
28
29 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__))
30 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__))
31
32 /* For debug builds, always enable the debug traces in this library */
33 #ifndef NDEBUG
34 #  define LOGV(...)  ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__))
35 #else
36 #  define LOGV(...)  ((void)0)
37 #endif
38
39 static void free_saved_state(struct android_app* android_app) {
40     pthread_mutex_lock(&android_app->mutex);
41     if (android_app->savedState != NULL) {
42         free(android_app->savedState);
43         android_app->savedState = NULL;
44         android_app->savedStateSize = 0;
45     }
46     pthread_mutex_unlock(&android_app->mutex);
47 }
48
49 int8_t android_app_read_cmd(struct android_app* android_app) {
50     int8_t cmd;
51     if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
52         switch (cmd) {
53             case APP_CMD_SAVE_STATE:
54                 free_saved_state(android_app);
55                 break;
56         }
57         return cmd;
58     } else {
59         LOGE("No data on command pipe!");
60     }
61     return -1;
62 }
63
64 static void print_cur_config(struct android_app* android_app) {
65     char lang[2], country[2];
66     AConfiguration_getLanguage(android_app->config, lang);
67     AConfiguration_getCountry(android_app->config, country);
68
69     LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
70             "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
71             "modetype=%d modenight=%d",
72             AConfiguration_getMcc(android_app->config),
73             AConfiguration_getMnc(android_app->config),
74             lang[0], lang[1], country[0], country[1],
75             AConfiguration_getOrientation(android_app->config),
76             AConfiguration_getTouchscreen(android_app->config),
77             AConfiguration_getDensity(android_app->config),
78             AConfiguration_getKeyboard(android_app->config),
79             AConfiguration_getNavigation(android_app->config),
80             AConfiguration_getKeysHidden(android_app->config),
81             AConfiguration_getNavHidden(android_app->config),
82             AConfiguration_getSdkVersion(android_app->config),
83             AConfiguration_getScreenSize(android_app->config),
84             AConfiguration_getScreenLong(android_app->config),
85             AConfiguration_getUiModeType(android_app->config),
86             AConfiguration_getUiModeNight(android_app->config));
87 }
88
89 void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
90     switch (cmd) {
91         case APP_CMD_INPUT_CHANGED:
92             LOGV("APP_CMD_INPUT_CHANGED\n");
93             pthread_mutex_lock(&android_app->mutex);
94             if (android_app->inputQueue != NULL) {
95                 AInputQueue_detachLooper(android_app->inputQueue);
96             }
97             android_app->inputQueue = android_app->pendingInputQueue;
98             if (android_app->inputQueue != NULL) {
99                 LOGV("Attaching input queue to looper");
100                 AInputQueue_attachLooper(android_app->inputQueue,
101                         android_app->looper, LOOPER_ID_INPUT, NULL,
102                         &android_app->inputPollSource);
103             }
104             pthread_cond_broadcast(&android_app->cond);
105             pthread_mutex_unlock(&android_app->mutex);
106             break;
107
108         case APP_CMD_INIT_WINDOW:
109             LOGV("APP_CMD_INIT_WINDOW\n");
110             pthread_mutex_lock(&android_app->mutex);
111             android_app->window = android_app->pendingWindow;
112             pthread_cond_broadcast(&android_app->cond);
113             pthread_mutex_unlock(&android_app->mutex);
114             break;
115
116         case APP_CMD_TERM_WINDOW:
117             LOGV("APP_CMD_TERM_WINDOW\n");
118             pthread_cond_broadcast(&android_app->cond);
119             break;
120
121         case APP_CMD_RESUME:
122         case APP_CMD_START:
123         case APP_CMD_PAUSE:
124         case APP_CMD_STOP:
125             LOGV("activityState=%d\n", cmd);
126             pthread_mutex_lock(&android_app->mutex);
127             android_app->activityState = cmd;
128             pthread_cond_broadcast(&android_app->cond);
129             pthread_mutex_unlock(&android_app->mutex);
130             break;
131
132         case APP_CMD_CONFIG_CHANGED:
133             LOGV("APP_CMD_CONFIG_CHANGED\n");
134             AConfiguration_fromAssetManager(android_app->config,
135                     android_app->activity->assetManager);
136             print_cur_config(android_app);
137             break;
138
139         case APP_CMD_DESTROY:
140             LOGV("APP_CMD_DESTROY\n");
141             android_app->destroyRequested = 1;
142             break;
143     }
144 }
145
146 void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
147     switch (cmd) {
148         case APP_CMD_TERM_WINDOW:
149             LOGV("APP_CMD_TERM_WINDOW\n");
150             pthread_mutex_lock(&android_app->mutex);
151             android_app->window = NULL;
152             pthread_cond_broadcast(&android_app->cond);
153             pthread_mutex_unlock(&android_app->mutex);
154             break;
155
156         case APP_CMD_SAVE_STATE:
157             LOGV("APP_CMD_SAVE_STATE\n");
158             pthread_mutex_lock(&android_app->mutex);
159             android_app->stateSaved = 1;
160             pthread_cond_broadcast(&android_app->cond);
161             pthread_mutex_unlock(&android_app->mutex);
162             break;
163
164         case APP_CMD_RESUME:
165             free_saved_state(android_app);
166             break;
167     }
168 }
169
170 void app_dummy() {
171
172 }
173
174 static void android_app_destroy(struct android_app* android_app) {
175     LOGV("android_app_destroy!");
176     free_saved_state(android_app);
177     pthread_mutex_lock(&android_app->mutex);
178     if (android_app->inputQueue != NULL) {
179         AInputQueue_detachLooper(android_app->inputQueue);
180     }
181     AConfiguration_delete(android_app->config);
182     android_app->destroyed = 1;
183     pthread_cond_broadcast(&android_app->cond);
184     pthread_mutex_unlock(&android_app->mutex);
185     /* Can't touch android_app object after this. */
186 }
187
188 static void process_input(struct android_app* app, struct android_poll_source* source) {
189     AInputEvent* event = NULL;
190     while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
191         LOGV("New input event: type=%d\n", AInputEvent_getType(event));
192         if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
193             continue;
194         }
195         int32_t handled = 0;
196         if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
197         AInputQueue_finishEvent(app->inputQueue, event, handled);
198     }
199 }
200
201 static void process_cmd(struct android_app* app, struct android_poll_source* source) {
202     int8_t cmd = android_app_read_cmd(app);
203     android_app_pre_exec_cmd(app, cmd);
204     if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
205     android_app_post_exec_cmd(app, cmd);
206 }
207
208 static void* android_app_entry(void* param) {
209     struct android_app* android_app = (struct android_app*)param;
210
211     android_app->config = AConfiguration_new();
212     AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
213
214     print_cur_config(android_app);
215
216     android_app->cmdPollSource.id = LOOPER_ID_MAIN;
217     android_app->cmdPollSource.app = android_app;
218     android_app->cmdPollSource.process = process_cmd;
219     android_app->inputPollSource.id = LOOPER_ID_INPUT;
220     android_app->inputPollSource.app = android_app;
221     android_app->inputPollSource.process = process_input;
222
223     ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
224     ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
225             &android_app->cmdPollSource);
226     android_app->looper = looper;
227
228     pthread_mutex_lock(&android_app->mutex);
229     android_app->running = 1;
230     pthread_cond_broadcast(&android_app->cond);
231     pthread_mutex_unlock(&android_app->mutex);
232
233     android_main(android_app);
234
235     android_app_destroy(android_app);
236     return NULL;
237 }
238
239 /* --------------------------------------------------------------------
240  * Native activity interaction (called from main thread)
241  * --------------------------------------------------------------------
242 */
243
244 static struct android_app* android_app_create(ANativeActivity* activity,
245         void* savedState, size_t savedStateSize) {
246     struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
247     memset(android_app, 0, sizeof(struct android_app));
248     android_app->activity = activity;
249
250     pthread_mutex_init(&android_app->mutex, NULL);
251     pthread_cond_init(&android_app->cond, NULL);
252
253     if (savedState != NULL) {
254         android_app->savedState = malloc(savedStateSize);
255         android_app->savedStateSize = savedStateSize;
256         memcpy(android_app->savedState, savedState, savedStateSize);
257     }
258
259     int msgpipe[2];
260     if (pipe(msgpipe)) {
261         LOGE("could not create pipe: %s", strerror(errno));
262         return NULL;
263     }
264     android_app->msgread = msgpipe[0];
265     android_app->msgwrite = msgpipe[1];
266
267     pthread_attr_t attr;
268     pthread_attr_init(&attr);
269     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
270     pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
271
272     /* Wait for thread to start. */
273     pthread_mutex_lock(&android_app->mutex);
274     while (!android_app->running) {
275         pthread_cond_wait(&android_app->cond, &android_app->mutex);
276     }
277     pthread_mutex_unlock(&android_app->mutex);
278
279     return android_app;
280 }
281
282 static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
283     if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
284         LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
285     }
286 }
287
288 static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
289     pthread_mutex_lock(&android_app->mutex);
290     android_app->pendingInputQueue = inputQueue;
291     android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
292     while (android_app->inputQueue != android_app->pendingInputQueue) {
293         pthread_cond_wait(&android_app->cond, &android_app->mutex);
294     }
295     pthread_mutex_unlock(&android_app->mutex);
296 }
297
298 static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
299     pthread_mutex_lock(&android_app->mutex);
300     if (android_app->pendingWindow != NULL) {
301         android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
302     }
303     android_app->pendingWindow = window;
304     if (window != NULL) {
305         android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
306     }
307     while (android_app->window != android_app->pendingWindow) {
308         pthread_cond_wait(&android_app->cond, &android_app->mutex);
309     }
310     pthread_mutex_unlock(&android_app->mutex);
311 }
312
313 static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
314     pthread_mutex_lock(&android_app->mutex);
315     android_app_write_cmd(android_app, cmd);
316     while (android_app->activityState != cmd) {
317         pthread_cond_wait(&android_app->cond, &android_app->mutex);
318     }
319     pthread_mutex_unlock(&android_app->mutex);
320 }
321
322 static void android_app_free(struct android_app* android_app) {
323     pthread_mutex_lock(&android_app->mutex);
324     android_app_write_cmd(android_app, APP_CMD_DESTROY);
325     while (!android_app->destroyed) {
326         pthread_cond_wait(&android_app->cond, &android_app->mutex);
327     }
328     pthread_mutex_unlock(&android_app->mutex);
329
330     close(android_app->msgread);
331     close(android_app->msgwrite);
332     pthread_cond_destroy(&android_app->cond);
333     pthread_mutex_destroy(&android_app->mutex);
334     free(android_app);
335 }
336
337 static void onDestroy(ANativeActivity* activity) {
338     LOGV("Destroy: %p\n", (void*)activity);
339     android_app_free((struct android_app*)activity->instance);
340 }
341
342 static void onStart(ANativeActivity* activity) {
343     LOGV("Start: %p\n", (void*)activity);
344     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
345 }
346
347 static void onResume(ANativeActivity* activity) {
348     LOGV("Resume: %p\n", (void*)activity);
349     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
350 }
351
352 static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
353     struct android_app* android_app = (struct android_app*)activity->instance;
354     void* savedState = NULL;
355
356     LOGV("SaveInstanceState: %p\n", (void*)activity);
357     pthread_mutex_lock(&android_app->mutex);
358     android_app->stateSaved = 0;
359     android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
360     while (!android_app->stateSaved) {
361         pthread_cond_wait(&android_app->cond, &android_app->mutex);
362     }
363
364     if (android_app->savedState != NULL) {
365         savedState = android_app->savedState;
366         *outLen = android_app->savedStateSize;
367         android_app->savedState = NULL;
368         android_app->savedStateSize = 0;
369     }
370
371     pthread_mutex_unlock(&android_app->mutex);
372
373     return savedState;
374 }
375
376 static void onPause(ANativeActivity* activity) {
377     LOGV("Pause: %p\n", (void*)activity);
378     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
379 }
380
381 static void onStop(ANativeActivity* activity) {
382     LOGV("Stop: %p\n", (void*)activity);
383     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
384 }
385
386 static void onConfigurationChanged(ANativeActivity* activity) {
387     struct android_app* android_app = (struct android_app*)activity->instance;
388     LOGV("ConfigurationChanged: %p\n", (void*)activity);
389     android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
390 }
391
392 static void onLowMemory(ANativeActivity* activity) {
393     struct android_app* android_app = (struct android_app*)activity->instance;
394     LOGV("LowMemory: %p\n", (void*)activity);
395     android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
396 }
397
398 static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
399     LOGV("WindowFocusChanged: %p -- %d\n", (void*)activity, focused);
400     android_app_write_cmd((struct android_app*)activity->instance,
401             focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
402 }
403
404 static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
405     LOGV("NativeWindowCreated: %p -- %p\n", (void*)activity, (void*)window);
406     android_app_set_window((struct android_app*)activity->instance, window);
407 }
408
409 static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
410     LOGV("NativeWindowDestroyed: %p -- %p\n", (void*)activity, (void*)window);
411     android_app_set_window((struct android_app*)activity->instance, NULL);
412 }
413
414 static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
415     LOGV("InputQueueCreated: %p -- %p\n", (void*)activity, (void*)queue);
416     android_app_set_input((struct android_app*)activity->instance, queue);
417 }
418
419 static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
420     LOGV("InputQueueDestroyed: %p -- %p\n", (void*)activity, (void*)queue);
421     android_app_set_input((struct android_app*)activity->instance, NULL);
422 }
423
424 JNIEXPORT
425 void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState,
426                               size_t savedStateSize) {
427     LOGV("Creating: %p\n", (void*)activity);
428     activity->callbacks->onDestroy = onDestroy;
429     activity->callbacks->onStart = onStart;
430     activity->callbacks->onResume = onResume;
431     activity->callbacks->onSaveInstanceState = onSaveInstanceState;
432     activity->callbacks->onPause = onPause;
433     activity->callbacks->onStop = onStop;
434     activity->callbacks->onConfigurationChanged = onConfigurationChanged;
435     activity->callbacks->onLowMemory = onLowMemory;
436     activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
437     activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
438     activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
439     activity->callbacks->onInputQueueCreated = onInputQueueCreated;
440     activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
441
442     activity->instance = android_app_create(activity, savedState, savedStateSize);
443 }