android_native_app_glue.c 16 KB

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