nuclear@0: /* nuclear@0: * Copyright (C) 2010 The Android Open Source Project nuclear@0: * nuclear@0: * Licensed under the Apache License, Version 2.0 (the "License"); nuclear@0: * you may not use this file except in compliance with the License. nuclear@0: * You may obtain a copy of the License at nuclear@0: * nuclear@0: * http://www.apache.org/licenses/LICENSE-2.0 nuclear@0: * nuclear@0: * Unless required by applicable law or agreed to in writing, software nuclear@0: * distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: * See the License for the specific language governing permissions and nuclear@0: * limitations under the License. nuclear@0: * nuclear@0: */ nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: nuclear@0: #include "android_native_app_glue.h" nuclear@0: #include nuclear@0: nuclear@0: #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__)) nuclear@0: #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__)) nuclear@0: nuclear@0: /* For debug builds, always enable the debug traces in this library */ nuclear@0: #ifndef NDEBUG nuclear@0: # define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__)) nuclear@0: #else nuclear@0: # define LOGV(...) ((void)0) nuclear@0: #endif nuclear@0: nuclear@0: static void free_saved_state(struct android_app* android_app) { nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: if (android_app->savedState != NULL) { nuclear@0: free(android_app->savedState); nuclear@0: android_app->savedState = NULL; nuclear@0: android_app->savedStateSize = 0; nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: } nuclear@0: nuclear@0: int8_t android_app_read_cmd(struct android_app* android_app) { nuclear@0: int8_t cmd; nuclear@0: if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) { nuclear@0: switch (cmd) { nuclear@0: case APP_CMD_SAVE_STATE: nuclear@0: free_saved_state(android_app); nuclear@0: break; nuclear@0: } nuclear@0: return cmd; nuclear@0: } else { nuclear@0: LOGE("No data on command pipe!"); nuclear@0: } nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: static void print_cur_config(struct android_app* android_app) { nuclear@0: char lang[2], country[2]; nuclear@0: AConfiguration_getLanguage(android_app->config, lang); nuclear@0: AConfiguration_getCountry(android_app->config, country); nuclear@0: nuclear@0: LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d " nuclear@0: "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d " nuclear@0: "modetype=%d modenight=%d", nuclear@0: AConfiguration_getMcc(android_app->config), nuclear@0: AConfiguration_getMnc(android_app->config), nuclear@0: lang[0], lang[1], country[0], country[1], nuclear@0: AConfiguration_getOrientation(android_app->config), nuclear@0: AConfiguration_getTouchscreen(android_app->config), nuclear@0: AConfiguration_getDensity(android_app->config), nuclear@0: AConfiguration_getKeyboard(android_app->config), nuclear@0: AConfiguration_getNavigation(android_app->config), nuclear@0: AConfiguration_getKeysHidden(android_app->config), nuclear@0: AConfiguration_getNavHidden(android_app->config), nuclear@0: AConfiguration_getSdkVersion(android_app->config), nuclear@0: AConfiguration_getScreenSize(android_app->config), nuclear@0: AConfiguration_getScreenLong(android_app->config), nuclear@0: AConfiguration_getUiModeType(android_app->config), nuclear@0: AConfiguration_getUiModeNight(android_app->config)); nuclear@0: } nuclear@0: nuclear@0: void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) { nuclear@0: switch (cmd) { nuclear@0: case APP_CMD_INPUT_CHANGED: nuclear@0: LOGV("APP_CMD_INPUT_CHANGED\n"); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: if (android_app->inputQueue != NULL) { nuclear@0: AInputQueue_detachLooper(android_app->inputQueue); nuclear@0: } nuclear@0: android_app->inputQueue = android_app->pendingInputQueue; nuclear@0: if (android_app->inputQueue != NULL) { nuclear@0: LOGV("Attaching input queue to looper"); nuclear@0: AInputQueue_attachLooper(android_app->inputQueue, nuclear@0: android_app->looper, LOOPER_ID_INPUT, NULL, nuclear@0: &android_app->inputPollSource); nuclear@0: } nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_INIT_WINDOW: nuclear@0: LOGV("APP_CMD_INIT_WINDOW\n"); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->window = android_app->pendingWindow; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_TERM_WINDOW: nuclear@0: LOGV("APP_CMD_TERM_WINDOW\n"); nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_RESUME: nuclear@0: case APP_CMD_START: nuclear@0: case APP_CMD_PAUSE: nuclear@0: case APP_CMD_STOP: nuclear@0: LOGV("activityState=%d\n", cmd); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->activityState = cmd; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_CONFIG_CHANGED: nuclear@0: LOGV("APP_CMD_CONFIG_CHANGED\n"); nuclear@0: AConfiguration_fromAssetManager(android_app->config, nuclear@0: android_app->activity->assetManager); nuclear@0: print_cur_config(android_app); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_DESTROY: nuclear@0: LOGV("APP_CMD_DESTROY\n"); nuclear@0: android_app->destroyRequested = 1; nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) { nuclear@0: switch (cmd) { nuclear@0: case APP_CMD_TERM_WINDOW: nuclear@0: LOGV("APP_CMD_TERM_WINDOW\n"); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->window = NULL; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_SAVE_STATE: nuclear@0: LOGV("APP_CMD_SAVE_STATE\n"); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->stateSaved = 1; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: break; nuclear@0: nuclear@0: case APP_CMD_RESUME: nuclear@0: free_saved_state(android_app); nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void app_dummy() { nuclear@0: nuclear@0: } nuclear@0: nuclear@0: static void android_app_destroy(struct android_app* android_app) { nuclear@0: LOGV("android_app_destroy!"); nuclear@0: free_saved_state(android_app); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: if (android_app->inputQueue != NULL) { nuclear@0: AInputQueue_detachLooper(android_app->inputQueue); nuclear@0: } nuclear@0: AConfiguration_delete(android_app->config); nuclear@0: android_app->destroyed = 1; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: // Can't touch android_app object after this. nuclear@0: } nuclear@0: nuclear@0: static void process_input(struct android_app* app, struct android_poll_source* source) { nuclear@0: AInputEvent* event = NULL; nuclear@0: while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) { nuclear@0: LOGV("New input event: type=%d\n", AInputEvent_getType(event)); nuclear@0: if (AInputQueue_preDispatchEvent(app->inputQueue, event)) { nuclear@0: continue; nuclear@0: } nuclear@0: int32_t handled = 0; nuclear@0: if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event); nuclear@0: AInputQueue_finishEvent(app->inputQueue, event, handled); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: static void process_cmd(struct android_app* app, struct android_poll_source* source) { nuclear@0: int8_t cmd = android_app_read_cmd(app); nuclear@0: android_app_pre_exec_cmd(app, cmd); nuclear@0: if (app->onAppCmd != NULL) app->onAppCmd(app, cmd); nuclear@0: android_app_post_exec_cmd(app, cmd); nuclear@0: } nuclear@0: nuclear@0: static void* android_app_entry(void* param) { nuclear@0: struct android_app* android_app = (struct android_app*)param; nuclear@0: nuclear@0: android_app->config = AConfiguration_new(); nuclear@0: AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager); nuclear@0: nuclear@0: print_cur_config(android_app); nuclear@0: nuclear@0: android_app->cmdPollSource.id = LOOPER_ID_MAIN; nuclear@0: android_app->cmdPollSource.app = android_app; nuclear@0: android_app->cmdPollSource.process = process_cmd; nuclear@0: android_app->inputPollSource.id = LOOPER_ID_INPUT; nuclear@0: android_app->inputPollSource.app = android_app; nuclear@0: android_app->inputPollSource.process = process_input; nuclear@0: nuclear@0: ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); nuclear@0: ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL, nuclear@0: &android_app->cmdPollSource); nuclear@0: android_app->looper = looper; nuclear@0: nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->running = 1; nuclear@0: pthread_cond_broadcast(&android_app->cond); nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: nuclear@0: android_main(android_app); nuclear@0: nuclear@0: android_app_destroy(android_app); nuclear@0: return NULL; nuclear@0: } nuclear@0: nuclear@0: // -------------------------------------------------------------------- nuclear@0: // Native activity interaction (called from main thread) nuclear@0: // -------------------------------------------------------------------- nuclear@0: nuclear@0: static struct android_app* android_app_create(ANativeActivity* activity, nuclear@0: void* savedState, size_t savedStateSize) { nuclear@0: struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app)); nuclear@0: memset(android_app, 0, sizeof(struct android_app)); nuclear@0: android_app->activity = activity; nuclear@0: nuclear@0: pthread_mutex_init(&android_app->mutex, NULL); nuclear@0: pthread_cond_init(&android_app->cond, NULL); nuclear@0: nuclear@0: if (savedState != NULL) { nuclear@0: android_app->savedState = malloc(savedStateSize); nuclear@0: android_app->savedStateSize = savedStateSize; nuclear@0: memcpy(android_app->savedState, savedState, savedStateSize); nuclear@0: } nuclear@0: nuclear@0: int msgpipe[2]; nuclear@0: if (pipe(msgpipe)) { nuclear@0: LOGE("could not create pipe: %s", strerror(errno)); nuclear@0: return NULL; nuclear@0: } nuclear@0: android_app->msgread = msgpipe[0]; nuclear@0: android_app->msgwrite = msgpipe[1]; nuclear@0: nuclear@0: pthread_attr_t attr; nuclear@0: pthread_attr_init(&attr); nuclear@0: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); nuclear@0: pthread_create(&android_app->thread, &attr, android_app_entry, android_app); nuclear@0: nuclear@0: // Wait for thread to start. nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: while (!android_app->running) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: nuclear@0: return android_app; nuclear@0: } nuclear@0: nuclear@0: static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) { nuclear@0: if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) { nuclear@0: LOGE("Failure writing android_app cmd: %s\n", strerror(errno)); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) { nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->pendingInputQueue = inputQueue; nuclear@0: android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED); nuclear@0: while (android_app->inputQueue != android_app->pendingInputQueue) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: } nuclear@0: nuclear@0: static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) { nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: if (android_app->pendingWindow != NULL) { nuclear@0: android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW); nuclear@0: } nuclear@0: android_app->pendingWindow = window; nuclear@0: if (window != NULL) { nuclear@0: android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW); nuclear@0: } nuclear@0: while (android_app->window != android_app->pendingWindow) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: } nuclear@0: nuclear@0: static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) { nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app_write_cmd(android_app, cmd); nuclear@0: while (android_app->activityState != cmd) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: } nuclear@0: nuclear@0: static void android_app_free(struct android_app* android_app) { nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app_write_cmd(android_app, APP_CMD_DESTROY); nuclear@0: while (!android_app->destroyed) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: nuclear@0: close(android_app->msgread); nuclear@0: close(android_app->msgwrite); nuclear@0: pthread_cond_destroy(&android_app->cond); nuclear@0: pthread_mutex_destroy(&android_app->mutex); nuclear@0: free(android_app); nuclear@0: } nuclear@0: nuclear@0: static void onDestroy(ANativeActivity* activity) { nuclear@0: LOGV("Destroy: %p\n", activity); nuclear@0: android_app_free((struct android_app*)activity->instance); nuclear@0: } nuclear@0: nuclear@0: static void onStart(ANativeActivity* activity) { nuclear@0: LOGV("Start: %p\n", activity); nuclear@0: android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START); nuclear@0: } nuclear@0: nuclear@0: static void onResume(ANativeActivity* activity) { nuclear@0: LOGV("Resume: %p\n", activity); nuclear@0: android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME); nuclear@0: } nuclear@0: nuclear@0: static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) { nuclear@0: struct android_app* android_app = (struct android_app*)activity->instance; nuclear@0: void* savedState = NULL; nuclear@0: nuclear@0: LOGV("SaveInstanceState: %p\n", activity); nuclear@0: pthread_mutex_lock(&android_app->mutex); nuclear@0: android_app->stateSaved = 0; nuclear@0: android_app_write_cmd(android_app, APP_CMD_SAVE_STATE); nuclear@0: while (!android_app->stateSaved) { nuclear@0: pthread_cond_wait(&android_app->cond, &android_app->mutex); nuclear@0: } nuclear@0: nuclear@0: if (android_app->savedState != NULL) { nuclear@0: savedState = android_app->savedState; nuclear@0: *outLen = android_app->savedStateSize; nuclear@0: android_app->savedState = NULL; nuclear@0: android_app->savedStateSize = 0; nuclear@0: } nuclear@0: nuclear@0: pthread_mutex_unlock(&android_app->mutex); nuclear@0: nuclear@0: return savedState; nuclear@0: } nuclear@0: nuclear@0: static void onPause(ANativeActivity* activity) { nuclear@0: LOGV("Pause: %p\n", activity); nuclear@0: android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE); nuclear@0: } nuclear@0: nuclear@0: static void onStop(ANativeActivity* activity) { nuclear@0: LOGV("Stop: %p\n", activity); nuclear@0: android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP); nuclear@0: } nuclear@0: nuclear@0: static void onConfigurationChanged(ANativeActivity* activity) { nuclear@0: struct android_app* android_app = (struct android_app*)activity->instance; nuclear@0: LOGV("ConfigurationChanged: %p\n", activity); nuclear@0: android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED); nuclear@0: } nuclear@0: nuclear@0: static void onLowMemory(ANativeActivity* activity) { nuclear@0: struct android_app* android_app = (struct android_app*)activity->instance; nuclear@0: LOGV("LowMemory: %p\n", activity); nuclear@0: android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY); nuclear@0: } nuclear@0: nuclear@0: static void onWindowFocusChanged(ANativeActivity* activity, int focused) { nuclear@0: LOGV("WindowFocusChanged: %p -- %d\n", activity, focused); nuclear@0: android_app_write_cmd((struct android_app*)activity->instance, nuclear@0: focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS); nuclear@0: } nuclear@0: nuclear@0: static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) { nuclear@0: LOGV("NativeWindowCreated: %p -- %p\n", activity, window); nuclear@0: android_app_set_window((struct android_app*)activity->instance, window); nuclear@0: } nuclear@0: nuclear@0: static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) { nuclear@0: LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window); nuclear@0: android_app_set_window((struct android_app*)activity->instance, NULL); nuclear@0: } nuclear@0: nuclear@0: static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) { nuclear@0: LOGV("InputQueueCreated: %p -- %p\n", activity, queue); nuclear@0: android_app_set_input((struct android_app*)activity->instance, queue); nuclear@0: } nuclear@0: nuclear@0: static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) { nuclear@0: LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue); nuclear@0: android_app_set_input((struct android_app*)activity->instance, NULL); nuclear@0: } nuclear@0: nuclear@0: void ANativeActivity_onCreate(ANativeActivity* activity, nuclear@0: void* savedState, size_t savedStateSize) { nuclear@0: LOGV("Creating: %p\n", activity); nuclear@0: activity->callbacks->onDestroy = onDestroy; nuclear@0: activity->callbacks->onStart = onStart; nuclear@0: activity->callbacks->onResume = onResume; nuclear@0: activity->callbacks->onSaveInstanceState = onSaveInstanceState; nuclear@0: activity->callbacks->onPause = onPause; nuclear@0: activity->callbacks->onStop = onStop; nuclear@0: activity->callbacks->onConfigurationChanged = onConfigurationChanged; nuclear@0: activity->callbacks->onLowMemory = onLowMemory; nuclear@0: activity->callbacks->onWindowFocusChanged = onWindowFocusChanged; nuclear@0: activity->callbacks->onNativeWindowCreated = onNativeWindowCreated; nuclear@0: activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed; nuclear@0: activity->callbacks->onInputQueueCreated = onInputQueueCreated; nuclear@0: activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed; nuclear@0: nuclear@0: activity->instance = android_app_create(activity, savedState, savedStateSize); nuclear@0: }