nuclear@23: /* nuclear@23: * Copyright (C) 2010 The Android Open Source Project nuclear@23: * nuclear@23: * Licensed under the Apache License, Version 2.0 (the "License"); nuclear@23: * you may not use this file except in compliance with the License. nuclear@23: * You may obtain a copy of the License at nuclear@23: * nuclear@23: * http://www.apache.org/licenses/LICENSE-2.0 nuclear@23: * nuclear@23: * Unless required by applicable law or agreed to in writing, software nuclear@23: * distributed under the License is distributed on an "AS IS" BASIS, nuclear@23: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@23: * See the License for the specific language governing permissions and nuclear@23: * limitations under the License. nuclear@23: * nuclear@23: */ nuclear@23: nuclear@23: #ifndef _ANDROID_NATIVE_APP_GLUE_H nuclear@23: #define _ANDROID_NATIVE_APP_GLUE_H nuclear@23: nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: nuclear@23: #include nuclear@23: #include nuclear@23: #include nuclear@23: nuclear@23: #ifdef __cplusplus nuclear@23: extern "C" { nuclear@23: #endif nuclear@23: nuclear@23: /** nuclear@23: * The native activity interface provided by nuclear@23: * is based on a set of application-provided callbacks that will be called nuclear@23: * by the Activity's main thread when certain events occur. nuclear@23: * nuclear@23: * This means that each one of this callbacks _should_ _not_ block, or they nuclear@23: * risk having the system force-close the application. This programming nuclear@23: * model is direct, lightweight, but constraining. nuclear@23: * nuclear@23: * The 'android_native_app_glue' static library is used to provide a different nuclear@23: * execution model where the application can implement its own main event nuclear@23: * loop in a different thread instead. Here's how it works: nuclear@23: * nuclear@23: * 1/ The application must provide a function named "android_main()" that nuclear@23: * will be called when the activity is created, in a new thread that is nuclear@23: * distinct from the activity's main thread. nuclear@23: * nuclear@23: * 2/ android_main() receives a pointer to a valid "android_app" structure nuclear@23: * that contains references to other important objects, e.g. the nuclear@23: * ANativeActivity obejct instance the application is running in. nuclear@23: * nuclear@23: * 3/ the "android_app" object holds an ALooper instance that already nuclear@23: * listens to two important things: nuclear@23: * nuclear@23: * - activity lifecycle events (e.g. "pause", "resume"). See APP_CMD_XXX nuclear@23: * declarations below. nuclear@23: * nuclear@23: * - input events coming from the AInputQueue attached to the activity. nuclear@23: * nuclear@23: * Each of these correspond to an ALooper identifier returned by nuclear@23: * ALooper_pollOnce with values of LOOPER_ID_MAIN and LOOPER_ID_INPUT, nuclear@23: * respectively. nuclear@23: * nuclear@23: * Your application can use the same ALooper to listen to additional nuclear@23: * file-descriptors. They can either be callback based, or with return nuclear@23: * identifiers starting with LOOPER_ID_USER. nuclear@23: * nuclear@23: * 4/ Whenever you receive a LOOPER_ID_MAIN or LOOPER_ID_INPUT event, nuclear@23: * the returned data will point to an android_poll_source structure. You nuclear@23: * can call the process() function on it, and fill in android_app->onAppCmd nuclear@23: * and android_app->onInputEvent to be called for your own processing nuclear@23: * of the event. nuclear@23: * nuclear@23: * Alternatively, you can call the low-level functions to read and process nuclear@23: * the data directly... look at the process_cmd() and process_input() nuclear@23: * implementations in the glue to see how to do this. nuclear@23: * nuclear@23: * See the sample named "native-activity" that comes with the NDK with a nuclear@23: * full usage example. Also look at the JavaDoc of NativeActivity. nuclear@23: */ nuclear@23: nuclear@23: struct android_app; nuclear@23: nuclear@23: /** nuclear@23: * Data associated with an ALooper fd that will be returned as the "outData" nuclear@23: * when that source has data ready. nuclear@23: */ nuclear@23: struct android_poll_source { nuclear@23: // The identifier of this source. May be LOOPER_ID_MAIN or nuclear@23: // LOOPER_ID_INPUT. nuclear@23: int32_t id; nuclear@23: nuclear@23: // The android_app this ident is associated with. nuclear@23: struct android_app* app; nuclear@23: nuclear@23: // Function to call to perform the standard processing of data from nuclear@23: // this source. nuclear@23: void (*process)(struct android_app* app, struct android_poll_source* source); nuclear@23: }; nuclear@23: nuclear@23: /** nuclear@23: * This is the interface for the standard glue code of a threaded nuclear@23: * application. In this model, the application's code is running nuclear@23: * in its own thread separate from the main thread of the process. nuclear@23: * It is not required that this thread be associated with the Java nuclear@23: * VM, although it will need to be in order to make JNI calls any nuclear@23: * Java objects. nuclear@23: */ nuclear@23: struct android_app { nuclear@23: // The application can place a pointer to its own state object nuclear@23: // here if it likes. nuclear@23: void* userData; nuclear@23: nuclear@23: // Fill this in with the function to process main app commands (APP_CMD_*) nuclear@23: void (*onAppCmd)(struct android_app* app, int32_t cmd); nuclear@23: nuclear@23: // Fill this in with the function to process input events. At this point nuclear@23: // the event has already been pre-dispatched, and it will be finished upon nuclear@23: // return. Return 1 if you have handled the event, 0 for any default nuclear@23: // dispatching. nuclear@23: int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event); nuclear@23: nuclear@23: // The ANativeActivity object instance that this app is running in. nuclear@23: ANativeActivity* activity; nuclear@23: nuclear@23: // The current configuration the app is running in. nuclear@23: AConfiguration* config; nuclear@23: nuclear@23: // This is the last instance's saved state, as provided at creation time. nuclear@23: // It is NULL if there was no state. You can use this as you need; the nuclear@23: // memory will remain around until you call android_app_exec_cmd() for nuclear@23: // APP_CMD_RESUME, at which point it will be freed and savedState set to NULL. nuclear@23: // These variables should only be changed when processing a APP_CMD_SAVE_STATE, nuclear@23: // at which point they will be initialized to NULL and you can malloc your nuclear@23: // state and place the information here. In that case the memory will be nuclear@23: // freed for you later. nuclear@23: void* savedState; nuclear@23: size_t savedStateSize; nuclear@23: nuclear@23: // The ALooper associated with the app's thread. nuclear@23: ALooper* looper; nuclear@23: nuclear@23: // When non-NULL, this is the input queue from which the app will nuclear@23: // receive user input events. nuclear@23: AInputQueue* inputQueue; nuclear@23: nuclear@23: // When non-NULL, this is the window surface that the app can draw in. nuclear@23: ANativeWindow* window; nuclear@23: nuclear@23: // Current content rectangle of the window; this is the area where the nuclear@23: // window's content should be placed to be seen by the user. nuclear@23: ARect contentRect; nuclear@23: nuclear@23: // Current state of the app's activity. May be either APP_CMD_START, nuclear@23: // APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below. nuclear@23: int activityState; nuclear@23: nuclear@23: // This is non-zero when the application's NativeActivity is being nuclear@23: // destroyed and waiting for the app thread to complete. nuclear@23: int destroyRequested; nuclear@23: nuclear@23: // ------------------------------------------------- nuclear@23: // Below are "private" implementation of the glue code. nuclear@23: nuclear@23: pthread_mutex_t mutex; nuclear@23: pthread_cond_t cond; nuclear@23: nuclear@23: int msgread; nuclear@23: int msgwrite; nuclear@23: nuclear@23: pthread_t thread; nuclear@23: nuclear@23: struct android_poll_source cmdPollSource; nuclear@23: struct android_poll_source inputPollSource; nuclear@23: nuclear@23: int running; nuclear@23: int stateSaved; nuclear@23: int destroyed; nuclear@23: int redrawNeeded; nuclear@23: AInputQueue* pendingInputQueue; nuclear@23: ANativeWindow* pendingWindow; nuclear@23: ARect pendingContentRect; nuclear@23: }; nuclear@23: nuclear@23: enum { nuclear@23: /** nuclear@23: * Looper data ID of commands coming from the app's main thread, which nuclear@23: * is returned as an identifier from ALooper_pollOnce(). The data for this nuclear@23: * identifier is a pointer to an android_poll_source structure. nuclear@23: * These can be retrieved and processed with android_app_read_cmd() nuclear@23: * and android_app_exec_cmd(). nuclear@23: */ nuclear@23: LOOPER_ID_MAIN = 1, nuclear@23: nuclear@23: /** nuclear@23: * Looper data ID of events coming from the AInputQueue of the nuclear@23: * application's window, which is returned as an identifier from nuclear@23: * ALooper_pollOnce(). The data for this identifier is a pointer to an nuclear@23: * android_poll_source structure. These can be read via the inputQueue nuclear@23: * object of android_app. nuclear@23: */ nuclear@23: LOOPER_ID_INPUT = 2, nuclear@23: nuclear@23: /** nuclear@23: * Start of user-defined ALooper identifiers. nuclear@23: */ nuclear@23: LOOPER_ID_USER = 3, nuclear@23: }; nuclear@23: nuclear@23: enum { nuclear@23: /** nuclear@23: * Command from main thread: the AInputQueue has changed. Upon processing nuclear@23: * this command, android_app->inputQueue will be updated to the new queue nuclear@23: * (or NULL). nuclear@23: */ nuclear@23: APP_CMD_INPUT_CHANGED, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: a new ANativeWindow is ready for use. Upon nuclear@23: * receiving this command, android_app->window will contain the new window nuclear@23: * surface. nuclear@23: */ nuclear@23: APP_CMD_INIT_WINDOW, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the existing ANativeWindow needs to be nuclear@23: * terminated. Upon receiving this command, android_app->window still nuclear@23: * contains the existing window; after calling android_app_exec_cmd nuclear@23: * it will be set to NULL. nuclear@23: */ nuclear@23: APP_CMD_TERM_WINDOW, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the current ANativeWindow has been resized. nuclear@23: * Please redraw with its new size. nuclear@23: */ nuclear@23: APP_CMD_WINDOW_RESIZED, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the system needs that the current ANativeWindow nuclear@23: * be redrawn. You should redraw the window before handing this to nuclear@23: * android_app_exec_cmd() in order to avoid transient drawing glitches. nuclear@23: */ nuclear@23: APP_CMD_WINDOW_REDRAW_NEEDED, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the content area of the window has changed, nuclear@23: * such as from the soft input window being shown or hidden. You can nuclear@23: * find the new content rect in android_app::contentRect. nuclear@23: */ nuclear@23: APP_CMD_CONTENT_RECT_CHANGED, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity window has gained nuclear@23: * input focus. nuclear@23: */ nuclear@23: APP_CMD_GAINED_FOCUS, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity window has lost nuclear@23: * input focus. nuclear@23: */ nuclear@23: APP_CMD_LOST_FOCUS, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the current device configuration has changed. nuclear@23: */ nuclear@23: APP_CMD_CONFIG_CHANGED, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the system is running low on memory. nuclear@23: * Try to reduce your memory use. nuclear@23: */ nuclear@23: APP_CMD_LOW_MEMORY, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity has been started. nuclear@23: */ nuclear@23: APP_CMD_START, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity has been resumed. nuclear@23: */ nuclear@23: APP_CMD_RESUME, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app should generate a new saved state nuclear@23: * for itself, to restore from later if needed. If you have saved state, nuclear@23: * allocate it with malloc and place it in android_app.savedState with nuclear@23: * the size in android_app.savedStateSize. The will be freed for you nuclear@23: * later. nuclear@23: */ nuclear@23: APP_CMD_SAVE_STATE, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity has been paused. nuclear@23: */ nuclear@23: APP_CMD_PAUSE, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity has been stopped. nuclear@23: */ nuclear@23: APP_CMD_STOP, nuclear@23: nuclear@23: /** nuclear@23: * Command from main thread: the app's activity is being destroyed, nuclear@23: * and waiting for the app thread to clean up and exit before proceeding. nuclear@23: */ nuclear@23: APP_CMD_DESTROY, nuclear@23: }; nuclear@23: nuclear@23: /** nuclear@23: * Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next nuclear@23: * app command message. nuclear@23: */ nuclear@23: int8_t android_app_read_cmd(struct android_app* android_app); nuclear@23: nuclear@23: /** nuclear@23: * Call with the command returned by android_app_read_cmd() to do the nuclear@23: * initial pre-processing of the given command. You can perform your own nuclear@23: * actions for the command after calling this function. nuclear@23: */ nuclear@23: void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd); nuclear@23: nuclear@23: /** nuclear@23: * Call with the command returned by android_app_read_cmd() to do the nuclear@23: * final post-processing of the given command. You must have done your own nuclear@23: * actions for the command before calling this function. nuclear@23: */ nuclear@23: void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd); nuclear@23: nuclear@23: /** nuclear@23: * Dummy function you can call to ensure glue code isn't stripped. nuclear@23: */ nuclear@23: void app_dummy(); nuclear@23: nuclear@23: /** nuclear@23: * This is the function that application code must implement, representing nuclear@23: * the main entry to the app. nuclear@23: */ nuclear@23: extern void android_main(struct android_app* app); nuclear@23: nuclear@23: #ifdef __cplusplus nuclear@23: } nuclear@23: #endif nuclear@23: nuclear@23: #endif /* _ANDROID_NATIVE_APP_GLUE_H */