istereo2

annotate src/android/amain.c @ 27:f0da8b2b61ec

removed iOS cpu restriction and bumped build number to 3 implemented android JNI calls to show/hide ads (untested)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Oct 2015 17:15:02 +0300
parents 9d53a4938ce8
children c6c45fa9658d
rev   line source
nuclear@23 1 #include <stdio.h>
nuclear@23 2 #include <stdlib.h>
nuclear@23 3 #include <assert.h>
nuclear@23 4 #include <EGL/egl.h>
nuclear@27 5 #include <jni.h>
nuclear@23 6 #include "android_native_app_glue.h"
nuclear@24 7 #include <android/window.h>
nuclear@23 8 #include <android/sensor.h>
nuclear@23 9 #include "logger.h"
nuclear@23 10 #include "istereo.h"
nuclear@23 11
nuclear@23 12 struct android_app *app;
nuclear@23 13
nuclear@23 14 static void handle_command(struct android_app *app, int32_t cmd);
nuclear@23 15 static int handle_input(struct android_app *app, AInputEvent *ev);
nuclear@23 16 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
nuclear@23 17 static int init_gl(void);
nuclear@23 18 static void destroy_gl(void);
nuclear@23 19
nuclear@23 20 static EGLDisplay dpy;
nuclear@23 21 static EGLSurface surf;
nuclear@23 22 static EGLContext ctx;
nuclear@23 23
nuclear@23 24 static int redisp_pending = 1; /* TODO stop busy-looping */
nuclear@23 25
nuclear@23 26 static int width, height;
nuclear@23 27 static int init_done;
nuclear@23 28
nuclear@27 29 static JavaVM *jvm;
nuclear@27 30 static JNIEnv *jni;
nuclear@27 31 static jclass activity_class;
nuclear@27 32
nuclear@23 33 void android_main(struct android_app *app_ptr)
nuclear@23 34 {
nuclear@23 35 app_dummy();
nuclear@23 36 app = app_ptr;
nuclear@23 37
nuclear@23 38 app->onAppCmd = handle_command;
nuclear@23 39 app->onInputEvent = handle_input;
nuclear@23 40
nuclear@24 41 //ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0);
nuclear@24 42
nuclear@23 43 start_logger();
nuclear@23 44
nuclear@27 45 jvm = app->activity->vm;
nuclear@27 46 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
nuclear@27 47 fprintf(stderr, "failed to attach native thread to Java VM\n");
nuclear@27 48 exit(1);
nuclear@27 49 }
nuclear@27 50 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
nuclear@27 51
nuclear@23 52 for(;;) {
nuclear@23 53 int num_events;
nuclear@23 54 struct android_poll_source *pollsrc;
nuclear@23 55
nuclear@23 56 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
nuclear@23 57 if(pollsrc) {
nuclear@23 58 pollsrc->process(app, pollsrc);
nuclear@23 59 }
nuclear@23 60 }
nuclear@23 61
nuclear@23 62 if(app->destroyRequested) {
nuclear@23 63 return;
nuclear@23 64 }
nuclear@23 65
nuclear@23 66 if(init_done && redisp_pending) {
nuclear@23 67 redraw();
nuclear@23 68 eglSwapBuffers(dpy, surf);
nuclear@23 69 }
nuclear@23 70 }
nuclear@23 71 }
nuclear@23 72
nuclear@23 73 void set_mouse_pos(int x, int y)
nuclear@23 74 {
nuclear@23 75 }
nuclear@23 76
nuclear@23 77 void set_mouse_cursor(int enable)
nuclear@23 78 {
nuclear@23 79 }
nuclear@23 80
nuclear@23 81 /* TODO */
nuclear@23 82 void ad_banner_show(void)
nuclear@23 83 {
nuclear@27 84 jmethodID method;
nuclear@27 85 if(!jvm) return;
nuclear@27 86
nuclear@27 87 if(!(method = (*jni)->GetMethodID(jni, activity_class, "show_ad", "()V"))) {
nuclear@27 88 fprintf(stderr, "failed to retrieve MainActivity.show_ad method\n");
nuclear@27 89 return;
nuclear@27 90 }
nuclear@27 91 (*jni)->CallVoidMethod(jni, activity_class, method);
nuclear@23 92 }
nuclear@23 93
nuclear@23 94 void ad_banner_hide(void)
nuclear@23 95 {
nuclear@27 96 jmethodID method;
nuclear@27 97 if(!jvm) return;
nuclear@27 98
nuclear@27 99 if(!(method = (*jni)->GetMethodID(jni, activity_class, "hide_ad", "()V"))) {
nuclear@27 100 fprintf(stderr, "failed to retrieve MainActivity.hide_ad method\n");
nuclear@27 101 return;
nuclear@27 102 }
nuclear@27 103 (*jni)->CallVoidMethod(jni, activity_class, method);
nuclear@23 104 }
nuclear@23 105
nuclear@23 106 static void handle_command(struct android_app *app, int32_t cmd)
nuclear@23 107 {
nuclear@23 108 switch(cmd) {
nuclear@23 109 case APP_CMD_SAVE_STATE:
nuclear@23 110 /* save the application state to be reloaded on restart if needed */
nuclear@23 111 break;
nuclear@23 112
nuclear@23 113 case APP_CMD_INIT_WINDOW:
nuclear@24 114 printf("APP_CMD_INIT_WINDOW\n");
nuclear@23 115 if(init_gl() == -1) {
nuclear@23 116 exit(1);
nuclear@23 117 }
nuclear@23 118 /* initialize the application */
nuclear@23 119 if(init() == -1) {
nuclear@23 120 exit(1); /* initialization failed, quit */
nuclear@23 121 }
nuclear@24 122 reshape(width, height);
nuclear@23 123 init_done = 1;
nuclear@23 124 break;
nuclear@23 125
nuclear@23 126 case APP_CMD_TERM_WINDOW:
nuclear@23 127 /* cleanup */
nuclear@24 128 printf("APP_CMD_TERM_WINDOW\n");
nuclear@23 129 init_done = 0;
nuclear@23 130 cleanup();
nuclear@23 131 destroy_gl();
nuclear@23 132 break;
nuclear@23 133
nuclear@23 134 case APP_CMD_GAINED_FOCUS:
nuclear@24 135 printf("APP_CMD_GAINED_FOCUS\n");
nuclear@23 136 /* app focused */
nuclear@23 137 break;
nuclear@23 138
nuclear@23 139 case APP_CMD_LOST_FOCUS:
nuclear@24 140 printf("APP_CMD_LOST_FOCUS\n");
nuclear@23 141 /* app lost focus */
nuclear@23 142 break;
nuclear@23 143
nuclear@23 144 case APP_CMD_WINDOW_RESIZED:
nuclear@23 145 case APP_CMD_CONFIG_CHANGED:
nuclear@23 146 {
nuclear@23 147 int nx = ANativeWindow_getWidth(app->window);
nuclear@23 148 int ny = ANativeWindow_getHeight(app->window);
nuclear@23 149 if(nx != width || ny != height) {
nuclear@24 150 printf("reshape(%d, %d)\n", nx, ny);
nuclear@23 151 reshape(nx, ny);
nuclear@23 152 width = nx;
nuclear@23 153 height = ny;
nuclear@23 154 }
nuclear@23 155 }
nuclear@23 156 break;
nuclear@23 157
nuclear@23 158 default:
nuclear@23 159 break;
nuclear@23 160 }
nuclear@23 161 }
nuclear@23 162
nuclear@23 163 static int handle_input(struct android_app *app, AInputEvent *ev)
nuclear@23 164 {
nuclear@23 165 int evtype = AInputEvent_getType(ev);
nuclear@23 166
nuclear@23 167 switch(evtype) {
nuclear@23 168 case AINPUT_EVENT_TYPE_MOTION:
nuclear@23 169 return handle_touch_input(app, ev);
nuclear@23 170
nuclear@23 171 default:
nuclear@23 172 break;
nuclear@23 173 }
nuclear@23 174 return 0;
nuclear@23 175 }
nuclear@23 176
nuclear@23 177 #define MAX_TOUCH_IDS 32
nuclear@23 178
nuclear@23 179 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
nuclear@23 180 {
nuclear@23 181 int x, y, idx, touch_id;
nuclear@23 182 unsigned int action;
nuclear@23 183 static int prev_pos[MAX_TOUCH_IDS][2];
nuclear@23 184
nuclear@23 185 action = AMotionEvent_getAction(ev);
nuclear@23 186
nuclear@23 187 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
nuclear@23 188 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
nuclear@23 189 touch_id = AMotionEvent_getPointerId(ev, idx);
nuclear@23 190
nuclear@23 191 x = AMotionEvent_getX(ev, idx);
nuclear@23 192 y = AMotionEvent_getY(ev, idx);
nuclear@23 193
nuclear@23 194 switch(action & AMOTION_EVENT_ACTION_MASK) {
nuclear@23 195 case AMOTION_EVENT_ACTION_DOWN:
nuclear@23 196 case AMOTION_EVENT_ACTION_POINTER_DOWN:
nuclear@23 197 if(touch_id == 0) {
nuclear@23 198 mouse_button(0, 1, x, y);
nuclear@23 199 }
nuclear@23 200 if(touch_id < MAX_TOUCH_IDS) {
nuclear@23 201 prev_pos[touch_id][0] = x;
nuclear@23 202 prev_pos[touch_id][1] = y;
nuclear@23 203 }
nuclear@23 204 break;
nuclear@23 205
nuclear@23 206 case AMOTION_EVENT_ACTION_UP:
nuclear@23 207 case AMOTION_EVENT_ACTION_POINTER_UP:
nuclear@23 208 if(touch_id == 0) {
nuclear@23 209 mouse_button(0, 0, x, y);
nuclear@23 210 }
nuclear@23 211 if(touch_id < MAX_TOUCH_IDS) {
nuclear@23 212 prev_pos[touch_id][0] = x;
nuclear@23 213 prev_pos[touch_id][1] = y;
nuclear@23 214 }
nuclear@23 215 break;
nuclear@23 216
nuclear@23 217 case AMOTION_EVENT_ACTION_MOVE:
nuclear@23 218 {
nuclear@23 219 int i, pcount = AMotionEvent_getPointerCount(ev);
nuclear@23 220 for(i=0; i<pcount; i++) {
nuclear@23 221 int id = AMotionEvent_getPointerId(ev, i);
nuclear@23 222 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
nuclear@23 223 if(id == 0) {
nuclear@23 224 mouse_motion(x, y);
nuclear@23 225 }
nuclear@23 226 prev_pos[id][0] = x;
nuclear@23 227 prev_pos[id][1] = y;
nuclear@23 228 }
nuclear@23 229 }
nuclear@23 230 }
nuclear@23 231 break;
nuclear@23 232
nuclear@23 233 default:
nuclear@23 234 break;
nuclear@23 235 }
nuclear@23 236
nuclear@23 237 return 1;
nuclear@23 238 }
nuclear@23 239
nuclear@23 240
nuclear@23 241 static int init_gl(void)
nuclear@23 242 {
nuclear@23 243 static const int eglattr[] = {
nuclear@23 244 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
nuclear@23 245 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
nuclear@23 246 EGL_RED_SIZE, 5,
nuclear@23 247 EGL_GREEN_SIZE, 5,
nuclear@23 248 EGL_BLUE_SIZE, 5,
nuclear@23 249 EGL_DEPTH_SIZE, 16,
nuclear@23 250 EGL_NONE
nuclear@23 251 };
nuclear@23 252 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
nuclear@23 253
nuclear@23 254 EGLConfig eglcfg;
nuclear@23 255 int count, vis;
nuclear@23 256
nuclear@23 257 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
nuclear@23 258 if(!dpy || !eglInitialize(dpy, 0, 0)) {
nuclear@23 259 fprintf(stderr, "failed to initialize EGL\n");
nuclear@23 260 destroy_gl();
nuclear@23 261 return -1;
nuclear@23 262 }
nuclear@23 263
nuclear@23 264 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
nuclear@23 265 fprintf(stderr, "no matching EGL config found\n");
nuclear@23 266 destroy_gl();
nuclear@23 267 return -1;
nuclear@23 268 }
nuclear@23 269
nuclear@23 270 /* configure the native window visual according to the chosen EGL config */
nuclear@23 271 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
nuclear@23 272 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
nuclear@23 273
nuclear@23 274 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
nuclear@23 275 fprintf(stderr, "failed to create window\n");
nuclear@23 276 destroy_gl();
nuclear@23 277 return -1;
nuclear@23 278 }
nuclear@23 279
nuclear@23 280 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
nuclear@23 281 fprintf(stderr, "failed to create OpenGL ES context\n");
nuclear@23 282 destroy_gl();
nuclear@23 283 return -1;
nuclear@23 284 }
nuclear@23 285 eglMakeCurrent(dpy, surf, surf, ctx);
nuclear@23 286
nuclear@23 287 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
nuclear@23 288 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
nuclear@24 289 printf("initial reshape call: %dx%d\n", width, height);
nuclear@23 290 reshape(width, height);
nuclear@23 291
nuclear@23 292 return 0;
nuclear@23 293 }
nuclear@23 294
nuclear@23 295 static void destroy_gl(void)
nuclear@23 296 {
nuclear@23 297 if(!dpy) return;
nuclear@23 298
nuclear@23 299 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
nuclear@23 300
nuclear@23 301 if(ctx) {
nuclear@23 302 eglDestroyContext(dpy, ctx);
nuclear@23 303 ctx = 0;
nuclear@23 304 }
nuclear@23 305 if(surf) {
nuclear@23 306 eglDestroySurface(dpy, surf);
nuclear@23 307 surf = 0;
nuclear@23 308 }
nuclear@23 309
nuclear@23 310 eglTerminate(dpy);
nuclear@23 311 dpy = 0;
nuclear@23 312 }