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