Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…tform/ndk/+/1180321

some APP_CMDs callbacks were never triggered:
APP_CMD_WINDOW_RESIZED / APP_CMD_WINDOW_REDRAW_NEEDED / APP_CMD_CONTENT_RECT_CHANGED

See android/ndk#1139
  • Loading branch information
pazos committed Oct 24, 2020
1 parent c389509 commit 4fb55d9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 75 deletions.
151 changes: 83 additions & 68 deletions jni/android_native_app_glue/android_native_app_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>

#include <android/log.h>

Expand Down Expand Up @@ -50,20 +49,14 @@ static void free_saved_state(struct android_app* android_app) {

int8_t android_app_read_cmd(struct android_app* android_app) {
int8_t cmd;
if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
switch (cmd) {
case APP_CMD_SAVE_STATE:
free_saved_state(android_app);
break;
}
return cmd;
} else {
if (read(android_app->msgread, &cmd, sizeof(cmd)) != sizeof(cmd)) {
LOGE("%s: No data on command pipe!", TAG);
return -1;
}
return -1;
if (cmd == APP_CMD_SAVE_STATE) free_saved_state(android_app);
return cmd;
}


/*static void print_cur_config(struct android_app* android_app) {
char lang[2], country[2];
AConfiguration_getLanguage(android_app->config, lang);
Expand Down Expand Up @@ -92,7 +85,7 @@ int8_t android_app_read_cmd(struct android_app* android_app) {
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
switch (cmd) {
case APP_CMD_INPUT_CHANGED:
LOGD("%s: APP_CMD_INPUT_CHANGED\n", TAG);
LOGD("%s: APP_CMD_INPUT_CHANGED", TAG);
pthread_mutex_lock(&android_app->mutex);
if (android_app->inputQueue != NULL) {
AInputQueue_detachLooper(android_app->inputQueue);
Expand All @@ -109,38 +102,38 @@ void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
break;

case APP_CMD_INIT_WINDOW:
LOGD("%s: APP_CMD_INIT_WINDOW\n", TAG);
LOGD("%s: APP_CMD_INIT_WINDOW", TAG);
pthread_mutex_lock(&android_app->mutex);
android_app->window = android_app->pendingWindow;
pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex);
break;

case APP_CMD_TERM_WINDOW:
LOGD("%s: APP_CMD_TERM_WINDOW\n", TAG);
LOGD("%s: APP_CMD_TERM_WINDOW", TAG);
pthread_cond_broadcast(&android_app->cond);
break;

case APP_CMD_RESUME:
case APP_CMD_START:
case APP_CMD_PAUSE:
case APP_CMD_STOP:
LOGD("%s: activityState=%d\n", TAG, cmd);
LOGD("%s: activityState=%d", TAG, cmd);
pthread_mutex_lock(&android_app->mutex);
android_app->activityState = cmd;
pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex);
break;

case APP_CMD_CONFIG_CHANGED:
LOGD("%s: APP_CMD_CONFIG_CHANGED\n", TAG);
LOGD("%s: APP_CMD_CONFIG_CHANGED", TAG);
AConfiguration_fromAssetManager(android_app->config,
android_app->activity->assetManager);
//print_cur_config(android_app);
break;

case APP_CMD_DESTROY:
LOGD("%s: APP_CMD_DESTROY\n", TAG);
LOGD("%s: APP_CMD_DESTROY", TAG);
android_app->destroyRequested = 1;
break;
}
Expand All @@ -149,15 +142,15 @@ void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
switch (cmd) {
case APP_CMD_TERM_WINDOW:
LOGD("%s: APP_CMD_TERM_WINDOW\n", TAG);
LOGD("%s: APP_CMD_TERM_WINDOW", TAG);
pthread_mutex_lock(&android_app->mutex);
android_app->window = NULL;
pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex);
break;

case APP_CMD_SAVE_STATE:
LOGD("%s: APP_CMD_SAVE_STATE\n", TAG);
LOGD("%s: APP_CMD_SAVE_STATE", TAG);
pthread_mutex_lock(&android_app->mutex);
android_app->stateSaved = 1;
pthread_cond_broadcast(&android_app->cond);
Expand All @@ -170,9 +163,8 @@ void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
}
}

/*void app_dummy(void) {
}*/
void app_dummy() {
}

static void android_app_destroy(struct android_app* android_app) {
LOGD("%s: android_app_destroy!", TAG);
Expand All @@ -191,7 +183,7 @@ static void android_app_destroy(struct android_app* android_app) {
static void process_input(struct android_app* app, struct android_poll_source* source) {
AInputEvent* event = NULL;
while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
LOGD("%s: New input event: type=%d\n", TAG, AInputEvent_getType(event));
LOGD("%s: New input event: type=%d", TAG, AInputEvent_getType(event));
if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
continue;
}
Expand Down Expand Up @@ -244,9 +236,8 @@ static void* android_app_entry(void* param) {
// --------------------------------------------------------------------

static struct android_app* android_app_create(ANativeActivity* activity,
void* savedState, size_t savedStateSize) {
struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
memset(android_app, 0, sizeof(struct android_app));
void* savedState, size_t savedStateSize) {
struct android_app* android_app = calloc(1, sizeof(struct android_app));
android_app->activity = activity;

pthread_mutex_init(&android_app->mutex, NULL);
Expand Down Expand Up @@ -283,7 +274,7 @@ static struct android_app* android_app_create(ANativeActivity* activity,

static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
LOGE("%s: Failure writing android_app cmd: %s\n", TAG, strerror(errno));
LOGE("%s: Failure writing android_app cmd: %s", TAG, strerror(errno));
}
}

Expand Down Expand Up @@ -336,26 +327,30 @@ static void android_app_free(struct android_app* android_app) {
free(android_app);
}

static struct android_app* ToApp(ANativeActivity* activity) {
return (struct android_app*) activity->instance;
}

static void onDestroy(ANativeActivity* activity) {
LOGD("%s: Destroy: %p\n", TAG, activity);
android_app_free((struct android_app*)activity->instance);
LOGD("%s: Destroy: %p", TAG, activity);
android_app_free(ToApp(activity));
}

static void onStart(ANativeActivity* activity) {
LOGD("%s: Start: %p\n", TAG, activity);
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
LOGD("%s: Start: %p", TAG, activity);
android_app_set_activity_state(ToApp(activity), APP_CMD_START);
}

static void onResume(ANativeActivity* activity) {
LOGD("%s: Resume: %p\n", TAG, activity);
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
LOGD("%s: Resume: %p", TAG, activity);
android_app_set_activity_state(ToApp(activity), APP_CMD_RESUME);
}

static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
struct android_app* android_app = (struct android_app*)activity->instance;
void* savedState = NULL;
LOGD("%s: SaveInstanceState: %p", TAG, activity);

LOGD("%s: SaveInstanceState: %p\n", TAG, activity);
struct android_app* android_app = ToApp(activity);
void* savedState = NULL;
pthread_mutex_lock(&android_app->mutex);
android_app->stateSaved = 0;
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
Expand All @@ -376,69 +371,89 @@ static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
}

static void onPause(ANativeActivity* activity) {
LOGD("%s: Pause: %p\n", TAG, activity);
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
LOGD("%s: Pause: %p", TAG, activity);
android_app_set_activity_state(ToApp(activity), APP_CMD_PAUSE);
}

static void onStop(ANativeActivity* activity) {
LOGD("%s: Stop: %p\n", TAG, activity);
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
LOGD("%s: Stop: %p", TAG, activity);
android_app_set_activity_state(ToApp(activity), APP_CMD_STOP);
}

static void onConfigurationChanged(ANativeActivity* activity) {
struct android_app* android_app = (struct android_app*)activity->instance;
LOGD("%s: ConfigurationChanged: %p\n", TAG, activity);
android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
LOGD("%s: ConfigurationChanged: %p", TAG, activity);
android_app_write_cmd(ToApp(activity), APP_CMD_CONFIG_CHANGED);
}

static void onContentRectChanged(ANativeActivity* activity, const ARect* r) {
LOGD("%s: ContentRectChanged: l=%d,t=%d,r=%d,b=%d", TAG, r->left, r->top, r->right, r->bottom);
struct android_app* android_app = ToApp(activity);
pthread_mutex_lock(&android_app->mutex);
android_app->contentRect = *r;
pthread_mutex_unlock(&android_app->mutex);
android_app_write_cmd(ToApp(activity), APP_CMD_CONTENT_RECT_CHANGED);
}

static void onLowMemory(ANativeActivity* activity) {
struct android_app* android_app = (struct android_app*)activity->instance;
LOGD("%s: LowMemory: %p\n", TAG, activity);
android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
LOGD("%s: LowMemory: %p", TAG, activity);
android_app_write_cmd(ToApp(activity), APP_CMD_LOW_MEMORY);
}

static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
LOGD("%s: WindowFocusChanged: %p -- %d\n", TAG, activity, focused);
android_app_write_cmd((struct android_app*)activity->instance,
focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
LOGD("%s: WindowFocusChanged: %p -- %d", TAG, activity, focused);
android_app_write_cmd(ToApp(activity), focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
}

static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
LOGD("%s: NativeWindowCreated: %p -- %p\n", TAG, activity, window);
android_app_set_window((struct android_app*)activity->instance, window);
LOGD("%s: NativeWindowCreated: %p -- %p", TAG, activity, window);
android_app_set_window(ToApp(activity), window);
}

static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
LOGD("%s: NativeWindowDestroyed: %p -- %p\n", TAG, activity, window);
android_app_set_window((struct android_app*)activity->instance, NULL);
LOGD("%s: NativeWindowDestroyed: %p -- %p", TAG, activity, window);
android_app_set_window(ToApp(activity), NULL);
}

static void onNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* window) {
LOGD("%s: NativeWindowRedrawNeeded: %p -- %p", TAG, activity, window);
android_app_write_cmd(ToApp(activity), APP_CMD_WINDOW_REDRAW_NEEDED);
}

static void onNativeWindowResized(ANativeActivity* activity, ANativeWindow* window) {
LOGD("%s: NativeWindowResized: %p -- %p", TAG, activity, window);
android_app_write_cmd(ToApp(activity), APP_CMD_WINDOW_RESIZED);
}

static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
LOGD("%s: InputQueueCreated: %p -- %p\n", TAG, activity, queue);
android_app_set_input((struct android_app*)activity->instance, queue);
LOGD("%s: InputQueueCreated: %p -- %p", TAG, activity, queue);
android_app_set_input(ToApp(activity), queue);
}

