- android fullscreen & "immersive mode"
[andemo] / src / android / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <unistd.h>
5 #include <sys/time.h>
6 #include <EGL/egl.h>
7 #include <GLES2/gl2.h>
8 #include "demo.h"
9 #include "android_native_app_glue.h"
10 #include "logger.h"
11 #include "demosys.h"
12
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);
20
21 struct android_app *app;
22
23 static EGLDisplay dpy;
24 static EGLSurface surf;
25 static EGLContext ctx;
26 static int init_done, paused;
27
28 static int width, height;
29
30 static long start_time;
31
32
33 void android_main(struct android_app *app_ptr)
34 {
35         app = app_ptr;
36
37         app->onAppCmd = handle_command;
38         app->onInputEvent = handle_input;
39
40         hide_navbar(app);
41
42         start_logger();
43
44         for(;;) {
45                 int num_events;
46                 struct android_poll_source *pollsrc;
47
48                 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
49                         if(pollsrc) {
50                                 pollsrc->process(app, pollsrc);
51                         }
52                 }
53
54                 if(app->destroyRequested) {
55                         return;
56                 }
57                 if(init_done && !paused) {
58                         time_msec = (long)get_time_msec() - start_time;
59                         demo_display();
60                         eglSwapBuffers(dpy, surf);
61                 }
62         }
63 }
64
65 void swap_buffers(void)
66 {
67         eglSwapBuffers(dpy, surf);
68 }
69
70 static void handle_command(struct android_app *app, int32_t cmd)
71 {
72         int xsz, ysz;
73
74         switch(cmd) {
75         case APP_CMD_PAUSE:
76                 paused = 1;     /* TODO: handle timers */
77                 dsys_stop();
78                 break;
79         case APP_CMD_RESUME:
80                 paused = 0;
81                 dsys_run();
82                 break;
83
84         case APP_CMD_INIT_WINDOW:
85                 if(init_gl() == -1) {
86                         exit(1);
87                 }
88                 if(demo_init() == -1) {
89                         exit(1);
90                 }
91                 demo_reshape(width, height);
92                 start_time = (long)get_time_msec();
93                 init_done = 1;
94                 dsys_run();
95                 break;
96
97         case APP_CMD_TERM_WINDOW:
98                 init_done = 0;
99                 demo_cleanup();
100                 destroy_gl();
101                 break;
102
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);
110                         width = xsz;
111                         height = ysz;
112                 }
113                 break;
114
115         /*
116         case APP_CMD_SAVE_STATE:
117         case APP_CMD_GAINED_FOCUS:
118         case APP_CMD_LOST_FOCUS:
119         */
120         default:
121                 break;
122         }
123 }
124
125 static int handle_input(struct android_app *app, AInputEvent *ev)
126 {
127         int evtype = AInputEvent_getType(ev);
128
129         switch(evtype) {
130         case AINPUT_EVENT_TYPE_MOTION:
131                 return handle_touch_input(app, ev);
132
133         default:
134                 break;
135         }
136         return 0;
137 }
138
139 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
140 {
141         int i, pcount, x, y, idx;
142         unsigned int action;
143         static int prev_pos[2];
144
145         action = AMotionEvent_getAction(ev);
146
147         idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
148                 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
149
150         x = AMotionEvent_getX(ev, idx);
151         y = AMotionEvent_getY(ev, idx);
152
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);
157
158                 prev_pos[0] = x;
159                 prev_pos[1] = y;
160                 break;
161
162         case AMOTION_EVENT_ACTION_UP:
163         case AMOTION_EVENT_ACTION_POINTER_UP:
164                 demo_mouse(0, 0, x, y);
165
166                 prev_pos[0] = x;
167                 prev_pos[1] = y;
168                 break;
169
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);
174                         if(id == 0) {
175                                 demo_motion(x, y);
176                                 prev_pos[0] = x;
177                                 prev_pos[1] = y;
178                                 break;
179                         }
180                 }
181                 break;
182
183         default:
184                 break;
185         }
186
187         return 1;
188 }
189
190 static int init_gl(void)
191 {
192         static const int eglattr[] = {
193                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
194                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
195                 EGL_RED_SIZE, 5,
196                 EGL_GREEN_SIZE, 5,
197                 EGL_BLUE_SIZE, 5,
198                 EGL_DEPTH_SIZE, 16,
199                 EGL_NONE
200         };
201         static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
202
203         EGLConfig eglcfg;
204         int count, vis;
205
206         dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
207         if(!dpy || !eglInitialize(dpy, 0, 0)) {
208                 fprintf(stderr, "failed to initialize EGL\n");
209                 destroy_gl();
210                 return -1;
211         }
212
213         if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
214                 fprintf(stderr, "no matching EGL config found\n");
215                 destroy_gl();
216                 return -1;
217         }
218
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);
222
223         if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
224                 fprintf(stderr, "failed to create window\n");
225                 destroy_gl();
226                 return -1;
227         }
228
229         if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
230                 fprintf(stderr, "failed to create OpenGL ES context\n");
231                 destroy_gl();
232                 return -1;
233         }
234         eglMakeCurrent(dpy, surf, surf, ctx);
235
236         eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
237         eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
238         return 0;
239 }
240
241 static void destroy_gl(void)
242 {
243         if(!dpy) return;
244
245         eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
246
247         if(ctx) {
248                 eglDestroyContext(dpy, ctx);
249                 ctx = 0;
250         }
251         if(surf) {
252                 eglDestroySurface(dpy, surf);
253                 surf = 0;
254         }
255
256         eglTerminate(dpy);
257         dpy = 0;
258 }
259
260 static unsigned long get_time_msec(void)
261 {
262         struct timespec ts;
263         static struct timespec ts0;
264
265         clock_gettime(CLOCK_MONOTONIC, &ts);
266         if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) {
267                 ts0 = ts;
268                 return 0;
269         }
270         return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000;
271 }
272
273 static void hide_navbar(struct android_app *state)
274 {
275         JNIEnv *env;
276         jclass cactivity, cwin, cview;
277         jobject win, view;
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;
281
282         (*state->activity->vm)->AttachCurrentThread(state->activity->vm, &env, 0);
283
284         cactivity = (*env)->FindClass(env, "android/app/NativeActivity");
285         get_window = (*env)->GetMethodID(env, cactivity, "getWindow", "()Landroid/view/Window;");
286
287         cwin = (*env)->FindClass(env, "android/view/Window");
288         get_decor_view = (*env)->GetMethodID(env, cwin, "getDecorView", "()Landroid/view/View;");
289
290         cview = (*env)->FindClass(env, "android/view/View");
291         set_system_ui_visibility = (*env)->GetMethodID(env, cview, "setSystemUiVisibility", "(I)V");
292
293         win = (*env)->CallObjectMethod(env, state->activity->clazz, get_window);
294         view = (*env)->CallObjectMethod(env, win, get_decor_view);
295
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");
299
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);
303
304         (*env)->CallVoidMethod(env, view, set_system_ui_visibility, flag_fs | flag_hidenav | flag_immersive);
305
306         (*state->activity->vm)->DetachCurrentThread(state->activity->vm);
307 }