android_native_app_glue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #include <jni.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/resource.h>
  22. #include <unistd.h>
  23. #include "android_native_app_glue.h"
  24. #include <android/log.h>
  25. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__))
  26. #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__))
  27. /* For debug builds, always enable the debug traces in this library */
  28. #ifndef NDEBUG
  29. #define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__))
  30. #else
  31. #define LOGV(...) ((void)0)
  32. #endif
  33. static void free_saved_state(struct android_app *android_app) {
  34. pthread_mutex_lock(&android_app->mutex);
  35. if (android_app->savedState != NULL) {
  36. free(android_app->savedState);
  37. android_app->savedState = NULL;
  38. android_app->savedStateSize = 0;
  39. }
  40. pthread_mutex_unlock(&android_app->mutex);
  41. }
  42. int8_t android_app_read_cmd(struct android_app *android_app) {
  43. int8_t cmd;
  44. if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
  45. switch (cmd) {
  46. case APP_CMD_SAVE_STATE:
  47. free_saved_state(android_app);
  48. break;
  49. }
  50. return cmd;
  51. }
  52. else {
  53. LOGE("No data on command pipe!");
  54. }
  55. return -1;
  56. }
  57. static void print_cur_config(struct android_app *android_app) {
  58. char lang[2], country[2];
  59. AConfiguration_getLanguage(android_app->config, lang);
  60. AConfiguration_getCountry(android_app->config, country);
  61. LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
  62. "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
  63. "modetype=%d modenight=%d",
  64. AConfiguration_getMcc(android_app->config), AConfiguration_getMnc(android_app->config), lang[0], lang[1], country[0], country[1],
  65. AConfiguration_getOrientation(android_app->config), AConfiguration_getTouchscreen(android_app->config), AConfiguration_getDensity(android_app->config),
  66. AConfiguration_getKeyboard(android_app->config), AConfiguration_getNavigation(android_app->config), AConfiguration_getKeysHidden(android_app->config),
  67. AConfiguration_getNavHidden(android_app->config), AConfiguration_getSdkVersion(android_app->config), AConfiguration_getScreenSize(android_app->config),
  68. AConfiguration_getScreenLong(android_app->config), AConfiguration_getUiModeType(android_app->config),
  69. AConfiguration_getUiModeNight(android_app->config));
  70. }
  71. void android_app_pre_exec_cmd(struct android_app *android_app, int8_t cmd) {
  72. switch (cmd) {
  73. case APP_CMD_INPUT_CHANGED:
  74. LOGV("APP_CMD_INPUT_CHANGED\n");
  75. pthread_mutex_lock(&android_app->mutex);
  76. if (android_app->inputQueue != NULL) {
  77. AInputQueue_detachLooper(android_app->inputQueue);
  78. }
  79. android_app->inputQueue = android_app->pendingInputQueue;
  80. if (android_app->inputQueue != NULL) {
  81. LOGV("Attaching input queue to looper");
  82. AInputQueue_attachLooper(android_app->inputQueue, android_app->looper, LOOPER_ID_INPUT, NULL, &android_app->inputPollSource);
  83. }
  84. pthread_cond_broadcast(&android_app->cond);
  85. pthread_mutex_unlock(&android_app->mutex);
  86. break;
  87. case APP_CMD_INIT_WINDOW:
  88. LOGV("APP_CMD_INIT_WINDOW\n");
  89. pthread_mutex_lock(&android_app->mutex);
  90. android_app->window = android_app->pendingWindow;
  91. pthread_cond_broadcast(&android_app->cond);
  92. pthread_mutex_unlock(&android_app->mutex);
  93. break;
  94. case APP_CMD_TERM_WINDOW:
  95. LOGV("APP_CMD_TERM_WINDOW\n");
  96. pthread_cond_broadcast(&android_app->cond);
  97. break;
  98. case APP_CMD_RESUME:
  99. case APP_CMD_START:
  100. case APP_CMD_PAUSE:
  101. case APP_CMD_STOP:
  102. LOGV("activityState=%d\n", cmd);
  103. pthread_mutex_lock(&android_app->mutex);
  104. android_app->activityState = cmd;
  105. pthread_cond_broadcast(&android_app->cond);
  106. pthread_mutex_unlock(&android_app->mutex);
  107. break;
  108. case APP_CMD_CONFIG_CHANGED:
  109. LOGV("APP_CMD_CONFIG_CHANGED\n");
  110. AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
  111. print_cur_config(android_app);
  112. break;
  113. case APP_CMD_DESTROY:
  114. LOGV("APP_CMD_DESTROY\n");
  115. android_app->destroyRequested = 1;
  116. break;
  117. }
  118. }
  119. void android_app_post_exec_cmd(struct android_app *android_app, int8_t cmd) {
  120. switch (cmd) {
  121. case APP_CMD_TERM_WINDOW:
  122. LOGV("APP_CMD_TERM_WINDOW\n");
  123. pthread_mutex_lock(&android_app->mutex);
  124. android_app->window = NULL;
  125. pthread_cond_broadcast(&android_app->cond);
  126. pthread_mutex_unlock(&android_app->mutex);
  127. break;
  128. case APP_CMD_SAVE_STATE:
  129. LOGV("APP_CMD_SAVE_STATE\n");
  130. pthread_mutex_lock(&android_app->mutex);
  131. android_app->stateSaved = 1;
  132. pthread_cond_broadcast(&android_app->cond);
  133. pthread_mutex_unlock(&android_app->mutex);
  134. break;
  135. case APP_CMD_RESUME:
  136. free_saved_state(android_app);
  137. break;
  138. }
  139. }
  140. void app_dummy() {}
  141. static void android_app_destroy(struct android_app *android_app) {
  142. LOGV("android_app_destroy!");
  143. free_saved_state(android_app);
  144. pthread_mutex_lock(&android_app->mutex);
  145. if (android_app->inputQueue != NULL) {
  146. AInputQueue_detachLooper(android_app->inputQueue);
  147. }
  148. AConfiguration_delete(android_app->config);
  149. android_app->destroyed = 1;
  150. pthread_cond_broadcast(&android_app->cond);
  151. pthread_mutex_unlock(&android_app->mutex);
  152. // Can't touch android_app object after this.
  153. }
  154. static void process_input(struct android_app *app, struct android_poll_source *source) {
  155. AInputEvent *event = NULL;
  156. while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
  157. // LOGV("New input event: type=%d\n", AInputEvent_getType(event));
  158. if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
  159. continue;
  160. }
  161. int32_t handled = 0;
  162. if (app->onInputEvent != NULL)
  163. handled = app->onInputEvent(app, event);
  164. AInputQueue_finishEvent(app->inputQueue, event, handled);
  165. }
  166. }
  167. static void process_cmd(struct android_app *app, struct android_poll_source *source) {
  168. int8_t cmd = android_app_read_cmd(app);
  169. android_app_pre_exec_cmd(app, cmd);
  170. if (app->onAppCmd != NULL)
  171. app->onAppCmd(app, cmd);
  172. android_app_post_exec_cmd(app, cmd);
  173. }
  174. static void *android_app_entry(void *param) {
  175. struct android_app *android_app = (struct android_app *)param;
  176. android_app->config = AConfiguration_new();
  177. AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
  178. print_cur_config(android_app);
  179. android_app->cmdPollSource.id = LOOPER_ID_MAIN;
  180. android_app->cmdPollSource.app = android_app;
  181. android_app->cmdPollSource.process = process_cmd;
  182. android_app->inputPollSource.id = LOOPER_ID_INPUT;
  183. android_app->inputPollSource.app = android_app;
  184. android_app->inputPollSource.process = process_input;
  185. ALooper *looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  186. ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL, &android_app->cmdPollSource);
  187. android_app->looper = looper;
  188. pthread_mutex_lock(&android_app->mutex);
  189. android_app->running = 1;
  190. pthread_cond_broadcast(&android_app->cond);
  191. pthread_mutex_unlock(&android_app->mutex);
  192. android_main(android_app);
  193. android_app_destroy(android_app);
  194. return NULL;
  195. }
  196. // --------------------------------------------------------------------
  197. // Native activity interaction (called from main thread)
  198. // --------------------------------------------------------------------
  199. static struct android_app *android_app_create(ANativeActivity *activity, void *savedState, size_t savedStateSize) {
  200. struct android_app *android_app = (struct android_app *)malloc(sizeof(struct android_app));
  201. memset(android_app, 0, sizeof(struct android_app));
  202. android_app->activity = activity;
  203. pthread_mutex_init(&android_app->mutex, NULL);
  204. pthread_cond_init(&android_app->cond, NULL);
  205. if (savedState != NULL) {
  206. android_app->savedState = malloc(savedStateSize);
  207. android_app->savedStateSize = savedStateSize;
  208. memcpy(android_app->savedState, savedState, savedStateSize);
  209. }
  210. int msgpipe[2];
  211. if (pipe(msgpipe)) {
  212. LOGE("could not create pipe: %s", strerror(errno));
  213. return NULL;
  214. }
  215. android_app->msgread = msgpipe[0];
  216. android_app->msgwrite = msgpipe[1];
  217. pthread_attr_t attr;
  218. pthread_attr_init(&attr);
  219. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  220. pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
  221. // Wait for thread to start.
  222. pthread_mutex_lock(&android_app->mutex);
  223. while (!android_app->running) {
  224. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  225. }
  226. pthread_mutex_unlock(&android_app->mutex);
  227. return android_app;
  228. }
  229. static void android_sys_write_cmd(struct android_app *android_app, int8_t cmd) {
  230. if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  231. LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
  232. }
  233. }
  234. static void android_app_set_input(struct android_app *android_app, AInputQueue *inputQueue) {
  235. pthread_mutex_lock(&android_app->mutex);
  236. android_app->pendingInputQueue = inputQueue;
  237. android_sys_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
  238. while (android_app->inputQueue != android_app->pendingInputQueue) {
  239. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  240. }
  241. pthread_mutex_unlock(&android_app->mutex);
  242. }
  243. static void android_app_set_window(struct android_app *android_app, ANativeWindow *window) {
  244. pthread_mutex_lock(&android_app->mutex);
  245. if (android_app->pendingWindow != NULL) {
  246. android_sys_write_cmd(android_app, APP_CMD_TERM_WINDOW);
  247. }
  248. android_app->pendingWindow = window;
  249. if (window != NULL) {
  250. android_sys_write_cmd(android_app, APP_CMD_INIT_WINDOW);
  251. }
  252. while (android_app->window != android_app->pendingWindow) {
  253. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  254. }
  255. pthread_mutex_unlock(&android_app->mutex);
  256. }
  257. static void android_app_set_activity_state(struct android_app *android_app, int8_t cmd) {
  258. pthread_mutex_lock(&android_app->mutex);
  259. android_sys_write_cmd(android_app, cmd);
  260. while (android_app->activityState != cmd) {
  261. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  262. }
  263. pthread_mutex_unlock(&android_app->mutex);
  264. }
  265. static void android_app_free(struct android_app *android_app) {
  266. pthread_mutex_lock(&android_app->mutex);
  267. android_sys_write_cmd(android_app, APP_CMD_DESTROY);
  268. while (!android_app->destroyed) {
  269. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  270. }
  271. pthread_mutex_unlock(&android_app->mutex);
  272. close(android_app->msgread);
  273. close(android_app->msgwrite);
  274. pthread_cond_destroy(&android_app->cond);
  275. pthread_mutex_destroy(&android_app->mutex);
  276. free(android_app);
  277. }
  278. static void onDestroy(ANativeActivity *activity) {
  279. LOGV("Destroy: %p\n", activity);
  280. android_app_free((struct android_app *)activity->instance);
  281. }
  282. static void onStart(ANativeActivity *activity) {
  283. LOGV("Start: %p\n", activity);
  284. android_app_set_activity_state((struct android_app *)activity->instance, APP_CMD_START);
  285. }
  286. static void onResume(ANativeActivity *activity) {
  287. LOGV("Resume: %p\n", activity);
  288. android_app_set_activity_state((struct android_app *)activity->instance, APP_CMD_RESUME);
  289. }
  290. static void *onSaveInstanceState(ANativeActivity *activity, size_t *outLen) {
  291. struct android_app *android_app = (struct android_app *)activity->instance;
  292. void *savedState = NULL;
  293. LOGV("SaveInstanceState: %p\n", activity);
  294. pthread_mutex_lock(&android_app->mutex);
  295. android_app->stateSaved = 0;
  296. android_sys_write_cmd(android_app, APP_CMD_SAVE_STATE);
  297. while (!android_app->stateSaved) {
  298. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  299. }
  300. if (android_app->savedState != NULL) {
  301. savedState = android_app->savedState;
  302. *outLen = android_app->savedStateSize;
  303. android_app->savedState = NULL;
  304. android_app->savedStateSize = 0;
  305. }
  306. pthread_mutex_unlock(&android_app->mutex);
  307. return savedState;
  308. }
  309. static void onPause(ANativeActivity *activity) {
  310. LOGV("Pause: %p\n", activity);
  311. android_app_set_activity_state((struct android_app *)activity->instance, APP_CMD_PAUSE);
  312. }
  313. static void onStop(ANativeActivity *activity) {
  314. LOGV("Stop: %p\n", activity);
  315. android_app_set_activity_state((struct android_app *)activity->instance, APP_CMD_STOP);
  316. }
  317. static void onConfigurationChanged(ANativeActivity *activity) {
  318. struct android_app *android_app = (struct android_app *)activity->instance;
  319. LOGV("ConfigurationChanged: %p\n", activity);
  320. android_sys_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
  321. }
  322. static void onLowMemory(ANativeActivity *activity) {
  323. struct android_app *android_app = (struct android_app *)activity->instance;
  324. LOGV("LowMemory: %p\n", activity);
  325. android_sys_write_cmd(android_app, APP_CMD_LOW_MEMORY);
  326. }
  327. static void onWindowFocusChanged(ANativeActivity *activity, int focused) {
  328. LOGV("WindowFocusChanged: %p -- %d\n", activity, focused);
  329. android_sys_write_cmd((struct android_app *)activity->instance, focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
  330. }
  331. static void onNativeWindowCreated(ANativeActivity *activity, ANativeWindow *window) {
  332. LOGV("NativeWindowCreated: %p -- %p\n", activity, window);
  333. android_app_set_window((struct android_app *)activity->instance, window);
  334. }
  335. static void onNativeWindowDestroyed(ANativeActivity *activity, ANativeWindow *window) {
  336. LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window);
  337. android_app_set_window((struct android_app *)activity->instance, NULL);
  338. }
  339. static void onInputQueueCreated(ANativeActivity *activity, AInputQueue *queue) {
  340. LOGV("InputQueueCreated: %p -- %p\n", activity, queue);
  341. android_app_set_input((struct android_app *)activity->instance, queue);
  342. }
  343. static void onInputQueueDestroyed(ANativeActivity *activity, AInputQueue *queue) {
  344. LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue);
  345. android_app_set_input((struct android_app *)activity->instance, NULL);
  346. }
  347. void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState, size_t savedStateSize) {
  348. LOGV("Creating: %p\n", activity);
  349. activity->callbacks->onDestroy = onDestroy;
  350. activity->callbacks->onStart = onStart;
  351. activity->callbacks->onResume = onResume;
  352. activity->callbacks->onSaveInstanceState = onSaveInstanceState;
  353. activity->callbacks->onPause = onPause;
  354. activity->callbacks->onStop = onStop;
  355. activity->callbacks->onConfigurationChanged = onConfigurationChanged;
  356. activity->callbacks->onLowMemory = onLowMemory;
  357. activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
  358. activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
  359. activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
  360. activity->callbacks->onInputQueueCreated = onInputQueueCreated;
  361. activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
  362. activity->instance = android_app_create(activity, savedState, savedStateSize);
  363. }