Path: blob/main_old/util/android/third_party/android_native_app_glue.h
1695 views
/*1* Copyright (C) 2010 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*15*/1617#ifndef _ANDROID_NATIVE_APP_GLUE_H18#define _ANDROID_NATIVE_APP_GLUE_H1920#include <poll.h>21#include <pthread.h>22#include <sched.h>2324#include <android/configuration.h>25#include <android/looper.h>26#include <android/native_activity.h>2728#ifdef __cplusplus29extern "C" {30#endif3132/**33* The native activity interface provided by <android/native_activity.h>34* is based on a set of application-provided callbacks that will be called35* by the Activity's main thread when certain events occur.36*37* This means that each one of this callbacks _should_ _not_ block, or they38* risk having the system force-close the application. This programming39* model is direct, lightweight, but constraining.40*41* The 'android_native_app_glue' static library is used to provide a different42* execution model where the application can implement its own main event43* loop in a different thread instead. Here's how it works:44*45* 1/ The application must provide a function named "android_main()" that46* will be called when the activity is created, in a new thread that is47* distinct from the activity's main thread.48*49* 2/ android_main() receives a pointer to a valid "android_app" structure50* that contains references to other important objects, e.g. the51* ANativeActivity obejct instance the application is running in.52*53* 3/ the "android_app" object holds an ALooper instance that already54* listens to two important things:55*56* - activity lifecycle events (e.g. "pause", "resume"). See APP_CMD_XXX57* declarations below.58*59* - input events coming from the AInputQueue attached to the activity.60*61* Each of these correspond to an ALooper identifier returned by62* ALooper_pollOnce with values of LOOPER_ID_MAIN and LOOPER_ID_INPUT,63* respectively.64*65* Your application can use the same ALooper to listen to additional66* file-descriptors. They can either be callback based, or with return67* identifiers starting with LOOPER_ID_USER.68*69* 4/ Whenever you receive a LOOPER_ID_MAIN or LOOPER_ID_INPUT event,70* the returned data will point to an android_poll_source structure. You71* can call the process() function on it, and fill in android_app->onAppCmd72* and android_app->onInputEvent to be called for your own processing73* of the event.74*75* Alternatively, you can call the low-level functions to read and process76* the data directly... look at the process_cmd() and process_input()77* implementations in the glue to see how to do this.78*79* See the sample named "native-activity" that comes with the NDK with a80* full usage example. Also look at the JavaDoc of NativeActivity.81*/8283struct android_app;8485/**86* Data associated with an ALooper fd that will be returned as the "outData"87* when that source has data ready.88*/89struct android_poll_source {90// The identifier of this source. May be LOOPER_ID_MAIN or91// LOOPER_ID_INPUT.92int32_t id;9394// The android_app this ident is associated with.95struct android_app* app;9697// Function to call to perform the standard processing of data from98// this source.99void (*process)(struct android_app* app, struct android_poll_source* source);100};101102/**103* This is the interface for the standard glue code of a threaded104* application. In this model, the application's code is running105* in its own thread separate from the main thread of the process.106* It is not required that this thread be associated with the Java107* VM, although it will need to be in order to make JNI calls any108* Java objects.109*/110struct android_app {111// The application can place a pointer to its own state object112// here if it likes.113void* userData;114115// Fill this in with the function to process main app commands (APP_CMD_*)116void (*onAppCmd)(struct android_app* app, int32_t cmd);117118// Fill this in with the function to process input events. At this point119// the event has already been pre-dispatched, and it will be finished upon120// return. Return 1 if you have handled the event, 0 for any default121// dispatching.122int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);123124// The ANativeActivity object instance that this app is running in.125ANativeActivity* activity;126127// The current configuration the app is running in.128AConfiguration* config;129130// This is the last instance's saved state, as provided at creation time.131// It is NULL if there was no state. You can use this as you need; the132// memory will remain around until you call android_app_exec_cmd() for133// APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.134// These variables should only be changed when processing a APP_CMD_SAVE_STATE,135// at which point they will be initialized to NULL and you can malloc your136// state and place the information here. In that case the memory will be137// freed for you later.138void* savedState;139size_t savedStateSize;140141// The ALooper associated with the app's thread.142ALooper* looper;143144// When non-NULL, this is the input queue from which the app will145// receive user input events.146AInputQueue* inputQueue;147148// When non-NULL, this is the window surface that the app can draw in.149ANativeWindow* window;150151// Current content rectangle of the window; this is the area where the152// window's content should be placed to be seen by the user.153ARect contentRect;154155// Current state of the app's activity. May be either APP_CMD_START,156// APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below.157int activityState;158159// This is non-zero when the application's NativeActivity is being160// destroyed and waiting for the app thread to complete.161int destroyRequested;162163// -------------------------------------------------164// Below are "private" implementation of the glue code.165166pthread_mutex_t mutex;167pthread_cond_t cond;168169int msgread;170int msgwrite;171172pthread_t thread;173174struct android_poll_source cmdPollSource;175struct android_poll_source inputPollSource;176177int running;178int stateSaved;179int destroyed;180int redrawNeeded;181AInputQueue* pendingInputQueue;182ANativeWindow* pendingWindow;183ARect pendingContentRect;184};185186enum {187/**188* Looper data ID of commands coming from the app's main thread, which189* is returned as an identifier from ALooper_pollOnce(). The data for this190* identifier is a pointer to an android_poll_source structure.191* These can be retrieved and processed with android_app_read_cmd()192* and android_app_exec_cmd().193*/194LOOPER_ID_MAIN = 1,195196/**197* Looper data ID of events coming from the AInputQueue of the198* application's window, which is returned as an identifier from199* ALooper_pollOnce(). The data for this identifier is a pointer to an200* android_poll_source structure. These can be read via the inputQueue201* object of android_app.202*/203LOOPER_ID_INPUT = 2,204205/**206* Start of user-defined ALooper identifiers.207*/208LOOPER_ID_USER = 3,209};210211enum {212/**213* Command from main thread: the AInputQueue has changed. Upon processing214* this command, android_app->inputQueue will be updated to the new queue215* (or NULL).216*/217APP_CMD_INPUT_CHANGED,218219/**220* Command from main thread: a new ANativeWindow is ready for use. Upon221* receiving this command, android_app->window will contain the new window222* surface.223*/224APP_CMD_INIT_WINDOW,225226/**227* Command from main thread: the existing ANativeWindow needs to be228* terminated. Upon receiving this command, android_app->window still229* contains the existing window; after calling android_app_exec_cmd230* it will be set to NULL.231*/232APP_CMD_TERM_WINDOW,233234/**235* Command from main thread: the current ANativeWindow has been resized.236* Please redraw with its new size.237*/238APP_CMD_WINDOW_RESIZED,239240/**241* Command from main thread: the system needs that the current ANativeWindow242* be redrawn. You should redraw the window before handing this to243* android_app_exec_cmd() in order to avoid transient drawing glitches.244*/245APP_CMD_WINDOW_REDRAW_NEEDED,246247/**248* Command from main thread: the content area of the window has changed,249* such as from the soft input window being shown or hidden. You can250* find the new content rect in android_app::contentRect.251*/252APP_CMD_CONTENT_RECT_CHANGED,253254/**255* Command from main thread: the app's activity window has gained256* input focus.257*/258APP_CMD_GAINED_FOCUS,259260/**261* Command from main thread: the app's activity window has lost262* input focus.263*/264APP_CMD_LOST_FOCUS,265266/**267* Command from main thread: the current device configuration has changed.268*/269APP_CMD_CONFIG_CHANGED,270271/**272* Command from main thread: the system is running low on memory.273* Try to reduce your memory use.274*/275APP_CMD_LOW_MEMORY,276277/**278* Command from main thread: the app's activity has been started.279*/280APP_CMD_START,281282/**283* Command from main thread: the app's activity has been resumed.284*/285APP_CMD_RESUME,286287/**288* Command from main thread: the app should generate a new saved state289* for itself, to restore from later if needed. If you have saved state,290* allocate it with malloc and place it in android_app.savedState with291* the size in android_app.savedStateSize. The will be freed for you292* later.293*/294APP_CMD_SAVE_STATE,295296/**297* Command from main thread: the app's activity has been paused.298*/299APP_CMD_PAUSE,300301/**302* Command from main thread: the app's activity has been stopped.303*/304APP_CMD_STOP,305306/**307* Command from main thread: the app's activity is being destroyed,308* and waiting for the app thread to clean up and exit before proceeding.309*/310APP_CMD_DESTROY,311};312313/**314* Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next315* app command message.316*/317int8_t android_app_read_cmd(struct android_app* android_app);318319/**320* Call with the command returned by android_app_read_cmd() to do the321* initial pre-processing of the given command. You can perform your own322* actions for the command after calling this function.323*/324void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd);325326/**327* Call with the command returned by android_app_read_cmd() to do the328* final post-processing of the given command. You must have done your own329* actions for the command before calling this function.330*/331void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd);332333/**334* Dummy function you can call to ensure glue code isn't stripped.335*/336void app_dummy(void);337338/**339* This is the function that application code must implement, representing340* the main entry to the app.341*/342extern void android_main(struct android_app* app);343344#ifdef __cplusplus345}346#endif347348#endif /* _ANDROID_NATIVE_APP_GLUE_H */349350351