static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
LOGD("%s: InputQueueDestroyed: %p -- %p\n", TAG, activity, queue);
android_app_set_input((struct android_app*)activity->instance, NULL);
LOGD("%s: InputQueueDestroyed: %p -- %p", TAG, activity, queue);
android_app_set_input(ToApp(activity), NULL);
}

__attribute__((visibility("default"))) void ANativeActivity_onCreate(ANativeActivity* activity,
void* savedState, size_t savedStateSize) {
LOGD("%s: Creating: %p\n", TAG, activity);
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize) {
LOGD("%s: Creating: %p", TAG, activity);

activity->callbacks->onConfigurationChanged = onConfigurationChanged;
activity->callbacks->onContentRectChanged = onContentRectChanged;
activity->callbacks->onDestroy = onDestroy;
activity->callbacks->onStart = onStart;
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
activity->callbacks->onLowMemory = onLowMemory;
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
activity->callbacks->onNativeWindowRedrawNeeded = onNativeWindowRedrawNeeded;
activity->callbacks->onNativeWindowResized = onNativeWindowResized;
activity->callbacks->onPause = onPause;
activity->callbacks->onResume = onResume;
activity->callbacks->onSaveInstanceState = onSaveInstanceState;
activity->callbacks->onPause = onPause;
activity->callbacks->onStart = onStart;
activity->callbacks->onStop = onStop;
activity->callbacks->onConfigurationChanged = onConfigurationChanged;
activity->callbacks->onLowMemory = onLowMemory;
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;

activity->instance = android_app_create(activity, savedState, savedStateSize);
}
15 changes: 8 additions & 7 deletions jni/android_native_app_glue/android_native_app_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef _ANDROID_NATIVE_APP_GLUE_H
#define _ANDROID_NATIVE_APP_GLUE_H
#pragma once

#include <poll.h>
#include <pthread.h>
Expand Down Expand Up @@ -332,9 +330,14 @@ void android_app_pre_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 that used to be used to prevent the linker from stripping app
* glue code. No longer necessary, since __attribute__((visibility("default")))
* does this for us.
*/
void app_dummy(void);
__attribute__((
deprecated("Calls to app_dummy are no longer necessary. See "
"https://github.com/android-ndk/ndk/issues/381."))) void
app_dummy();

/**
* This is the function that application code must implement, representing
Expand All @@ -345,5 +348,3 @@ extern void android_main(struct android_app* app);
#ifdef __cplusplus
}
#endif

#endif /* _ANDROID_NATIVE_APP_GLUE_H */

0 comments on commit 4fb55d9

Please sign in to comment.