X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fandroid%2Fmain.c;h=88d7fb5321a8ea4b70fbdbfa49189f94da2e53dc;hb=86b5ef7c43c2a3485850b8a4f58c33aaf6ca2566;hp=2ec85b6a4ed241362a7adb63bbd6f7bc0594a390;hpb=71cf764907df2869a2ac33cbf19aff5710760e23;p=andemo diff --git a/src/android/main.c b/src/android/main.c index 2ec85b6..88d7fb5 100644 --- a/src/android/main.c +++ b/src/android/main.c @@ -1,25 +1,35 @@ #include #include +#include +#include +#include #include #include - -#include "demo.h" +#include #include "android_native_app_glue.h" +#include "demo.h" +#include "logger.h" +#include "demosys.h" static void handle_command(struct android_app *app, int32_t cmd); static int handle_input(struct android_app *app, AInputEvent *ev); static int handle_touch_input(struct android_app *app, AInputEvent *ev); static int init_gl(void); static void destroy_gl(void); +static unsigned long get_time_msec(void); +static void hide_navbar(struct android_app *state); + +struct android_app *app; -static struct android_app *app; static EGLDisplay dpy; static EGLSurface surf; static EGLContext ctx; -static int running; +static int init_done, paused; static int width, height; +static long start_time; + void android_main(struct android_app *app_ptr) { @@ -28,6 +38,12 @@ void android_main(struct android_app *app_ptr) app->onAppCmd = handle_command; app->onInputEvent = handle_input; + hide_navbar(app); + + start_logger(); + + printf("Running %d bit version\n", (int)sizeof(void*) << 3); + for(;;) { int num_events; struct android_poll_source *pollsrc; @@ -41,26 +57,35 @@ void android_main(struct android_app *app_ptr) if(app->destroyRequested) { return; } - if(running) { + if(init_done && !paused) { + time_msec = (long)get_time_msec() - start_time; demo_display(); eglSwapBuffers(dpy, surf); } } } +void swap_buffers(void) +{ + eglSwapBuffers(dpy, surf); +} + static void handle_command(struct android_app *app, int32_t cmd) { int xsz, ysz; switch(cmd) { case APP_CMD_PAUSE: - running = 0; /* TODO: handle timers */ + paused = 1; /* TODO: handle timers */ + dsys_stop(); break; case APP_CMD_RESUME: - running = 1; + paused = 0; + dsys_run(); break; case APP_CMD_INIT_WINDOW: + ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0); if(init_gl() == -1) { exit(1); } @@ -68,11 +93,13 @@ static void handle_command(struct android_app *app, int32_t cmd) exit(1); } demo_reshape(width, height); - running = 1; + start_time = (long)get_time_msec(); + init_done = 1; + dsys_run(); break; case APP_CMD_TERM_WINDOW: - running = 0; + init_done = 0; demo_cleanup(); destroy_gl(); break; @@ -233,3 +260,52 @@ static void destroy_gl(void) eglTerminate(dpy); dpy = 0; } + +static unsigned long get_time_msec(void) +{ + struct timespec ts; + static struct timespec ts0; + + clock_gettime(CLOCK_MONOTONIC, &ts); + if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) { + ts0 = ts; + return 0; + } + return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000; +} + +static void hide_navbar(struct android_app *state) +{ + JNIEnv *env; + jclass cactivity, cwin, cview; + jobject win, view; + jmethodID get_window, get_decor_view, set_system_ui_visibility; + jfieldID field_flag_fs, field_flag_hidenav, field_flag_immersive; + int flag_fs, flag_hidenav, flag_immersive; + + (*state->activity->vm)->AttachCurrentThread(state->activity->vm, &env, 0); + + cactivity = (*env)->FindClass(env, "android/app/NativeActivity"); + get_window = (*env)->GetMethodID(env, cactivity, "getWindow", "()Landroid/view/Window;"); + + cwin = (*env)->FindClass(env, "android/view/Window"); + get_decor_view = (*env)->GetMethodID(env, cwin, "getDecorView", "()Landroid/view/View;"); + + cview = (*env)->FindClass(env, "android/view/View"); + set_system_ui_visibility = (*env)->GetMethodID(env, cview, "setSystemUiVisibility", "(I)V"); + + win = (*env)->CallObjectMethod(env, state->activity->clazz, get_window); + view = (*env)->CallObjectMethod(env, win, get_decor_view); + + field_flag_fs = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_FULLSCREEN", "I"); + field_flag_hidenav = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_HIDE_NAVIGATION", "I"); + field_flag_immersive = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY", "I"); + + flag_fs = (*env)->GetStaticIntField(env, cview, field_flag_fs); + flag_hidenav = (*env)->GetStaticIntField(env, cview, field_flag_hidenav); + flag_immersive = (*env)->GetStaticIntField(env, cview, field_flag_immersive); + + (*env)->CallVoidMethod(env, view, set_system_ui_visibility, flag_fs | flag_hidenav | flag_immersive); + + (*state->activity->vm)->DetachCurrentThread(state->activity->vm); +}