android_native_app_glue.c 17 KB

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