9 #include "android_native_app_glue.h"
13 static void handle_command(struct android_app *app, int32_t cmd);
14 static int handle_input(struct android_app *app, AInputEvent *ev);
15 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
16 static int init_gl(void);
17 static void destroy_gl(void);
18 static unsigned long get_time_msec(void);
19 static void hide_navbar(struct android_app *state);
21 struct android_app *app;
23 static EGLDisplay dpy;
24 static EGLSurface surf;
25 static EGLContext ctx;
26 static int init_done, paused;
28 static int width, height;
30 static long start_time;
33 void android_main(struct android_app *app_ptr)
37 app->onAppCmd = handle_command;
38 app->onInputEvent = handle_input;
46 struct android_poll_source *pollsrc;
48 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
50 pollsrc->process(app, pollsrc);
54 if(app->destroyRequested) {
57 if(init_done && !paused) {
58 time_msec = (long)get_time_msec() - start_time;
60 eglSwapBuffers(dpy, surf);
65 void swap_buffers(void)
67 eglSwapBuffers(dpy, surf);
70 static void handle_command(struct android_app *app, int32_t cmd)
76 paused = 1; /* TODO: handle timers */
84 case APP_CMD_INIT_WINDOW:
88 if(demo_init() == -1) {
91 demo_reshape(width, height);
92 start_time = (long)get_time_msec();
97 case APP_CMD_TERM_WINDOW:
103 case APP_CMD_WINDOW_RESIZED:
104 case APP_CMD_CONFIG_CHANGED:
105 xsz = ANativeWindow_getWidth(app->window);
106 ysz = ANativeWindow_getHeight(app->window);
107 if(xsz != width || ysz != height) {
108 printf("reshape(%d, %d)\n", xsz, ysz);
109 demo_reshape(xsz, ysz);
116 case APP_CMD_SAVE_STATE:
117 case APP_CMD_GAINED_FOCUS:
118 case APP_CMD_LOST_FOCUS:
125 static int handle_input(struct android_app *app, AInputEvent *ev)
127 int evtype = AInputEvent_getType(ev);
130 case AINPUT_EVENT_TYPE_MOTION:
131 return handle_touch_input(app, ev);
139 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
141 int i, pcount, x, y, idx;
143 static int prev_pos[2];
145 action = AMotionEvent_getAction(ev);
147 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
148 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
150 x = AMotionEvent_getX(ev, idx);
151 y = AMotionEvent_getY(ev, idx);
153 switch(action & AMOTION_EVENT_ACTION_MASK) {
154 case AMOTION_EVENT_ACTION_DOWN:
155 case AMOTION_EVENT_ACTION_POINTER_DOWN:
156 demo_mouse(0, 1, x, y);
162 case AMOTION_EVENT_ACTION_UP:
163 case AMOTION_EVENT_ACTION_POINTER_UP:
164 demo_mouse(0, 0, x, y);
170 case AMOTION_EVENT_ACTION_MOVE:
171 pcount = AMotionEvent_getPointerCount(ev);
172 for(i=0; i<pcount; i++) {
173 int id = AMotionEvent_getPointerId(ev, i);
190 static int init_gl(void)
192 static const int eglattr[] = {
193 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
194 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
201 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
206 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
207 if(!dpy || !eglInitialize(dpy, 0, 0)) {
208 fprintf(stderr, "failed to initialize EGL\n");
213 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
214 fprintf(stderr, "no matching EGL config found\n");
219 /* configure the native window visual according to the chosen EGL config */
220 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
221 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
223 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
224 fprintf(stderr, "failed to create window\n");
229 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
230 fprintf(stderr, "failed to create OpenGL ES context\n");
234 eglMakeCurrent(dpy, surf, surf, ctx);
236 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
237 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
241 static void destroy_gl(void)
245 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
248 eglDestroyContext(dpy, ctx);
252 eglDestroySurface(dpy, surf);
260 static unsigned long get_time_msec(void)
263 static struct timespec ts0;
265 clock_gettime(CLOCK_MONOTONIC, &ts);
266 if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) {
270 return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000;
273 static void hide_navbar(struct android_app *state)
276 jclass cactivity, cwin, cview;
278 jmethodID get_window, get_decor_view, set_system_ui_visibility;
279 jfieldID field_flag_fs, field_flag_hidenav, field_flag_immersive;
280 int flag_fs, flag_hidenav, flag_immersive;
282 (*state->activity->vm)->AttachCurrentThread(state->activity->vm, &env, 0);
284 cactivity = (*env)->FindClass(env, "android/app/NativeActivity");
285 get_window = (*env)->GetMethodID(env, cactivity, "getWindow", "()Landroid/view/Window;");
287 cwin = (*env)->FindClass(env, "android/view/Window");
288 get_decor_view = (*env)->GetMethodID(env, cwin, "getDecorView", "()Landroid/view/View;");
290 cview = (*env)->FindClass(env, "android/view/View");
291 set_system_ui_visibility = (*env)->GetMethodID(env, cview, "setSystemUiVisibility", "(I)V");
293 win = (*env)->CallObjectMethod(env, state->activity->clazz, get_window);
294 view = (*env)->CallObjectMethod(env, win, get_decor_view);
296 field_flag_fs = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_FULLSCREEN", "I");
297 field_flag_hidenav = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_HIDE_NAVIGATION", "I");
298 field_flag_immersive = (*env)->GetStaticFieldID(env, cview, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY", "I");
300 flag_fs = (*env)->GetStaticIntField(env, cview, field_flag_fs);
301 flag_hidenav = (*env)->GetStaticIntField(env, cview, field_flag_hidenav);
302 flag_immersive = (*env)->GetStaticIntField(env, cview, field_flag_immersive);
304 (*env)->CallVoidMethod(env, view, set_system_ui_visibility, flag_fs | flag_hidenav | flag_immersive);
306 (*state->activity->vm)->DetachCurrentThread(state->activity->vm);