3dphotoshoot
diff 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 |
line diff
1.1 --- a/src/android/amain.c Sat Jun 13 05:35:43 2015 +0300 1.2 +++ b/src/android/amain.c Tue Jun 16 06:17:59 2015 +0300 1.3 @@ -5,11 +5,15 @@ 1.4 #include <jni.h> 1.5 #include "amain.h" 1.6 #include "native_glue.h" 1.7 +#include <android/sensor.h> 1.8 +#include "sensordef.h" 1.9 #include "logger.h" 1.10 #include "game.h" 1.11 #include "camera.h" 1.12 #include "timer.h" 1.13 1.14 +#define EVID_SENS_ACCEL (LOOPER_ID_USER + 0) 1.15 +#define EVID_SENS_ROT (LOOPER_ID_USER + 1) 1.16 1.17 static void handle_command(struct android_app *app, int32_t cmd); 1.18 static int handle_input(struct android_app *app, AInputEvent *ev); 1.19 @@ -21,6 +25,8 @@ 1.20 static EGLSurface surf; 1.21 static EGLContext ctx; 1.22 1.23 +static int redisp_pending = 1; /* TODO stop busy-looping */ 1.24 + 1.25 static int width, height; 1.26 static int init_done; 1.27 1.28 @@ -28,7 +34,11 @@ 1.29 static JNIEnv *jni; 1.30 static jclass activity_class; 1.31 1.32 - 1.33 +static ASensorManager *sensorman; 1.34 +static ASensor const *sens_accel; 1.35 +static ASensor const *sens_rot; 1.36 +static ASensorEventQueue *sens_evq_accel; 1.37 +static ASensorEventQueue *sens_evq_rot; 1.38 1.39 void android_main(struct android_app *app_ptr) 1.40 { 1.41 @@ -47,13 +57,96 @@ 1.42 } 1.43 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz); 1.44 1.45 + { 1.46 + int i, num_sensors; 1.47 + ASensorList sensors; 1.48 + 1.49 + sensorman = ASensorManager_getInstance(); 1.50 + num_sensors = ASensorManager_getSensorList(sensorman, &sensors); 1.51 + printf("found %d sensors:\n", num_sensors); 1.52 + 1.53 + for(i=0; i<num_sensors; i++) { 1.54 + ASensor const *sens = sensors[i]; 1.55 + int type = ASensor_getType(sens); 1.56 + float res = ASensor_getResolution(sens); 1.57 + int mindel = ASensor_getMinDelay(sens); 1.58 + 1.59 + printf(" %d) [%d:%s] %s - %s (res: %g, min-delay: %d)\n", i, 1.60 + type, sensor_typestr(type), ASensor_getName(sens), 1.61 + ASensor_getVendor(sens), res, mindel); 1.62 + } 1.63 + 1.64 + // try to get a linear acceleration sensor 1.65 + printf("Requesting \"linear acceleration\" sensor ...\n"); 1.66 + if(!(sens_accel = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_LINEAR_ACCELERATION))) { 1.67 + fprintf(stderr, "No linear acceleration sensor found, aborting!\n"); 1.68 + return; 1.69 + } 1.70 + 1.71 + // try to get a game rotation vector sensor 1.72 + printf("Requesting \"game rotation vector\" sensor ...\n"); 1.73 + if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_GAME_ROTATION_VECTOR))) { 1.74 + printf("Requesting \"rotation vector\" sensor ...\n"); 1.75 + if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_ROTATION_VECTOR))) { 1.76 + fprintf(stderr, "No rotation sensor found, aborting!\n"); 1.77 + return; 1.78 + } 1.79 + } 1.80 + 1.81 + if(!(sens_evq_accel = ASensorManager_createEventQueue(sensorman, app->looper, 1.82 + EVID_SENS_ACCEL, 0, 0))) { 1.83 + fprintf(stderr, "failed to create acceleration sensor event queue\n"); 1.84 + return; 1.85 + } 1.86 + 1.87 + if(!(sens_evq_rot = ASensorManager_createEventQueue(sensorman, app->looper, 1.88 + EVID_SENS_ROT, 0, 0))) { 1.89 + fprintf(stderr, "failed to create rotation sensor event queue\n"); 1.90 + return; 1.91 + } 1.92 + } 1.93 + 1.94 + 1.95 for(;;) { 1.96 - int num_events; 1.97 + int num_events, evid; 1.98 struct android_poll_source *pollsrc; 1.99 + ASensorEvent sens_ev; 1.100 1.101 - while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) { 1.102 - if(pollsrc) { 1.103 - pollsrc->process(app, pollsrc); 1.104 + while((evid = ALooper_pollAll(redisp_pending ? 0 : -1, 0, &num_events, (void**)&pollsrc)) >= 0) { 1.105 + 1.106 + switch(evid) { 1.107 + case EVID_SENS_ACCEL: 1.108 + if(sens_accel) { 1.109 + float accel[3]; 1.110 + while(ASensorEventQueue_getEvents(sens_evq_accel, &sens_ev, 1) > 0) { 1.111 + accel[0] = sens_ev.acceleration.x; 1.112 + accel[1] = sens_ev.acceleration.y; 1.113 + accel[2] = sens_ev.acceleration.z; 1.114 + } 1.115 + 1.116 + // TODO: integrate over time 1.117 + game_6dof_translation(accel[0], accel[1], accel[2]); 1.118 + } 1.119 + break; 1.120 + 1.121 + case EVID_SENS_ROT: 1.122 + if(sens_rot) { 1.123 + float qrot[4]; 1.124 + while(ASensorEventQueue_getEvents(sens_evq_rot, &sens_ev, 1) > 0) { 1.125 + int i; 1.126 + for(i=0; i<4; i++) { 1.127 + qrot[i] = sens_ev.data[i]; 1.128 + } 1.129 + } 1.130 + 1.131 + game_6dof_rotation(qrot[0], qrot[1], qrot[2], qrot[3]); 1.132 + } 1.133 + break; 1.134 + 1.135 + default: 1.136 + if(pollsrc) { 1.137 + pollsrc->process(app, pollsrc); 1.138 + } 1.139 } 1.140 } 1.141 1.142 @@ -62,7 +155,7 @@ 1.143 return; 1.144 } 1.145 1.146 - if(init_done) { 1.147 + if(init_done && redisp_pending) { 1.148 game_display(get_time_msec()); 1.149 eglSwapBuffers(dpy, surf); 1.150 } 1.151 @@ -115,11 +208,27 @@ 1.152 1.153 case APP_CMD_GAINED_FOCUS: 1.154 /* app focused */ 1.155 + printf("Command: APP_CMD_GAINED_FOCUS\n"); 1.156 + if(sens_accel) { 1.157 + ASensorEventQueue_enableSensor(sens_evq_accel, sens_accel); 1.158 + ASensorEventQueue_setEventRate(sens_evq_accel, sens_accel, 1000000 / 60); 1.159 + } 1.160 + if(sens_rot) { 1.161 + ASensorEventQueue_enableSensor(sens_evq_rot, sens_rot); 1.162 + ASensorEventQueue_setEventRate(sens_evq_rot, sens_rot, 1000000 / 60); 1.163 + } 1.164 //cam_start_video(); 1.165 break; 1.166 1.167 case APP_CMD_LOST_FOCUS: 1.168 /* app lost focus */ 1.169 + printf("Command: APP_CMD_LOST_FOCUS\n"); 1.170 + if(sens_accel) { 1.171 + ASensorEventQueue_disableSensor(sens_evq_accel, sens_accel); 1.172 + } 1.173 + if(sens_rot) { 1.174 + ASensorEventQueue_disableSensor(sens_evq_rot, sens_rot); 1.175 + } 1.176 //cam_stop_video(); 1.177 break; 1.178