8 #include <android/window.h>
9 #include "android_native_app_glue.h"
14 static void handle_command(struct android_app *app, int32_t cmd);
15 static int handle_input(struct android_app *app, AInputEvent *ev);
16 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
17 static int init_gl(void);
18 static void destroy_gl(void);
19 static unsigned long get_time_msec(void);
20 static void hide_navbar(struct android_app *state);
22 struct android_app *app;
24 static EGLDisplay dpy;
25 static EGLSurface surf;
26 static EGLContext ctx;
27 static int init_done, paused;
29 static int width, height;
31 static long start_time;
34 void android_main(struct android_app *app_ptr)
38 app->onAppCmd = handle_command;
39 app->onInputEvent = handle_input;
47 struct android_poll_source *pollsrc;
49 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
51 pollsrc->process(app, pollsrc);
55 if(app->destroyRequested) {
58 if(init_done && !paused) {
59 time_msec = (long)get_time_msec() - start_time;
61 eglSwapBuffers(dpy, surf);
66 void swap_buffers(void)
68 eglSwapBuffers(dpy, surf);
71 static void handle_command(struct android_app *app, int32_t cmd)
77 paused = 1; /* TODO: handle timers */
85 case APP_CMD_INIT_WINDOW:
86 ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0);
90 if(demo_init() == -1) {
93 demo_reshape(width, height);
94 start_time = (long)get_time_msec();
99 case APP_CMD_TERM_WINDOW:
105 case APP_CMD_WINDOW_RESIZED:
106 case APP_CMD_CONFIG_CHANGED:
107 xsz = ANativeWindow_getWidth(app->window);
108 ysz = ANativeWindow_getHeight(app->window);
109 if(xsz != width || ysz != height) {
110 printf("reshape(%d, %d)\n", xsz, ysz);
111 demo_reshape(xsz, ysz);
118 case APP_CMD_SAVE_STATE:
119 case APP_CMD_GAINED_FOCUS:
120 case APP_CMD_LOST_FOCUS:
127 static int handle_input(struct android_app *app, AInputEvent *ev)
129 int evtype = AInputEvent_getType(ev);
132 case AINPUT_EVENT_TYPE_MOTION:
133 return handle_touch_input(app, ev);
141 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
143 int i, pcount, x, y, idx;
145 static int prev_pos[2];
147 action = AMotionEvent_getAction(ev);
149 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
150 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
152 x = AMotionEvent_getX(ev, idx);
153 y = AMotionEvent_getY(ev, idx);
155 switch(action & AMOTION_EVENT_ACTION_MASK) {
156 case AMOTION_EVENT_ACTION_DOWN:
157 case AMOTION_EVENT_ACTION_POINTER_DOWN:
158 demo_mouse(0, 1, x, y);
164 case AMOTION_EVENT_ACTION_UP:
165 case AMOTION_EVENT_ACTION_POINTER_UP:
166 demo_mouse(0, 0, x, y);
172 case AMOTION_EVENT_ACTION_MOVE:
173 pcount = AMotionEvent_getPointerCount(ev);
174 for(i=0; i<pcount; i++) {
175 int id = AMotionEvent_getPointerId(ev, i);
192 static int init_gl(void)
194 static const int eglattr[] = {
195 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
196 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
203 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
208 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
209 if(!dpy || !eglInitialize(dpy, 0, 0)) {
210 fprintf(stderr, "failed to initialize EGL\n");
215 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
216 fprintf(stderr, "no matching EGL config found\n");
221 /* configure the native window visual according to the chosen EGL config */
222 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
223 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
225 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
226 fprintf(stderr, "failed to create window\n");
231 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
232 fprintf(stderr, "failed to create OpenGL ES context\n");
236 eglMakeCurrent(dpy, surf, surf, ctx);
238 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
239 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
243 static void destroy_gl(void)
247 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
250 eglDestroyContext(dpy, ctx);
254 eglDestroySurface(dpy, surf);
262 static unsigned long get_time_msec(void)
265 static struct timespec ts0;
267 clock_gettime(CLOCK_MONOTONIC, &ts);
268 if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) {
272 return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000;
275 static void hide_navbar(struct android_app *state)
278 jclass cactivity, cwin, cview;
280 jmethodID get_window, get_decor_view, set_system_ui_visibility;
281 jfieldID field_flag_fs, field_flag_hidenav, field_flag_immersive;
282 int flag_fs, flag_hidenav, flag_immersive;
284 (*state->activity->vm)->AttachCurrentThread(state->activity->vm, &env, 0);
286 cactivity = (*env)->FindClass(env, "android/app/NativeActivity");
287 get_window = (*env)->GetMethodID(env, cactivity, "getWindow", "()Landroid/view/Window;");
289 cwin = (*env)->FindClass(env, "android/view/Window");
290 get_decor_view = (*env)->GetMethodID(env, cwin, "getDecorView", "()Landroid/view/View;");
292 cview = (*env)->FindClass(env, "android/view/View");
293 set_system_ui_visibility = (*env)->GetMethodID(env, cview, "setSystemUiVisibility", "(I)V");
295 win = (*env)->CallObjectMethod(env, state->activity->clazz, get_window);
296 view = (*env)->CallObjectMethod(env, win, get_decor_view);
298 field_flag_fs = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_FULLSCREEN", "I");
299 field_flag_hidenav = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_HIDE_NAVIGATION", "I");
300 field_flag_immersive = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY", "I");
302 flag_fs = (*env)->GetStaticIntField(env, cview, field_flag_fs);
303 flag_hidenav = (*env)->GetStaticIntField(env, cview, field_flag_hidenav);
304 flag_immersive = (*env)->GetStaticIntField(env, cview, field_flag_immersive);
306 (*env)->CallVoidMethod(env, view, set_system_ui_visibility, flag_fs | flag_hidenav | flag_immersive);
308 (*state->activity->vm)->DetachCurrentThread(state->activity->vm);