istereo2

annotate src/android/amain.c @ 29:c6c45fa9658d

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