Some native activity sample code cleanup.

Update to include newest headers and library, tweak glue code to
work better with state saving and add support for config changes.

Change-Id: I4d27bd4a0f542f217efaec86cf4f219aca020426
This commit is contained in:
Dianne Hackborn
2010-08-04 11:13:03 -07:00
parent f4790e6f9e
commit 79b946e8f2
15 changed files with 922 additions and 182 deletions

View File

@@ -411,53 +411,37 @@ static int engine_term_display(struct engine* engine) {
engine->animating = 0;
}
static int engine_do_ui_event(struct engine* engine) {
AInputEvent* event = NULL;
if (AInputQueue_getEvent(engine->app->inputQueue, &event) >= 0) {
if (AInputQueue_preDispatchEvent(engine->app->inputQueue, event)) {
return 1;
}
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
engine->animating = 1;
AInputQueue_finishEvent(engine->app->inputQueue, event, 1);
} else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
LOGI("Key event: action=%d keyCode=%d metaState=0x%x",
AKeyEvent_getAction(event),
AKeyEvent_getKeyCode(event),
AKeyEvent_getMetaState(event));
AInputQueue_finishEvent(engine->app->inputQueue, event, 0);
} else {
AInputQueue_finishEvent(engine->app->inputQueue, event, 0);
}
} else {
LOGI("Failure reading next input event: %s\n", strerror(errno));
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
struct engine* engine = (struct engine*)app->userData;
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
engine->animating = 1;
return 1;
} else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
LOGI("Key event: action=%d keyCode=%d metaState=0x%x",
AKeyEvent_getAction(event),
AKeyEvent_getKeyCode(event),
AKeyEvent_getMetaState(event));
}
return 1;
return 0;
}
static int32_t engine_do_main_cmd(struct engine* engine) {
int32_t res;
int8_t cmd = android_app_read_cmd(engine->app);
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
struct engine* engine = (struct engine*)app->userData;
switch (cmd) {
case APP_CMD_WINDOW_CHANGED:
engine_term_display(engine);
res = android_app_exec_cmd(engine->app, cmd);
case APP_CMD_INIT_WINDOW:
if (engine->app->window != NULL) {
engine_draw_frame(engine);
}
break;
case APP_CMD_TERM_WINDOW:
engine_term_display(engine);
break;
case APP_CMD_LOST_FOCUS:
res = android_app_exec_cmd(engine->app, cmd);
engine->animating = 0;
engine_draw_frame(engine);
break;
default:
res = android_app_exec_cmd(engine->app, cmd);
break;
}
return res;
}
void android_main(struct android_app* state) {
@@ -465,8 +449,13 @@ void android_main(struct android_app* state) {
struct engine engine;
// Make sure glue isn't stripped.
app_dummy();
memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.app = state;
if (!init) {
@@ -482,19 +471,24 @@ void android_main(struct android_app* state) {
// Read all pending events.
int fd;
int events;
void* data;
while ((fd=ALooper_pollAll(engine.animating ? 0 : -1, &events, &data)) >= 0) {
switch ((int)data) {
case LOOPER_ID_MAIN:
if (!engine_do_main_cmd(&engine)) {
LOGI("Engine thread destroy requested!");
engine_term_display(&engine);
return;
}
break;
case LOOPER_ID_EVENT:
engine_do_ui_event(&engine);
break;
struct android_poll_source* source;
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((fd=ALooper_pollAll(engine.animating ? 0 : -1, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) {
source->process(state);
}
// Check if we are exiting.
if (state->destroyRequested != 0) {
LOGI("Engine thread destroy requested!");
engine_term_display(&engine);
return;
}
}