istereo2

annotate src/android/amain.c @ 24:9d53a4938ce8

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