3dphotoshoot

annotate src/android/amain.c @ 24:2712c5da2e00

getting sensor input (hack)
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 16 Jun 2015 06:17:59 +0300
parents a94af102872f
children ac80210d5fbe
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@5 3 #include <assert.h>
nuclear@0 4 #include <EGL/egl.h>
nuclear@1 5 #include <jni.h>
nuclear@16 6 #include "amain.h"
nuclear@1 7 #include "native_glue.h"
nuclear@24 8 #include <android/sensor.h>
nuclear@24 9 #include "sensordef.h"
nuclear@0 10 #include "logger.h"
nuclear@0 11 #include "game.h"
nuclear@6 12 #include "camera.h"
nuclear@0 13 #include "timer.h"
nuclear@0 14
nuclear@24 15 #define EVID_SENS_ACCEL (LOOPER_ID_USER + 0)
nuclear@24 16 #define EVID_SENS_ROT (LOOPER_ID_USER + 1)
nuclear@0 17
nuclear@0 18 static void handle_command(struct android_app *app, int32_t cmd);
nuclear@0 19 static int handle_input(struct android_app *app, AInputEvent *ev);
nuclear@0 20 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
nuclear@0 21 static int init_gl(void);
nuclear@0 22 static void destroy_gl(void);
nuclear@0 23
nuclear@0 24 static EGLDisplay dpy;
nuclear@0 25 static EGLSurface surf;
nuclear@0 26 static EGLContext ctx;
nuclear@0 27
nuclear@24 28 static int redisp_pending = 1; /* TODO stop busy-looping */
nuclear@24 29
nuclear@23 30 static int width, height;
nuclear@0 31 static int init_done;
nuclear@0 32
nuclear@1 33 static JavaVM *jvm;
nuclear@1 34 static JNIEnv *jni;
nuclear@1 35 static jclass activity_class;
nuclear@1 36
nuclear@24 37 static ASensorManager *sensorman;
nuclear@24 38 static ASensor const *sens_accel;
nuclear@24 39 static ASensor const *sens_rot;
nuclear@24 40 static ASensorEventQueue *sens_evq_accel;
nuclear@24 41 static ASensorEventQueue *sens_evq_rot;
nuclear@1 42
nuclear@0 43 void android_main(struct android_app *app_ptr)
nuclear@0 44 {
nuclear@0 45 app_dummy();
nuclear@0 46 app = app_ptr;
nuclear@0 47
nuclear@0 48 app->onAppCmd = handle_command;
nuclear@0 49 app->onInputEvent = handle_input;
nuclear@0 50
nuclear@0 51 start_logger();
nuclear@0 52
nuclear@1 53 jvm = app->activity->vm;
nuclear@1 54 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
nuclear@1 55 fprintf(stderr, "failed to attach native thread to Java VM\n");
nuclear@1 56 exit(1);
nuclear@1 57 }
nuclear@2 58 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
nuclear@1 59
nuclear@24 60 {
nuclear@24 61 int i, num_sensors;
nuclear@24 62 ASensorList sensors;
nuclear@24 63
nuclear@24 64 sensorman = ASensorManager_getInstance();
nuclear@24 65 num_sensors = ASensorManager_getSensorList(sensorman, &sensors);
nuclear@24 66 printf("found %d sensors:\n", num_sensors);
nuclear@24 67
nuclear@24 68 for(i=0; i<num_sensors; i++) {
nuclear@24 69 ASensor const *sens = sensors[i];
nuclear@24 70 int type = ASensor_getType(sens);
nuclear@24 71 float res = ASensor_getResolution(sens);
nuclear@24 72 int mindel = ASensor_getMinDelay(sens);
nuclear@24 73
nuclear@24 74 printf(" %d) [%d:%s] %s - %s (res: %g, min-delay: %d)\n", i,
nuclear@24 75 type, sensor_typestr(type), ASensor_getName(sens),
nuclear@24 76 ASensor_getVendor(sens), res, mindel);
nuclear@24 77 }
nuclear@24 78
nuclear@24 79 // try to get a linear acceleration sensor
nuclear@24 80 printf("Requesting \"linear acceleration\" sensor ...\n");
nuclear@24 81 if(!(sens_accel = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_LINEAR_ACCELERATION))) {
nuclear@24 82 fprintf(stderr, "No linear acceleration sensor found, aborting!\n");
nuclear@24 83 return;
nuclear@24 84 }
nuclear@24 85
nuclear@24 86 // try to get a game rotation vector sensor
nuclear@24 87 printf("Requesting \"game rotation vector\" sensor ...\n");
nuclear@24 88 if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_GAME_ROTATION_VECTOR))) {
nuclear@24 89 printf("Requesting \"rotation vector\" sensor ...\n");
nuclear@24 90 if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_ROTATION_VECTOR))) {
nuclear@24 91 fprintf(stderr, "No rotation sensor found, aborting!\n");
nuclear@24 92 return;
nuclear@24 93 }
nuclear@24 94 }
nuclear@24 95
nuclear@24 96 if(!(sens_evq_accel = ASensorManager_createEventQueue(sensorman, app->looper,
nuclear@24 97 EVID_SENS_ACCEL, 0, 0))) {
nuclear@24 98 fprintf(stderr, "failed to create acceleration sensor event queue\n");
nuclear@24 99 return;
nuclear@24 100 }
nuclear@24 101
nuclear@24 102 if(!(sens_evq_rot = ASensorManager_createEventQueue(sensorman, app->looper,
nuclear@24 103 EVID_SENS_ROT, 0, 0))) {
nuclear@24 104 fprintf(stderr, "failed to create rotation sensor event queue\n");
nuclear@24 105 return;
nuclear@24 106 }
nuclear@24 107 }
nuclear@24 108
nuclear@24 109
nuclear@0 110 for(;;) {
nuclear@24 111 int num_events, evid;
nuclear@0 112 struct android_poll_source *pollsrc;
nuclear@24 113 ASensorEvent sens_ev;
nuclear@0 114
nuclear@24 115 while((evid = ALooper_pollAll(redisp_pending ? 0 : -1, 0, &num_events, (void**)&pollsrc)) >= 0) {
nuclear@24 116
nuclear@24 117 switch(evid) {
nuclear@24 118 case EVID_SENS_ACCEL:
nuclear@24 119 if(sens_accel) {
nuclear@24 120 float accel[3];
nuclear@24 121 while(ASensorEventQueue_getEvents(sens_evq_accel, &sens_ev, 1) > 0) {
nuclear@24 122 accel[0] = sens_ev.acceleration.x;
nuclear@24 123 accel[1] = sens_ev.acceleration.y;
nuclear@24 124 accel[2] = sens_ev.acceleration.z;
nuclear@24 125 }
nuclear@24 126
nuclear@24 127 // TODO: integrate over time
nuclear@24 128 game_6dof_translation(accel[0], accel[1], accel[2]);
nuclear@24 129 }
nuclear@24 130 break;
nuclear@24 131
nuclear@24 132 case EVID_SENS_ROT:
nuclear@24 133 if(sens_rot) {
nuclear@24 134 float qrot[4];
nuclear@24 135 while(ASensorEventQueue_getEvents(sens_evq_rot, &sens_ev, 1) > 0) {
nuclear@24 136 int i;
nuclear@24 137 for(i=0; i<4; i++) {
nuclear@24 138 qrot[i] = sens_ev.data[i];
nuclear@24 139 }
nuclear@24 140 }
nuclear@24 141
nuclear@24 142 game_6dof_rotation(qrot[0], qrot[1], qrot[2], qrot[3]);
nuclear@24 143 }
nuclear@24 144 break;
nuclear@24 145
nuclear@24 146 default:
nuclear@24 147 if(pollsrc) {
nuclear@24 148 pollsrc->process(app, pollsrc);
nuclear@24 149 }
nuclear@0 150 }
nuclear@0 151 }
nuclear@0 152
nuclear@0 153 if(app->destroyRequested) {
nuclear@1 154 (*jvm)->DetachCurrentThread(jvm);
nuclear@0 155 return;
nuclear@0 156 }
nuclear@0 157
nuclear@24 158 if(init_done && redisp_pending) {
nuclear@0 159 game_display(get_time_msec());
nuclear@0 160 eglSwapBuffers(dpy, surf);
nuclear@0 161 }
nuclear@0 162 }
nuclear@0 163 }
nuclear@0 164
nuclear@0 165 void set_mouse_pos(int x, int y)
nuclear@0 166 {
nuclear@0 167 }
nuclear@0 168
nuclear@0 169 void set_mouse_cursor(int enable)
nuclear@0 170 {
nuclear@0 171 }
nuclear@0 172
nuclear@0 173 static void handle_command(struct android_app *app, int32_t cmd)
nuclear@0 174 {
nuclear@6 175 struct cam_android_platform_data cam_data;
nuclear@6 176
nuclear@0 177 switch(cmd) {
nuclear@0 178 case APP_CMD_SAVE_STATE:
nuclear@0 179 /* save the application state to be reloaded on restart if needed */
nuclear@0 180 break;
nuclear@0 181
nuclear@0 182 case APP_CMD_INIT_WINDOW:
nuclear@0 183 if(init_gl() == -1) {
nuclear@0 184 exit(1);
nuclear@0 185 }
nuclear@5 186
nuclear@6 187 cam_data.vm = jvm;
nuclear@6 188 cam_data.jni = jni;
nuclear@6 189 cam_data.activity_class = activity_class;
nuclear@6 190
nuclear@6 191 if(cam_init(&cam_data) == -1) {
nuclear@6 192 exit(1);
nuclear@6 193 }
nuclear@5 194
nuclear@0 195 /* initialize the application */
nuclear@0 196 if(game_init() == -1) {
nuclear@0 197 exit(1); /* initialization failed, quit */
nuclear@0 198 }
nuclear@0 199 init_done = 1;
nuclear@0 200 break;
nuclear@0 201
nuclear@0 202 case APP_CMD_TERM_WINDOW:
nuclear@0 203 /* cleanup */
nuclear@0 204 init_done = 0;
nuclear@0 205 game_shutdown();
nuclear@0 206 destroy_gl();
nuclear@0 207 break;
nuclear@0 208
nuclear@0 209 case APP_CMD_GAINED_FOCUS:
nuclear@0 210 /* app focused */
nuclear@24 211 printf("Command: APP_CMD_GAINED_FOCUS\n");
nuclear@24 212 if(sens_accel) {
nuclear@24 213 ASensorEventQueue_enableSensor(sens_evq_accel, sens_accel);
nuclear@24 214 ASensorEventQueue_setEventRate(sens_evq_accel, sens_accel, 1000000 / 60);
nuclear@24 215 }
nuclear@24 216 if(sens_rot) {
nuclear@24 217 ASensorEventQueue_enableSensor(sens_evq_rot, sens_rot);
nuclear@24 218 ASensorEventQueue_setEventRate(sens_evq_rot, sens_rot, 1000000 / 60);
nuclear@24 219 }
nuclear@8 220 //cam_start_video();
nuclear@0 221 break;
nuclear@0 222
nuclear@0 223 case APP_CMD_LOST_FOCUS:
nuclear@0 224 /* app lost focus */
nuclear@24 225 printf("Command: APP_CMD_LOST_FOCUS\n");
nuclear@24 226 if(sens_accel) {
nuclear@24 227 ASensorEventQueue_disableSensor(sens_evq_accel, sens_accel);
nuclear@24 228 }
nuclear@24 229 if(sens_rot) {
nuclear@24 230 ASensorEventQueue_disableSensor(sens_evq_rot, sens_rot);
nuclear@24 231 }
nuclear@8 232 //cam_stop_video();
nuclear@0 233 break;
nuclear@0 234
nuclear@0 235 case APP_CMD_WINDOW_RESIZED:
nuclear@0 236 case APP_CMD_CONFIG_CHANGED:
nuclear@0 237 {
nuclear@0 238 int nx = ANativeWindow_getWidth(app->window);
nuclear@0 239 int ny = ANativeWindow_getHeight(app->window);
nuclear@23 240 if(nx != width || ny != height) {
nuclear@0 241 game_reshape(nx, ny);
nuclear@23 242 width = nx;
nuclear@23 243 height = ny;
nuclear@0 244 }
nuclear@0 245 }
nuclear@0 246 break;
nuclear@0 247
nuclear@0 248 default:
nuclear@0 249 break;
nuclear@0 250 }
nuclear@0 251 }
nuclear@0 252
nuclear@0 253 static int handle_input(struct android_app *app, AInputEvent *ev)
nuclear@0 254 {
nuclear@0 255 int evtype = AInputEvent_getType(ev);
nuclear@0 256
nuclear@0 257 switch(evtype) {
nuclear@0 258 case AINPUT_EVENT_TYPE_MOTION:
nuclear@0 259 return handle_touch_input(app, ev);
nuclear@0 260
nuclear@0 261 default:
nuclear@0 262 break;
nuclear@0 263 }
nuclear@0 264 return 0;
nuclear@0 265 }
nuclear@0 266
nuclear@0 267 #define MAX_TOUCH_IDS 32
nuclear@0 268
nuclear@0 269 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
nuclear@0 270 {
nuclear@0 271 int x, y, idx, touch_id;
nuclear@0 272 unsigned int action;
nuclear@0 273 static int prev_pos[MAX_TOUCH_IDS][2];
nuclear@0 274
nuclear@0 275 action = AMotionEvent_getAction(ev);
nuclear@0 276
nuclear@0 277 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
nuclear@0 278 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
nuclear@0 279 touch_id = AMotionEvent_getPointerId(ev, idx);
nuclear@0 280
nuclear@0 281 x = AMotionEvent_getX(ev, idx);
nuclear@0 282 y = AMotionEvent_getY(ev, idx);
nuclear@0 283
nuclear@0 284 switch(action & AMOTION_EVENT_ACTION_MASK) {
nuclear@0 285 case AMOTION_EVENT_ACTION_DOWN:
nuclear@0 286 case AMOTION_EVENT_ACTION_POINTER_DOWN:
nuclear@0 287 game_mouse_button(touch_id, 0, 1, x, y);
nuclear@0 288 if(touch_id < MAX_TOUCH_IDS) {
nuclear@0 289 prev_pos[touch_id][0] = x;
nuclear@0 290 prev_pos[touch_id][1] = y;
nuclear@0 291 }
nuclear@0 292 break;
nuclear@0 293
nuclear@0 294 case AMOTION_EVENT_ACTION_UP:
nuclear@0 295 case AMOTION_EVENT_ACTION_POINTER_UP:
nuclear@0 296 game_mouse_button(touch_id, 0, 0, x, y);
nuclear@0 297 if(touch_id < MAX_TOUCH_IDS) {
nuclear@0 298 prev_pos[touch_id][0] = x;
nuclear@0 299 prev_pos[touch_id][1] = y;
nuclear@0 300 }
nuclear@0 301 break;
nuclear@0 302
nuclear@0 303 case AMOTION_EVENT_ACTION_MOVE:
nuclear@0 304 {
nuclear@0 305 int i, pcount = AMotionEvent_getPointerCount(ev);
nuclear@0 306 for(i=0; i<pcount; i++) {
nuclear@0 307 int id = AMotionEvent_getPointerId(ev, i);
nuclear@0 308 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
nuclear@0 309 game_mouse_motion(id, x, y);
nuclear@0 310 prev_pos[id][0] = x;
nuclear@0 311 prev_pos[id][1] = y;
nuclear@0 312 }
nuclear@0 313 }
nuclear@0 314 }
nuclear@0 315 break;
nuclear@0 316
nuclear@0 317 default:
nuclear@0 318 break;
nuclear@0 319 }
nuclear@0 320
nuclear@0 321 return 1;
nuclear@0 322 }
nuclear@0 323
nuclear@0 324
nuclear@0 325 static int init_gl(void)
nuclear@0 326 {
nuclear@0 327 static const int eglattr[] = {
nuclear@0 328 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
nuclear@10 329 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
nuclear@0 330 EGL_RED_SIZE, 5,
nuclear@0 331 EGL_GREEN_SIZE, 5,
nuclear@0 332 EGL_BLUE_SIZE, 5,
nuclear@0 333 EGL_DEPTH_SIZE, 16,
nuclear@0 334 EGL_NONE
nuclear@0 335 };
nuclear@10 336 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
nuclear@0 337
nuclear@0 338 EGLConfig eglcfg;
nuclear@0 339 int count, vis;
nuclear@0 340
nuclear@0 341 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
nuclear@0 342 if(!dpy || !eglInitialize(dpy, 0, 0)) {
nuclear@0 343 fprintf(stderr, "failed to initialize EGL\n");
nuclear@0 344 destroy_gl();
nuclear@0 345 return -1;
nuclear@0 346 }
nuclear@0 347
nuclear@0 348 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
nuclear@0 349 fprintf(stderr, "no matching EGL config found\n");
nuclear@0 350 destroy_gl();
nuclear@0 351 return -1;
nuclear@0 352 }
nuclear@0 353
nuclear@0 354 /* configure the native window visual according to the chosen EGL config */
nuclear@0 355 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
nuclear@0 356 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
nuclear@0 357
nuclear@0 358 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
nuclear@0 359 fprintf(stderr, "failed to create window\n");
nuclear@0 360 destroy_gl();
nuclear@0 361 return -1;
nuclear@0 362 }
nuclear@0 363
nuclear@0 364 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
nuclear@0 365 fprintf(stderr, "failed to create OpenGL ES context\n");
nuclear@0 366 destroy_gl();
nuclear@0 367 return -1;
nuclear@0 368 }
nuclear@0 369 eglMakeCurrent(dpy, surf, surf, ctx);
nuclear@0 370
nuclear@23 371 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
nuclear@23 372 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
nuclear@23 373 game_reshape(width, height);
nuclear@0 374
nuclear@0 375 return 0;
nuclear@0 376 }
nuclear@0 377
nuclear@0 378 static void destroy_gl(void)
nuclear@0 379 {
nuclear@0 380 if(!dpy) return;
nuclear@0 381
nuclear@0 382 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
nuclear@0 383
nuclear@0 384 if(ctx) {
nuclear@0 385 eglDestroyContext(dpy, ctx);
nuclear@0 386 ctx = 0;
nuclear@0 387 }
nuclear@0 388 if(surf) {
nuclear@0 389 eglDestroySurface(dpy, surf);
nuclear@0 390 surf = 0;
nuclear@0 391 }
nuclear@0 392
nuclear@0 393 eglTerminate(dpy);
nuclear@0 394 dpy = 0;
nuclear@0 395 }