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