istereo2

view src/android/amain.c @ 35:643f4ab609a4

added readme and license
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Oct 2015 05:45:35 +0200
parents 900651a2f401
children
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;
32 static jobject activity;
34 void android_main(struct android_app *app_ptr)
35 {
36 app_dummy();
37 app = app_ptr;
39 app->onAppCmd = handle_command;
40 app->onInputEvent = handle_input;
42 //ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0);
44 start_logger();
46 jvm = app->activity->vm;
47 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
48 fprintf(stderr, "failed to attach native thread to Java VM\n");
49 exit(1);
50 }
51 activity = app->activity->clazz;
52 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
54 for(;;) {
55 int num_events;
56 struct android_poll_source *pollsrc;
58 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
59 if(pollsrc) {
60 pollsrc->process(app, pollsrc);
61 }
62 }
64 if(app->destroyRequested) {
65 return;
66 }
68 if(init_done && redisp_pending) {
69 redraw();
70 eglSwapBuffers(dpy, surf);
71 }
72 }
73 }
75 void set_mouse_pos(int x, int y)
76 {
77 }
79 void set_mouse_cursor(int enable)
80 {
81 }
83 void ad_banner_show(void)
84 {
85 jmethodID method;
86 if(!jvm) return;
88 printf("ad_banner_show called\n");
90 if(!(method = (*jni)->GetMethodID(jni, activity_class, "show_ad", "()V"))) {
91 fprintf(stderr, "failed to retrieve MainActivity.show_ad method\n");
92 return;
93 }
94 (*jni)->CallVoidMethod(jni, activity, method);
95 }
97 void ad_banner_hide(void)
98 {
99 jmethodID method;
100 if(!jvm) return;
102 printf("ad_banner_hide called\n");
104 if(!(method = (*jni)->GetMethodID(jni, activity_class, "hide_ad", "()V"))) {
105 fprintf(stderr, "failed to retrieve MainActivity.hide_ad method\n");
106 return;
107 }
108 (*jni)->CallVoidMethod(jni, activity, method);
109 }
111 static void handle_command(struct android_app *app, int32_t cmd)
112 {
113 switch(cmd) {
114 case APP_CMD_SAVE_STATE:
115 /* save the application state to be reloaded on restart if needed */
116 break;
118 case APP_CMD_INIT_WINDOW:
119 printf("APP_CMD_INIT_WINDOW\n");
120 //ad_banner_show();
122 if(init_gl() == -1) {
123 exit(1);
124 }
125 /* initialize the application */
126 if(init() == -1) {
127 exit(1); /* initialization failed, quit */
128 }
129 reshape(width, height);
130 init_done = 1;
131 break;
133 case APP_CMD_TERM_WINDOW:
134 /* cleanup */
135 printf("APP_CMD_TERM_WINDOW\n");
136 init_done = 0;
137 cleanup();
138 destroy_gl();
139 break;
141 case APP_CMD_GAINED_FOCUS:
142 printf("APP_CMD_GAINED_FOCUS\n");
143 /* app focused */
144 break;
146 case APP_CMD_LOST_FOCUS:
147 printf("APP_CMD_LOST_FOCUS\n");
148 /* app lost focus */
149 break;
151 case APP_CMD_WINDOW_RESIZED:
152 case APP_CMD_CONFIG_CHANGED:
153 {
154 int nx = ANativeWindow_getWidth(app->window);
155 int ny = ANativeWindow_getHeight(app->window);
156 if(nx != width || ny != height) {
157 printf("reshape(%d, %d)\n", nx, ny);
158 reshape(nx, ny);
159 width = nx;
160 height = ny;
161 }
162 }
163 break;
165 default:
166 break;
167 }
168 }
170 static int handle_input(struct android_app *app, AInputEvent *ev)
171 {
172 int evtype = AInputEvent_getType(ev);
174 switch(evtype) {
175 case AINPUT_EVENT_TYPE_MOTION:
176 return handle_touch_input(app, ev);
178 default:
179 break;
180 }
181 return 0;
182 }
184 #define MAX_TOUCH_IDS 32
186 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
187 {
188 int x, y, idx, touch_id;
189 unsigned int action;
190 static int prev_pos[MAX_TOUCH_IDS][2];
192 action = AMotionEvent_getAction(ev);
194 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
195 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
196 touch_id = AMotionEvent_getPointerId(ev, idx);
198 x = AMotionEvent_getX(ev, idx);
199 y = AMotionEvent_getY(ev, idx);
201 switch(action & AMOTION_EVENT_ACTION_MASK) {
202 case AMOTION_EVENT_ACTION_DOWN:
203 case AMOTION_EVENT_ACTION_POINTER_DOWN:
204 if(touch_id == 0) {
205 mouse_button(0, 1, x, y);
206 }
207 if(touch_id < MAX_TOUCH_IDS) {
208 prev_pos[touch_id][0] = x;
209 prev_pos[touch_id][1] = y;
210 }
211 break;
213 case AMOTION_EVENT_ACTION_UP:
214 case AMOTION_EVENT_ACTION_POINTER_UP:
215 if(touch_id == 0) {
216 mouse_button(0, 0, x, y);
217 }
218 if(touch_id < MAX_TOUCH_IDS) {
219 prev_pos[touch_id][0] = x;
220 prev_pos[touch_id][1] = y;
221 }
222 break;
224 case AMOTION_EVENT_ACTION_MOVE:
225 {
226 int i, pcount = AMotionEvent_getPointerCount(ev);
227 for(i=0; i<pcount; i++) {
228 int id = AMotionEvent_getPointerId(ev, i);
229 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
230 if(id == 0) {
231 mouse_motion(x, y);
232 }
233 prev_pos[id][0] = x;
234 prev_pos[id][1] = y;
235 }
236 }
237 }
238 break;
240 default:
241 break;
242 }
244 return 1;
245 }
248 static int init_gl(void)
249 {
250 static const int eglattr[] = {
251 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
252 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
253 EGL_RED_SIZE, 5,
254 EGL_GREEN_SIZE, 5,
255 EGL_BLUE_SIZE, 5,
256 EGL_DEPTH_SIZE, 16,
257 EGL_NONE
258 };
259 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
261 EGLConfig eglcfg;
262 int count, vis;
264 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
265 if(!dpy || !eglInitialize(dpy, 0, 0)) {
266 fprintf(stderr, "failed to initialize EGL\n");
267 destroy_gl();
268 return -1;
269 }
271 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
272 fprintf(stderr, "no matching EGL config found\n");
273 destroy_gl();
274 return -1;
275 }
277 /* configure the native window visual according to the chosen EGL config */
278 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
279 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
281 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
282 fprintf(stderr, "failed to create window\n");
283 destroy_gl();
284 return -1;
285 }
287 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
288 fprintf(stderr, "failed to create OpenGL ES context\n");
289 destroy_gl();
290 return -1;
291 }
292 eglMakeCurrent(dpy, surf, surf, ctx);
294 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
295 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
296 printf("initial reshape call: %dx%d\n", width, height);
297 reshape(width, height);
299 return 0;
300 }
302 static void destroy_gl(void)
303 {
304 if(!dpy) return;
306 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
308 if(ctx) {
309 eglDestroyContext(dpy, ctx);
310 ctx = 0;
311 }
312 if(surf) {
313 eglDestroySurface(dpy, surf);
314 surf = 0;
315 }
317 eglTerminate(dpy);
318 dpy = 0;
319 }