minor fix on CMakeLists for checking MSVC_VERSION
[freeglut] / src / android / native_app_glue / android_native_app_glue.c
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <jni.h>
19
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/resource.h>
24
25 #include "android_native_app_glue.h"
26 #include <android/log.h>
27
28 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__))
29
30 static void free_saved_state(struct android_app* android_app) {
31     pthread_mutex_lock(&android_app->mutex);
32     if (android_app->savedState != NULL) {
33         free(android_app->savedState);
34         android_app->savedState = NULL;
35         android_app->savedStateSize = 0;
36     }
37     pthread_mutex_unlock(&android_app->mutex);
38 }
39
40 int8_t android_app_read_cmd(struct android_app* android_app) {
41     int8_t cmd;
42     if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
43         switch (cmd) {
44             case APP_CMD_SAVE_STATE:
45                 free_saved_state(android_app);
46                 break;
47         }
48         return cmd;
49     } else {
50         LOGI("No data on command pipe!");
51     }
52     return -1;
53 }
54
55 static void print_cur_config(struct android_app* android_app) {
56     char lang[2], country[2];
57     AConfiguration_getLanguage(android_app->config, lang);
58     AConfiguration_getCountry(android_app->config, country);
59
60     LOGI("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
61             "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
62             "modetype=%d modenight=%d",
63             AConfiguration_getMcc(android_app->config),
64             AConfiguration_getMnc(android_app->config),
65             lang[0], lang[1], country[0], country[1],
66             AConfiguration_getOrientation(android_app->config),
67             AConfiguration_getTouchscreen(android_app->config),
68             AConfiguration_getDensity(android_app->config),
69             AConfiguration_getKeyboard(android_app->config),
70             AConfiguration_getNavigation(android_app->config),
71             AConfiguration_getKeysHidden(android_app->config),
72             AConfiguration_getNavHidden(android_app->config),
73             AConfiguration_getSdkVersion(android_app->config),
74             AConfiguration_getScreenSize(android_app->config),
75             AConfiguration_getScreenLong(android_app->config),
76             AConfiguration_getUiModeType(android_app->config),
77             AConfiguration_getUiModeNight(android_app->config));
78 }
79
80 void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
81     switch (cmd) {
82         case APP_CMD_INPUT_CHANGED:
83             LOGI("APP_CMD_INPUT_CHANGED\n");
84             pthread_mutex_lock(&android_app->mutex);
85             if (android_app->inputQueue != NULL) {
86                 AInputQueue_detachLooper(android_app->inputQueue);
87             }
88             android_app->inputQueue = android_app->pendingInputQueue;
89             if (android_app->inputQueue != NULL) {
90                 LOGI("Attaching input queue to looper");
91                 AInputQueue_attachLooper(android_app->inputQueue,
92                         android_app->looper, LOOPER_ID_INPUT, NULL,
93                         &android_app->inputPollSource);
94             }
95             pthread_cond_broadcast(&android_app->cond);
96             pthread_mutex_unlock(&android_app->mutex);
97             break;
98
99         case APP_CMD_INIT_WINDOW:
100             LOGI("APP_CMD_INIT_WINDOW\n");
101             pthread_mutex_lock(&android_app->mutex);
102             android_app->window = android_app->pendingWindow;
103             pthread_cond_broadcast(&android_app->cond);
104             pthread_mutex_unlock(&android_app->mutex);
105             break;
106
107         case APP_CMD_TERM_WINDOW:
108             LOGI("APP_CMD_TERM_WINDOW\n");
109             pthread_cond_broadcast(&android_app->cond);
110             break;
111
112         case APP_CMD_RESUME:
113         case APP_CMD_START:
114         case APP_CMD_PAUSE:
115         case APP_CMD_STOP:
116             LOGI("activityState=%d\n", cmd);
117             pthread_mutex_lock(&android_app->mutex);
118             android_app->activityState = cmd;
119             pthread_cond_broadcast(&android_app->cond);
120             pthread_mutex_unlock(&android_app->mutex);
121             break;
122
123         case APP_CMD_CONFIG_CHANGED:
124             LOGI("APP_CMD_CONFIG_CHANGED\n");
125             AConfiguration_fromAssetManager(android_app->config,
126                     android_app->activity->assetManager);
127             print_cur_config(android_app);
128             break;
129
130         case APP_CMD_DESTROY:
131             LOGI("APP_CMD_DESTROY\n");
132             android_app->destroyRequested = 1;
133             break;
134     }
135 }
136
137 void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
138     switch (cmd) {
139         case APP_CMD_TERM_WINDOW:
140             LOGI("APP_CMD_TERM_WINDOW\n");
141             pthread_mutex_lock(&android_app->mutex);
142             android_app->window = NULL;
143             pthread_cond_broadcast(&android_app->cond);
144             pthread_mutex_unlock(&android_app->mutex);
145             break;
146
147         case APP_CMD_SAVE_STATE:
148             LOGI("APP_CMD_SAVE_STATE\n");
149             pthread_mutex_lock(&android_app->mutex);
150             android_app->stateSaved = 1;
151             pthread_cond_broadcast(&android_app->cond);
152             pthread_mutex_unlock(&android_app->mutex);
153             break;
154
155         case APP_CMD_RESUME:
156             free_saved_state(android_app);
157             break;
158     }
159 }
160
161 void app_dummy() {
162
163 }
164
165 static void android_app_destroy(struct android_app* android_app) {
166     LOGI("android_app_destroy!");
167     free_saved_state(android_app);
168     pthread_mutex_lock(&android_app->mutex);
169     if (android_app->inputQueue != NULL) {
170         AInputQueue_detachLooper(android_app->inputQueue);
171     }
172     AConfiguration_delete(android_app->config);
173     android_app->destroyed = 1;
174     pthread_cond_broadcast(&android_app->cond);
175     pthread_mutex_unlock(&android_app->mutex);
176     /* // Can't touch android_app object after this. */
177 }
178
179 static void process_input(struct android_app* app, struct android_poll_source* source) {
180     AInputEvent* event = NULL;
181     if (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
182         LOGI("New input event: type=%d\n", AInputEvent_getType(event));
183         if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
184             return;
185         }
186         {
187           int32_t handled = 0;
188           if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
189           AInputQueue_finishEvent(app->inputQueue, event, handled);
190         }
191     } else {
192         LOGI("Failure reading next input event: %s\n", strerror(errno));
193     }
194 }
195
196 static void process_cmd(struct android_app* app, struct android_poll_source* source) {
197     int8_t cmd = android_app_read_cmd(app);
198     android_app_pre_exec_cmd(app, cmd);
199     if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
200     android_app_post_exec_cmd(app, cmd);
201 }
202
203 static void* android_app_entry(void* param) {
204     struct android_app* android_app = (struct android_app*)param;
205     ALooper* looper;
206
207     android_app->config = AConfiguration_new();
208     AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
209
210     print_cur_config(android_app);
211
212     android_app->cmdPollSource.id = LOOPER_ID_MAIN;
213     android_app->cmdPollSource.app = android_app;
214     android_app->cmdPollSource.process = process_cmd;
215     android_app->inputPollSource.id = LOOPER_ID_INPUT;
216     android_app->inputPollSource.app = android_app;
217     android_app->inputPollSource.process = process_input;
218
219     looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
220     ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
221             &android_app->cmdPollSource);
222     android_app->looper = looper;
223
224     pthread_mutex_lock(&android_app->mutex);
225     android_app->running = 1;
226     pthread_cond_broadcast(&android_app->cond);
227     pthread_mutex_unlock(&android_app->mutex);
228
229     android_main(android_app);
230
231     android_app_destroy(android_app);
232     return NULL;
233 }
234
235 /* // -------------------------------------------------------------------- */
236 /* // Native activity interaction (called from main thread) */
237 /* // -------------------------------------------------------------------- */
238
239 static struct android_app* android_app_create(ANativeActivity* activity,
240         void* savedState, size_t savedStateSize) {
241     struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
242     int msgpipe[2];
243     pthread_attr_t attr; 
244     memset(android_app, 0, sizeof(struct android_app));
245     android_app->activity = activity;
246
247     pthread_mutex_init(&android_app->mutex, NULL);
248     pthread_cond_init(&android_app->cond, NULL);
249
250     if (savedState != NULL) {
251         android_app->savedState = malloc(savedStateSize);
252         android_app->savedStateSize = savedStateSize;
253         memcpy(android_app->savedState, savedState, savedStateSize);
254     }
255
256     if (pipe(msgpipe)) {
257         LOGI("could not create pipe: %s", strerror(errno));
258     }
259     android_app->msgread = msgpipe[0];
260     android_app->msgwrite = msgpipe[1];
261
262     pthread_attr_init(&attr);
263     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
264     pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
265
266     /* // Wait for thread to start. */
267     pthread_mutex_lock(&android_app->mutex);
268     while (!android_app->running) {
269         pthread_cond_wait(&android_app->cond, &android_app->mutex);
270     }
271     pthread_mutex_unlock(&android_app->mutex);
272
273     return android_app;
274 }
275
276 /* static  */void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
277     if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
278         LOGI("Failure writing android_app cmd: %s\n", strerror(errno));
279     }
280 }
281
282 static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
283     pthread_mutex_lock(&android_app->mutex);
284     android_app->pendingInputQueue = inputQueue;
285     android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
286     while (android_app->inputQueue != android_app->pendingInputQueue) {
287         pthread_cond_wait(&android_app->cond, &android_app->mutex);
288     }
289     pthread_mutex_unlock(&android_app->mutex);
290 }
291
292 static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
293     pthread_mutex_lock(&android_app->mutex);
294     if (android_app->pendingWindow != NULL) {
295         android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
296     }
297     android_app->pendingWindow = window;
298     if (window != NULL) {
299         android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
300     }
301     while (android_app->window != android_app->pendingWindow) {
302         pthread_cond_wait(&android_app->cond, &android_app->mutex);
303     }
304     pthread_mutex_unlock(&android_app->mutex);
305 }
306
307 static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
308     pthread_mutex_lock(&android_app->mutex);
309     android_app_write_cmd(android_app, cmd);
310     while (android_app->activityState != cmd) {
311         pthread_cond_wait(&android_app->cond, &android_app->mutex);
312     }
313     pthread_mutex_unlock(&android_app->mutex);
314 }
315
316 static void android_app_free(struct android_app* android_app) {
317     pthread_mutex_lock(&android_app->mutex);
318     android_app_write_cmd(android_app, APP_CMD_DESTROY);
319     while (!android_app->destroyed) {
320         pthread_cond_wait(&android_app->cond, &android_app->mutex);
321     }
322     pthread_mutex_unlock(&android_app->mutex);
323
324     close(android_app->msgread);
325     close(android_app->msgwrite);
326     pthread_cond_destroy(&android_app->cond);
327     pthread_mutex_destroy(&android_app->mutex);
328     free(android_app);
329 }
330
331 static void onDestroy(ANativeActivity* activity) {
332   LOGI("Destroy: %p\n", (void*)activity);
333     android_app_free((struct android_app*)activity->instance);
334 }
335
336 static void onStart(ANativeActivity* activity) {
337     LOGI("Start: %p\n", (void*)activity);
338     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
339 }
340
341 static void onResume(ANativeActivity* activity) {
342     LOGI("Resume: %p\n", (void*)activity);
343     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
344 }
345
346 static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
347     struct android_app* android_app = (struct android_app*)activity->instance;
348     void* savedState = NULL;
349
350     LOGI("SaveInstanceState: %p\n", (void*)activity);
351     pthread_mutex_lock(&android_app->mutex);
352     android_app->stateSaved = 0;
353     android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
354     while (!android_app->stateSaved) {
355         pthread_cond_wait(&android_app->cond, &android_app->mutex);
356     }
357
358     if (android_app->savedState != NULL) {
359         savedState = android_app->savedState;
360         *outLen = android_app->savedStateSize;
361         android_app->savedState = NULL;
362         android_app->savedStateSize = 0;
363     }
364
365     pthread_mutex_unlock(&android_app->mutex);
366
367     return savedState;
368 }
369
370 static void onPause(ANativeActivity* activity) {
371     LOGI("Pause: %p\n", (void*)activity);
372     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
373 }
374
375 static void onStop(ANativeActivity* activity) {
376     LOGI("Stop: %p\n", (void*)activity);
377     android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
378 }
379
380 static void onConfigurationChanged(ANativeActivity* activity) {
381     struct android_app* android_app = (struct android_app*)activity->instance;
382     LOGI("ConfigurationChanged: %p\n", (void*)activity);
383     android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
384 }
385
386 static void onLowMemory(ANativeActivity* activity) {
387     struct android_app* android_app = (struct android_app*)activity->instance;
388     LOGI("LowMemory: %p\n", (void*)activity);
389     android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
390 }
391
392 static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
393   LOGI("WindowFocusChanged: %p -- %d\n", (void*)activity, focused);
394     android_app_write_cmd((struct android_app*)activity->instance,
395             focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
396 }
397
398 static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
399     LOGI("NativeWindowCreated: %p -- %p\n", (void*)activity, (void*)window);
400     android_app_set_window((struct android_app*)activity->instance, window);
401 }
402
403 static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
404     LOGI("NativeWindowDestroyed: %p -- %p\n", (void*)activity, (void*)window);
405     android_app_set_window((struct android_app*)activity->instance, NULL);
406 }
407
408 static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
409     LOGI("InputQueueCreated: %p -- %p\n", (void*)activity, (void*)queue);
410     android_app_set_input((struct android_app*)activity->instance, queue);
411 }
412
413 static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
414     LOGI("InputQueueDestroyed: %p -- %p\n", (void*)activity, (void*)queue);
415     android_app_set_input((struct android_app*)activity->instance, NULL);
416 }
417
418 void ANativeActivity_onCreate(ANativeActivity* activity,
419         void* savedState, size_t savedStateSize) {
420     LOGI("Creating: %p\n", (void*)activity);
421     activity->callbacks->onDestroy = onDestroy;
422     activity->callbacks->onStart = onStart;
423     activity->callbacks->onResume = onResume;
424     activity->callbacks->onSaveInstanceState = onSaveInstanceState;
425     activity->callbacks->onPause = onPause;
426     activity->callbacks->onStop = onStop;
427     activity->callbacks->onConfigurationChanged = onConfigurationChanged;
428     activity->callbacks->onLowMemory = onLowMemory;
429     activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
430     activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
431     activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
432     activity->callbacks->onInputQueueCreated = onInputQueueCreated;
433     activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
434
435     activity->instance = android_app_create(activity, savedState, savedStateSize);
436 }