(Android) native glue code - fix indenting and remove savestate

leftovers
This commit is contained in:
twinaphex 2012-10-31 18:52:43 +01:00
parent 4d99d93df0
commit 5240f2d9f1
2 changed files with 355 additions and 421 deletions

View File

@ -23,21 +23,6 @@
#include <sys/resource.h> #include <sys/resource.h>
#include "android_native_app_glue.h" #include "android_native_app_glue.h"
#include <android/log.h>
static void free_saved_state(struct android_app* android_app)
{
pthread_mutex_lock(&android_app->mutex);
if (android_app->savedState != NULL)
{
free(android_app->savedState);
android_app->savedState = NULL;
android_app->savedStateSize = 0;
}
pthread_mutex_unlock(&android_app->mutex);
}
int8_t android_app_read_cmd(struct android_app* android_app) int8_t android_app_read_cmd(struct android_app* android_app)
{ {
@ -48,7 +33,6 @@ int8_t android_app_read_cmd(struct android_app* android_app)
switch (cmd) switch (cmd)
{ {
case APP_CMD_SAVE_STATE: case APP_CMD_SAVE_STATE:
free_saved_state(android_app);
break; break;
} }
return cmd; return cmd;
@ -63,7 +47,7 @@ static void print_cur_config(struct android_app* android_app)
AConfiguration_getLanguage(android_app->config, lang); AConfiguration_getLanguage(android_app->config, lang);
AConfiguration_getCountry(android_app->config, country); AConfiguration_getCountry(android_app->config, country);
/* /*
LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d " LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
"keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d " "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
"modetype=%d modenight=%d", "modetype=%d modenight=%d",
@ -82,7 +66,7 @@ static void print_cur_config(struct android_app* android_app)
AConfiguration_getScreenLong(android_app->config), AConfiguration_getScreenLong(android_app->config),
AConfiguration_getUiModeType(android_app->config), AConfiguration_getUiModeType(android_app->config),
AConfiguration_getUiModeNight(android_app->config)); AConfiguration_getUiModeNight(android_app->config));
*/ */
} }
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd)
@ -142,13 +126,8 @@ void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd)
pthread_mutex_unlock(&android_app->mutex); pthread_mutex_unlock(&android_app->mutex);
break; break;
case APP_CMD_SAVE_STATE: case APP_CMD_SAVE_STATE:
pthread_mutex_lock(&android_app->mutex);
android_app->stateSaved = 1;
pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex);
break; break;
case APP_CMD_RESUME: case APP_CMD_RESUME:
free_saved_state(android_app);
break; break;
} }
} }
@ -159,8 +138,6 @@ void app_dummy()
static void android_app_destroy(struct android_app* android_app) static void android_app_destroy(struct android_app* android_app)
{ {
free_saved_state(android_app);
pthread_mutex_lock(&android_app->mutex); pthread_mutex_lock(&android_app->mutex);
if (android_app->inputQueue != NULL) if (android_app->inputQueue != NULL)
@ -173,7 +150,7 @@ static void android_app_destroy(struct android_app* android_app)
// Can't touch android_app object after this. // Can't touch android_app object after this.
} }
static void process_input(struct android_app* app, struct android_poll_source* source) void process_input(struct android_app* app, struct android_poll_source* source)
{ {
AInputEvent* event = NULL; AInputEvent* event = NULL;
@ -249,13 +226,6 @@ static struct android_app* android_app_create(ANativeActivity* activity,
pthread_mutex_init(&android_app->mutex, NULL); pthread_mutex_init(&android_app->mutex, NULL);
pthread_cond_init(&android_app->cond, NULL); pthread_cond_init(&android_app->cond, NULL);
if (savedState != NULL)
{
android_app->savedState = malloc(savedStateSize);
android_app->savedStateSize = savedStateSize;
memcpy(android_app->savedState, savedState, savedStateSize);
}
int msgpipe[2]; int msgpipe[2];
if (pipe(msgpipe)) if (pipe(msgpipe))
return NULL; return NULL;
@ -360,26 +330,7 @@ static void onResume(ANativeActivity* activity)
static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen)
{ {
struct android_app* android_app = (struct android_app*)activity->instance; return NULL;
void* savedState = NULL;
pthread_mutex_lock(&android_app->mutex);
android_app->stateSaved = 0;
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
while (!android_app->stateSaved)
pthread_cond_wait(&android_app->cond, &android_app->mutex);
if (android_app->savedState != NULL)
{
savedState = android_app->savedState;
*outLen = android_app->savedStateSize;
android_app->savedState = NULL;
android_app->savedStateSize = 0;
}
pthread_mutex_unlock(&android_app->mutex);
return savedState;
} }
static void onPause(ANativeActivity* activity) static void onPause(ANativeActivity* activity)
@ -431,12 +382,11 @@ static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue)
} }
void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState,
size_t savedStateSize) size_t savedStateSize)
{ {
activity->callbacks->onDestroy = onDestroy; activity->callbacks->onDestroy = onDestroy;
activity->callbacks->onStart = onStart; activity->callbacks->onStart = onStart;
activity->callbacks->onResume = onResume; activity->callbacks->onResume = onResume;
activity->callbacks->onSaveInstanceState = onSaveInstanceState;
activity->callbacks->onPause = onPause; activity->callbacks->onPause = onPause;
activity->callbacks->onStop = onStop; activity->callbacks->onStop = onStop;
activity->callbacks->onConfigurationChanged = onConfigurationChanged; activity->callbacks->onConfigurationChanged = onConfigurationChanged;

View File

@ -30,7 +30,7 @@
extern "C" { extern "C" {
#endif #endif
/** /**
* The native activity interface provided by <android/native_activity.h> * The native activity interface provided by <android/native_activity.h>
* is based on a set of application-provided callbacks that will be called * is based on a set of application-provided callbacks that will be called
* by the Activity's main thread when certain events occur. * by the Activity's main thread when certain events occur.
@ -81,13 +81,13 @@ extern "C" {
* full usage example. Also look at the JavaDoc of NativeActivity. * full usage example. Also look at the JavaDoc of NativeActivity.
*/ */
struct android_app; struct android_app;
/** /**
* Data associated with an ALooper fd that will be returned as the "outData" * Data associated with an ALooper fd that will be returned as the "outData"
* when that source has data ready. * when that source has data ready.
*/ */
struct android_poll_source { struct android_poll_source {
// The identifier of this source. May be LOOPER_ID_MAIN or // The identifier of this source. May be LOOPER_ID_MAIN or
// LOOPER_ID_INPUT. // LOOPER_ID_INPUT.
int32_t id; int32_t id;
@ -98,9 +98,9 @@ struct android_poll_source {
// Function to call to perform the standard processing of data from // Function to call to perform the standard processing of data from
// this source. // this source.
void (*process)(struct android_app* app, struct android_poll_source* source); void (*process)(struct android_app* app, struct android_poll_source* source);
}; };
/** /**
* This is the interface for the standard glue code of a threaded * This is the interface for the standard glue code of a threaded
* application. In this model, the application's code is running * application. In this model, the application's code is running
* in its own thread separate from the main thread of the process. * in its own thread separate from the main thread of the process.
@ -108,11 +108,7 @@ struct android_poll_source {
* VM, although it will need to be in order to make JNI calls any * VM, although it will need to be in order to make JNI calls any
* Java objects. * Java objects.
*/ */
struct android_app { struct android_app {
// The application can place a pointer to its own state object
// here if it likes.
void* userData;
// Fill this in with the function to process main app commands (APP_CMD_*) // Fill this in with the function to process main app commands (APP_CMD_*)
void (*onAppCmd)(struct android_app* app, int32_t cmd); void (*onAppCmd)(struct android_app* app, int32_t cmd);
@ -128,17 +124,6 @@ struct android_app {
// The current configuration the app is running in. // The current configuration the app is running in.
AConfiguration* config; AConfiguration* config;
// This is the last instance's saved state, as provided at creation time.
// It is NULL if there was no state. You can use this as you need; the
// memory will remain around until you call android_app_exec_cmd() for
// APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
// These variables should only be changed when processing a APP_CMD_SAVE_STATE,
// at which point they will be initialized to NULL and you can malloc your
// state and place the information here. In that case the memory will be
// freed for you later.
void* savedState;
size_t savedStateSize;
// The ALooper associated with the app's thread. // The ALooper associated with the app's thread.
ALooper* looper; ALooper* looper;
@ -176,15 +161,14 @@ struct android_app {
struct android_poll_source inputPollSource; struct android_poll_source inputPollSource;
int running; int running;
int stateSaved;
int destroyed; int destroyed;
int redrawNeeded; int redrawNeeded;
AInputQueue* pendingInputQueue; AInputQueue* pendingInputQueue;
ANativeWindow* pendingWindow; ANativeWindow* pendingWindow;
ARect pendingContentRect; ARect pendingContentRect;
}; };
enum { enum {
/** /**
* Looper data ID of commands coming from the app's main thread, which * Looper data ID of commands coming from the app's main thread, which
* is returned as an identifier from ALooper_pollOnce(). The data for this * is returned as an identifier from ALooper_pollOnce(). The data for this
@ -207,9 +191,9 @@ enum {
* Start of user-defined ALooper identifiers. * Start of user-defined ALooper identifiers.
*/ */
LOOPER_ID_USER = 3, LOOPER_ID_USER = 3,
}; };
enum { enum {
/** /**
* Command from main thread: the AInputQueue has changed. Upon processing * Command from main thread: the AInputQueue has changed. Upon processing
* this command, android_app->inputQueue will be updated to the new queue * this command, android_app->inputQueue will be updated to the new queue
@ -309,38 +293,38 @@ enum {
* and waiting for the app thread to clean up and exit before proceeding. * and waiting for the app thread to clean up and exit before proceeding.
*/ */
APP_CMD_DESTROY, APP_CMD_DESTROY,
}; };
/** /**
* Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next * Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next
* app command message. * app command message.
*/ */
int8_t android_app_read_cmd(struct android_app* android_app); int8_t android_app_read_cmd(struct android_app* android_app);
/** /**
* Call with the command returned by android_app_read_cmd() to do the * Call with the command returned by android_app_read_cmd() to do the
* initial pre-processing of the given command. You can perform your own * initial pre-processing of the given command. You can perform your own
* actions for the command after calling this function. * actions for the command after calling this function.
*/ */
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd); void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd);
/** /**
* Call with the command returned by android_app_read_cmd() to do the * Call with the command returned by android_app_read_cmd() to do the
* final post-processing of the given command. You must have done your own * final post-processing of the given command. You must have done your own
* actions for the command before calling this function. * actions for the command before calling this function.
*/ */
void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd); void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd);
/** /**
* Dummy function you can call to ensure glue code isn't stripped. * Dummy function you can call to ensure glue code isn't stripped.
*/ */
void app_dummy(); void app_dummy();
/** /**
* This is the function that application code must implement, representing * This is the function that application code must implement, representing
* the main entry to the app. * the main entry to the app.
*/ */
extern void android_main(struct android_app* app); extern void android_main(struct android_app* app);
#ifdef __cplusplus #ifdef __cplusplus
} }