istereo2

annotate src/android/amain.c @ 35:643f4ab609a4

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