3dphotoshoot

view src/android/native_glue.c @ 1:7eb73ce46dd0

fucking jni man ... wtf
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 May 2015 23:46:56 +0300
parents src/android/android_native_app_glue.c@a4bf2687e406
children cf5964db7ff3
line source
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 */
18 #include <jni.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/resource.h>
25 #include "native_glue.h"
26 #include <android/log.h>
28 #define ACTIVITY_NAME "com/mutantstargoat/photoshoot3d/MainActivity"
30 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, APP_NAME, __VA_ARGS__))
31 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__))
33 /* For debug builds, always enable the debug traces in this library */
34 #ifndef NDEBUG
35 # define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, APP_NAME, __VA_ARGS__))
36 #else
37 # define LOGV(...) ((void)0)
38 #endif
40 static void free_saved_state(struct android_app* android_app) {
41 pthread_mutex_lock(&android_app->mutex);
42 if (android_app->savedState != NULL) {
43 free(android_app->savedState);
44 android_app->savedState = NULL;
45 android_app->savedStateSize = 0;
46 }
47 pthread_mutex_unlock(&android_app->mutex);
48 }
50 int8_t android_app_read_cmd(struct android_app* android_app) {
51 int8_t cmd;
52 if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
53 switch (cmd) {
54 case APP_CMD_SAVE_STATE:
55 free_saved_state(android_app);
56 break;
57 }
58 return cmd;
59 } else {
60 LOGE("No data on command pipe!");
61 }
62 return -1;
63 }
65 static void print_cur_config(struct android_app* android_app) {
66 char lang[2], country[2];
67 AConfiguration_getLanguage(android_app->config, lang);
68 AConfiguration_getCountry(android_app->config, country);
70 LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
71 "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
72 "modetype=%d modenight=%d",
73 AConfiguration_getMcc(android_app->config),
74 AConfiguration_getMnc(android_app->config),
75 lang[0], lang[1], country[0], country[1],
76 AConfiguration_getOrientation(android_app->config),
77 AConfiguration_getTouchscreen(android_app->config),
78 AConfiguration_getDensity(android_app->config),
79 AConfiguration_getKeyboard(android_app->config),
80 AConfiguration_getNavigation(android_app->config),
81 AConfiguration_getKeysHidden(android_app->config),
82 AConfiguration_getNavHidden(android_app->config),
83 AConfiguration_getSdkVersion(android_app->config),
84 AConfiguration_getScreenSize(android_app->config),
85 AConfiguration_getScreenLong(android_app->config),
86 AConfiguration_getUiModeType(android_app->config),
87 AConfiguration_getUiModeNight(android_app->config));
88 }
90 void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
91 switch (cmd) {
92 case APP_CMD_INPUT_CHANGED:
93 LOGV("APP_CMD_INPUT_CHANGED\n");
94 pthread_mutex_lock(&android_app->mutex);
95 if (android_app->inputQueue != NULL) {
96 AInputQueue_detachLooper(android_app->inputQueue);
97 }
98 android_app->inputQueue = android_app->pendingInputQueue;
99 if (android_app->inputQueue != NULL) {
100 LOGV("Attaching input queue to looper");
101 AInputQueue_attachLooper(android_app->inputQueue,
102 android_app->looper, LOOPER_ID_INPUT, NULL,
103 &android_app->inputPollSource);
104 }
105 pthread_cond_broadcast(&android_app->cond);
106 pthread_mutex_unlock(&android_app->mutex);
107 break;
109 case APP_CMD_INIT_WINDOW:
110 LOGV("APP_CMD_INIT_WINDOW\n");
111 pthread_mutex_lock(&android_app->mutex);
112 android_app->window = android_app->pendingWindow;
113 pthread_cond_broadcast(&android_app->cond);
114 pthread_mutex_unlock(&android_app->mutex);
115 break;
117 case APP_CMD_TERM_WINDOW:
118 LOGV("APP_CMD_TERM_WINDOW\n");
119 pthread_cond_broadcast(&android_app->cond);
120 break;
122 case APP_CMD_RESUME:
123 case APP_CMD_START:
124 case APP_CMD_PAUSE:
125 case APP_CMD_STOP:
126 LOGV("activityState=%d\n", cmd);
127 pthread_mutex_lock(&android_app->mutex);
128 android_app->activityState = cmd;
129 pthread_cond_broadcast(&android_app->cond);
130 pthread_mutex_unlock(&android_app->mutex);
131 break;
133 case APP_CMD_CONFIG_CHANGED:
134 LOGV("APP_CMD_CONFIG_CHANGED\n");
135 AConfiguration_fromAssetManager(android_app->config,
136 android_app->activity->assetManager);
137 print_cur_config(android_app);
138 break;
140 case APP_CMD_DESTROY:
141 LOGV("APP_CMD_DESTROY\n");
142 android_app->destroyRequested = 1;
143 break;
144 }
145 }
147 void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
148 switch (cmd) {
149 case APP_CMD_TERM_WINDOW:
150 LOGV("APP_CMD_TERM_WINDOW\n");
151 pthread_mutex_lock(&android_app->mutex);
152 android_app->window = NULL;
153 pthread_cond_broadcast(&android_app->cond);
154 pthread_mutex_unlock(&android_app->mutex);
155 break;
157 case APP_CMD_SAVE_STATE:
158 LOGV("APP_CMD_SAVE_STATE\n");
159 pthread_mutex_lock(&android_app->mutex);
160 android_app->stateSaved = 1;
161 pthread_cond_broadcast(&android_app->cond);
162 pthread_mutex_unlock(&android_app->mutex);
163 break;
165 case APP_CMD_RESUME:
166 free_saved_state(android_app);
167 break;
168 }
169 }
171 void app_dummy() {
173 }
175 static void android_app_destroy(struct android_app* android_app) {
176 LOGV("android_app_destroy!");
177 free_saved_state(android_app);
178 pthread_mutex_lock(&android_app->mutex);
179 if (android_app->inputQueue != NULL) {
180 AInputQueue_detachLooper(android_app->inputQueue);
181 }
182 AConfiguration_delete(android_app->config);
183 android_app->destroyed = 1;
184 pthread_cond_broadcast(&android_app->cond);
185 pthread_mutex_unlock(&android_app->mutex);
186 // Can't touch android_app object after this.
187 }
189 static void process_input(struct android_app* app, struct android_poll_source* source) {
190 AInputEvent* event = NULL;
191 while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
192 LOGV("New input event: type=%d\n", AInputEvent_getType(event));
193 if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
194 continue;
195 }
196 int32_t handled = 0;
197 if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
198 AInputQueue_finishEvent(app->inputQueue, event, handled);
199 }
200 }
202 static void process_cmd(struct android_app* app, struct android_poll_source* source) {
203 int8_t cmd = android_app_read_cmd(app);
204 android_app_pre_exec_cmd(app, cmd);
205 if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
206 android_app_post_exec_cmd(app, cmd);
207 }
209 static void* android_app_entry(void* param) {
210 struct android_app* android_app = (struct android_app*)param;
212 android_app->config = AConfiguration_new();
213 AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
215 print_cur_config(android_app);
217 android_app->cmdPollSource.id = LOOPER_ID_MAIN;
218 android_app->cmdPollSource.app = android_app;
219 android_app->cmdPollSource.process = process_cmd;
220 android_app->inputPollSource.id = LOOPER_ID_INPUT;
221 android_app->inputPollSource.app = android_app;
222 android_app->inputPollSource.process = process_input;
224 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
225 ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
226 &android_app->cmdPollSource);
227 android_app->looper = looper;
229 pthread_mutex_lock(&android_app->mutex);
230 android_app->running = 1;
231 pthread_cond_broadcast(&android_app->cond);
232 pthread_mutex_unlock(&android_app->mutex);
234 android_main(android_app);
236 android_app_destroy(android_app);
237 return NULL;
238 }
240 // --------------------------------------------------------------------
241 // Native activity interaction (called from main thread)
242 // --------------------------------------------------------------------
244 static struct android_app* android_app_create(ANativeActivity* activity,
245 void* savedState, size_t savedStateSize) {
246 JNIEnv *jni = activity->env;
247 struct android_app* android_app = malloc(sizeof(struct android_app));
248 memset(android_app, 0, sizeof(struct android_app));
249 android_app->activity = activity;
251 {
252 jclass foo = (*jni)->FindClass(jni, "java/lang/String");
253 if(!foo) {
254 LOGE("String class not found!!!!!!\n");
255 } else {
256 LOGE("Found String class!!!!!!\n");
257 }
258 }
260 if(!(android_app->activity_class = (*jni)->FindClass(jni, ACTIVITY_NAME))) {
261 LOGE("could not find activity: " ACTIVITY_NAME);
262 return 0;
263 }
265 pthread_mutex_init(&android_app->mutex, NULL);
266 pthread_cond_init(&android_app->cond, NULL);
268 if (savedState != NULL) {
269 android_app->savedState = malloc(savedStateSize);
270 android_app->savedStateSize = savedStateSize;
271 memcpy(android_app->savedState, savedState, savedStateSize);
272 }
274 int msgpipe[2];
275 if (pipe(msgpipe)) {
276 LOGE("could not create pipe: %s", strerror(errno));
277 return NULL;
278 }
279 android_app->msgread = msgpipe[0];
280 android_app->msgwrite = msgpipe[1];
282 pthread_attr_t attr;
283 pthread_attr_init(&attr);
284 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
285 pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
287 // Wait for thread to start.
288 pthread_mutex_lock(&android_app->mutex);
289 while (!android_app->running) {
290 pthread_cond_wait(&android_app->cond, &android_app->mutex);
291 }
292 pthread_mutex_unlock(&android_app->mutex);
294 return android_app;
295 }
297 static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
298 if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
299 LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
300 }
301 }
303 static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
304 pthread_mutex_lock(&android_app->mutex);
305 android_app->pendingInputQueue = inputQueue;
306 android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
307 while (android_app->inputQueue != android_app->pendingInputQueue) {
308 pthread_cond_wait(&android_app->cond, &android_app->mutex);
309 }
310 pthread_mutex_unlock(&android_app->mutex);
311 }
313 static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
314 pthread_mutex_lock(&android_app->mutex);
315 if (android_app->pendingWindow != NULL) {
316 android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
317 }
318 android_app->pendingWindow = window;
319 if (window != NULL) {
320 android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
321 }
322 while (android_app->window != android_app->pendingWindow) {
323 pthread_cond_wait(&android_app->cond, &android_app->mutex);
324 }
325 pthread_mutex_unlock(&android_app->mutex);
326 }
328 static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
329 pthread_mutex_lock(&android_app->mutex);
330 android_app_write_cmd(android_app, cmd);
331 while (android_app->activityState != cmd) {
332 pthread_cond_wait(&android_app->cond, &android_app->mutex);
333 }
334 pthread_mutex_unlock(&android_app->mutex);
335 }
337 static void android_app_free(struct android_app* android_app) {
338 pthread_mutex_lock(&android_app->mutex);
339 android_app_write_cmd(android_app, APP_CMD_DESTROY);
340 while (!android_app->destroyed) {
341 pthread_cond_wait(&android_app->cond, &android_app->mutex);
342 }
343 pthread_mutex_unlock(&android_app->mutex);
345 close(android_app->msgread);
346 close(android_app->msgwrite);
347 pthread_cond_destroy(&android_app->cond);
348 pthread_mutex_destroy(&android_app->mutex);
349 free(android_app);
350 }
352 static void onDestroy(ANativeActivity* activity) {
353 LOGV("Destroy: %p\n", activity);
354 android_app_free((struct android_app*)activity->instance);
355 }
357 static void onStart(ANativeActivity* activity) {
358 LOGV("Start: %p\n", activity);
359 android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
360 }
362 static void onResume(ANativeActivity* activity) {
363 LOGV("Resume: %p\n", activity);
364 android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
365 }
367 static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
368 struct android_app* android_app = (struct android_app*)activity->instance;
369 void* savedState = NULL;
371 LOGV("SaveInstanceState: %p\n", activity);
372 pthread_mutex_lock(&android_app->mutex);
373 android_app->stateSaved = 0;
374 android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
375 while (!android_app->stateSaved) {
376 pthread_cond_wait(&android_app->cond, &android_app->mutex);
377 }
379 if (android_app->savedState != NULL) {
380 savedState = android_app->savedState;
381 *outLen = android_app->savedStateSize;
382 android_app->savedState = NULL;
383 android_app->savedStateSize = 0;
384 }
386 pthread_mutex_unlock(&android_app->mutex);
388 return savedState;
389 }
391 static void onPause(ANativeActivity* activity) {
392 LOGV("Pause: %p\n", activity);
393 android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
394 }
396 static void onStop(ANativeActivity* activity) {
397 LOGV("Stop: %p\n", activity);
398 android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
399 }
401 static void onConfigurationChanged(ANativeActivity* activity) {
402 struct android_app* android_app = (struct android_app*)activity->instance;
403 LOGV("ConfigurationChanged: %p\n", activity);
404 android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
405 }
407 static void onLowMemory(ANativeActivity* activity) {
408 struct android_app* android_app = (struct android_app*)activity->instance;
409 LOGV("LowMemory: %p\n", activity);
410 android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
411 }
413 static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
414 LOGV("WindowFocusChanged: %p -- %d\n", activity, focused);
415 android_app_write_cmd((struct android_app*)activity->instance,
416 focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
417 }
419 static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
420 LOGV("NativeWindowCreated: %p -- %p\n", activity, window);
421 android_app_set_window((struct android_app*)activity->instance, window);
422 }
424 static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
425 LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window);
426 android_app_set_window((struct android_app*)activity->instance, NULL);
427 }
429 static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
430 LOGV("InputQueueCreated: %p -- %p\n", activity, queue);
431 android_app_set_input((struct android_app*)activity->instance, queue);
432 }
434 static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
435 LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue);
436 android_app_set_input((struct android_app*)activity->instance, NULL);
437 }
439 void ANativeActivity_onCreate(ANativeActivity* activity,
440 void* savedState, size_t savedStateSize) {
441 LOGV("Creating: %p\n", activity);
442 activity->callbacks->onDestroy = onDestroy;
443 activity->callbacks->onStart = onStart;
444 activity->callbacks->onResume = onResume;
445 activity->callbacks->onSaveInstanceState = onSaveInstanceState;
446 activity->callbacks->onPause = onPause;
447 activity->callbacks->onStop = onStop;
448 activity->callbacks->onConfigurationChanged = onConfigurationChanged;
449 activity->callbacks->onLowMemory = onLowMemory;
450 activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
451 activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
452 activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
453 activity->callbacks->onInputQueueCreated = onInputQueueCreated;
454 activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
456 activity->instance = android_app_create(activity, savedState, savedStateSize);
457 }