istereo2

annotate src/android/amain.c @ 23:7d795dade0bc

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