istereo2

view src/android/amain.c @ 27:f0da8b2b61ec

removed iOS cpu restriction and bumped build number to 3 implemented android JNI calls to show/hide ads (untested)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Oct 2015 17:15:02 +0300
parents 9d53a4938ce8
children c6c45fa9658d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <EGL/egl.h>
5 #include <jni.h>
6 #include "android_native_app_glue.h"
7 #include <android/window.h>
8 #include <android/sensor.h>
9 #include "logger.h"
10 #include "istereo.h"
12 struct android_app *app;
14 static void handle_command(struct android_app *app, int32_t cmd);
15 static int handle_input(struct android_app *app, AInputEvent *ev);
16 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
17 static int init_gl(void);
18 static void destroy_gl(void);
20 static EGLDisplay dpy;
21 static EGLSurface surf;
22 static EGLContext ctx;
24 static int redisp_pending = 1; /* TODO stop busy-looping */
26 static int width, height;
27 static int init_done;
29 static JavaVM *jvm;
30 static JNIEnv *jni;
31 static jclass activity_class;
33 void android_main(struct android_app *app_ptr)
34 {
35 app_dummy();
36 app = app_ptr;
38 app->onAppCmd = handle_command;
39 app->onInputEvent = handle_input;
41 //ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0);
43 start_logger();
45 jvm = app->activity->vm;
46 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
47 fprintf(stderr, "failed to attach native thread to Java VM\n");
48 exit(1);
49 }
50 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
52 for(;;) {
53 int num_events;
54 struct android_poll_source *pollsrc;
56 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
57 if(pollsrc) {
58 pollsrc->process(app, pollsrc);
59 }
60 }
62 if(app->destroyRequested) {
63 return;
64 }
66 if(init_done && redisp_pending) {
67 redraw();
68 eglSwapBuffers(dpy, surf);
69 }
70 }
71 }
73 void set_mouse_pos(int x, int y)
74 {
75 }
77 void set_mouse_cursor(int enable)
78 {
79 }
81 /* TODO */
82 void ad_banner_show(void)
83 {
84 jmethodID method;
85 if(!jvm) return;
87 if(!(method = (*jni)->GetMethodID(jni, activity_class, "show_ad", "()V"))) {
88 fprintf(stderr, "failed to retrieve MainActivity.show_ad method\n");
89 return;
90 }
91 (*jni)->CallVoidMethod(jni, activity_class, method);
92 }
94 void ad_banner_hide(void)
95 {
96 jmethodID method;
97 if(!jvm) return;
99 if(!(method = (*jni)->GetMethodID(jni, activity_class, "hide_ad", "()V"))) {
100 fprintf(stderr, "failed to retrieve MainActivity.hide_ad method\n");
101 return;
102 }
103 (*jni)->CallVoidMethod(jni, activity_class, method);
104 }
106 static void handle_command(struct android_app *app, int32_t cmd)
107 {
108 switch(cmd) {
109 case APP_CMD_SAVE_STATE:
110 /* save the application state to be reloaded on restart if needed */
111 break;
113 case APP_CMD_INIT_WINDOW:
114 printf("APP_CMD_INIT_WINDOW\n");
115 if(init_gl() == -1) {
116 exit(1);
117 }
118 /* initialize the application */
119 if(init() == -1) {
120 exit(1); /* initialization failed, quit */
121 }
122 reshape(width, height);
123 init_done = 1;
124 break;
126 case APP_CMD_TERM_WINDOW:
127 /* cleanup */
128 printf("APP_CMD_TERM_WINDOW\n");
129 init_done = 0;
130 cleanup();
131 destroy_gl();
132 break;
134 case APP_CMD_GAINED_FOCUS:
135 printf("APP_CMD_GAINED_FOCUS\n");
136 /* app focused */
137 break;
139 case APP_CMD_LOST_FOCUS:
140 printf("APP_CMD_LOST_FOCUS\n");
141 /* app lost focus */
142 break;
144 case APP_CMD_WINDOW_RESIZED:
145 case APP_CMD_CONFIG_CHANGED:
146 {
147 int nx = ANativeWindow_getWidth(app->window);
148 int ny = ANativeWindow_getHeight(app->window);
149 if(nx != width || ny != height) {
150 printf("reshape(%d, %d)\n", nx, ny);
151 reshape(nx, ny);
152 width = nx;
153 height = ny;
154 }
155 }
156 break;
158 default:
159 break;
160 }
161 }
163 static int handle_input(struct android_app *app, AInputEvent *ev)
164 {
165 int evtype = AInputEvent_getType(ev);
167 switch(evtype) {
168 case AINPUT_EVENT_TYPE_MOTION:
169 return handle_touch_input(app, ev);
171 default:
172 break;
173 }
174 return 0;
175 }
177 #define MAX_TOUCH_IDS 32
179 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
180 {
181 int x, y, idx, touch_id;
182 unsigned int action;
183 static int prev_pos[MAX_TOUCH_IDS][2];
185 action = AMotionEvent_getAction(ev);
187 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
188 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
189 touch_id = AMotionEvent_getPointerId(ev, idx);
191 x = AMotionEvent_getX(ev, idx);
192 y = AMotionEvent_getY(ev, idx);
194 switch(action & AMOTION_EVENT_ACTION_MASK) {
195 case AMOTION_EVENT_ACTION_DOWN:
196 case AMOTION_EVENT_ACTION_POINTER_DOWN:
197 if(touch_id == 0) {
198 mouse_button(0, 1, x, y);
199 }
200 if(touch_id < MAX_TOUCH_IDS) {
201 prev_pos[touch_id][0] = x;
202 prev_pos[touch_id][1] = y;
203 }
204 break;
206 case AMOTION_EVENT_ACTION_UP:
207 case AMOTION_EVENT_ACTION_POINTER_UP:
208 if(touch_id == 0) {
209 mouse_button(0, 0, x, y);
210 }
211 if(touch_id < MAX_TOUCH_IDS) {
212 prev_pos[touch_id][0] = x;
213 prev_pos[touch_id][1] = y;
214 }
215 break;
217 case AMOTION_EVENT_ACTION_MOVE:
218 {
219 int i, pcount = AMotionEvent_getPointerCount(ev);
220 for(i=0; i<pcount; i++) {
221 int id = AMotionEvent_getPointerId(ev, i);
222 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
223 if(id == 0) {
224 mouse_motion(x, y);
225 }
226 prev_pos[id][0] = x;
227 prev_pos[id][1] = y;
228 }
229 }
230 }
231 break;
233 default:
234 break;
235 }
237 return 1;
238 }
241 static int init_gl(void)
242 {
243 static const int eglattr[] = {
244 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
245 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
246 EGL_RED_SIZE, 5,
247 EGL_GREEN_SIZE, 5,
248 EGL_BLUE_SIZE, 5,
249 EGL_DEPTH_SIZE, 16,
250 EGL_NONE
251 };
252 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
254 EGLConfig eglcfg;
255 int count, vis;
257 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
258 if(!dpy || !eglInitialize(dpy, 0, 0)) {
259 fprintf(stderr, "failed to initialize EGL\n");
260 destroy_gl();
261 return -1;
262 }
264 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
265 fprintf(stderr, "no matching EGL config found\n");
266 destroy_gl();
267 return -1;
268 }
270 /* configure the native window visual according to the chosen EGL config */
271 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
272 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
274 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
275 fprintf(stderr, "failed to create window\n");
276 destroy_gl();
277 return -1;
278 }
280 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
281 fprintf(stderr, "failed to create OpenGL ES context\n");
282 destroy_gl();
283 return -1;
284 }
285 eglMakeCurrent(dpy, surf, surf, ctx);
287 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
288 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
289 printf("initial reshape call: %dx%d\n", width, height);
290 reshape(width, height);
292 return 0;
293 }
295 static void destroy_gl(void)
296 {
297 if(!dpy) return;
299 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
301 if(ctx) {
302 eglDestroyContext(dpy, ctx);
303 ctx = 0;
304 }
305 if(surf) {
306 eglDestroySurface(dpy, surf);
307 surf = 0;
308 }
310 eglTerminate(dpy);
311 dpy = 0;
312 }