istereo2

view 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
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/sensor.h>
8 #include "logger.h"
9 #include "istereo.h"
11 struct android_app *app;
13 static void handle_command(struct android_app *app, int32_t cmd);
14 static int handle_input(struct android_app *app, AInputEvent *ev);
15 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
16 static int init_gl(void);
17 static void destroy_gl(void);
19 static EGLDisplay dpy;
20 static EGLSurface surf;
21 static EGLContext ctx;
23 static int redisp_pending = 1; /* TODO stop busy-looping */
25 static int width, height;
26 static int init_done;
28 void android_main(struct android_app *app_ptr)
29 {
30 app_dummy();
31 app = app_ptr;
33 app->onAppCmd = handle_command;
34 app->onInputEvent = handle_input;
36 start_logger();
38 for(;;) {
39 int num_events;
40 struct android_poll_source *pollsrc;
42 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
43 if(pollsrc) {
44 pollsrc->process(app, pollsrc);
45 }
46 }
48 if(app->destroyRequested) {
49 return;
50 }
52 if(init_done && redisp_pending) {
53 redraw();
54 eglSwapBuffers(dpy, surf);
55 }
56 }
57 }
59 void set_mouse_pos(int x, int y)
60 {
61 }
63 void set_mouse_cursor(int enable)
64 {
65 }
67 /* TODO */
68 void ad_banner_show(void)
69 {
70 }
72 void ad_banner_hide(void)
73 {
74 }
76 static void handle_command(struct android_app *app, int32_t cmd)
77 {
78 switch(cmd) {
79 case APP_CMD_SAVE_STATE:
80 /* save the application state to be reloaded on restart if needed */
81 break;
83 case APP_CMD_INIT_WINDOW:
84 if(init_gl() == -1) {
85 exit(1);
86 }
87 /* initialize the application */
88 if(init() == -1) {
89 exit(1); /* initialization failed, quit */
90 }
91 init_done = 1;
92 break;
94 case APP_CMD_TERM_WINDOW:
95 /* cleanup */
96 init_done = 0;
97 cleanup();
98 destroy_gl();
99 break;
101 case APP_CMD_GAINED_FOCUS:
102 /* app focused */
103 break;
105 case APP_CMD_LOST_FOCUS:
106 /* app lost focus */
107 break;
109 case APP_CMD_WINDOW_RESIZED:
110 case APP_CMD_CONFIG_CHANGED:
111 {
112 int nx = ANativeWindow_getWidth(app->window);
113 int ny = ANativeWindow_getHeight(app->window);
114 if(nx != width || ny != height) {
115 reshape(nx, ny);
116 width = nx;
117 height = ny;
118 }
119 }
120 break;
122 default:
123 break;
124 }
125 }
127 static int handle_input(struct android_app *app, AInputEvent *ev)
128 {
129 int evtype = AInputEvent_getType(ev);
131 switch(evtype) {
132 case AINPUT_EVENT_TYPE_MOTION:
133 return handle_touch_input(app, ev);
135 default:
136 break;
137 }
138 return 0;
139 }
141 #define MAX_TOUCH_IDS 32
143 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
144 {
145 int x, y, idx, touch_id;
146 unsigned int action;
147 static int prev_pos[MAX_TOUCH_IDS][2];
149 action = AMotionEvent_getAction(ev);
151 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
152 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
153 touch_id = AMotionEvent_getPointerId(ev, idx);
155 x = AMotionEvent_getX(ev, idx);
156 y = AMotionEvent_getY(ev, idx);
158 switch(action & AMOTION_EVENT_ACTION_MASK) {
159 case AMOTION_EVENT_ACTION_DOWN:
160 case AMOTION_EVENT_ACTION_POINTER_DOWN:
161 if(touch_id == 0) {
162 mouse_button(0, 1, x, y);
163 }
164 if(touch_id < MAX_TOUCH_IDS) {
165 prev_pos[touch_id][0] = x;
166 prev_pos[touch_id][1] = y;
167 }
168 break;
170 case AMOTION_EVENT_ACTION_UP:
171 case AMOTION_EVENT_ACTION_POINTER_UP:
172 if(touch_id == 0) {
173 mouse_button(0, 0, x, y);
174 }
175 if(touch_id < MAX_TOUCH_IDS) {
176 prev_pos[touch_id][0] = x;
177 prev_pos[touch_id][1] = y;
178 }
179 break;
181 case AMOTION_EVENT_ACTION_MOVE:
182 {
183 int i, pcount = AMotionEvent_getPointerCount(ev);
184 for(i=0; i<pcount; i++) {
185 int id = AMotionEvent_getPointerId(ev, i);
186 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
187 if(id == 0) {
188 mouse_motion(x, y);
189 }
190 prev_pos[id][0] = x;
191 prev_pos[id][1] = y;
192 }
193 }
194 }
195 break;
197 default:
198 break;
199 }
201 return 1;
202 }
205 static int init_gl(void)
206 {
207 static const int eglattr[] = {
208 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
209 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
210 EGL_RED_SIZE, 5,
211 EGL_GREEN_SIZE, 5,
212 EGL_BLUE_SIZE, 5,
213 EGL_DEPTH_SIZE, 16,
214 EGL_NONE
215 };
216 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
218 EGLConfig eglcfg;
219 int count, vis;
221 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
222 if(!dpy || !eglInitialize(dpy, 0, 0)) {
223 fprintf(stderr, "failed to initialize EGL\n");
224 destroy_gl();
225 return -1;
226 }
228 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
229 fprintf(stderr, "no matching EGL config found\n");
230 destroy_gl();
231 return -1;
232 }
234 /* configure the native window visual according to the chosen EGL config */
235 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
236 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
238 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
239 fprintf(stderr, "failed to create window\n");
240 destroy_gl();
241 return -1;
242 }
244 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
245 fprintf(stderr, "failed to create OpenGL ES context\n");
246 destroy_gl();
247 return -1;
248 }
249 eglMakeCurrent(dpy, surf, surf, ctx);
251 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
252 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
253 reshape(width, height);
255 return 0;
256 }
258 static void destroy_gl(void)
259 {
260 if(!dpy) return;
262 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
264 if(ctx) {
265 eglDestroyContext(dpy, ctx);
266 ctx = 0;
267 }
268 if(surf) {
269 eglDestroySurface(dpy, surf);
270 surf = 0;
271 }
273 eglTerminate(dpy);
274 dpy = 0;
275 }